Skip to content

Commit c1d6515

Browse files
committed
Switch to mocket via httpretty API
1 parent 6fbc11a commit c1d6515

1 file changed

Lines changed: 50 additions & 23 deletions

File tree

tests/test_webservice.py

Lines changed: 50 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
import os
22

33
import json
4-
import requests_mock # type: ignore
54
from io import open
5+
6+
# httpretty currently doesn't work, but mocket with the compat interface
7+
# does.
8+
from mocket.plugins.httpretty import HTTPretty as httpretty, httprettified # type: ignore
9+
610
from minfraud.errors import (
711
HTTPError,
812
InvalidRequestError,
@@ -31,6 +35,7 @@ def setUp(self):
3135

3236
base_uri = "https://minfraud.maxmind.com/minfraud/v2.0"
3337

38+
@httprettified
3439
def test_invalid_auth(self):
3540
for error in (
3641
"ACCOUNT_ID_REQUIRED",
@@ -44,23 +49,27 @@ def test_invalid_auth(self):
4449
status_code=401,
4550
)
4651

52+
@httprettified
4753
def test_invalid_request(self):
4854
with self.assertRaisesRegex(InvalidRequestError, "IP invalid"):
4955
self.create_error(text='{"code":"IP_ADDRESS_INVALID","error":"IP invalid"}')
5056

57+
@httprettified
5158
def test_300_error(self):
5259
with self.assertRaisesRegex(
5360
HTTPError, "Received an unexpected HTTP status \(300\) for"
5461
):
5562
self.create_error(status_code=300)
5663

64+
@httprettified
5765
def test_permission_required(self):
5866
with self.assertRaisesRegex(PermissionRequiredError, "permission"):
5967
self.create_error(
6068
text='{"code":"PERMISSION_REQUIRED","error":"permission required"}',
6169
status_code=403,
6270
)
6371

72+
@httprettified
6473
def test_400_with_invalid_json(self):
6574
with self.assertRaisesRegex(
6675
HTTPError,
@@ -69,16 +78,19 @@ def test_400_with_invalid_json(self):
6978
):
7079
self.create_error(text="{blah}")
7180

81+
@httprettified
7282
def test_400_with_no_body(self):
7383
with self.assertRaisesRegex(HTTPError, "Received a 400 error with no body"):
7484
self.create_error()
7585

86+
@httprettified
7687
def test_400_with_unexpected_content_type(self):
7788
with self.assertRaisesRegex(
7889
HTTPError, "Received a 400 with the following body: b?'?plain'?"
7990
):
80-
self.create_error(headers={"Content-Type": "text/plain"}, text="plain")
91+
self.create_error(content_type="text/plain", text="plain")
8192

93+
@httprettified
8294
def test_400_without_json_body(self):
8395
with self.assertRaisesRegex(
8496
HTTPError,
@@ -87,6 +99,7 @@ def test_400_without_json_body(self):
8799
):
88100
self.create_error(text="plain")
89101

102+
@httprettified
90103
def test_400_with_unexpected_json(self):
91104
with self.assertRaisesRegex(
92105
HTTPError,
@@ -95,50 +108,55 @@ def test_400_with_unexpected_json(self):
95108
):
96109
self.create_error(text='{"not":"expected"}')
97110

111+
@httprettified
98112
def test_500_error(self):
99113
with self.assertRaisesRegex(HTTPError, "Received a server error \(500\) for"):
100114
self.create_error(status_code=500)
101115

102-
@requests_mock.mock()
103-
def create_error(self, mock, status_code=400, text="", headers=None):
116+
def create_error(self, status_code=400, text="", content_type=None):
104117
uri = "/".join(
105118
[self.base_uri, "transactions", "report"]
106119
if self.type == "report"
107120
else [self.base_uri, self.type]
108121
)
109-
if headers is None:
110-
headers = {
111-
"Content-Type": "application/json"
122+
if content_type is None:
123+
content_type = (
124+
"application/json"
112125
if self.type == "report"
113-
else "application/vnd.maxmind.com-error+json; charset=UTF-8;"
114-
" version=2.0"
115-
}
116-
mock.post(uri, status_code=status_code, text=text, headers=headers)
126+
else "application/vnd.maxmind.com-error+json; charset=UTF-8; version=2.0"
127+
)
128+
httpretty.register_uri(
129+
httpretty.POST,
130+
uri=uri,
131+
status=status_code,
132+
body=text,
133+
content_type=content_type,
134+
)
117135
return getattr(self.client, self.type)(self.full_request)
118136

119-
@requests_mock.mock()
120-
def create_success(self, mock, text=None, headers=None, client=None, request=None):
137+
def create_success(self, text=None, client=None, request=None):
121138
uri = "/".join(
122139
[self.base_uri, "transactions", "report"]
123140
if self.type == "report"
124141
else [self.base_uri, self.type]
125142
)
126-
params = {
127-
"status_code": 204 if self.type == "report" else 200,
128-
"text": self.response if text is None else text,
129-
}
130-
if headers is None:
131-
params["headers"] = {
132-
"Content-Type": "application/vnd.maxmind.com-minfraud-{0}+json;"
133-
" charset=UTF-8; version=2.0".format(self.type)
134-
}
135-
mock.post(uri, **params)
143+
httpretty.register_uri(
144+
httpretty.POST,
145+
uri=uri,
146+
status=204 if self.type == "report" else 200,
147+
body=self.response if text is None else text,
148+
content_type="application/vnd.maxmind.com-minfraud-{0}+json; charset=UTF-8; version=2.0".format(
149+
self.type
150+
),
151+
)
136152
if client is None:
137153
client = self.client
138154
if request is None:
139155
request = self.full_request
156+
print(client)
140157
return getattr(client, self.type)(request)
141158

159+
@httprettified
142160
def test_named_constructor_args(self):
143161
id = "47"
144162
key = "1234567890ab"
@@ -149,6 +167,7 @@ def test_named_constructor_args(self):
149167
self.assertEqual(client._account_id, id)
150168
self.assertEqual(client._license_key, key)
151169

170+
@httprettified
152171
def test_missing_constructor_args(self):
153172
with self.assertRaises(TypeError):
154173
Client(license_key="1234567890ab")
@@ -161,6 +180,7 @@ class BaseTransactionTest(BaseTest):
161180
def has_ip_location(self):
162181
return self.type in ["factors", "insights"]
163182

183+
@httprettified
164184
def test_200(self):
165185
model = self.create_success()
166186
response = json.loads(self.response)
@@ -170,6 +190,7 @@ def test_200(self):
170190
if self.has_ip_location():
171191
self.assertEqual("United Kingdom", model.ip_address.country.name)
172192

193+
@httprettified
173194
def test_200_on_request_with_nones(self):
174195
model = self.create_success(
175196
request={
@@ -181,6 +202,7 @@ def test_200_on_request_with_nones(self):
181202
response = self.response
182203
self.assertEqual(0.01, model.risk_score)
183204

205+
@httprettified
184206
def test_200_with_locales(self):
185207
locales = ("fr",)
186208
client = Client(42, "abcdef123456", locales=locales)
@@ -193,6 +215,7 @@ def test_200_with_locales(self):
193215
self.assertEqual("Royaume-Uni", model.ip_address.country.name)
194216
self.assertEqual("Londres", model.ip_address.city.name)
195217

218+
@httprettified
196219
def test_200_with_no_body(self):
197220
with self.assertRaisesRegex(
198221
MinFraudError,
@@ -201,6 +224,7 @@ def test_200_with_no_body(self):
201224
):
202225
self.create_success(text="")
203226

227+
@httprettified
204228
def test_200_with_invalid_json(self):
205229
with self.assertRaisesRegex(
206230
MinFraudError,
@@ -209,6 +233,7 @@ def test_200_with_invalid_json(self):
209233
):
210234
self.create_success(text="{")
211235

236+
@httprettified
212237
def test_insufficient_funds(self):
213238
with self.assertRaisesRegex(InsufficientFundsError, "out of funds"):
214239
self.create_error(
@@ -247,9 +272,11 @@ class TestReportTransaction(BaseTest):
247272
request_file = "full-report-request.json"
248273
response_file = "report-response.json"
249274

275+
@httprettified
250276
def test_204(self):
251277
self.create_success()
252278

279+
@httprettified
253280
def test_204_on_request_with_nones(self):
254281
self.create_success(
255282
request={

0 commit comments

Comments
 (0)