comparison gpp/antispam/tests/rate_limit_tests.py @ 472:7c3816d76c6c

Implement rate limiting on registration and login for #224.
author Brian Neal <bgneal@gmail.com>
date Thu, 25 Aug 2011 02:23:55 +0000
parents
children 6f5fff924877
comparison
equal deleted inserted replaced
471:d83296cac940 472:7c3816d76c6c
1 """
2 Tests for the rate limiting function in the antispam application.
3
4 """
5 import redis
6 from django.test import TestCase
7 from django.core.urlresolvers import reverse
8
9 from antispam.rate_limit import _make_key
10
11
12 class RateLimitTestCase(TestCase):
13 KEY = _make_key('127.0.0.1')
14
15 def setUp(self):
16 self.conn = redis.Redis(host='localhost', port=6379, db=0)
17 self.conn.delete(self.KEY)
18
19 def tearDown(self):
20 self.conn.delete(self.KEY)
21
22 def testRegistrationLockout(self):
23
24 for i in range(1, 11):
25 response = self.client.post(
26 reverse('accounts-register'),
27 {},
28 follow=True)
29
30 if i < 10:
31 self.assertEqual(response.status_code, 200)
32 self.assertTemplateUsed(response, 'accounts/register.html')
33 elif i >= 10:
34 self.assertEqual(response.status_code, 403)
35 self.assertTemplateUsed(response, 'antispam/blocked.html')
36
37 def testLoginLockout(self):
38
39 for i in range(1, 11):
40 response = self.client.post(
41 reverse('accounts-login'),
42 {},
43 follow=True)
44
45 if i < 10:
46 self.assertEqual(response.status_code, 200)
47 self.assertTemplateUsed(response, 'accounts/login.html')
48 elif i >= 10:
49 self.assertEqual(response.status_code, 403)
50 self.assertTemplateUsed(response, 'antispam/blocked.html')
51
52 def testHoneypotLockout(self):
53
54 response = self.client.post(
55 reverse('accounts-register'), {
56 'username': u'test_user',
57 'email': u'test_user@example.com',
58 'password1': u'password',
59 'password2': u'password',
60 'agree_age': u'on',
61 'agree_tos': u'on',
62 'agree_privacy': u'on',
63 'question1': u'101',
64 'question2': u'DsjkdE$',
65 },
66 follow=True)
67
68 val = self.conn.get(self.KEY)
69 self.assertEqual(val, '1000001')
70
71 response = self.client.post(
72 reverse('accounts-login'),
73 {},
74 follow=True)
75
76 self.assertEqual(response.status_code, 403)
77 self.assertTemplateUsed(response, 'antispam/blocked.html')