Skip to content

Commit 9e60cf7

Browse files
committed
add tests
1 parent 4ab3e59 commit 9e60cf7

File tree

3 files changed

+57
-6
lines changed

3 files changed

+57
-6
lines changed

requests_oauthlib/compliance_fixes/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from .fitbit import fitbit_compliance_fix
55
from .linkedin import linkedin_compliance_fix
66
from .slack import slack_compliance_fix
7+
from .instagram import instagram_compliance_fix
78
from .mailchimp import mailchimp_compliance_fix
89
from .weibo import weibo_compliance_fix
910
from .plentymarkets import plentymarkets_compliance_fix

requests_oauthlib/compliance_fixes/instagram.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,12 @@
88

99
def instagram_compliance_fix(session):
1010
def _non_compliant_param_name(url, headers, data):
11-
# If the user has already specified the token, either in the URL
12-
# or in a data dictionary, then there's nothing to do.
11+
# If the user has already specified the token in the URL
12+
# then there's nothing to do.
1313
# If the specified token is different from ``session.access_token``,
1414
# we assume the user intends to override the access token.
1515
url_query = dict(parse_qs(urlparse(url).query))
16-
token = url_query.get("token")
17-
if not token and isinstance(data, dict):
18-
token = data.get("token")
19-
16+
token = url_query.get("access_token")
2017
if token:
2118
# Nothing to do, just return.
2219
return url, headers, data

tests/test_compliance_fixes.py

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
from requests_oauthlib.compliance_fixes import mailchimp_compliance_fix
1818
from requests_oauthlib.compliance_fixes import weibo_compliance_fix
1919
from requests_oauthlib.compliance_fixes import slack_compliance_fix
20+
from requests_oauthlib.compliance_fixes import instagram_compliance_fix
2021
from requests_oauthlib.compliance_fixes import plentymarkets_compliance_fix
2122

2223

@@ -275,6 +276,58 @@ def test_protected_request_override_token_url(self):
275276
self.assertIsNone(response.request.body)
276277

277278

279+
class InstagramComplianceFixTest(TestCase):
280+
281+
def setUp(self):
282+
mocker = requests_mock.Mocker()
283+
mocker.request(
284+
method="GET",
285+
url="https://api.instagram.com/v1/users/self",
286+
json={
287+
"data": {
288+
"id": "1574083",
289+
"username": "snoopdogg",
290+
"full_name": "Snoop Dogg",
291+
"profile_picture": "http://distillery.s3.amazonaws.com/profiles/profile_1574083_75sq_1295469061.jpg",
292+
"bio": "This is my bio",
293+
"website": "http://snoopdogg.com",
294+
"is_business": False,
295+
"counts": {
296+
"media": 1320,
297+
"follows": 420,
298+
"followed_by": 3410
299+
}
300+
}
301+
}
302+
)
303+
mocker.start()
304+
self.addCleanup(mocker.stop)
305+
306+
instagram = OAuth2Session('someclientid', redirect_uri='https://i.b')
307+
self.session = instagram_compliance_fix(instagram)
308+
309+
def test_protected_request(self):
310+
self.session.token = {"access_token": 'dummy-access-token'}
311+
response = self.session.get(
312+
"https://api.instagram.com/v1/users/self"
313+
)
314+
url = response.request.url
315+
query = parse_qs(urlparse(url).query)
316+
self.assertIn("access_token", query)
317+
self.assertEqual(query["access_token"], ["dummy-access-token"])
318+
319+
def test_protected_request_dont_override(self):
320+
"""check that if the access_token param
321+
already exist we don't override it"""
322+
self.session.token = {"access_token": 'dummy-access-token'}
323+
response = self.session.get(
324+
"https://api.instagram.com/v1/users/self?access_token=correct-access-token"
325+
)
326+
url = response.request.url
327+
query = parse_qs(urlparse(url).query)
328+
self.assertIn("access_token", query)
329+
self.assertEqual(query["access_token"], ["correct-access-token"])
330+
278331
class PlentymarketsComplianceFixTest(TestCase):
279332

280333
def setUp(self):

0 commit comments

Comments
 (0)