def test_clean(self): token = RequestToken(login_mode=RequestToken.LOGIN_MODE_NONE, # user=self.user ) token.clean() # set a user, should now fail validation token.user = self.user self.assertRaises(ValidationError, token.clean) # request mode token.login_mode = RequestToken.LOGIN_MODE_REQUEST token.clean() token.user = None self.assertRaises(ValidationError, token.clean) def reset_session(): """Reset properties so that token passes validation.""" token.login_mode = RequestToken.LOGIN_MODE_SESSION token.user = self.user token.issued_at = tz_now() token.expiration_time = token.issued_at + datetime.timedelta( minutes=1) token.max_uses = 1 def assertValidationFails(field_name): with self.assertRaises(ValidationError) as ctx: token.clean() self.assertTrue(field_name in dict(ctx.exception)) # check the rest_session works! reset_session() token.clean() token.max_uses = 10 assertValidationFails('max_uses') reset_session() token.user = None assertValidationFails('user') reset_session() token.expiration_time = None assertValidationFails('expiration_time')
def test_claims(self): token = RequestToken() # raises error with no id set - put into context manager as it's # an attr, not a callable self.assertEqual(len(token.claims), 3) self.assertEqual(token.max, 1) self.assertEqual(token.sub, '') self.assertIsNone(token.jti) self.assertIsNone(token.aud) self.assertIsNone(token.exp) self.assertIsNone(token.nbf) self.assertIsNone(token.iat) # now let's set some properties token.user = self.user self.assertEqual(token.aud, self.user.id) self.assertEqual(len(token.claims), 4) token.login_mode = RequestToken.LOGIN_MODE_REQUEST self.assertEqual(token.claims['mod'], RequestToken.LOGIN_MODE_REQUEST[:1].lower()) self.assertEqual(len(token.claims), 4) now = tz_now() now_sec = to_seconds(now) token.expiration_time = now self.assertEqual(token.exp, now_sec) self.assertEqual(len(token.claims), 5) token.not_before_time = now self.assertEqual(token.nbf, now_sec) self.assertEqual(len(token.claims), 6) # saving updates the id and issued_at timestamp with mock.patch('request_token.models.tz_now', lambda: now): token.save() self.assertEqual(token.iat, now_sec) self.assertEqual(token.jti, token.id) self.assertEqual(len(token.claims), 8)
def test_clean(self): # LOGIN_MODE_NONE doesn't care about user. token = RequestToken(login_mode=RequestToken.LOGIN_MODE_NONE) token.clean() token.user = self.user token.clean() # request mode token.login_mode = RequestToken.LOGIN_MODE_REQUEST token.clean() token.user = None self.assertRaises(ValidationError, token.clean) def reset_session(): """Reset properties so that token passes validation.""" token.login_mode = RequestToken.LOGIN_MODE_SESSION token.user = self.user token.issued_at = tz_now() token.expiration_time = token.issued_at + datetime.timedelta( minutes=1) token.max_uses = DEFAULT_MAX_USES def assertValidationFails(field_name): with self.assertRaises(ValidationError) as ctx: token.clean() self.assertTrue(field_name in dict(ctx.exception)) # check the reset_session works! reset_session() token.user = None assertValidationFails("user") reset_session() token.expiration_time = None assertValidationFails("expiration_time")