Skip to content

Commit 40e20e5

Browse files
committed
Linting, translations, tests
1 parent 8317150 commit 40e20e5

File tree

8 files changed

+288
-433
lines changed

8 files changed

+288
-433
lines changed

tests/unit/accounts/test_views.py

Lines changed: 81 additions & 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,
@@ -4252,6 +4251,86 @@ def test_add_pending_oidc_publisher(
42524251
)
42534252
]
42544253

4254+
def test_delete_pending_oidc_publisher_admin_disabled(
4255+
self, monkeypatch, pyramid_request
4256+
):
4257+
pyramid_request.user = pretend.stub()
4258+
pyramid_request.registry = pretend.stub(
4259+
settings={
4260+
"github.token": "fake-api-token",
4261+
}
4262+
)
4263+
pyramid_request.flags = pretend.stub(
4264+
disallow_oidc=pretend.call_recorder(lambda f=None: True)
4265+
)
4266+
pyramid_request.session = pretend.stub(
4267+
flash=pretend.call_recorder(lambda *a, **kw: None)
4268+
)
4269+
4270+
project_factory = pretend.stub()
4271+
project_factory_cls = pretend.call_recorder(lambda r: project_factory)
4272+
monkeypatch.setattr(views, "ProjectFactory", project_factory_cls)
4273+
4274+
pending_github_publisher_form_obj = pretend.stub()
4275+
pending_github_publisher_form_cls = pretend.call_recorder(
4276+
lambda *a, **kw: pending_github_publisher_form_obj
4277+
)
4278+
monkeypatch.setattr(
4279+
views, "PendingGitHubPublisherForm", pending_github_publisher_form_cls
4280+
)
4281+
pending_google_publisher_form_obj = pretend.stub()
4282+
pending_google_publisher_form_cls = pretend.call_recorder(
4283+
lambda *a, **kw: pending_google_publisher_form_obj
4284+
)
4285+
monkeypatch.setattr(
4286+
views, "PendingGooglePublisherForm", pending_google_publisher_form_cls
4287+
)
4288+
pending_activestate_publisher_form_obj = pretend.stub()
4289+
pending_activestate_publisher_form_cls = pretend.call_recorder(
4290+
lambda *a, **kw: pending_activestate_publisher_form_obj
4291+
)
4292+
monkeypatch.setattr(
4293+
views,
4294+
"PendingActiveStatePublisherForm",
4295+
pending_activestate_publisher_form_cls,
4296+
)
4297+
4298+
view = views.ManageAccountPublishingViews(pyramid_request)
4299+
4300+
assert view.delete_pending_oidc_publisher() == {
4301+
"disabled": {
4302+
"GitHub": True,
4303+
"Google": True,
4304+
"ActiveState": True,
4305+
},
4306+
"pending_github_publisher_form": pending_github_publisher_form_obj,
4307+
"pending_google_publisher_form": pending_google_publisher_form_obj,
4308+
"pending_activestate_publisher_form": pending_activestate_publisher_form_obj, # noqa
4309+
}
4310+
4311+
assert pyramid_request.flags.disallow_oidc.calls == [
4312+
pretend.call(),
4313+
pretend.call(AdminFlagValue.DISALLOW_GITHUB_OIDC),
4314+
pretend.call(AdminFlagValue.DISALLOW_GOOGLE_OIDC),
4315+
pretend.call(AdminFlagValue.DISALLOW_ACTIVESTATE_OIDC),
4316+
]
4317+
assert pyramid_request.session.flash.calls == [
4318+
pretend.call(
4319+
(
4320+
"Trusted publishing is temporarily disabled. "
4321+
"See https://pypi.org/help#admin-intervention for details."
4322+
),
4323+
queue="error",
4324+
)
4325+
]
4326+
assert pending_github_publisher_form_cls.calls == [
4327+
pretend.call(
4328+
pyramid_request.POST,
4329+
api_token="fake-api-token",
4330+
project_factory=project_factory,
4331+
)
4332+
]
4333+
42554334
def test_delete_pending_oidc_publisher_invalid_form(
42564335
self, monkeypatch, pyramid_request
42574336
):

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)