bgneal@472: """ bgneal@472: Tests for the rate limiting function in the antispam application. bgneal@472: bgneal@472: """ bgneal@472: import redis bgneal@472: from django.test import TestCase bgneal@472: from django.core.urlresolvers import reverse bgneal@472: bgneal@472: from antispam.rate_limit import _make_key bgneal@472: bgneal@472: bgneal@472: class RateLimitTestCase(TestCase): bgneal@472: KEY = _make_key('127.0.0.1') bgneal@472: bgneal@472: def setUp(self): bgneal@472: self.conn = redis.Redis(host='localhost', port=6379, db=0) bgneal@472: self.conn.delete(self.KEY) bgneal@472: bgneal@472: def tearDown(self): bgneal@472: self.conn.delete(self.KEY) bgneal@472: bgneal@472: def testRegistrationLockout(self): bgneal@472: bgneal@472: for i in range(1, 11): bgneal@472: response = self.client.post( bgneal@472: reverse('accounts-register'), bgneal@472: {}, bgneal@472: follow=True) bgneal@472: bgneal@472: if i < 10: bgneal@472: self.assertEqual(response.status_code, 200) bgneal@472: self.assertTemplateUsed(response, 'accounts/register.html') bgneal@472: elif i >= 10: bgneal@472: self.assertEqual(response.status_code, 403) bgneal@472: self.assertTemplateUsed(response, 'antispam/blocked.html') bgneal@472: bgneal@472: def testLoginLockout(self): bgneal@472: bgneal@472: for i in range(1, 11): bgneal@472: response = self.client.post( bgneal@472: reverse('accounts-login'), bgneal@472: {}, bgneal@472: follow=True) bgneal@472: bgneal@472: if i < 10: bgneal@472: self.assertEqual(response.status_code, 200) bgneal@472: self.assertTemplateUsed(response, 'accounts/login.html') bgneal@472: elif i >= 10: bgneal@472: self.assertEqual(response.status_code, 403) bgneal@472: self.assertTemplateUsed(response, 'antispam/blocked.html') bgneal@472: bgneal@472: def testHoneypotLockout(self): bgneal@472: bgneal@472: response = self.client.post( bgneal@472: reverse('accounts-register'), { bgneal@472: 'username': u'test_user', bgneal@472: 'email': u'test_user@example.com', bgneal@472: 'password1': u'password', bgneal@472: 'password2': u'password', bgneal@472: 'agree_age': u'on', bgneal@472: 'agree_tos': u'on', bgneal@472: 'agree_privacy': u'on', bgneal@472: 'question1': u'101', bgneal@472: 'question2': u'DsjkdE$', bgneal@472: }, bgneal@472: follow=True) bgneal@472: bgneal@472: val = self.conn.get(self.KEY) bgneal@472: self.assertEqual(val, '1000001') bgneal@472: bgneal@472: response = self.client.post( bgneal@472: reverse('accounts-login'), bgneal@472: {}, bgneal@472: follow=True) bgneal@472: bgneal@472: self.assertEqual(response.status_code, 403) bgneal@472: self.assertTemplateUsed(response, 'antispam/blocked.html')