1212test_user_id = 121
1313invalid_test_user_id = 120
1414test_timeout = 1000
15+ test_now = int (time ())
1516test_claims = {"foo" : "bar" , "baz" : "quux" , "meaning" : 42 }
16-
17-
18- def get_test_now ():
19- """Get current time for test tokens."""
20- return int (time ())
21-
22-
23- def get_expected_full_token (test_now ):
24- """Generate expected token with current timestamp."""
25- return {
26- "lms_user_id" : test_user_id ,
27- "iat" : test_now ,
28- "exp" : test_now + test_timeout ,
29- "iss" : "token-test-issuer" , # these lines from test_settings.py
30- "version" : "1.2.0" , # these lines from test_settings.py
31- }
17+ expected_full_token = {
18+ "lms_user_id" : test_user_id ,
19+ "iat" : test_now ,
20+ "exp" : test_now + test_timeout ,
21+ "iss" : "token-test-issuer" , # these lines from test_settings.py
22+ "version" : "1.2.0" , # these lines from test_settings.py
23+ }
3224
3325
3426@skip_unless_lms
@@ -38,28 +30,25 @@ class TestSign(unittest.TestCase):
3830 """
3931
4032 def test_create_jwt (self ):
41- test_now = get_test_now ()
4233 token = create_jwt (test_user_id , test_timeout , {}, test_now )
4334
4435 decoded = unpack_and_verify (token )
45- self .assertEqual (get_expected_full_token ( test_now ) , decoded ) # noqa: PT009
36+ self .assertEqual (expected_full_token , decoded ) # noqa: PT009
4637
4738 def test_create_jwt_with_claims (self ):
48- test_now = get_test_now ()
4939 token = create_jwt (test_user_id , test_timeout , test_claims , test_now )
5040
51- expected_token_with_claims = get_expected_full_token ( test_now ) .copy ()
41+ expected_token_with_claims = expected_full_token .copy ()
5242 expected_token_with_claims .update (test_claims )
5343
5444 decoded = unpack_and_verify (token )
5545 self .assertEqual (expected_token_with_claims , decoded ) # noqa: PT009
5646
5747 def test_malformed_token (self ):
58- test_now = get_test_now ()
5948 token = create_jwt (test_user_id , test_timeout , test_claims , test_now )
6049 token = token + "a"
6150
62- expected_token_with_claims = get_expected_full_token ( test_now ) .copy ()
51+ expected_token_with_claims = expected_full_token .copy ()
6352 expected_token_with_claims .update (test_claims )
6453
6554 with self .assertRaises (InvalidSignatureError ): # noqa: PT027
@@ -73,62 +62,56 @@ class TestUnpack(unittest.TestCase):
7362 """
7463
7564 def test_unpack_jwt (self ):
76- test_now = get_test_now ()
7765 token = create_jwt (test_user_id , test_timeout , {}, test_now )
7866 decoded = unpack_jwt (token , test_user_id , test_now )
7967
80- self .assertEqual (get_expected_full_token ( test_now ) , decoded ) # noqa: PT009
68+ self .assertEqual (expected_full_token , decoded ) # noqa: PT009
8169
8270 def test_unpack_jwt_with_claims (self ):
83- test_now = get_test_now ()
8471 token = create_jwt (test_user_id , test_timeout , test_claims , test_now )
8572
86- expected_token_with_claims = get_expected_full_token ( test_now ) .copy ()
73+ expected_token_with_claims = expected_full_token .copy ()
8774 expected_token_with_claims .update (test_claims )
8875
8976 decoded = unpack_jwt (token , test_user_id , test_now )
9077
9178 self .assertEqual (expected_token_with_claims , decoded ) # noqa: PT009
9279
9380 def test_malformed_token (self ):
94- test_now = get_test_now ()
9581 token = create_jwt (test_user_id , test_timeout , test_claims , test_now )
9682 token = token + "a"
9783
98- expected_token_with_claims = get_expected_full_token ( test_now ) .copy ()
84+ expected_token_with_claims = expected_full_token .copy ()
9985 expected_token_with_claims .update (test_claims )
10086
10187 with self .assertRaises (InvalidSignatureError ): # noqa: PT027
10288 unpack_jwt (token , test_user_id , test_now )
10389
10490 def test_unpack_token_with_invalid_user (self ):
105- test_now = get_test_now ()
10691 token = create_jwt (invalid_test_user_id , test_timeout , {}, test_now )
10792
10893 with self .assertRaises (InvalidSignatureError ): # noqa: PT027
10994 unpack_jwt (token , test_user_id , test_now )
11095
11196 def test_unpack_expired_token (self ):
112- test_now = get_test_now ()
11397 token = create_jwt (test_user_id , test_timeout , {}, test_now )
11498
11599 with self .assertRaises (ExpiredSignatureError ): # noqa: PT027
116100 unpack_jwt (token , test_user_id , test_now + test_timeout + 1 )
117101
118102 def test_missing_expired_lms_user_id (self ):
119- test_now = get_test_now ()
120- payload = get_expected_full_token (test_now ).copy ()
103+ payload = expected_full_token .copy ()
121104 del payload ['lms_user_id' ]
122105 token = _encode_and_sign (payload )
123106
124107 with self .assertRaises (MissingRequiredClaimError ): # noqa: PT027
125108 unpack_jwt (token , test_user_id , test_now )
126109
127110 def test_missing_expired_key (self ):
128- test_now = get_test_now ()
129- payload = get_expected_full_token (test_now ).copy ()
111+ payload = expected_full_token .copy ()
130112 del payload ['exp' ]
131113 token = _encode_and_sign (payload )
132114
133115 with self .assertRaises (MissingRequiredClaimError ): # noqa: PT027
134116 unpack_jwt (token , test_user_id , test_now )
117+
0 commit comments