| 1 |
import json |
| 2 |
from http import HTTPStatus |
| 3 |
from urllib.parse import parse_qs, urlparse |
| 4 |
|
| 5 |
from django.test import TestCase |
| 6 |
from django.test.utils import override_settings |
| 7 |
from django.urls import reverse |
| 8 |
from django.utils.http import urlencode |
| 9 |
|
| 10 |
from allauth.socialaccount.models import SocialAccount |
| 11 |
from allauth.socialaccount.providers.shopify.provider import ShopifyProvider |
| 12 |
from tests.apps.socialaccount.base import OAuth2TestsMixin |
| 13 |
from tests.mocking import MockedResponse, mocked_response |
| 14 |
|
| 15 |
|
| 16 |
class ShopifyTests(OAuth2TestsMixin, TestCase): |
| 17 |
provider_id = ShopifyProvider.id |
| 18 |
|
| 19 |
def _complete_shopify_login(self, q, resp, resp_mock, with_refresh_token): |
| 20 |
complete_url = reverse(f"{self.provider.id}_callback") |
| 21 |
self.assertGreater(q["redirect_uri"][0].find(complete_url), 0) |
| 22 |
response_json = self.get_login_response_json( |
| 23 |
with_refresh_token=with_refresh_token |
| 24 |
) |
| 25 |
with mocked_response( |
| 26 |
MockedResponse( |
| 27 |
HTTPStatus.OK, response_json, {"content-type": "application/json"} |
| 28 |
), |
| 29 |
resp_mock, |
| 30 |
): |
| 31 |
resp = self.client.get( |
| 32 |
complete_url, |
| 33 |
{ |
| 34 |
"code": "test", |
| 35 |
"state": q["state"][0], |
| 36 |
"shop": "test", |
| 37 |
}, |
| 38 |
) |
| 39 |
return resp |
| 40 |
|
| 41 |
def login(self, resp_mock, process="login", with_refresh_token=True): |
| 42 |
url = ( |
| 43 |
reverse(f"{self.provider.id}_login") |
| 44 |
+ "?" |
| 45 |
+ urlencode({"process": process, "shop": "test"}) |
| 46 |
) |
| 47 |
resp = self.client.post(url) |
| 48 |
self.assertEqual(resp.status_code, HTTPStatus.FOUND) |
| 49 |
p = urlparse(resp["location"]) |
| 50 |
q = parse_qs(p.query) |
| 51 |
resp = self._complete_shopify_login(q, resp, resp_mock, with_refresh_token) |
| 52 |
return resp |
| 53 |
|
| 54 |
def get_mocked_response(self): |
| 55 |
return MockedResponse( |
| 56 |
HTTPStatus.OK, |
| 57 |
""" |
| 58 |
{ |
| 59 |
"shop": { |
| 60 |
"id": "1234566", |
| 61 |
"name": "Test Shop", |
| 62 |
"email": "[email protected]" |
| 63 |
} |
| 64 |
} |
| 65 |
""", |
| 66 |
) |
| 67 |
|
| 68 |
def get_expected_to_str(self): |
| 69 |
return "[email protected]" |
| 70 |
|
| 71 |
|
| 72 |
@override_settings(SOCIALACCOUNT_PROVIDERS={"shopify": {"IS_EMBEDDED": True}}) |
| 73 |
class ShopifyEmbeddedTests(ShopifyTests): |
| 74 |
""" |
| 75 |
Shopify embedded apps (that run within an iFrame) require a JS (not server) |
| 76 |
redirect for starting the oauth2 process. |
| 77 |
|
| 78 |
See Also: |
| 79 |
https://help.shopify.com/api/sdks/embedded-app-sdk/getting-started#oauth |
| 80 |
""" |
| 81 |
|
| 82 |
def login(self, resp_mock, process="login", with_refresh_token=True): |
| 83 |
resp = self.client.post( |
| 84 |
reverse(f"{self.provider.id}_login") |
| 85 |
+ "?" |
| 86 |
+ urlencode({"process": process, "shop": "test"}), |
| 87 |
) |
| 88 |
self.assertEqual(resp.status_code, HTTPStatus.OK) # No re-direct, JS must do it |
| 89 |
actual_content = resp.content.decode("utf8") |
| 90 |
self.assertTrue( |
| 91 |
"script" in actual_content, |
| 92 |
f"Content missing script tag. [Actual: {actual_content}]", |
| 93 |
) |
| 94 |
self.assertTrue( |
| 95 |
resp.xframe_options_exempt, |
| 96 |
"Redirect JS must be allowed to run in Shopify iframe", |
| 97 |
) |
| 98 |
self.assertTrue( |
| 99 |
"<!DOCTYPE html><html><head>" in actual_content |
| 100 |
and "</head><body></body></html>" in actual_content, |
| 101 |
f"Expected standard HTML skeleton. [Actual: {actual_content}]", |
| 102 |
) |
| 103 |
p = urlparse( |
| 104 |
actual_content.split(";</script>")[0].split('location.href = "')[1] |
| 105 |
) |
| 106 |
q = parse_qs(p.query) |
| 107 |
resp = self._complete_shopify_login(q, resp, resp_mock, with_refresh_token) |
| 108 |
return resp |
| 109 |
|
| 110 |
|
| 111 |
@override_settings( |
| 112 |
SOCIALACCOUNT_PROVIDERS={ |
| 113 |
"shopify": {"AUTH_PARAMS": {"grant_options[]": "per-user"}} |
| 114 |
} |
| 115 |
) |
| 116 |
class ShopifyPerUserAccessTests(ShopifyTests): |
| 117 |
""" |
| 118 |
Shopify has two access modes, offline (the default) and online/per-user. |
| 119 |
Enabling 'online' access should cause all-auth to tie the logged in |
| 120 |
Shopify user to the all-auth account (rather than the shop as a whole). |
| 121 |
|
| 122 |
See Also: |
| 123 |
https://help.shopify.com/api/getting-started/authentication/ |
| 124 |
oauth#api-access-modes |
| 125 |
""" |
| 126 |
|
| 127 |
def get_login_response_json(self, with_refresh_token=True): |
| 128 |
response_data = { |
| 129 |
"access_token": "testac", |
| 130 |
"account_number": None, |
| 131 |
"associated_user": { |
| 132 |
"account_owner": True, |
| 133 |
"collaborator": False, |
| 134 |
"email": "[email protected]", |
| 135 |
"email_verified": True, |
| 136 |
"first_name": "John", |
| 137 |
"id": 902541635, |
| 138 |
"last_name": "Smith", |
| 139 |
"locale": "en", |
| 140 |
}, |
| 141 |
"associated_user_scope": "read_products,read_customers,read_inventory,write_merchant_managed_fulfillment_orders,write_third_party_fulfillment_orders,read_orders,write_assigned_fulfillment_orders", |
| 142 |
"expires_in": 86381, |
| 143 |
"scope": "read_products,read_customers,read_inventory,write_merchant_managed_fulfillment_orders,write_third_party_fulfillment_orders,read_orders,write_assigned_fulfillment_orders", |
| 144 |
"session": None, |
| 145 |
} |
| 146 |
if with_refresh_token: |
| 147 |
response_data["refresh_token"] = "testrf" |
| 148 |
|
| 149 |
return json.dumps(response_data) |
| 150 |
|
| 151 |
@override_settings( |
| 152 |
SOCIALACCOUNT_AUTO_SIGNUP=True, |
| 153 |
SOCIALACCOUNT_EMAIL_REQUIRED=True, |
| 154 |
ACCOUNT_EMAIL_REQUIRED=True, |
| 155 |
) |
| 156 |
def test_associated_user(self): |
| 157 |
resp_mocks = self.get_mocked_response() |
| 158 |
resp = self.login(resp_mocks) |
| 159 |
self.assertRedirects(resp, "/accounts/profile/", fetch_redirect_response=False) |
| 160 |
|
| 161 |
social_account = SocialAccount.objects.filter( |
| 162 |
provider=self.provider.id, |
| 163 |
uid=902541635, |
| 164 |
).first() |
| 165 |
self.assertIsNotNone(social_account) |
| 166 |
self.assertTrue("associated_user" in social_account.extra_data) |
| 167 |
|
| 168 |
self.assertEqual(social_account.user.email, "[email protected]") |
| 169 |
self.assertEqual(social_account.user.first_name, "John") |
| 170 |
self.assertEqual(social_account.user.last_name, "Smith") |