referrer-tracking.t 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. # Copyright (C) 2009–2015 Alex Schroeder <alex@gnu.org>
  2. #
  3. # This program is free software; you can redistribute it and/or modify
  4. # it under the terms of the GNU General Public License as published by
  5. # the Free Software Foundation; either version 3 of the License, or
  6. # (at your option) any later version.
  7. #
  8. # This program is distributed in the hope that it will be useful,
  9. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. # GNU General Public License for more details.
  12. #
  13. # You should have received a copy of the GNU General Public License
  14. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. require 't/test.pl';
  16. package OddMuse;
  17. use Test::More tests => 12; # update two numbers below!
  18. use utf8; # tests contain UTF-8 characters and it matters
  19. SKIP: {
  20. eval {
  21. require LWP::UserAgent;
  22. };
  23. skip "LWP::UserAgent not installed", 12 if $@;
  24. my $wiki = 'http://localhost/cgi-bin/wiki.pl';
  25. my $ua = LWP::UserAgent->new;
  26. my $response = $ua->get("$wiki?action=version");
  27. skip("No wiki running at $wiki", 12)
  28. unless $response->is_success;
  29. # check that the wiki is capable of running these tests
  30. skip("Wiki running at $wiki doesn't have the Referrer-Tracking Extension installed", 12)
  31. unless $response->content =~ /referrer-tracking\.pl/;
  32. # if we're running in some random environment where localhost is not
  33. # a wiki for us to interact with
  34. skip("Wiki running at $wiki has the Question Asker Extension installed", 12)
  35. if $response->content =~ /questionasker\.pl/;
  36. # if HTTPS is not supported
  37. skip("No HTTPS support available: $@", 12)
  38. unless eval { require LWP::Protocol::https; };
  39. my $id = 'Random' . time;
  40. # make sure we're not being fooled by 404 errors
  41. $response = $ua->get("$wiki?title=$id;text=test");
  42. ok($response->is_success, "Created $wiki/$id");
  43. # If the tests are running into a lot of errors here, make sure
  44. # questionasker.pl and other spam protection is not installed.
  45. # Also make sure that no german-utf8.pl is installed.
  46. # This will fail because example.com doesn't really link back (spam protection)
  47. $response = $ua->get("$wiki/$id", 'Referer' => 'http://example.com/');
  48. ok($response->is_success, "Request $wiki/$id with faked referrer");
  49. $response = $ua->get("$wiki?action=refer");
  50. ok($response->is_success, 'Get list of all referrers');
  51. negative_xpath_test($response->content,
  52. qq{//div[\@class="content refer"]/div/p/a[text()="$id"]});
  53. # This page must actually exist and link back!
  54. $response = $ua->get('https://oddmuse.org/test.html');
  55. ok($response->is_success, "https://oddmuse.org/test.html exists");
  56. test_page($response->content, "http://localhost/cgi-bin/wiki.pl");
  57. # If it is lost, here's what it should contain:
  58. # <head><title>Tëst</title></head>
  59. # <body>
  60. # This file is required for an Oddmuse unit test
  61. # involving referrer tracking.
  62. # <a href="http://localhost/cgi-bin/wiki.pl">Test</a>.
  63. $response = $ua->get("$wiki/$id", 'Referer' => 'https://oddmuse.org/test.html');
  64. ok($response->is_success, "Request $wiki/$id with existing referrer");
  65. $response = $ua->get("$wiki?action=refer");
  66. ok($response->is_success, 'Get list of all referrers');
  67. xpath_test($response->content,
  68. '//h1[text()="All Referrers"]',
  69. qq{//div[\@class="content refer"]/div[\@class="page"]},
  70. qq{//div[\@class="page"]/p/a[\@href="$wiki/$id"][text()="$id"]},
  71. qq{//div[\@class="refer"]/p/a[\@href="https://oddmuse.org/test.html"][text()="Tëst"]});
  72. # Clean up
  73. $ua->get("$wiki?title=$id;text=");
  74. }