ban-contributors.t 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. # Copyright (C) 2013-2016 Alex Schroeder <alex@gnu.org>
  2. #
  3. # This program is free software; you can redistribute it and/or modify it under
  4. # the terms of the GNU General Public License as published by the Free Software
  5. # Foundation; either version 3 of the License, or (at your option) any later
  6. # version.
  7. #
  8. # This program is distributed in the hope that it will be useful, but WITHOUT
  9. # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  10. # FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  11. #
  12. # You should have received a copy of the GNU General Public License along with
  13. # this program. If not, see <http://www.gnu.org/licenses/>.
  14. require './t/test.pl';
  15. package OddMuse;
  16. use Test::More;
  17. use Net::IP;
  18. add_module('ban-contributors.pl');
  19. # 0-255
  20. is(BanContributors::get_regexp_ip('185.244.214.0', '185.244.214.255'),
  21. '^185\.244\.214\.',
  22. '185.244.214.0 - 185.244.214.255');
  23. # 48.0-63.255
  24. is(BanContributors::get_regexp_ip('42.118.48.0', '42.118.63.255'),
  25. '^42\.118\.(4[8-9]|5[0-9]|6[0-3])\.',
  26. '42.118.48.0 - 42.118.63.255');
  27. # 192.0-223.255
  28. is(BanContributors::get_regexp_ip('118.71.192.0', '118.71.223.255'),
  29. '^118\.71\.(19[2-9]|2[0-1][0-9]|22[0-3])\.',
  30. '118.71.192.0 - 118.71.223.255');
  31. # 56.180-57.70
  32. is(BanContributors::get_regexp_ip('77.56.180.0', '77.57.70.255'),
  33. '^77\.(56\.(1[8-9][0-9]|2[0-4][0-9]|25[0-5])|57\.([0-9]|[1-6][0-9]|70)\.',
  34. '77.56.180.0 - 77.57.70.255');
  35. # 45.87.2.128 - 45.87.2.255
  36. is(BanContributors::get_regexp_ip('45.87.2.128', '45.87.2.255'),
  37. '^45\.87\.2\.(12[8-9]|1[3-9][0-9]|2[0-4][0-9]|25[0-5])',
  38. '45.87.2.128 - 45.87.2.255');
  39. # 191.101.0.0/16
  40. # verify that Net::IP works as intended
  41. my $ip = Net::IP->new('191.101.0.0/16');
  42. ok($ip, 'Net::IP parsed CIDR');
  43. is($ip->ip, '191.101.0.0', 'First IP in range');
  44. is($ip->last_ip, '191.101.255.255', 'Last IP in range');
  45. $localhost = '127.0.0.1';
  46. $ENV{'REMOTE_ADDR'} = $localhost;
  47. update_page('Test', 'insults');
  48. test_page_negative(get_page('action=admin id=Test'), 'Ban contributors');
  49. test_page(get_page('action=admin id=Test pwd=foo'), 'Ban contributors');
  50. test_page(get_page('action=ban id=Test pwd=foo'), $localhost, 'Ban!');
  51. test_page(get_page("action=ban id=Test regexp=$localhost pwd=foo"),
  52. 'Location: http://localhost/wiki.pl/BannedHosts');
  53. test_page(get_page('BannedHosts'), $localhost, 'Test');
  54. clear_pages();
  55. add_module('ban-contributors.pl');
  56. update_page('Test', 'no spam');
  57. ok(get_page('action=browse id=Test raw=2')
  58. =~ /(\d+) # Do not delete this line/,
  59. 'raw=2 returns timestamp');
  60. $to = $1;
  61. ok($to, 'timestamp stored');
  62. sleep(1);
  63. update_page('Test', "http://spam/amoxil/ http://spam/doxycycline/");
  64. test_page(get_page("action=rollback id=Test to=$to pwd=foo"),
  65. 'Rolling back changes', 'These URLs were rolled back',
  66. 'amoxil', 'doxycycline', 'Consider banning the IP number');
  67. test_page(get_page("action=ban id=Test content=amoxil pwd=foo"),
  68. 'Location: http://localhost/wiki.pl/BannedContent');
  69. test_page(get_page('BannedContent'), 'amoxil', 'Test');
  70. update_page('Test', "http://spam/amoxil/ http://spam/doxycycline/");
  71. $page = get_page("action=rollback id=Test to=$to pwd=foo");
  72. test_page($page, 'Rolling back changes', 'These URLs were rolled back',
  73. 'doxycycline');
  74. test_page_negative($page, 'amoxil');
  75. test_page(get_page("action=ban id=Test"),
  76. 'Ban Contributors to Test',
  77. quotemeta('127.0.0.1 () [127.0.0.0 - 127.255.255.255]'));
  78. SKIP: {
  79. skip "Net::Whois::Parser doesn't always return the same result", 4;
  80. $ENV{'REMOTE_ADDR'} = '46.101.109.194';
  81. update_page('Test', "this is phone number spam");
  82. test_page(get_page("action=ban id=Test"),
  83. 'Ban Contributors to Test',
  84. quotemeta('^46\.101\.([0-9]|[1-9][0-9]|1[0-1][0-9]|12[0-7])'));
  85. test_page(get_page('action=ban id=Test regexp="^46\.101\.([0-9]|[1-9][0-9]|1[0-1][0-9]|12[0-7])" range="[46.101.0.0 - 46.101.127.255]" recent_edit=on pwd=foo'),
  86. 'Location: http://localhost/wiki.pl/BannedHosts');
  87. test_page(get_page('BannedHosts'),
  88. quotemeta('^46\.101\.([0-9]|[1-9][0-9]|1[0-1][0-9]|12[0-7]) # '
  89. . CalcDay($Now)
  90. . ' [46.101.0.0 - 46.101.127.255] Test'));
  91. }
  92. done_testing();