1
- from shopify . utils import *
1
+ from shopify import session_token
2
2
from test .test_helper import TestCase
3
3
from datetime import datetime , timedelta
4
4
@@ -13,7 +13,7 @@ def timestamp(date):
13
13
return time .mktime (date .timetuple ()) if sys .version_info [0 ] < 3 else date .timestamp ()
14
14
15
15
16
- class TestSessionTokenUtilityGetDecodedSessionToken (TestCase ):
16
+ class TestSessionTokenGetDecodedSessionToken (TestCase ):
17
17
@classmethod
18
18
def setUpClass (self ):
19
19
self .secret = "API Secret"
@@ -42,45 +42,45 @@ def build_auth_header(self):
42
42
def test_raises_if_token_authentication_header_is_not_bearer (self ):
43
43
authorization_header = "Bad auth header"
44
44
45
- with self .assertRaises (TokenAuthenticationError ):
46
- SessionTokenUtility .get_decoded_session_token (
47
- authorization_header , api_key = self . api_key , secret = self . secret
48
- )
45
+ with self .assertRaises (session_token . TokenAuthenticationError ) as cm :
46
+ session_token .get_decoded_session_token (authorization_header , api_key = self . api_key , secret = self . secret )
47
+
48
+ self . assertEqual ( "The HTTP_AUTHORIZATION_HEADER provided does not contain a Bearer token" , str ( cm . exception ) )
49
49
50
50
def test_raises_jwt_error_if_session_token_is_expired (self ):
51
51
self .payload ["exp" ] = timestamp ((datetime .now () + timedelta (0 , - 10 )))
52
52
53
- with self .assertRaises (jwt . exceptions . ExpiredSignatureError ) :
54
- SessionTokenUtility .get_decoded_session_token (
55
- self . build_auth_header (), api_key = self . api_key , secret = self . secret
56
- )
53
+ with self .assertRaises (session_token . SessionTokenError , msg = "Expird" ) as cm :
54
+ session_token .get_decoded_session_token (self . build_auth_header (), api_key = self . api_key , secret = self . secret )
55
+
56
+ self . assertEqual ( "Signature has expired" , str ( cm . exception ) )
57
57
58
58
def test_raises_if_aud_doesnt_match_api_key (self ):
59
59
self .payload ["aud" ] = "bad audience"
60
60
61
- with self .assertRaises (jwt . exceptions . InvalidAudienceError ) :
62
- SessionTokenUtility .get_decoded_session_token (
63
- self . build_auth_header (), api_key = self . api_key , secret = self . secret
64
- )
61
+ with self .assertRaises (session_token . SessionTokenError ) as cm :
62
+ session_token .get_decoded_session_token (self . build_auth_header (), api_key = self . api_key , secret = self . secret )
63
+
64
+ self . assertEqual ( "Invalid audience" , str ( cm . exception ) )
65
65
66
66
def test_raises_if_issuer_hostname_is_invalid (self ):
67
67
self .payload ["iss" ] = "bad_shop_hostname"
68
68
69
- with self .assertRaises (InvalidIssuerError ):
70
- SessionTokenUtility .get_decoded_session_token (
71
- self . build_auth_header (), api_key = self . api_key , secret = self . secret
72
- )
69
+ with self .assertRaises (session_token . InvalidIssuerError ) as cm :
70
+ session_token .get_decoded_session_token (self . build_auth_header (), api_key = self . api_key , secret = self . secret )
71
+
72
+ self . assertEqual ( "Invalid issuer" , str ( cm . exception ) )
73
73
74
74
def test_raises_if_iss_and_dest_dont_match (self ):
75
75
self .payload ["dest" ] = "bad_shop.myshopify.com"
76
76
77
- with self .assertRaises (MismatchedHostsError ):
78
- SessionTokenUtility .get_decoded_session_token (
79
- self . build_auth_header (), api_key = self . api_key , secret = self . secret
80
- )
77
+ with self .assertRaises (session_token . MismatchedHostsError ) as cm :
78
+ session_token .get_decoded_session_token (self . build_auth_header (), api_key = self . api_key , secret = self . secret )
79
+
80
+ self . assertEqual ( "The issuer and destination do not match" , str ( cm . exception ) )
81
81
82
82
def test_returns_decoded_payload (self ):
83
- decoded_payload = SessionTokenUtility .get_decoded_session_token (
83
+ decoded_payload = session_token .get_decoded_session_token (
84
84
self .build_auth_header (), api_key = self .api_key , secret = self .secret
85
85
)
86
86
0 commit comments