Skip to content

Commit

Permalink
OIDC forms folder/file structure allows inclusion of new Publishers (p…
Browse files Browse the repository at this point in the history
…ypi#14127)

* OIDC forms folder/file structure allows inclusion of new Publishers

* Updating translations file

* Add init file to fix test file import error

* Move generic Form class to generic form file

* Fix translations
  • Loading branch information
th3coop authored Aug 16, 2023
1 parent 496338e commit eb8270b
Show file tree
Hide file tree
Showing 7 changed files with 169 additions and 141 deletions.
11 changes: 11 additions & 0 deletions tests/unit/oidc/forms/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
from requests import ConnectionError, HTTPError, Timeout
from webob.multidict import MultiDict

from warehouse.oidc import forms
from warehouse.oidc.forms import github


class TestPendingGitHubPublisherForm:
Expand All @@ -31,7 +31,7 @@ def test_validate(self, monkeypatch):
"project_name": "some-project",
}
)
form = forms.PendingGitHubPublisherForm(
form = github.PendingGitHubPublisherForm(
MultiDict(data), api_token=pretend.stub(), project_factory=project_factory
)

Expand All @@ -44,7 +44,7 @@ def test_validate(self, monkeypatch):

def test_validate_project_name_already_in_use(self):
project_factory = ["some-project"]
form = forms.PendingGitHubPublisherForm(
form = github.PendingGitHubPublisherForm(
api_token="fake-token", project_factory=project_factory
)

Expand Down Expand Up @@ -72,7 +72,7 @@ def test_validate(self, token, headers, monkeypatch):
"workflow_filename": "some-workflow.yml",
}
)
form = forms.GitHubPublisherForm(MultiDict(data), api_token=token)
form = github.GitHubPublisherForm(MultiDict(data), api_token=token)

# We're testing only the basic validation here.
owner_info = {"login": "fake-username", "id": "1234"}
Expand All @@ -89,9 +89,9 @@ def test_lookup_owner_404(self, monkeypatch):
requests = pretend.stub(
get=pretend.call_recorder(lambda o, **kw: response), HTTPError=HTTPError
)
monkeypatch.setattr(forms, "requests", requests)
monkeypatch.setattr(github, "requests", requests)

form = forms.GitHubPublisherForm(api_token="fake-token")
form = github.GitHubPublisherForm(api_token="fake-token")
with pytest.raises(wtforms.validators.ValidationError):
form._lookup_owner("some-owner")

Expand All @@ -116,12 +116,12 @@ def test_lookup_owner_403(self, monkeypatch):
requests = pretend.stub(
get=pretend.call_recorder(lambda o, **kw: response), HTTPError=HTTPError
)
monkeypatch.setattr(forms, "requests", requests)
monkeypatch.setattr(github, "requests", requests)

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

form = forms.GitHubPublisherForm(api_token="fake-token")
form = github.GitHubPublisherForm(api_token="fake-token")
with pytest.raises(wtforms.validators.ValidationError):
form._lookup_owner("some-owner")

Expand Down Expand Up @@ -153,12 +153,12 @@ def test_lookup_owner_other_http_error(self, monkeypatch):
requests = pretend.stub(
get=pretend.call_recorder(lambda o, **kw: response), HTTPError=HTTPError
)
monkeypatch.setattr(forms, "requests", requests)
monkeypatch.setattr(github, "requests", requests)

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

form = forms.GitHubPublisherForm(api_token="fake-token")
form = github.GitHubPublisherForm(api_token="fake-token")
with pytest.raises(wtforms.validators.ValidationError):
form._lookup_owner("some-owner")

Expand Down Expand Up @@ -188,12 +188,12 @@ def test_lookup_owner_http_timeout(self, monkeypatch):
HTTPError=HTTPError,
ConnectionError=ConnectionError,
)
monkeypatch.setattr(forms, "requests", requests)
monkeypatch.setattr(github, "requests", requests)

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

