Skip to content

Commit

Permalink
Linting, translations, tests
Browse files Browse the repository at this point in the history
  • Loading branch information
th3coop committed Jan 12, 2024
1 parent 8317150 commit 997c59e
Show file tree
Hide file tree
Showing 7 changed files with 290 additions and 432 deletions.
83 changes: 81 additions & 2 deletions tests/unit/accounts/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import pretend
import pytest
import pytz
import wtforms

from pyramid.httpexceptions import (
HTTPBadRequest,
Expand Down Expand Up @@ -59,9 +58,9 @@
from warehouse.metrics.interfaces import IMetricsService
from warehouse.oidc.interfaces import TooManyOIDCRegistrations
from warehouse.oidc.models import (
PendingActiveStatePublisher,
PendingGitHubPublisher,
PendingGooglePublisher,
PendingActiveStatePublisher,
)
from warehouse.organizations.models import (
OrganizationInvitation,
Expand Down Expand Up @@ -4252,6 +4251,86 @@ def test_add_pending_oidc_publisher(
)
]

def test_delete_pending_oidc_publisher_admin_disabled(
self, monkeypatch, pyramid_request
):
pyramid_request.user = pretend.stub()
pyramid_request.registry = pretend.stub(
settings={
"github.token": "fake-api-token",
}
)
pyramid_request.flags = pretend.stub(
disallow_oidc=pretend.call_recorder(lambda f=None: True)
)
pyramid_request.session = pretend.stub(
flash=pretend.call_recorder(lambda *a, **kw: None)
)

project_factory = pretend.stub()
project_factory_cls = pretend.call_recorder(lambda r: project_factory)
monkeypatch.setattr(views, "ProjectFactory", project_factory_cls)

pending_github_publisher_form_obj = pretend.stub()
pending_github_publisher_form_cls = pretend.call_recorder(
lambda *a, **kw: pending_github_publisher_form_obj
)
monkeypatch.setattr(
views, "PendingGitHubPublisherForm", pending_github_publisher_form_cls
)
pending_google_publisher_form_obj = pretend.stub()
pending_google_publisher_form_cls = pretend.call_recorder(
lambda *a, **kw: pending_google_publisher_form_obj
)
monkeypatch.setattr(
views, "PendingGooglePublisherForm", pending_google_publisher_form_cls
)
pending_activestate_publisher_form_obj = pretend.stub()
pending_activestate_publisher_form_cls = pretend.call_recorder(
lambda *a, **kw: pending_activestate_publisher_form_obj
)
monkeypatch.setattr(
views,
"PendingActiveStatePublisherForm",
pending_activestate_publisher_form_cls,
)

view = views.ManageAccountPublishingViews(pyramid_request)

assert view.delete_pending_oidc_publisher() == {
"disabled": {
"GitHub": True,
"Google": True,
"ActiveState": True,
},
"pending_github_publisher_form": pending_github_publisher_form_obj,
"pending_google_publisher_form": pending_google_publisher_form_obj,
"pending_activestate_publisher_form": pending_activestate_publisher_form_obj,
}

assert pyramid_request.flags.disallow_oidc.calls == [
pretend.call(),
pretend.call(AdminFlagValue.DISALLOW_GITHUB_OIDC),
pretend.call(AdminFlagValue.DISALLOW_GOOGLE_OIDC),
pretend.call(AdminFlagValue.DISALLOW_ACTIVESTATE_OIDC),
]
assert pyramid_request.session.flash.calls == [
pretend.call(
(
"Trusted publishing is temporarily disabled. "
"See https://pypi.org/help#admin-intervention for details."
),
queue="error",
)
]
assert pending_github_publisher_form_cls.calls == [
pretend.call(
pyramid_request.POST,
api_token="fake-api-token",
project_factory=project_factory,
)
]

def test_delete_pending_oidc_publisher_invalid_form(
self, monkeypatch, pyramid_request
):
Expand Down
106 changes: 90 additions & 16 deletions tests/unit/oidc/forms/test_activestate.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

import pretend
import pytest
import requests
import wtforms

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

_requests = requests


def _raise(exception):
raise exception


class TestPendingActiveStatePublisherForm:
def test_validate(self, monkeypatch):
Expand Down Expand Up @@ -137,8 +144,7 @@ def test_lookup_actor_other_http_error(self, monkeypatch):

assert sentry_sdk.capture_message.calls == [
pretend.call(
"Unexpected error from ActiveState user lookup: "
"response.content=b'fake-content'"
"Unexpected error from ActiveState actor lookup: " "b'fake-content'"
)
]

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

assert sentry_sdk.capture_message.calls == [
pretend.call("Timeout from ActiveState user lookup API (possibly offline)")
pretend.call("Timeout from ActiveState actor lookup API (possibly offline)")
]

def test_lookup_actor_connection_error(self, monkeypatch):
Expand All @@ -180,10 +186,36 @@ def test_lookup_actor_connection_error(self, monkeypatch):

assert sentry_sdk.capture_message.calls == [
pretend.call(
"Connection error from ActiveState user lookup API (possibly offline)"
"Connection error from ActiveState actor lookup API (possibly offline)"
)
]

def test_lookup_actor_non_json(self, monkeypatch):
response = pretend.stub(
status_code=200,
raise_for_status=pretend.call_recorder(lambda: None),
json=lambda: _raise(_requests.exceptions.JSONDecodeError("", "", 0)),
content=b"",
)

requests = pretend.stub(
post=pretend.call_recorder(lambda o, **kw: response),
HTTPError=HTTPError,
exceptions=_requests.exceptions,
)
monkeypatch.setattr(activestate, "requests", requests)

sentry_sdk = pretend.stub(capture_message=pretend.call_recorder(lambda s: None))
monkeypatch.setattr(activestate, "sentry_sdk", sentry_sdk)

form = activestate.ActiveStatePublisherForm()
with pytest.raises(wtforms.validators.ValidationError):
form._lookup_actor(fake_username)

assert sentry_sdk.capture_message.calls == [
pretend.call("Unexpected error from ActiveState actor lookup: b''") # noqa
]

def test_lookup_actor_gql_error(self, monkeypatch):
response = pretend.stub(
status_code=200,
Expand All @@ -192,7 +224,9 @@ def test_lookup_actor_gql_error(self, monkeypatch):
content=b"fake-content",
)
requests = pretend.stub(
post=pretend.call_recorder(lambda o, **kw: response), HTTPError=HTTPError
post=pretend.call_recorder(lambda o, **kw: response),
HTTPError=HTTPError,
exceptions=_requests.exceptions,
)
monkeypatch.setattr(activestate, "requests", requests)

Expand All @@ -215,7 +249,7 @@ def test_lookup_actor_gql_error(self, monkeypatch):
]
assert sentry_sdk.capture_message.calls == [
pretend.call(
"Unexpected error from ActiveState user lookup: ['some error']"
"Unexpected error from ActiveState actor lookup: ['some error']"
)
]

