@@ -8,7 +8,7 @@ module Auth
88 class JwtPayloadTest < Test ::Unit ::TestCase
99 def setup
1010 super
11- @jwt_payload = {
11+ @admin_jwt_payload = {
1212 iss : "https://test-shop.myshopify.io/admin" ,
1313 dest : "https://test-shop.myshopify.io" ,
1414 aud : ShopifyAPI ::Context . api_key ,
@@ -19,12 +19,23 @@ def setup
1919 jti : "4321" ,
2020 sid : "abc123" ,
2121 }
22+
23+ @checkout_ui_extension_jwt_payload = {
24+ iss : "https://test-shop.myshopify.io/checkouts" ,
25+ dest : "test-shop.myshopify.io" ,
26+ aud : ShopifyAPI ::Context . api_key ,
27+ sub : "gid://shopify/Customer/123456789" ,
28+ exp : ( Time . now + 10 ) . to_i ,
29+ nbf : 1234 ,
30+ iat : 1234 ,
31+ jti : "4321" ,
32+ }
2233 end
2334
2435 def test_decode_jwt_payload_succeeds_with_valid_token
25- jwt_token = JWT . encode ( @jwt_payload , ShopifyAPI ::Context . api_secret_key , "HS256" )
36+ jwt_token = JWT . encode ( @admin_jwt_payload , ShopifyAPI ::Context . api_secret_key , "HS256" )
2637 decoded = ShopifyAPI ::Auth ::JwtPayload . new ( jwt_token )
27- assert_equal ( @jwt_payload ,
38+ assert_equal ( @admin_jwt_payload ,
2839 {
2940 iss : decoded . iss ,
3041 dest : decoded . dest ,
@@ -38,14 +49,14 @@ def test_decode_jwt_payload_succeeds_with_valid_token
3849 } )
3950
4051 # Helper methods
41- assert_equal ( decoded . expire_at , @jwt_payload [ :exp ] )
52+ assert_equal ( decoded . expire_at , @admin_jwt_payload [ :exp ] )
4253 assert_equal ( "test-shop.myshopify.io" , decoded . shopify_domain )
4354 assert_equal ( "test-shop.myshopify.io" , decoded . shop )
4455 assert_equal ( 1 , decoded . shopify_user_id )
4556 end
4657
4758 def test_decode_jwt_payload_succeeds_with_spin_domain
48- payload = @jwt_payload . dup
59+ payload = @admin_jwt_payload . dup
4960 payload [ :iss ] = "https://test-shop.other.spin.dev/admin"
5061 payload [ :dest ] = "https://test-shop.other.spin.dev"
5162 jwt_token = JWT . encode ( payload , ShopifyAPI ::Context . api_secret_key , "HS256" )
@@ -68,14 +79,14 @@ def test_decode_jwt_payload_succeeds_with_spin_domain
6879 end
6980
7081 def test_decode_jwt_payload_fails_with_wrong_key
71- jwt_token = JWT . encode ( @jwt_payload , "Wrong" , "HS256" )
82+ jwt_token = JWT . encode ( @admin_jwt_payload , "Wrong" , "HS256" )
7283 assert_raises ( ShopifyAPI ::Errors ::InvalidJwtTokenError ) do
7384 ShopifyAPI ::Auth ::JwtPayload . new ( jwt_token )
7485 end
7586 end
7687
7788 def test_decode_jwt_payload_fails_with_expired_token
78- payload = @jwt_payload . dup
89+ payload = @admin_jwt_payload . dup
7990 payload [ :exp ] = ( Time . now - 40 ) . to_i
8091 jwt_token = JWT . encode ( payload , ShopifyAPI ::Context . api_secret_key , "HS256" )
8192 assert_raises ( ShopifyAPI ::Errors ::InvalidJwtTokenError ) do
@@ -84,7 +95,7 @@ def test_decode_jwt_payload_fails_with_expired_token
8495 end
8596
8697 def test_decode_jwt_payload_fails_if_not_activated_yet
87- payload = @jwt_payload . dup
98+ payload = @admin_jwt_payload . dup
8899 payload [ :nbf ] = ( Time . now + 12 ) . to_i
89100 jwt_token = JWT . encode ( payload , ShopifyAPI ::Context . api_secret_key , "HS256" )
90101 assert_raises ( ShopifyAPI ::Errors ::InvalidJwtTokenError ) do
@@ -93,7 +104,7 @@ def test_decode_jwt_payload_fails_if_not_activated_yet
93104 end
94105
95106 def test_decode_jwt_payload_fails_with_invalid_api_key
96- jwt_token = JWT . encode ( @jwt_payload , ShopifyAPI ::Context . api_secret_key , "HS256" )
107+ jwt_token = JWT . encode ( @admin_jwt_payload , ShopifyAPI ::Context . api_secret_key , "HS256" )
97108
98109 modify_context ( api_key : "invalid" )
99110
@@ -103,7 +114,7 @@ def test_decode_jwt_payload_fails_with_invalid_api_key
103114 end
104115
105116 def test_decode_jwt_payload_succeeds_with_expiration_in_the_past_within_10s_leeway
106- payload = @jwt_payload . merge ( exp : Time . now . to_i - 8 )
117+ payload = @admin_jwt_payload . merge ( exp : Time . now . to_i - 8 )
107118 jwt_token = JWT . encode ( payload , ShopifyAPI ::Context . api_secret_key , "HS256" )
108119
109120 decoded = ShopifyAPI ::Auth ::JwtPayload . new ( jwt_token )
@@ -122,7 +133,7 @@ def test_decode_jwt_payload_succeeds_with_expiration_in_the_past_within_10s_leew
122133 end
123134
124135 def test_decode_jwt_payload_succeeds_with_not_before_in_the_future_within_10s_leeway
125- payload = @jwt_payload . merge ( nbf : Time . now . to_i + 8 )
136+ payload = @admin_jwt_payload . merge ( nbf : Time . now . to_i + 8 )
126137 jwt_token = JWT . encode ( payload , ShopifyAPI ::Context . api_secret_key , "HS256" )
127138
128139 decoded = ShopifyAPI ::Auth ::JwtPayload . new ( jwt_token )
@@ -141,8 +152,7 @@ def test_decode_jwt_payload_succeeds_with_not_before_in_the_future_within_10s_le
141152 end
142153
143154 def test_decode_jwt_payload_coming_from_checkout_ui_extension
144- payload = @jwt_payload . dup
145- payload [ :sid ] = nil
155+ payload = @checkout_ui_extension_jwt_payload . dup
146156 jwt_token = JWT . encode ( payload , ShopifyAPI ::Context . api_secret_key , "HS256" )
147157 decoded = ShopifyAPI ::Auth ::JwtPayload . new ( jwt_token )
148158 assert_equal ( payload ,
@@ -155,18 +165,16 @@ def test_decode_jwt_payload_coming_from_checkout_ui_extension
155165 nbf : decoded . nbf ,
156166 iat : decoded . iat ,
157167 jti : decoded . jti ,
158- sid : decoded . sid ,
159168 } )
160169
161- assert_equal ( decoded . expire_at , @jwt_payload [ :exp ] )
170+ assert_equal ( decoded . expire_at , @checkout_ui_extension_jwt_payload [ :exp ] )
162171 assert_equal ( "test-shop.myshopify.io" , decoded . shopify_domain )
163172 assert_equal ( "test-shop.myshopify.io" , decoded . shop )
164- assert_equal ( 1 , decoded . shopify_user_id )
173+ assert_nil ( decoded . shopify_user_id )
165174 end
166175
167176 def test_decode_jwt_payload_coming_from_checkout_ui_extension_without_user_logged_in
168- payload = @jwt_payload . dup
169- payload [ :sid ] = nil
177+ payload = @checkout_ui_extension_jwt_payload . dup
170178 payload [ :sub ] = nil
171179 jwt_token = JWT . encode ( payload , ShopifyAPI ::Context . api_secret_key , "HS256" )
172180 decoded = ShopifyAPI ::Auth ::JwtPayload . new ( jwt_token )
@@ -180,10 +188,9 @@ def test_decode_jwt_payload_coming_from_checkout_ui_extension_without_user_logge
180188 nbf : decoded . nbf ,
181189 iat : decoded . iat ,
182190 jti : decoded . jti ,
183- sid : decoded . sid ,
184191 } )
185192
186- assert_equal ( decoded . expire_at , @jwt_payload [ :exp ] )
193+ assert_equal ( decoded . expire_at , @checkout_ui_extension_jwt_payload [ :exp ] )
187194 assert_equal ( "test-shop.myshopify.io" , decoded . shopify_domain )
188195 assert_equal ( "test-shop.myshopify.io" , decoded . shop )
189196 assert_nil ( decoded . shopify_user_id )
0 commit comments