1- import unittest
1+ from __future__ import unicode_literals
2+ from calendar import timegm
3+ from datetime import datetime
4+ import sys
25import time
6+ import unittest
37
48import jwt
59
6- from datetime import datetime
7- from calendar import timegm
10+ if sys . version_info >= ( 3 , 0 , 0 ):
11+ unicode = str
812
913
1014def utc_timestamp ():
@@ -14,7 +18,8 @@ def utc_timestamp():
1418class TestJWT (unittest .TestCase ):
1519
1620 def setUp (self ):
17- self .payload = {"iss" : "jeff" , "exp" : utc_timestamp () + 1 , "claim" : "insanity" }
21+ self .payload = {"iss" : "jeff" , "exp" : utc_timestamp () + 1 ,
22+ "claim" : "insanity" }
1823
1924 def test_encode_decode (self ):
2025 secret = 'secret'
@@ -36,7 +41,8 @@ def test_encode_expiration_datetime(self):
3641 payload = {"exp" : current_datetime }
3742 jwt_message = jwt .encode (payload , secret )
3843 decoded_payload = jwt .decode (jwt_message , secret , leeway = 1 )
39- self .assertEqual (decoded_payload ['exp' ],
44+ self .assertEqual (
45+ decoded_payload ['exp' ],
4046 timegm (current_datetime .utctimetuple ()))
4147
4248 def test_bad_secret (self ):
@@ -49,27 +55,29 @@ def test_bad_secret(self):
4955 def test_decodes_valid_jwt (self ):
5056 example_payload = {"hello" : "world" }
5157 example_secret = "secret"
52- example_jwt = "eyJhbGciOiAiSFMyNTYiLCAidHlwIjogIkpXVCJ9.eyJoZWxsbyI6ICJ3b3JsZCJ9.tvagLDLoaiJKxOKqpBXSEGy7SYSifZhjntgm9ctpyj8"
58+ example_jwt = (
59+ b"eyJhbGciOiAiSFMyNTYiLCAidHlwIjogIkpXVCJ9"
60+ b".eyJoZWxsbyI6ICJ3b3JsZCJ9"
61+ b".tvagLDLoaiJKxOKqpBXSEGy7SYSifZhjntgm9ctpyj8" )
5362 decoded_payload = jwt .decode (example_jwt , example_secret )
5463 self .assertEqual (decoded_payload , example_payload )
5564
5665 def test_allow_skip_verification (self ):
5766 right_secret = 'foo'
58- bad_secret = 'bar'
5967 jwt_message = jwt .encode (self .payload , right_secret )
6068 decoded_payload = jwt .decode (jwt_message , verify = False )
6169 self .assertEqual (decoded_payload , self .payload )
6270
6371 def test_no_secret (self ):
6472 right_secret = 'foo'
65- bad_secret = 'bar'
6673 jwt_message = jwt .encode (self .payload , right_secret )
6774
6875 with self .assertRaises (jwt .DecodeError ):
6976 jwt .decode (jwt_message )
7077
7178 def test_invalid_crypto_alg (self ):
72- self .assertRaises (NotImplementedError , jwt .encode , self .payload , "secret" , "HS1024" )
79+ self .assertRaises (NotImplementedError , jwt .encode , self .payload ,
80+ "secret" , "HS1024" )
7381
7482 def test_unicode_secret (self ):
7583 secret = u'\xc2 '
@@ -78,47 +86,66 @@ def test_unicode_secret(self):
7886 self .assertEqual (decoded_payload , self .payload )
7987
8088 def test_nonascii_secret (self ):
81- secret = '\xc2 ' # char value that ascii codec cannot decode
89+ secret = '\xc2 ' # char value that ascii codec cannot decode
8290 jwt_message = jwt .encode (self .payload , secret )
8391 decoded_payload = jwt .decode (jwt_message , secret )
8492 self .assertEqual (decoded_payload , self .payload )
8593
8694 def test_decode_unicode_value (self ):
8795 example_payload = {"hello" : "world" }
8896 example_secret = "secret"
89- example_jwt = unicode ("eyJhbGciOiAiSFMyNTYiLCAidHlwIjogIkpXVCJ9.eyJoZWxsbyI6ICJ3b3JsZCJ9.tvagLDLoaiJKxOKqpBXSEGy7SYSifZhjntgm9ctpyj8" )
97+ example_jwt = (
98+ "eyJhbGciOiAiSFMyNTYiLCAidHlwIjogIkpXVCJ9"
99+ ".eyJoZWxsbyI6ICJ3b3JsZCJ9"
100+ ".tvagLDLoaiJKxOKqpBXSEGy7SYSifZhjntgm9ctpyj8" )
90101 decoded_payload = jwt .decode (example_jwt , example_secret )
91102 self .assertEqual (decoded_payload , example_payload )
92103
93104 def test_decode_invalid_header_padding (self ):
94- example_jwt = unicode ("aeyJhbGciOiAiSFMyNTYiLCAidHlwIjogIkpXVCJ9.eyJoZWxsbyI6ICJ3b3JsZCJ9.tvagLDLoaiJKxOKqpBXSEGy7SYSifZhjntgm9ctpyj8" )
105+ example_jwt = (
106+ "aeyJhbGciOiAiSFMyNTYiLCAidHlwIjogIkpXVCJ9"
107+ ".eyJoZWxsbyI6ICJ3b3JsZCJ9"
108+ ".tvagLDLoaiJKxOKqpBXSEGy7SYSifZhjntgm9ctpyj8" )
95109 example_secret = "secret"
96110 with self .assertRaises (jwt .DecodeError ):
97- jwt_message = jwt .decode (example_jwt , example_secret )
111+ jwt .decode (example_jwt , example_secret )
98112
99113 def test_decode_invalid_header_string (self ):
100- example_jwt = unicode ("eyJhbGciOiAiSFMyNTbpIiwgInR5cCI6ICJKV1QifQ==.eyJoZWxsbyI6ICJ3b3JsZCJ9.tvagLDLoaiJKxOKqpBXSEGy7SYSifZhjntgm9ctpyj8" )
114+ example_jwt = (
115+ "eyJhbGciOiAiSFMyNTbpIiwgInR5cCI6ICJKV1QifQ=="
116+ ".eyJoZWxsbyI6ICJ3b3JsZCJ9"
117+ ".tvagLDLoaiJKxOKqpBXSEGy7SYSifZhjntgm9ctpyj8" )
101118 example_secret = "secret"
102119 with self .assertRaisesRegexp (jwt .DecodeError , "Invalid header string" ):
103- jwt_message = jwt .decode (example_jwt , example_secret )
120+ jwt .decode (example_jwt , example_secret )
104121
105122 def test_decode_invalid_payload_padding (self ):
106- example_jwt = unicode ("eyJhbGciOiAiSFMyNTYiLCAidHlwIjogIkpXVCJ9.aeyJoZWxsbyI6ICJ3b3JsZCJ9.tvagLDLoaiJKxOKqpBXSEGy7SYSifZhjntgm9ctpyj8" )
123+ example_jwt = (
124+ "eyJhbGciOiAiSFMyNTYiLCAidHlwIjogIkpXVCJ9"
125+ ".aeyJoZWxsbyI6ICJ3b3JsZCJ9"
126+ ".tvagLDLoaiJKxOKqpBXSEGy7SYSifZhjntgm9ctpyj8" )
107127 example_secret = "secret"
108128 with self .assertRaises (jwt .DecodeError ):
109- jwt_message = jwt .decode (example_jwt , example_secret )
129+ jwt .decode (example_jwt , example_secret )
110130
111131 def test_decode_invalid_payload_string (self ):
112- example_jwt = unicode ("eyJhbGciOiAiSFMyNTYiLCAidHlwIjogIkpXVCJ9.eyJoZWxsb-kiOiAid29ybGQifQ==.tvagLDLoaiJKxOKqpBXSEGy7SYSifZhjntgm9ctpyj8" )
132+ example_jwt = (
133+ "eyJhbGciOiAiSFMyNTYiLCAidHlwIjogIkpXVCJ9"
134+ ".eyJoZWxsb-kiOiAid29ybGQifQ=="
135+ ".tvagLDLoaiJKxOKqpBXSEGy7SYSifZhjntgm9ctpyj8" )
113136 example_secret = "secret"
114- with self .assertRaisesRegexp (jwt .DecodeError , "Invalid payload string" ):
115- jwt_message = jwt .decode (example_jwt , example_secret )
137+ with self .assertRaisesRegexp (jwt .DecodeError ,
138+ "Invalid payload string" ):
139+ jwt .decode (example_jwt , example_secret )
116140
117141 def test_decode_invalid_crypto_padding (self ):
118- example_jwt = unicode ("eyJhbGciOiAiSFMyNTYiLCAidHlwIjogIkpXVCJ9.eyJoZWxsbyI6ICJ3b3JsZCJ9.aatvagLDLoaiJKxOKqpBXSEGy7SYSifZhjntgm9ctpyj8" )
142+ example_jwt = (
143+ "eyJhbGciOiAiSFMyNTYiLCAidHlwIjogIkpXVCJ9"
144+ ".eyJoZWxsbyI6ICJ3b3JsZCJ9"
145+ ".aatvagLDLoaiJKxOKqpBXSEGy7SYSifZhjntgm9ctpyj8" )
119146 example_secret = "secret"
120147 with self .assertRaises (jwt .DecodeError ):
121- jwt_message = jwt .decode (example_jwt , example_secret )
148+ jwt .decode (example_jwt , example_secret )
122149
123150 def test_decode_with_expiration (self ):
124151 self .payload ['exp' ] = utc_timestamp () - 1
0 commit comments