form = forms.GitHubPublisherForm(api_token="fake-token")
form = github.GitHubPublisherForm(api_token="fake-token")
with pytest.raises(wtforms.validators.ValidationError):
form._lookup_owner("some-owner")

Expand All @@ -208,12 +208,12 @@ def test_lookup_owner_connection_error(self, monkeypatch):
HTTPError=HTTPError,
ConnectionError=ConnectionError,
)
monkeypatch.setattr(forms, "requests", requests)
monkeypatch.setattr(github, "requests", requests)

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

form = forms.GitHubPublisherForm(api_token="fake-token")
form = github.GitHubPublisherForm(api_token="fake-token")
with pytest.raises(wtforms.validators.ValidationError):
form._lookup_owner("some-owner")

Expand All @@ -233,9 +233,9 @@ def test_lookup_owner_succeeds(self, monkeypatch):
requests = pretend.stub(
get=pretend.call_recorder(lambda o, **kw: response), HTTPError=HTTPError
)
monkeypatch.setattr(forms, "requests", requests)
monkeypatch.setattr(github, "requests", requests)

form = forms.GitHubPublisherForm(api_token="fake-token")
form = github.GitHubPublisherForm(api_token="fake-token")
info = form._lookup_owner("some-owner")

assert requests.get.calls == [
Expand Down Expand Up @@ -274,7 +274,7 @@ def test_lookup_owner_succeeds(self, monkeypatch):
],
)
def test_validate_basic_invalid_fields(self, monkeypatch, data):
form = forms.GitHubPublisherForm(MultiDict(data), api_token=pretend.stub())
form = github.GitHubPublisherForm(MultiDict(data), api_token=pretend.stub())

# We're testing only the basic validation here.
owner_info = {"login": "fake-username", "id": "1234"}
Expand All @@ -283,7 +283,7 @@ def test_validate_basic_invalid_fields(self, monkeypatch, data):
assert not form.validate()

def test_validate_owner(self, monkeypatch):
form = forms.GitHubPublisherForm(api_token=pretend.stub())
form = github.GitHubPublisherForm(api_token=pretend.stub())

owner_info = {"login": "some-username", "id": "1234"}
monkeypatch.setattr(form, "_lookup_owner", lambda o: owner_info)
Expand All @@ -298,7 +298,7 @@ def test_validate_owner(self, monkeypatch):
"workflow_filename", ["missing_suffix", "/slash", "/many/slashes", "/slash.yml"]
)
def test_validate_workflow_filename(self, workflow_filename):
form = forms.GitHubPublisherForm(api_token=pretend.stub())
form = github.GitHubPublisherForm(api_token=pretend.stub())
field = pretend.stub(data=workflow_filename)

with pytest.raises(wtforms.validators.ValidationError):
Expand All @@ -316,5 +316,5 @@ def test_validate_workflow_filename(self, workflow_filename):
],
)
def test_normalized_environment(self, data, expected):
form = forms.GitHubPublisherForm(api_token=pretend.stub(), environment=data)
form = github.GitHubPublisherForm(api_token=pretend.stub(), environment=data)
assert form.normalized_environment == expected
3 changes: 2 additions & 1 deletion warehouse/accounts/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@
)
from warehouse.events.tags import EventTag
from warehouse.metrics.interfaces import IMetricsService
from warehouse.oidc.forms import DeletePublisherForm, PendingGitHubPublisherForm
from warehouse.oidc.forms import DeletePublisherForm
from warehouse.oidc.forms.github import PendingGitHubPublisherForm
from warehouse.oidc.interfaces import TooManyOIDCRegistrations
from warehouse.oidc.models import PendingGitHubPublisher, PendingOIDCPublisher
from warehouse.organizations.interfaces import IOrganizationService
Expand Down
Loading

0 comments on commit eb8270b

Please sign in to comment.