def test_token_when_saved(self): """when a token is saved it get's a random token""" u = User() u.is_active = True u.save() # make a token t = Token() t.user = u t.save() # the token now has a 40 digit token self.assertEqual(40, len(t.token))
def test_email_sends_when_token_is_created(self): """when a token is created an email is sent""" # create a user u = User() u.is_active = True u.email = "*****@*****.**" u.save() # create a token t = Token() t.user = u t.save() # now there is one message self.assertEqual(1, len(mail.outbox))
def test_expires_when_save(self): """when a token is saved it get's a random token""" u = User() u.is_active = True u.save() # make a token t = Token() t.user = u # the token has no self.assertFalse(t.expires) # save it t.save() # the token now has an expiry self.assertTrue(t.expires)
def test_validate_user_has_to_be_active(self): """if a user is not active they are redirected to login""" u = User() u.is_active = False u.email = '*****@*****.**' u.save() # make token t = Token() t.next = '/' t.user = u t.save() # call view and we're logged in r = self.client.get(reverse('zeropass:validate', args=[t.token])) # we were redirect to '/' self.assertRedirects(r, reverse('zeropass:login'))
def test_match(self): """the backend returns the user when a match is found""" # make a user u = User() u.is_active = True u.email = '*****@*****.**' u.save() # make a token t = Token() t.user = u t.save() # send te tokem through the backend and get user back user = authenticate(token=t.token) # user the save as above self.assertEqual(user.pk, u.pk)
def test_validate_valid(self): """when validate is passed a valid token a user is logged in""" # make user u = User() u.is_active = True u.email = '*****@*****.**' u.save() # make token t = Token() t.next = '/' t.user = u t.save() # call view and we're logged in r = self.client.get(reverse('zeropass:validate', args=[t.token])) # we were redirect to '/' self.assertRedirects(r, '/')
def test_no_match_expired(self): """if a token is expired no match is return""" # make a user u = User() u.is_active = True u.email = '*****@*****.**' u.save() # make a token with expires in past t = Token() t.expires = dt.now() - td(seconds=300) t.user = u t.save() # send te tokem through the backend and get user back user = authenticate(token=t.token) # user is None self.assertEqual(user, None)
def test_command(self): """the management command deletes expired tokens""" # create a user u = User() u.is_active = True u.save() # create some tokens t1 = Token() t1.user = u t1.expires = dt(2011, 1, 1, 1, 1, 1) t1.save() t2 = Token() t2.user = u t2.expires = dt(2011, 1, 1, 1, 1, 1) t2.save() t3 = Token() t3.user = u t3.expires = dt(2013, 1, 1, 1, 1, 1) t3.save() # call the managemet command call_command("zeropass_cleanup") # we have none self.assertEqual(1, Token.objects.count())
def test_expired(self): """The expired manager returns expired tokens""" # create a user u = User() u.is_active = True u.save() # create some tokens t1 = Token() t1.user = u t1.expires = dt(2011, 1, 1, 1, 1, 1) t1.save() t2 = Token() t2.user = u t2.expires = dt(2011, 1, 1, 1, 1, 1) t2.save() # expired returns 2 self.assertEqual(2, Token.objects.expired().count()) # update t2 t2.expires = dt(2013, 1, 1, 1, 1, 1) t2.save() # now we have only one expired self.assertEqual(1, Token.objects.expired().count())
def save(self): token = Token() token.user = self.user token.next = self.cleaned_data.get("next") token.expires = datetime.now() + timedelta(seconds=conf.EXPIRES) token.save()