Expand All @@ -226,7 +260,9 @@ def test_lookup_actor_gql_no_data(self, monkeypatch):
json=lambda: {"data": {"users": []}},
)
requests = pretend.stub(
post=pretend.call_recorder(lambda o, **kw: response), HTTPError=HTTPError
post=pretend.call_recorder(lambda o, **kw: response),
HTTPError=HTTPError,
exceptions=_requests.exceptions,
)
monkeypatch.setattr(activestate, "requests", requests)

Expand Down Expand Up @@ -280,7 +316,9 @@ def test_lookup_organization_404(self, monkeypatch):
content=b"fake-content",
)
requests = pretend.stub(
post=pretend.call_recorder(lambda o, **kw: response), HTTPError=HTTPError
post=pretend.call_recorder(lambda o, **kw: response),
HTTPError=HTTPError,
exceptions=_requests.exceptions,
)

monkeypatch.setattr(activestate, "requests", requests)
Expand Down Expand Up @@ -333,8 +371,8 @@ def test_lookup_organization_other_http_error(self, monkeypatch):

assert sentry_sdk.capture_message.calls == [
pretend.call(
"Unexpected error from ActiveState user lookup: "
"response.content=b'fake-content'"
"Unexpected error from ActiveState organization lookup: "
"b'fake-content'"
)
]

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

