def test_save(self): token = RequestToken().save() self.assertIsNotNone(token) self.assertIsNone(token.user) self.assertEqual(token.scope, '') self.assertEqual(token.login_mode, RequestToken.LOGIN_MODE_NONE) self.assertIsNone(token.expiration_time) self.assertIsNone(token.not_before_time) self.assertEqual(token.data, {}) self.assertIsNotNone(token.issued_at) self.assertEqual(token.max_uses, 1) self.assertEqual(token.used_to_date, 0) token.issued_at = None token = token.save(update_fields=['issued_at']) self.assertIsNone(token.issued_at) now = tz_now() expires = now + datetime.timedelta(minutes=JWT_SESSION_TOKEN_EXPIRY) with mock.patch('request_token.models.tz_now', lambda: now): token = RequestToken(login_mode=RequestToken.LOGIN_MODE_SESSION, user=self.user, scope="foo") self.assertIsNone(token.issued_at) self.assertIsNone(token.expiration_time) token.save() self.assertEqual(token.issued_at, now) self.assertEqual(token.expiration_time, expires)
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_json(self): """Test the data field is really JSON.""" token = RequestToken(data={"foo": True}) token.save() self.assertTrue(token.data['foo'])