def shopify_callback(): if not 'shop_name' in session: abort(401) shop_name = session['shop_name'] # Get the base app credentials base_credentials = app.config.get('SHOPIFY_CREDENTIALS') #Generate a new credential object with the base values credentials = Credentials( api_key=base_credentials.api_key, secret=base_credentials.secret ) #Setup a shopify adapter instance to create the authorization url shopify = Shopify(shop_name=shop_name, credentials=credentials) #Verify the signature if not shopify.verify_signature(request.args): raise Exception("invalid signature") #Update the credentials object with the provided temporary code credentials.code = request.args.get('code') #Exchange the code for an access token shopify.setup_access_token() #Store the access token in the session session['access_token'] = credentials.oauth_access_token return redirect(url_for('shop.view'))
def test_oauth_authorize_url(self): credentials = Credentials() shopify = OAuthEngine(shop_name='test', credentials=credentials) credentials.scope = ['yup'] url = shopify.oauth_authorize_url( redirect_to='http://localhost/installed') expected = "https://test.myshopify.com/admin/oauth/authorize?" \ "client_id=&scope=yup&" \ "redirect_uri=http%3A%2F%2Flocalhost%2Finstalled" self.assertEquals(url, expected) url = shopify.oauth_authorize_url() expected = "https://test.myshopify.com/admin/oauth/authorize?" \ "client_id=&scope=yup" self.assertEquals(url, expected) api_key = "2e6fff2c-e28d-11e2-b6bf-4061860bdbf3" credentials.api_key = api_key url = shopify.oauth_authorize_url() expected = "https://test.myshopify.com/admin/oauth/authorize?" \ "client_id={api_key}&scope=yup".format(api_key=api_key) self.assertEquals(url, expected) credentials.scope = ["fun", "things", "to", "scope", "W$%3'#"] url = shopify.oauth_authorize_url() expected = "https://test.myshopify.com/admin/oauth/authorize?" \ "client_id=2e6fff2c-e28d-11e2-b6bf-4061860bdbf3&" \ "scope=fun%2Cthings%2Cto%2Cscope%2CW%24%253%27%23" self.assertEquals(url, expected)
def test_headers(self): credentials = Credentials() shopify = Shopify(shop_name='test', credentials=credentials) expected = "application/json; charset=utf-8" self.assertEquals(shopify.session.headers['Content-Type'], expected) credentials = Credentials(oauth_access_token="test") shopify = Shopify(shop_name='test', credentials=credentials) expected = "test" self.assertEquals(shopify.session.headers["X-Shopify-Access-Token"], expected)
def test_delete(self): credentials = Credentials() shopify = Shopify(shop_name='test', credentials=credentials) response = requests.Response() response.status_code = 200 shopify.session.delete = mock.Mock(return_value=response) # A new entity should not be removable. try: instance = TestModel() shopify.delete(instance) self.fail() except InvalidRequestException: pass instance = TestModel(id=1) result = shopify.delete(instance) self.assertTrue(result) response = requests.Response() response.status_code = 404 shopify.session.delete = mock.Mock(return_value=response) try: instance = TestModel(id=4) shopify.delete(instance) self.fail() except ShopifyException: pass
def test_custom_post(self): encoding = 'UTF-8' credentials = Credentials() shopify = Shopify(shop_name='test', credentials=credentials) data = '{"test_model": {"id": 1, "name": "test"}}' response = requests.Response() response.encoding = encoding response._content = data.encode(encoding) response.status_code = 200 shopify.session.post = mock.Mock(return_value=response) result = shopify.custom_post(TestModel, "/custom") self.assertEquals(result, json.loads(data)) instance = TestModel(id="test") try: response = requests.Response() response.encoding = encoding response._content = data.encode(encoding) response.status_code = 404 shopify.session.post = mock.Mock(return_value=response) shopify.custom_post(instance, "/custom") self.fail() except ShopifyException: pass
def test_count(self): encoding = 'UTF-8' credentials = Credentials() shopify = Shopify(shop_name='test', credentials=credentials) data = '{"count": 2}' response = requests.Response() response.encoding = encoding response._content = data.encode(encoding) response.status_code = 200 shopify.session.get = mock.Mock(return_value=response) result = shopify.count(TestModel) self.assertEquals(result, 2) try: response = requests.Response() response.encoding = encoding response._content = data.encode(encoding) response.status_code = 404 shopify.session.get = mock.Mock(return_value=response) result = shopify.count(TestModel) self.fail() except ShopifyException: pass
def test_fetch(self): encoding = 'UTF-8' credentials = Credentials() shopify = Shopify(shop_name='test', credentials=credentials) data = '{"test_model": {"id": 1, "name": "test"}}' response = requests.Response() response.encoding = encoding response._content = data.encode(encoding) response.status_code = 200 shopify.session.get = mock.Mock(return_value=response) instance = shopify.fetch(TestModel, 2) self.assertIsInstance(instance, TestModel) self.assertEquals(instance.name, "test") self.assertEquals(instance.id, 1) result = shopify.fetch(TestModel, 2, auto_instance=False) self.assertIsInstance(result, dict) try: response = requests.Response() response.encoding = encoding response._content = data.encode(encoding) response.status_code = 404 shopify.session.get = mock.Mock(return_value=response) result = shopify.fetch(TestModel, 2) self.fail() except ShopifyException: pass
def test_index(self): encoding = 'UTF-8' credentials = Credentials() shopify = Shopify(shop_name='test', credentials=credentials) data = '{"test_models": [{"id": 1, "name": "test"}]}' response = requests.Response() response.encoding = encoding response._content = data.encode(encoding) response.status_code = 200 shopify.session.get = mock.Mock(return_value=response) result = shopify.index(TestModel) self.assertIsInstance(result, Collection) result = shopify.index(TestModel, auto_instance=False) self.assertIsInstance(result, dict) self.assertTrue("test_models" in result) try: response = requests.Response() response.encoding = encoding response._content = data.encode(encoding) response.status_code = 404 shopify.session.get = mock.Mock(return_value=response) result = shopify.index(TestModel) self.fail() except ShopifyException: pass
def test_authorize_app_url(self): credentials = Credentials() shopify = Shopify(shop_name='test', credentials=credentials) expected = "https://test.myshopify.com/admin/oauth/authorize" \ "?client_id=&scope=" result = shopify.authorize_app_url() self.assertEquals(result, expected)
def test_verify_signature(self): credentials = Credentials(secret="test") shopify = OAuthEngine(shop_name='test', credentials=credentials) params = [("code", "86cbee47aea11249e7042167b90e38c7"), ("shop", "test"), ("timestamp", "1373383855"), ("signature", "6019d0e365811b206fdd7f89037e7400")] self.assertTrue(shopify.verify_signature(params)) params = [("code", "86cbee47aea11249e7"), ("shop", "test"), ("timestamp", "1373383855"), ("signature", "6019d0e365811b206fdd7f89037e7400")] self.assertFalse(shopify.verify_signature(params))
def test_update(self): encoding = 'UTF-8' credentials = Credentials() shopify = Shopify(shop_name='test', credentials=credentials) # A new entity should not be updateable without it's pk being set. try: instance = TestModel() shopify.update(instance) self.fail() except InvalidRequestException: pass data = '{"test_model": {"name": "test"}}' response = requests.Response() response.encoding = encoding response._content = data.encode(encoding) response.status_code = 200 shopify.session.put = mock.Mock(return_value=response) instance = TestModel(id=2) shopify.update(instance) self.assertEquals(instance.name, "test") #TODO Mock the OAuthEngine.put method to capture the extra prop shopify.ignore_model_properties = True instance = TestModel(id=1, extra_property="Hello") shopify.update(instance) self.assertEquals(instance.name, "test") self.assertEquals(instance.id, 1) shopify.ignore_model_properties = False instance = TestModel(id=1) result = shopify.update(instance, auto_update=False) self.assertIsInstance(result, dict) self.assertFalse(hasattr(instance, "name")) try: response = requests.Response() response.encoding = encoding response._content = data.encode(encoding) response.status_code = 404 shopify.session.put = mock.Mock(return_value=response) result = shopify.update(instance) self.fail() except ShopifyException: pass
def test_oauth_authorize_url(self): credentials = Credentials() shopify = OAuthEngine(shop_name='test', credentials=credentials) credentials.scope = ['yup'] url = shopify.oauth_authorize_url( redirect_to='http://localhost/installed' ) expected = "https://test.myshopify.com/admin/oauth/authorize?" \ "client_id=&scope=yup&" \ "redirect_uri=http%3A%2F%2Flocalhost%2Finstalled" self.assertEquals(url, expected) url = shopify.oauth_authorize_url() expected = "https://test.myshopify.com/admin/oauth/authorize?" \ "client_id=&scope=yup" self.assertEquals(url, expected) api_key = "2e6fff2c-e28d-11e2-b6bf-4061860bdbf3" credentials.api_key = api_key url = shopify.oauth_authorize_url() expected = "https://test.myshopify.com/admin/oauth/authorize?" \ "client_id={api_key}&scope=yup".format(api_key=api_key) self.assertEquals(url, expected) credentials.scope = ["fun", "things", "to", "scope", "W$%3'#"] url = shopify.oauth_authorize_url() expected = "https://test.myshopify.com/admin/oauth/authorize?" \ "client_id=2e6fff2c-e28d-11e2-b6bf-4061860bdbf3&" \ "scope=fun%2Cthings%2Cto%2Cscope%2CW%24%253%27%23" self.assertEquals(url, expected)
def test_setup_access_token(self): encoding = 'UTF-8' credentials = Credentials() shopify = Shopify(shop_name='test', credentials=credentials) data = '{"access_token": "test"}' response = requests.Response() response.encoding = encoding response._content = data.encode(encoding) response.status_code = 200 shopify.session.post = mock.Mock(return_value=response) shopify.setup_access_token() self.assertEquals(credentials.oauth_access_token, "test") self.assertTrue('X-Shopify-Access-Token' in shopify.session.headers)
def test_fetch_subresource(self): encoding = 'UTF-8' credentials = Credentials() shopify = Shopify(shop_name='test', credentials=credentials) data = '{"test_sub_resource": {"id": 1, "name": "test"}}' response = requests.Response() response.encoding = encoding response._content = data.encode(encoding) response.status_code = 200 shopify.session.get = mock.Mock(return_value=response) instance = shopify.fetch(TestSubResource, 2, parent_id=1) self.assertIsInstance(instance, TestModel) self.assertEquals(instance.name, "test") self.assertEquals(instance.id, 1)
def test_url_for_request(self): credentials = Credentials() shopify = OAuthEngine(shop_name='test', credentials=credentials) request = Request() request.resource = "/test" url = shopify.url_for_request(request) # Note: the base request class does not have an extension. expected = "https://test.myshopify.com/admin/test." self.assertEquals(url, expected) request.resource = "/test/mmmm food" url = shopify.url_for_request(request) # Note: The url generated by url_for_request are not escaped. The # actual request.{method} will escape the url for us. expected = "https://test.myshopify.com/admin/test/mmmm food." self.assertEquals(url, expected)
def test_add(self): encoding = 'UTF-8' credentials = Credentials() shopify = Shopify(shop_name='test', credentials=credentials) data = '{"test_model": {"id": 1, "name": "test"}}' response = requests.Response() response.encoding = encoding response._content = data.encode(encoding) response.status_code = 201 shopify.session.post = mock.Mock(return_value=response) instance = TestModel(name="test") shopify.add(instance) self.assertEquals(instance.name, "test") self.assertEquals(instance.id, 1) #TODO Mock the OAuthEngine.post method to capture the extra prop shopify.ignore_model_properties = True instance = TestModel(name="test", extra_property="Hello") shopify.add(instance) self.assertEquals(instance.name, "test") self.assertEquals(instance.id, 1) shopify.ignore_model_properties = False instance = TestModel(name="test") result = shopify.add(instance, auto_update=False) self.assertEquals(instance.name, "test") self.assertFalse(hasattr(instance, "id")) self.assertIsInstance(result, dict) try: response = requests.Response() response.encoding = encoding response._content = data.encode(encoding) response.status_code = 404 shopify.session.post = mock.Mock(return_value=response) result = shopify.add(instance) self.fail() except ShopifyException: pass
def test_oauth_access_token_url(self): credentials = Credentials() shopify = OAuthEngine(shop_name='test', credentials=credentials) expected = "https://test.myshopify.com/admin/oauth/access_token?" \ "client_id=&client_secret=&code=" url = shopify.oauth_access_token_url() self.assertEqual(url, expected) credentials.api_key = api_key = "2e6fff2c-e28d-11e2-b6bf-4061860bdbf3" credentials.secret = secret = "mmmmsecret" credentials.code = code = "loremipsum" expected = "https://test.myshopify.com/admin/oauth/access_token?" \ "client_id={api_key}&client_secret={secret}&code={code}" \ .format(api_key=api_key, secret=secret, code=code) url = shopify.oauth_access_token_url() self.assertEqual(url, expected)
def test_can_request(self): credentials = Credentials() shopify = Shopify(shop_name='test', credentials=credentials) model = Model() try: shopify._can_request("create", model) self.fail() except InvalidRequestException: pass try: shopify._can_request("create", Model) self.fail() except InvalidRequestException: pass model.supported.append("create") shopify._can_request("create", model)
def test_update_call_limit(self): encoding = 'UTF-8' credentials = Credentials() shopify = OAuthEngine(shop_name='test', credentials=credentials) self.assertEqual(shopify.api_call_limit, None) data = '{"test_models": [{"id": 1, "name": "test"}]}' response = requests.Response() response.encoding = encoding response._content = data.encode(encoding) response.status_code = 200 shopify._update_call_limit(response) self.assertEqual(shopify.api_call_limit, None) response.headers['x-shopify-shop-api-call-limit'] = "1" shopify._update_call_limit(response) self.assertEqual(shopify.api_call_limit, "1")
def test_oauth_access_token(self): encoding = 'UTF-8' credentials = Credentials() shopify = Shopify(shop_name='test', credentials=credentials) data = '{"access_token": "test"}' response = requests.Response() response.encoding = encoding response._content = data.encode(encoding) response.status_code = 200 shopify.session.post = mock.Mock(return_value=response) access_token = shopify.oauth_access_token() self.assertEquals(access_token, "test") response.status_code = 403 shopify.session.post = mock.Mock(return_value=response) try: access_token = shopify.oauth_access_token() self.fail() except ShopifyException: pass