Skip to content

Commit 147d676

Browse files
authored
Merge pull request #9 from MastercardDevs/master
Add Interceptor test, and fix query_params
2 parents 55e204b + f093019 commit 147d676

File tree

3 files changed

+80
-6
lines changed

3 files changed

+80
-6
lines changed

oauth1/signer_interceptor.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
from functools import wraps
33
from oauth1.oauth import OAuth
44
from oauth1 import authenticationutils
5+
from urllib.parse import urlencode
56

67

78
class SignerInterceptor(object):
@@ -19,8 +20,13 @@ def oauth_signing(self, func):
1920
@wraps(func)
2021
def request_function(*args, **kwargs): # pragma: no cover
2122
in_body = kwargs.get("body", None)
23+
query_params = kwargs.get("query_params", None)
2224

23-
auth_header = OAuth().get_authorization_header(args[1], args[0], in_body,
25+
uri = args[1]
26+
if query_params:
27+
uri += '?' + urlencode(query_params)
28+
29+
auth_header = OAuth().get_authorization_header(uri, args[0], in_body,
2430
self.consumer_key, self.signing_key)
2531

2632
in_headers = kwargs.get("headers", None)

tests/test_from_readme.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
class OAuthReadmeTest(unittest.TestCase):
3838

3939

40+
""" can't call the service because the test keys can't access the service, you'll need to insert a valid key file, password, and consumer key """
4041
def test_from_readme(self):
4142
if os.path.exists('./test_key_container.p12'):
4243
uri = "https://sandbox.api.mastercard.com/fraud/merchant/v1/termination-inquiry?Format=XML&PageOffset=0"

tests/test_interceptor.py

Lines changed: 72 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,18 +50,85 @@ def test_add_interceptor(self):
5050
consumer_key = 'uLXKmWNmIkzIGKfA2injnNQqpZaxaBSKxa3ixEVu2f283c95!33b9b2bd960147e387fa6f3f238f07170000000000000000'
5151

5252
signing_layer1 = get_signing_layer(self, requests)
53-
5453
add_signing_layer(self, requests, key_file, key_password, consumer_key)
55-
5654
signing_layer2 = get_signing_layer(self, requests)
57-
5855
self.assertNotEqual(signing_layer1, signing_layer2)
59-
60-
6156
else:
6257
print("Please add a ./test_key_container.p12 file to enable key tests")
6358

6459

6560

61+
""" these will fail because the test keys can't access the service, you'll need to insert a valid key file, password, and consumer key """
62+
def test_without_interceptor(self):
63+
if os.path.exists('./test_key_container.p12'):
64+
key_file = './test_key_container.p12'
65+
key_password = "Password1"
66+
consumer_key = 'uLXKmWNmIkzIGKfA2injnNQqpZaxaBSKxa3ixEVu2f283c95!33b9b2bd960147e387fa6f3f238f07170000000000000000'
67+
signing_key = authenticationutils.load_signing_key(key_file, key_password)
68+
69+
baseUrl = 'https://sandbox.api.mastercard.com'
70+
71+
queryMap = {
72+
"Format": "JSON", # change this to toggle between and XML or JSON response
73+
"fxDate": "2016-09-30",
74+
"transCurr": "ALL",
75+
"crdhldBillCurr": "DZD",
76+
"bankFee": "5",
77+
"transAmt": "23"
78+
}
79+
80+
uri = baseUrl + "/settlement/currencyrate/conversion-rate?" + urlencode(queryMap)
81+
header = OAuth().get_authorization_header(uri, 'GET', None, consumer_key, signing_key)
82+
headers = {'Authorization': header, 'Content-Type': 'application/json'}
83+
84+
r = requests.get(uri, headers=headers)
85+
print(r.text)
86+
87+
""" these will fail because the test keys can't access the service, you'll need to insert a valid key file, password, and consumer key """
88+
def test_with_interceptor(self):
89+
if os.path.exists('./test_key_container.p12'):
90+
key_file = './test_key_container.p12'
91+
key_password = "Password1"
92+
consumer_key = 'uLXKmWNmIkzIGKfA2injnNQqpZaxaBSKxa3ixEVu2f283c95!33b9b2bd960147e387fa6f3f238f07170000000000000000'
93+
signing_key = authenticationutils.load_signing_key(key_file, key_password)
94+
95+
baseUrl = 'https://sandbox.api.mastercard.com'
96+
97+
test_cli = APIClientForTest()
98+
add_signing_layer(self, test_cli, key_file, key_password, consumer_key)
99+
100+
queryMap = {
101+
"Format": "JSON", # change this to toggle between and XML or JSON response
102+
"fxDate": "2016-09-30",
103+
"transCurr": "ALL",
104+
"crdhldBillCurr": "DZD",
105+
"bankFee": "5",
106+
"transAmt": "23"
107+
}
108+
109+
uri = baseUrl + "/settlement/currencyrate/conversion-rate"
110+
111+
r = test_cli.request('GET', uri, query_params=queryMap)
112+
print(r.text)
113+
114+
66115
if __name__ == '__main__':
67116
unittest.main()
117+
118+
119+
120+
class APIClientForTest():
121+
122+
123+
def request(self, method, uri, query_params=None, headers=None, post_params=None, body=None, _preload_content=True, _request_timeout=None):
124+
res = None
125+
126+
if query_params:
127+
uri += '?' + urlencode(query_params)
128+
129+
if method == 'GET':
130+
res = requests.get(uri, headers=headers)
131+
if method == 'POST':
132+
res = requests.post(uri, headers=headers, body=body)
133+
134+
return res

0 commit comments

Comments
 (0)