-
Notifications
You must be signed in to change notification settings - Fork 388
Expand file tree
/
Copy pathsession_test.py
More file actions
302 lines (252 loc) · 14.1 KB
/
session_test.py
File metadata and controls
302 lines (252 loc) · 14.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
import shopify
from test.test_helper import TestCase
import hmac
from hashlib import sha256
import time
import urllib.parse
class SessionTest(TestCase):
@classmethod
def setUpClass(self):
shopify.ApiVersion.define_known_versions()
shopify.ApiVersion.define_version(shopify.Release("2019-04"))
@classmethod
def tearDownClass(self):
shopify.ApiVersion.clear_defined_versions()
def test_not_be_valid_without_a_url(self):
session = shopify.Session("", "unstable", "any-token")
self.assertFalse(session.valid)
def test_not_be_valid_without_token(self):
session = shopify.Session("testshop.myshopify.com", "unstable")
self.assertFalse(session.valid)
def test_be_valid_with_any_token_and_any_url(self):
session = shopify.Session("testshop.myshopify.com", "unstable", "any-token")
self.assertTrue(session.valid)
def test_ignore_everything_but_the_subdomain_in_the_shop(self):
session = shopify.Session("http://user:pass@testshop.notshopify.net/path", "unstable", "any-token")
self.assertEqual("https://testshop.myshopify.com/admin/api/unstable", session.site)
def test_append_the_myshopify_domain_if_not_given(self):
session = shopify.Session("testshop", "unstable", "any-token")
self.assertEqual("https://testshop.myshopify.com/admin/api/unstable", session.site)
def test_raise_error_if_params_passed_but_signature_omitted(self):
with self.assertRaises(shopify.ValidationException):
session = shopify.Session("testshop.myshopify.com", "unstable")
token = session.request_token({"code": "any_code", "foo": "bar", "timestamp": "1234"})
def test_setup_api_key_and_secret_for_all_sessions(self):
shopify.Session.setup(api_key="My test key", secret="My test secret")
self.assertEqual("My test key", shopify.Session.api_key)
self.assertEqual("My test secret", shopify.Session.secret)
def test_use_https_protocol_by_default_for_all_sessions(self):
self.assertEqual("https", shopify.Session.protocol)
def test_temp_reset_shopify_shopify_resource_site_to_original_value(self):
shopify.Session.setup(api_key="key", secret="secret")
session1 = shopify.Session("fakeshop.myshopify.com", "2019-04", "token1")
shopify.ShopifyResource.activate_session(session1)
assigned_site = ""
with shopify.Session.temp("testshop.myshopify.com", "unstable", "any-token"):
assigned_site = shopify.ShopifyResource.site
self.assertEqual("https://testshop.myshopify.com/admin/api/unstable", assigned_site)
self.assertEqual("https://fakeshop.myshopify.com/admin/api/2019-04", shopify.ShopifyResource.site)
def test_myshopify_domain_supports_non_standard_ports(self):
try:
shopify.Session.setup(api_key="key", secret="secret", myshopify_domain="localhost", port=3000)
session = shopify.Session("fakeshop.localhost:3000", "unstable", "token1")
shopify.ShopifyResource.activate_session(session)
self.assertEqual("https://fakeshop.localhost:3000/admin/api/unstable", shopify.ShopifyResource.site)
session = shopify.Session("fakeshop", "unstable", "token1")
shopify.ShopifyResource.activate_session(session)
self.assertEqual("https://fakeshop.localhost:3000/admin/api/unstable", shopify.ShopifyResource.site)
finally:
shopify.Session.setup(myshopify_domain="myshopify.com", port=None)
def test_temp_works_without_currently_active_session(self):
shopify.ShopifyResource.clear_session()
assigned_site = ""
with shopify.Session.temp("testshop.myshopify.com", "unstable", "any-token"):
assigned_site = shopify.ShopifyResource.site
self.assertEqual("https://testshop.myshopify.com/admin/api/unstable", assigned_site)
self.assertEqual("https://none/admin/api/unstable", shopify.ShopifyResource.site)
def test_create_permission_url_returns_correct_url_with_single_scope_and_redirect_uri(self):
shopify.Session.setup(api_key="My_test_key", secret="My test secret")
session = shopify.Session("http://localhost.myshopify.com", "unstable")
scope = ["write_products"]
permission_url = session.create_permission_url(scope, "my_redirect_uri.com")
self.assertEqual(
"https://localhost.myshopify.com/admin/oauth/authorize?client_id=My_test_key&redirect_uri=my_redirect_uri.com&scope=write_products",
self.normalize_url(permission_url),
)
def test_create_permission_url_returns_correct_url_with_dual_scope_and_redirect_uri(self):
shopify.Session.setup(api_key="My_test_key", secret="My test secret")
session = shopify.Session("http://localhost.myshopify.com", "unstable")
scope = ["write_products", "write_customers"]
permission_url = session.create_permission_url(scope, "my_redirect_uri.com")
self.assertEqual(
"https://localhost.myshopify.com/admin/oauth/authorize?client_id=My_test_key&redirect_uri=my_redirect_uri.com&scope=write_products%2Cwrite_customers",
self.normalize_url(permission_url),
)
def test_create_permission_url_returns_correct_url_with_no_scope_and_redirect_uri(self):
shopify.Session.setup(api_key="My_test_key", secret="My test secret")
session = shopify.Session("http://localhost.myshopify.com", "unstable")
scope = []
permission_url = session.create_permission_url(scope, "my_redirect_uri.com")
self.assertEqual(
"https://localhost.myshopify.com/admin/oauth/authorize?client_id=My_test_key&redirect_uri=my_redirect_uri.com&scope=",
self.normalize_url(permission_url),
)
def test_create_permission_url_returns_correct_url_with_no_scope_and_redirect_uri_and_state(self):
shopify.Session.setup(api_key="My_test_key", secret="My test secret")
session = shopify.Session("http://localhost.myshopify.com", "unstable")
scope = []
permission_url = session.create_permission_url(scope, "my_redirect_uri.com", state="mystate")
self.assertEqual(
"https://localhost.myshopify.com/admin/oauth/authorize?client_id=My_test_key&redirect_uri=my_redirect_uri.com&scope=&state=mystate",
self.normalize_url(permission_url),
)
def test_create_permission_url_returns_correct_url_with_single_scope_and_redirect_uri_and_state(self):
shopify.Session.setup(api_key="My_test_key", secret="My test secret")
session = shopify.Session("http://localhost.myshopify.com", "unstable")
scope = ["write_customers"]
permission_url = session.create_permission_url(scope, "my_redirect_uri.com", state="mystate")
self.assertEqual(
"https://localhost.myshopify.com/admin/oauth/authorize?client_id=My_test_key&redirect_uri=my_redirect_uri.com&scope=write_customers&state=mystate",
self.normalize_url(permission_url),
)
def test_raise_exception_if_code_invalid_in_request_token(self):
shopify.Session.setup(api_key="My test key", secret="My test secret")
session = shopify.Session("http://localhost.myshopify.com", "unstable")
self.fake(
None,
url="https://localhost.myshopify.com/admin/oauth/access_token",
method="POST",
code=404,
body='{"error" : "invalid_request"}',
has_user_agent=False,
)
with self.assertRaises(shopify.ValidationException):
session.request_token({"code": "any-code", "timestamp": "1234"})
self.assertFalse(session.valid)
def test_return_site_for_session(self):
session = shopify.Session("testshop.myshopify.com", "unstable", "any-token")
self.assertEqual("https://testshop.myshopify.com/admin/api/unstable", session.site)
def test_hmac_calculation(self):
# Test using the secret and parameter examples given in the Shopify API documentation.
shopify.Session.secret = "hush"
params = {
"shop": "some-shop.myshopify.com",
"code": "a94a110d86d2452eb3e2af4cfb8a3828",
"timestamp": "1337178173",
"hmac": "2cb1a277650a659f1b11e92a4a64275b128e037f2c3390e3c8fd2d8721dac9e2",
}
self.assertEqual(shopify.Session.calculate_hmac(params), params["hmac"])
def test_hmac_calculation_with_ampersand_and_equal_sign_characters(self):
shopify.Session.secret = "secret"
params = {"a": "1&b=2", "c=3&d": "4"}
to_sign = "a=1%26b=2&c%3D3%26d=4"
expected_hmac = hmac.new("secret".encode(), to_sign.encode(), sha256).hexdigest()
self.assertEqual(shopify.Session.calculate_hmac(params), expected_hmac)
def test_hmac_validation(self):
# Test using the secret and parameter examples given in the Shopify API documentation.
shopify.Session.secret = "hush"
params = {
"shop": "some-shop.myshopify.com",
"code": "a94a110d86d2452eb3e2af4cfb8a3828",
"timestamp": "1337178173",
"hmac": "2cb1a277650a659f1b11e92a4a64275b128e037f2c3390e3c8fd2d8721dac9e2",
}
self.assertTrue(shopify.Session.validate_hmac(params))
def test_parameter_validation_handles_missing_params(self):
# Test using the secret and parameter examples given in the Shopify API documentation.
shopify.Session.secret = "hush"
params = {
"shop": "some-shop.myshopify.com",
"code": "a94a110d86d2452eb3e2af4cfb8a3828",
"hmac": "2cb1a277650a659f1b11e92a4a64275b128e037f2c3390e3c8fd2d8721dac9e2",
}
self.assertFalse(shopify.Session.validate_params(params))
def test_param_validation_of_param_values_with_lists(self):
shopify.Session.secret = "hush"
params = {
"shop": "some-shop.myshopify.com",
"ids[]": [
2,
1,
],
"hmac": "b93b9f82996f6f8bf9f1b7bbddec284c8fabacdc4e12dc80550b4705f3003b1e",
}
self.assertEqual(True, shopify.Session.validate_hmac(params))
def test_return_token_and_scope_if_hmac_is_valid(self):
shopify.Session.secret = "secret"
params = {"code": "any-code", "timestamp": time.time()}
hmac = shopify.Session.calculate_hmac(params)
params["hmac"] = hmac
self.fake(
None,
url="https://localhost.myshopify.com/admin/oauth/access_token",
method="POST",
body='{"access_token" : "token", "scope": "read_products,write_orders"}',
has_user_agent=False,
)
session = shopify.Session("http://localhost.myshopify.com", "unstable")
token = session.request_token(params)
self.assertEqual("token", token)
self.assertEqual(shopify.ApiAccess("read_products,write_orders"), session.access_scopes)
def test_raise_error_if_hmac_is_invalid(self):
shopify.Session.secret = "secret"
params = {"code": "any-code", "timestamp": time.time()}
params["hmac"] = "a94a110d86d2452e92a4a64275b128e9273be3037f2c339eb3e2af4cfb8a3828"
with self.assertRaises(shopify.ValidationException):
session = shopify.Session("http://localhost.myshopify.com", "unstable")
session = session.request_token(params)
def test_raise_error_if_hmac_does_not_match_expected(self):
shopify.Session.secret = "secret"
params = {"foo": "hello", "timestamp": time.time()}
hmac = shopify.Session.calculate_hmac(params)
params["hmac"] = hmac
params["bar"] = "world"
params["code"] = "code"
with self.assertRaises(shopify.ValidationException):
session = shopify.Session("http://localhost.myshopify.com", "unstable")
session = session.request_token(params)
def test_raise_error_if_timestamp_is_too_old(self):
shopify.Session.secret = "secret"
one_day = 24 * 60 * 60
params = {"code": "any-code", "timestamp": time.time() - (2 * one_day)}
hmac = shopify.Session.calculate_hmac(params)
params["hmac"] = hmac
with self.assertRaises(shopify.ValidationException):
session = shopify.Session("http://localhost.myshopify.com", "unstable")
session = session.request_token(params)
def test_access_scopes_are_nil_by_default(self):
session = shopify.Session("testshop.myshopify.com", "unstable", "any-token")
self.assertIsNone(session.access_scopes)
def test_access_scopes_when_valid_scopes_passed_in(self):
session = shopify.Session(
shop_url="testshop.myshopify.com",
version="unstable",
token="any-token",
access_scopes="read_products, write_orders",
)
expected_access_scopes = shopify.ApiAccess("read_products, write_orders")
self.assertEqual(expected_access_scopes, session.access_scopes)
def test_access_scopes_set_with_api_access_object_passed_in(self):
session = shopify.Session(
shop_url="testshop.myshopify.com",
version="unstable",
token="any-token",
access_scopes=shopify.ApiAccess("read_products, write_orders"),
)
expected_access_scopes = shopify.ApiAccess("read_products, write_orders")
self.assertEqual(expected_access_scopes, session.access_scopes)
def normalize_url(self, url):
scheme, netloc, path, query, fragment = urllib.parse.urlsplit(url)
query = "&".join(sorted(query.split("&")))
return urllib.parse.urlunsplit((scheme, netloc, path, query, fragment))
def test_session_with_coerced_version(self):
future_version = "2030-01"
session = shopify.Session("test.myshopify.com", future_version, "token")
self.assertEqual(session.api_version.name, future_version)
self.assertEqual(
session.api_version.api_path("https://test.myshopify.com"),
f"https://test.myshopify.com/admin/api/{future_version}",
)
def test_session_with_invalid_version(self):
with self.assertRaises(shopify.VersionNotFoundError):
shopify.Session("test.myshopify.com", "invalid-version", "token")