Skip to content

Commit a66fad3

Browse files
committed
Linting, translations, tests
1 parent 8317150 commit a66fad3

File tree

7 files changed

+210
-432
lines changed

7 files changed

+210
-432
lines changed

tests/unit/accounts/test_views.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
import pretend
2121
import pytest
2222
import pytz
23-
import wtforms
2423

2524
from pyramid.httpexceptions import (
2625
HTTPBadRequest,
@@ -59,9 +58,9 @@
5958
from warehouse.metrics.interfaces import IMetricsService
6059
from warehouse.oidc.interfaces import TooManyOIDCRegistrations
6160
from warehouse.oidc.models import (
61+
PendingActiveStatePublisher,
6262
PendingGitHubPublisher,
6363
PendingGooglePublisher,
64-
PendingActiveStatePublisher,
6564
)
6665
from warehouse.organizations.models import (
6766
OrganizationInvitation,

tests/unit/oidc/forms/test_activestate.py

Lines changed: 90 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
import pretend
1414
import pytest
15+
import requests
1516
import wtforms
1617

1718
from requests import ConnectionError, HTTPError, Timeout
@@ -26,6 +27,12 @@
2627
fake_gql_org_response = {"data": {"organizations": [fake_org_info]}}
2728
fake_gql_user_response = {"data": {"users": [fake_user_info]}}
2829

30+
_requests = requests
31+
32+
33+
def _raise(exception):
34+
raise exception
35+
2936

3037
class TestPendingActiveStatePublisherForm:
3138
def test_validate(self, monkeypatch):
@@ -137,8 +144,7 @@ def test_lookup_actor_other_http_error(self, monkeypatch):
137144

138145
assert sentry_sdk.capture_message.calls == [
139146
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'"
142148
)
143149
]
144150

@@ -159,7 +165,7 @@ def test_lookup_actor_http_timeout(self, monkeypatch):
159165
form._lookup_actor(fake_username)
160166

161167
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)")
163169
]
164170

165171
def test_lookup_actor_connection_error(self, monkeypatch):
@@ -180,10 +186,36 @@ def test_lookup_actor_connection_error(self, monkeypatch):
180186

181187
assert sentry_sdk.capture_message.calls == [
182188
pretend.call(
183-
"Connection error from ActiveState user lookup API (possibly offline)"
189+
"Connection error from ActiveState actor lookup API (possibly offline)"
184190
)
185191
]
186192

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+
187219
def test_lookup_actor_gql_error(self, monkeypatch):
188220
response = pretend.stub(
189221
status_code=200,
@@ -192,7 +224,9 @@ def test_lookup_actor_gql_error(self, monkeypatch):
192224
content=b"fake-content",
193225
)
194226
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,
196230
)
197231
monkeypatch.setattr(activestate, "requests", requests)
198232

@@ -215,7 +249,7 @@ def test_lookup_actor_gql_error(self, monkeypatch):
215249
]
216250
assert sentry_sdk.capture_message.calls == [
217251
pretend.call(
218-
"Unexpected error from ActiveState user lookup: ['some error']"
252+
"Unexpected error from ActiveState actor lookup: ['some error']"
219253
)
220254
]
221255

@@ -226,7 +260,9 @@ def test_lookup_actor_gql_no_data(self, monkeypatch):
226260
json=lambda: {"data": {"users": []}},
227261
)
228262
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,
230266
)
231267
monkeypatch.setattr(activestate, "requests", requests)
232268

@@ -280,7 +316,9 @@ def test_lookup_organization_404(self, monkeypatch):
280316
content=b"fake-content",
281317
)
282318
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,
284322
)
285323

286324
monkeypatch.setattr(activestate, "requests", requests)
@@ -333,8 +371,8 @@ def test_lookup_organization_other_http_error(self, monkeypatch):
333371

334372
assert sentry_sdk.capture_message.calls == [
335373
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'"
338376
)
339377
]
340378

@@ -355,7 +393,9 @@ def test_lookup_organization_http_timeout(self, monkeypatch):
355393
form._lookup_organization(fake_org_name)
356394

357395
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+
)
359399
]
360400

361401
def test_lookup_organization_connection_error(self, monkeypatch):
@@ -376,7 +416,35 @@ def test_lookup_organization_connection_error(self, monkeypatch):
376416

377417
assert sentry_sdk.capture_message.calls == [
378418
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
380448
)
381449
]
382450

@@ -385,10 +453,13 @@ def test_lookup_organization_gql_error(self, monkeypatch):
385453
status_code=200,
386454
raise_for_status=pretend.call_recorder(lambda: None),
387455
json=lambda: {"errors": ["some error"]},
388-
content=b"fake-content",
456+
content=b'{"errors": ["some error"]}',
389457
)
458+
390459
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,
392463
)
393464
monkeypatch.setattr(activestate, "requests", requests)
394465

@@ -411,7 +482,7 @@ def test_lookup_organization_gql_error(self, monkeypatch):
411482
]
412483
assert sentry_sdk.capture_message.calls == [
413484
pretend.call(
414-
"Unexpected error from ActiveState user lookup: ['some error']"
485+
"Unexpected error from ActiveState organization lookup: ['some error']"
415486
)
416487
]
417488

@@ -420,9 +491,12 @@ def test_lookup_organization_gql_no_data(self, monkeypatch):
420491
status_code=200,
421492
raise_for_status=pretend.call_recorder(lambda: None),
422493
json=lambda: {"data": {"organizations": []}},
494+
content='{"data": {"organizations": []}}',
423495
)
424496
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,
426500
)
427501
monkeypatch.setattr(activestate, "requests", requests)
428502

warehouse/accounts/views.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1520,7 +1520,7 @@ def default_response(self):
15201520
return {
15211521
"pending_github_publisher_form": self.pending_github_publisher_form,
15221522
"pending_google_publisher_form": self.pending_google_publisher_form,
1523-
"pending_activestate_publisher_form": self.pending_activestate_publisher_form,
1523+
"pending_activestate_publisher_form": self.pending_activestate_publisher_form, # noqa
15241524
"disabled": {
15251525
"GitHub": self.request.flags.disallow_oidc(
15261526
AdminFlagValue.DISALLOW_GITHUB_OIDC

0 commit comments

Comments
 (0)