assert sentry_sdk.capture_message.calls == [
pretend.call("Timeout from ActiveState user lookup API (possibly offline)")
pretend.call(
"Timeout from ActiveState organization lookup API (possibly offline)"
)
]

def test_lookup_organization_connection_error(self, monkeypatch):
Expand All @@ -376,7 +416,35 @@ def test_lookup_organization_connection_error(self, monkeypatch):

assert sentry_sdk.capture_message.calls == [
pretend.call(
"Connection error from ActiveState user lookup API (possibly offline)"
"Connection error from ActiveState organization lookup API (possibly offline)" # noqa
)
]

def test_lookup_organization_non_json(self, monkeypatch):
response = pretend.stub(
status_code=200,
raise_for_status=pretend.call_recorder(lambda: None),
json=lambda: _raise(_requests.exceptions.JSONDecodeError("", "", 0)),
content=b"",
)

requests = pretend.stub(
post=pretend.call_recorder(lambda o, **kw: response),
HTTPError=HTTPError,
exceptions=_requests.exceptions,
)
monkeypatch.setattr(activestate, "requests", requests)

sentry_sdk = pretend.stub(capture_message=pretend.call_recorder(lambda s: None))
monkeypatch.setattr(activestate, "sentry_sdk", sentry_sdk)

form = activestate.ActiveStatePublisherForm()
with pytest.raises(wtforms.validators.ValidationError):
form._lookup_organization(fake_org_name)

assert sentry_sdk.capture_message.calls == [
pretend.call(
"Unexpected error from ActiveState organization lookup: b''" # noqa
)
]

Expand All @@ -385,10 +453,13 @@ def test_lookup_organization_gql_error(self, monkeypatch):
status_code=200,
raise_for_status=pretend.call_recorder(lambda: None),
json=lambda: {"errors": ["some error"]},
content=b"fake-content",
content=b'{"errors": ["some error"]}',
)

requests = pretend.stub(
post=pretend.call_recorder(lambda o, **kw: response), HTTPError=HTTPError
post=pretend.call_recorder(lambda o, **kw: response),
HTTPError=HTTPError,
exceptions=_requests.exceptions,
)
monkeypatch.setattr(activestate, "requests", requests)

Expand All @@ -411,7 +482,7 @@ def test_lookup_organization_gql_error(self, monkeypatch):
]
assert sentry_sdk.capture_message.calls == [
pretend.call(
"Unexpected error from ActiveState user lookup: ['some error']"
"Unexpected error from ActiveState organization lookup: ['some error']"
)
]

Expand All @@ -420,9 +491,12 @@ def test_lookup_organization_gql_no_data(self, monkeypatch):
status_code=200,
raise_for_status=pretend.call_recorder(lambda: None),
json=lambda: {"data": {"organizations": []}},
content='{"data": {"organizations": []}}',
)
requests = pretend.stub(
post=pretend.call_recorder(lambda o, **kw: response), HTTPError=HTTPError
post=pretend.call_recorder(lambda o, **kw: response),
HTTPError=HTTPError,
exceptions=_requests.exceptions,
)
monkeypatch.setattr(activestate, "requests", requests)

Expand Down
2 changes: 1 addition & 1 deletion warehouse/accounts/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -1520,7 +1520,7 @@ def default_response(self):
return {
"pending_github_publisher_form": self.pending_github_publisher_form,
"pending_google_publisher_form": self.pending_google_publisher_form,
"pending_activestate_publisher_form": self.pending_activestate_publisher_form,
"pending_activestate_publisher_form": self.pending_activestate_publisher_form, # noqa
"disabled": {
"GitHub": self.request.flags.disallow_oidc(
AdminFlagValue.DISALLOW_GITHUB_OIDC
Expand Down
Loading

0 comments on commit 997c59e

Please sign in to comment.