12
12
13
13
import pretend
14
14
import pytest
15
+ import requests
15
16
import wtforms
16
17
17
18
from requests import ConnectionError , HTTPError , Timeout
26
27
fake_gql_org_response = {"data" : {"organizations" : [fake_org_info ]}}
27
28
fake_gql_user_response = {"data" : {"users" : [fake_user_info ]}}
28
29
30
+ _requests = requests
31
+
32
+
33
+ def _raise (exception ):
34
+ raise exception
35
+
29
36
30
37
class TestPendingActiveStatePublisherForm :
31
38
def test_validate (self , monkeypatch ):
@@ -137,8 +144,7 @@ def test_lookup_actor_other_http_error(self, monkeypatch):
137
144
138
145
assert sentry_sdk .capture_message .calls == [
139
146
pretend .call (
140
- "Unexpected error from ActiveState user lookup: "
141
- "response.content=b'fake-content'"
147
+ "Unexpected error from ActiveState actor lookup: " "b'fake-content'"
142
148
)
143
149
]
144
150
@@ -159,7 +165,7 @@ def test_lookup_actor_http_timeout(self, monkeypatch):
159
165
form ._lookup_actor (fake_username )
160
166
161
167
assert sentry_sdk .capture_message .calls == [
162
- pretend .call ("Timeout from ActiveState user lookup API (possibly offline)" )
168
+ pretend .call ("Timeout from ActiveState actor lookup API (possibly offline)" )
163
169
]
164
170
165
171
def test_lookup_actor_connection_error (self , monkeypatch ):
@@ -180,10 +186,36 @@ def test_lookup_actor_connection_error(self, monkeypatch):
180
186
181
187
assert sentry_sdk .capture_message .calls == [
182
188
pretend .call (
183
- "Connection error from ActiveState user lookup API (possibly offline)"
189
+ "Connection error from ActiveState actor lookup API (possibly offline)"
184
190
)
185
191
]
186
192
193
+ def test_lookup_actor_non_json (self , monkeypatch ):
194
+ response = pretend .stub (
195
+ status_code = 200 ,
196
+ raise_for_status = pretend .call_recorder (lambda : None ),
197
+ json = lambda : _raise (_requests .exceptions .JSONDecodeError ("" , "" , 0 )),
198
+ content = b"" ,
199
+ )
200
+
201
+ requests = pretend .stub (
202
+ post = pretend .call_recorder (lambda o , ** kw : response ),
203
+ HTTPError = HTTPError ,
204
+ exceptions = _requests .exceptions ,
205
+ )
206
+ monkeypatch .setattr (activestate , "requests" , requests )
207
+
208
+ sentry_sdk = pretend .stub (capture_message = pretend .call_recorder (lambda s : None ))
209
+ monkeypatch .setattr (activestate , "sentry_sdk" , sentry_sdk )
210
+
211
+ form = activestate .ActiveStatePublisherForm ()
212
+ with pytest .raises (wtforms .validators .ValidationError ):
213
+ form ._lookup_actor (fake_username )
214
+
215
+ assert sentry_sdk .capture_message .calls == [
216
+ pretend .call ("Unexpected error from ActiveState actor lookup: b''" ) # noqa
217
+ ]
218
+
187
219
def test_lookup_actor_gql_error (self , monkeypatch ):
188
220
response = pretend .stub (
189
221
status_code = 200 ,
@@ -192,7 +224,9 @@ def test_lookup_actor_gql_error(self, monkeypatch):
192
224
content = b"fake-content" ,
193
225
)
194
226
requests = pretend .stub (
195
- post = pretend .call_recorder (lambda o , ** kw : response ), HTTPError = HTTPError
227
+ post = pretend .call_recorder (lambda o , ** kw : response ),
228
+ HTTPError = HTTPError ,
229
+ exceptions = _requests .exceptions ,
196
230
)
197
231
monkeypatch .setattr (activestate , "requests" , requests )
198
232
@@ -215,7 +249,7 @@ def test_lookup_actor_gql_error(self, monkeypatch):
215
249
]
216
250
assert sentry_sdk .capture_message .calls == [
217
251
pretend .call (
218
- "Unexpected error from ActiveState user lookup: ['some error']"
252
+ "Unexpected error from ActiveState actor lookup: ['some error']"
219
253
)
220
254
]
221
255
@@ -226,7 +260,9 @@ def test_lookup_actor_gql_no_data(self, monkeypatch):
226
260
json = lambda : {"data" : {"users" : []}},
227
261
)
228
262
requests = pretend .stub (
229
- post = pretend .call_recorder (lambda o , ** kw : response ), HTTPError = HTTPError
263
+ post = pretend .call_recorder (lambda o , ** kw : response ),
264
+ HTTPError = HTTPError ,
265
+ exceptions = _requests .exceptions ,
230
266
)
231
267
monkeypatch .setattr (activestate , "requests" , requests )
232
268
@@ -280,7 +316,9 @@ def test_lookup_organization_404(self, monkeypatch):
280
316
content = b"fake-content" ,
281
317
)
282
318
requests = pretend .stub (
283
- post = pretend .call_recorder (lambda o , ** kw : response ), HTTPError = HTTPError
319
+ post = pretend .call_recorder (lambda o , ** kw : response ),
320
+ HTTPError = HTTPError ,
321
+ exceptions = _requests .exceptions ,
284
322
)
285
323
286
324
monkeypatch .setattr (activestate , "requests" , requests )
@@ -333,8 +371,8 @@ def test_lookup_organization_other_http_error(self, monkeypatch):
333
371
334
372
assert sentry_sdk .capture_message .calls == [
335
373
pretend .call (
336
- "Unexpected error from ActiveState user lookup: "
337
- "response.content= b'fake-content'"
374
+ "Unexpected error from ActiveState organization lookup: "
375
+ "b'fake-content'"
338
376
)
339
377
]
340
378
@@ -355,7 +393,9 @@ def test_lookup_organization_http_timeout(self, monkeypatch):
355
393
form ._lookup_organization (fake_org_name )
356
394
357
395
assert sentry_sdk .capture_message .calls == [
358
- pretend .call ("Timeout from ActiveState user lookup API (possibly offline)" )
396
+ pretend .call (
397
+ "Timeout from ActiveState organization lookup API (possibly offline)"
398
+ )
359
399
]
360
400
361
401
def test_lookup_organization_connection_error (self , monkeypatch ):
@@ -376,7 +416,35 @@ def test_lookup_organization_connection_error(self, monkeypatch):
376
416
377
417
assert sentry_sdk .capture_message .calls == [
378
418
pretend .call (
379
- "Connection error from ActiveState user lookup API (possibly offline)"
419
+ "Connection error from ActiveState organization lookup API (possibly offline)" # noqa
420
+ )
421
+ ]
422
+
423
+ def test_lookup_organization_non_json (self , monkeypatch ):
424
+ response = pretend .stub (
425
+ status_code = 200 ,
426
+ raise_for_status = pretend .call_recorder (lambda : None ),
427
+ json = lambda : _raise (_requests .exceptions .JSONDecodeError ("" , "" , 0 )),
428
+ content = b"" ,
429
+ )
430
+
431
+ requests = pretend .stub (
432
+ post = pretend .call_recorder (lambda o , ** kw : response ),
433
+ HTTPError = HTTPError ,
434
+ exceptions = _requests .exceptions ,
435
+ )
436
+ monkeypatch .setattr (activestate , "requests" , requests )
437
+
438
+ sentry_sdk = pretend .stub (capture_message = pretend .call_recorder (lambda s : None ))
439
+ monkeypatch .setattr (activestate , "sentry_sdk" , sentry_sdk )
440
+
441
+ form = activestate .ActiveStatePublisherForm ()
442
+ with pytest .raises (wtforms .validators .ValidationError ):
443
+ form ._lookup_organization (fake_org_name )
444
+
445
+ assert sentry_sdk .capture_message .calls == [
446
+ pretend .call (
447
+ "Unexpected error from ActiveState organization lookup: b''" # noqa
380
448
)
381
449
]
382
450
@@ -385,10 +453,13 @@ def test_lookup_organization_gql_error(self, monkeypatch):
385
453
status_code = 200 ,
386
454
raise_for_status = pretend .call_recorder (lambda : None ),
387
455
json = lambda : {"errors" : ["some error" ]},
388
- content = b"fake-content" ,
456
+ content = b'{"errors": ["some error"]}' ,
389
457
)
458
+
390
459
requests = pretend .stub (
391
- post = pretend .call_recorder (lambda o , ** kw : response ), HTTPError = HTTPError
460
+ post = pretend .call_recorder (lambda o , ** kw : response ),
461
+ HTTPError = HTTPError ,
462
+ exceptions = _requests .exceptions ,
392
463
)
393
464
monkeypatch .setattr (activestate , "requests" , requests )
394
465
@@ -411,7 +482,7 @@ def test_lookup_organization_gql_error(self, monkeypatch):
411
482
]
412
483
assert sentry_sdk .capture_message .calls == [
413
484
pretend .call (
414
- "Unexpected error from ActiveState user lookup: ['some error']"
485
+ "Unexpected error from ActiveState organization lookup: ['some error']"
415
486
)
416
487
]
417
488
@@ -420,9 +491,12 @@ def test_lookup_organization_gql_no_data(self, monkeypatch):
420
491
status_code = 200 ,
421
492
raise_for_status = pretend .call_recorder (lambda : None ),
422
493
json = lambda : {"data" : {"organizations" : []}},
494
+ content = '{"data": {"organizations": []}}' ,
423
495
)
424
496
requests = pretend .stub (
425
- post = pretend .call_recorder (lambda o , ** kw : response ), HTTPError = HTTPError
497
+ post = pretend .call_recorder (lambda o , ** kw : response ),
498
+ HTTPError = HTTPError ,
499
+ exceptions = _requests .exceptions ,
426
500
)
427
501
monkeypatch .setattr (activestate , "requests" , requests )
428
502
0 commit comments