Skip to content

Commit 59af76f

Browse files
committed
Linting, translations, tests
1 parent 404077e commit 59af76f

File tree

3 files changed

+113
-37
lines changed

3 files changed

+113
-37
lines changed

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 JsonDecode 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/locale/messages.pot

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -363,7 +363,7 @@ msgid "Select project"
363363
msgstr ""
364364

365365
#: warehouse/manage/forms.py:495 warehouse/oidc/forms/_core.py:23
366-
#: warehouse/oidc/forms/activestate.py:201
366+
#: warehouse/oidc/forms/activestate.py:224
367367
msgid "Specify project name"
368368
msgstr ""
369369

@@ -550,11 +550,11 @@ msgstr ""
550550
msgid "Expired invitation for '${username}' deleted."
551551
msgstr ""
552552

553-
#: warehouse/oidc/forms/_core.py:25 warehouse/oidc/forms/activestate.py:203
553+
#: warehouse/oidc/forms/_core.py:25 warehouse/oidc/forms/activestate.py:226
554554
msgid "Invalid project name"
555555
msgstr ""
556556

557-
#: warehouse/oidc/forms/_core.py:35 warehouse/oidc/forms/activestate.py:217
557+
#: warehouse/oidc/forms/_core.py:35 warehouse/oidc/forms/activestate.py:240
558558
msgid "This project name is already in use"
559559
msgstr ""
560560

@@ -567,43 +567,45 @@ msgid "Publisher must be specified by ID"
567567
msgstr ""
568568

569569
#: warehouse/oidc/forms/activestate.py:46
570-
msgid "Specify ActiveState Organization URL name"
570+
msgid "Specify ActiveState organization name"
571571
msgstr ""
572572

573573
#: warehouse/oidc/forms/activestate.py:50
574-
msgid "Invalid ActiveState Platform organization name"
574+
msgid "Invalid ActiveState organization name"
575575
msgstr ""
576576

577577
#: warehouse/oidc/forms/activestate.py:58
578578
msgid "Specify ActiveState project name"
579579
msgstr ""
580580

581581
#: warehouse/oidc/forms/activestate.py:62
582-
msgid "Invalid ActiveState Platform project name"
582+
msgid "Invalid ActiveState project name"
583583
msgstr ""
584584

585-
#: warehouse/oidc/forms/activestate.py:98
586-
#: warehouse/oidc/forms/activestate.py:125
587-
#: warehouse/oidc/forms/activestate.py:154
588-
#: warehouse/oidc/forms/activestate.py:180
585+
#: warehouse/oidc/forms/activestate.py:100
586+
#: warehouse/oidc/forms/activestate.py:128
587+
#: warehouse/oidc/forms/activestate.py:142
588+
#: warehouse/oidc/forms/activestate.py:166
589+
#: warehouse/oidc/forms/activestate.py:194
590+
#: warehouse/oidc/forms/activestate.py:208
589591
msgid "Unexpected error from ActiveState. Try again"
590592
msgstr ""
591593

592-
#: warehouse/oidc/forms/activestate.py:105
593-
#: warehouse/oidc/forms/activestate.py:161
594+
#: warehouse/oidc/forms/activestate.py:107
595+
#: warehouse/oidc/forms/activestate.py:173
594596
msgid "Unexpected connection error from ActiveState. Try again in a few minutes"
595597
msgstr ""
596598

597-
#: warehouse/oidc/forms/activestate.py:115
598-
#: warehouse/oidc/forms/activestate.py:171
599+
#: warehouse/oidc/forms/activestate.py:117
600+
#: warehouse/oidc/forms/activestate.py:183
599601
msgid "Unexpected timeout from ActiveState. Try again in a few minutes"
600602
msgstr ""
601603

602-
#: warehouse/oidc/forms/activestate.py:130
604+
#: warehouse/oidc/forms/activestate.py:135
603605
msgid "ActiveState organization not found"
604606
msgstr ""
605607

606-
#: warehouse/oidc/forms/activestate.py:186
608+
#: warehouse/oidc/forms/activestate.py:201
607609
msgid "ActiveState actor not found"
608610
msgstr ""
609611

warehouse/oidc/forms/activestate.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -94,14 +94,14 @@ def _lookup_organization(self, org_url_name: str) -> None:
9494
response.raise_for_status()
9595
except requests.HTTPError:
9696
sentry_sdk.capture_message(
97-
f"Unexpected error from ActiveState organization lookup: {response.content=}"
97+
f"Unexpected error from ActiveState organization lookup: {response.content}" # noqa
9898
)
9999
raise wtforms.validators.ValidationError(
100100
_("Unexpected error from ActiveState. Try again")
101101
)
102102
except requests.ConnectionError:
103103
sentry_sdk.capture_message(
104-
"Connection error from ActiveState organization lookup API (possibly offline)"
104+
"Connection error from ActiveState organization lookup API (possibly offline)" # noqa
105105
)
106106
raise wtforms.validators.ValidationError(
107107
_(
@@ -136,7 +136,7 @@ def _lookup_organization(self, org_url_name: str) -> None:
136136
)
137137
except requests.exceptions.JSONDecodeError:
138138
sentry_sdk.capture_message(
139-
f"Unexpected error from ActiveState organization lookup: {response.content=}"
139+
f"Unexpected JsonDecode error from ActiveState organization lookup: {response.content}" # noqa
140140
)
141141
raise wtforms.validators.ValidationError(
142142
_("Unexpected error from ActiveState. Try again")
@@ -160,7 +160,7 @@ def _lookup_actor(self, actor: str) -> UserResponse:
160160
response.raise_for_status()
161161
except requests.HTTPError:
162162
sentry_sdk.capture_message(
163-
f"Unexpected error from ActiveState actor lookup: {response.content=}"
163+
f"Unexpected error from ActiveState actor lookup: {response.content}"
164164
)
165165
raise wtforms.validators.ValidationError(
166166
_("Unexpected error from ActiveState. Try again")
@@ -202,7 +202,7 @@ def _lookup_actor(self, actor: str) -> UserResponse:
202202
)
203203
except requests.exceptions.JSONDecodeError:
204204
sentry_sdk.capture_message(
205-
f"Unexpected error from ActiveState actor lookup: {response.content=}"
205+
f"Unexpected error from ActiveState actor lookup: {response.content}"
206206
)
207207
raise wtforms.validators.ValidationError(
208208
_("Unexpected error from ActiveState. Try again")

0 commit comments

Comments
 (0)