utils.token: Use time.time to generate the timestamps
Use `time.time()` rather than `timezone.now()` for generating the unix timestamps. This avoids conversion between year-month-day-hh-mm-ss formatted timestamp vs. unix timestamp and is therefore simpler and more robust. Add a test case for this too and amend test_token_endpoint, since it used to mock timezone.now, but now it needs to mock time.time.
This commit is contained in:
parent
5e3876f0c6
commit
65538b0f7d
3 changed files with 47 additions and 12 deletions
|
@ -1,6 +1,12 @@
|
|||
import time
|
||||
from datetime import datetime
|
||||
|
||||
from django.test import TestCase
|
||||
from django.utils import timezone
|
||||
|
||||
from oidc_provider.lib.utils.common import get_issuer
|
||||
from oidc_provider.lib.utils.token import create_id_token
|
||||
from oidc_provider.tests.app.utils import create_fake_user
|
||||
|
||||
|
||||
class Request(object):
|
||||
|
@ -42,3 +48,31 @@ class CommonTest(TestCase):
|
|||
self.assertEqual(get_issuer(site_url='http://127.0.0.1:9000',
|
||||
request=request),
|
||||
'http://127.0.0.1:9000/openid')
|
||||
|
||||
|
||||
def timestamp_to_datetime(timestamp):
|
||||
tz = timezone.get_current_timezone()
|
||||
return datetime.fromtimestamp(timestamp, tz=tz)
|
||||
|
||||
|
||||
class TokenTest(TestCase):
|
||||
def setUp(self):
|
||||
self.user = create_fake_user()
|
||||
|
||||
def test_create_id_token(self):
|
||||
start_time = int(time.time())
|
||||
login_timestamp = start_time - 1234
|
||||
self.user.last_login = timestamp_to_datetime(login_timestamp)
|
||||
id_token_data = create_id_token(self.user, aud='test-aud')
|
||||
iat = id_token_data['iat']
|
||||
self.assertEqual(type(iat), int)
|
||||
self.assertGreaterEqual(iat, start_time)
|
||||
self.assertLessEqual(iat - start_time, 5) # Can't take more than 5 s
|
||||
self.assertEqual(id_token_data, {
|
||||
'aud': 'test-aud',
|
||||
'auth_time': login_timestamp,
|
||||
'exp': iat + 600,
|
||||
'iat': iat,
|
||||
'iss': 'http://localhost:8000/openid',
|
||||
'sub': str(self.user.id),
|
||||
})
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue