11"""
22Tests for token handling
33"""
4+ import datetime
45import unittest
56from time import time
67
8+ import pytest
9+ from freezegun import freeze_time
710from jwt .exceptions import ExpiredSignatureError , InvalidSignatureError , MissingRequiredClaimError
811
912from openedx .core .djangolib .testing .utils import skip_unless_lms
1316invalid_test_user_id = 120
1417test_timeout = 1000
1518test_now = int (time ())
19+ time_snapshot = datetime .datetime .fromtimestamp (test_now , tz = datetime .UTC )
1620test_claims = {"foo" : "bar" , "baz" : "quux" , "meaning" : 42 }
1721expected_full_token = {
1822 "lms_user_id" : test_user_id ,
2428
2529
2630@skip_unless_lms
31+ @freeze_time (time_snapshot )
2732class TestSign (unittest .TestCase ):
2833 """
2934 Tests for JWT creation and signing.
@@ -33,7 +38,7 @@ def test_create_jwt(self):
3338 token = create_jwt (test_user_id , test_timeout , {}, test_now )
3439
3540 decoded = unpack_and_verify (token )
36- self . assertEqual ( expected_full_token , decoded ) # noqa: PT009
41+ assert decoded == expected_full_token
3742
3843 def test_create_jwt_with_claims (self ):
3944 token = create_jwt (test_user_id , test_timeout , test_claims , test_now )
@@ -42,20 +47,18 @@ def test_create_jwt_with_claims(self):
4247 expected_token_with_claims .update (test_claims )
4348
4449 decoded = unpack_and_verify (token )
45- self . assertEqual ( expected_token_with_claims , decoded ) # noqa: PT009
50+ assert decoded == expected_token_with_claims
4651
4752 def test_malformed_token (self ):
4853 token = create_jwt (test_user_id , test_timeout , test_claims , test_now )
4954 token = token + "a"
5055
51- expected_token_with_claims = expected_full_token .copy ()
52- expected_token_with_claims .update (test_claims )
53-
54- with self .assertRaises (InvalidSignatureError ): # noqa: PT027
56+ with pytest .raises (InvalidSignatureError ):
5557 unpack_and_verify (token )
5658
5759
5860@skip_unless_lms
61+ @freeze_time (time_snapshot )
5962class TestUnpack (unittest .TestCase ):
6063 """
6164 Tests for JWT unpacking.
@@ -65,7 +68,7 @@ def test_unpack_jwt(self):
6568 token = create_jwt (test_user_id , test_timeout , {}, test_now )
6669 decoded = unpack_jwt (token , test_user_id , test_now )
6770
68- self . assertEqual ( expected_full_token , decoded ) # noqa: PT009
71+ assert decoded == expected_full_token
6972
7073 def test_unpack_jwt_with_claims (self ):
7174 token = create_jwt (test_user_id , test_timeout , test_claims , test_now )
@@ -75,42 +78,39 @@ def test_unpack_jwt_with_claims(self):
7578
7679 decoded = unpack_jwt (token , test_user_id , test_now )
7780
78- self . assertEqual ( expected_token_with_claims , decoded ) # noqa: PT009
81+ assert decoded == expected_token_with_claims
7982
8083 def test_malformed_token (self ):
8184 token = create_jwt (test_user_id , test_timeout , test_claims , test_now )
8285 token = token + "a"
8386
84- expected_token_with_claims = expected_full_token .copy ()
85- expected_token_with_claims .update (test_claims )
86-
87- with self .assertRaises (InvalidSignatureError ): # noqa: PT027
87+ with pytest .raises (InvalidSignatureError ):
8888 unpack_jwt (token , test_user_id , test_now )
8989
9090 def test_unpack_token_with_invalid_user (self ):
9191 token = create_jwt (invalid_test_user_id , test_timeout , {}, test_now )
9292
93- with self . assertRaises (InvalidSignatureError ): # noqa: PT027
93+ with pytest . raises (InvalidSignatureError ):
9494 unpack_jwt (token , test_user_id , test_now )
9595
9696 def test_unpack_expired_token (self ):
9797 token = create_jwt (test_user_id , test_timeout , {}, test_now )
9898
99- with self . assertRaises (ExpiredSignatureError ): # noqa: PT027
99+ with pytest . raises (ExpiredSignatureError ):
100100 unpack_jwt (token , test_user_id , test_now + test_timeout + 1 )
101101
102102 def test_missing_expired_lms_user_id (self ):
103103 payload = expected_full_token .copy ()
104104 del payload ['lms_user_id' ]
105105 token = _encode_and_sign (payload )
106106
107- with self . assertRaises (MissingRequiredClaimError ): # noqa: PT027
107+ with pytest . raises (MissingRequiredClaimError ):
108108 unpack_jwt (token , test_user_id , test_now )
109109
110110 def test_missing_expired_key (self ):
111111 payload = expected_full_token .copy ()
112112 del payload ['exp' ]
113113 token = _encode_and_sign (payload )
114114
115- with self . assertRaises (MissingRequiredClaimError ): # noqa: PT027
115+ with pytest . raises (MissingRequiredClaimError ):
116116 unpack_jwt (token , test_user_id , test_now )
0 commit comments