def test_it_normalizes_email(self): emails = ("*****@*****.**", "*****@*****.**") for email in emails: TokenBucket.authorize_login_email(email) self.assertEqual(TokenBucket.objects.count(), 1)
def test_it_works(self): r = TokenBucket.authorize_login_email("*****@*****.**") self.assertTrue(r) obj = TokenBucket.objects.get() self.assertEqual(obj.tokens, 0.95) self.assertEqual(obj.value, "em-" + ALICE_HASH)
def test_it_tops_up(self): obj = TokenBucket(value="em-" + ALICE_HASH) obj.tokens = 0 obj.updated = now() - td(minutes=30) obj.save() r = TokenBucket.authorize_login_email("*****@*****.**") self.assertTrue(r) obj.refresh_from_db() self.assertAlmostEqual(obj.tokens, 0.45, places=5)
def clean_identity(self): v = self.cleaned_data["identity"] if not TokenBucket.authorize_login_email(v): raise forms.ValidationError("Too many attempts, please try later.") try: self.user = User.objects.get(email=v) except User.DoesNotExist: raise forms.ValidationError("Unknown email address.") return v
def test_it_handles_insufficient_tokens(self): TokenBucket.objects.create(value="em-" + ALICE_HASH, tokens=0.04) r = TokenBucket.authorize_login_email("*****@*****.**") self.assertFalse(r)