Skip to content

OIDC active state form #15168

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
Feb 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
189 changes: 187 additions & 2 deletions tests/unit/accounts/test_views.py

Large diffs are not rendered by default.

101 changes: 97 additions & 4 deletions tests/unit/manage/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,12 @@
from warehouse.manage.views import organizations as org_views
from warehouse.metrics.interfaces import IMetricsService
from warehouse.oidc.interfaces import TooManyOIDCRegistrations
from warehouse.oidc.models import GitHubPublisher, GooglePublisher, OIDCPublisher
from warehouse.oidc.models import (
ActiveStatePublisher,
GitHubPublisher,
GooglePublisher,
OIDCPublisher,
)
from warehouse.organizations.interfaces import IOrganizationService
from warehouse.organizations.models import (
OrganizationRoleType,
Expand Down Expand Up @@ -5835,16 +5840,18 @@ def test_manage_project_oidc_publishers(self, monkeypatch):

view = views.ManageOIDCPublisherViews(project, request)
assert view.manage_project_oidc_publishers() == {
"disabled": {"GitHub": False, "Google": False},
"disabled": {"GitHub": False, "Google": False, "ActiveState": False},
"project": project,
"github_publisher_form": view.github_publisher_form,
"google_publisher_form": view.google_publisher_form,
"activestate_publisher_form": view.activestate_publisher_form,
}

assert 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),
]

def test_manage_project_oidc_publishers_admin_disabled(
Expand All @@ -5869,16 +5876,18 @@ def test_manage_project_oidc_publishers_admin_disabled(
view = views.ManageOIDCPublisherViews(project, pyramid_request)

assert view.manage_project_oidc_publishers() == {
"disabled": {"GitHub": True, "Google": True},
"disabled": {"GitHub": True, "Google": True, "ActiveState": True},
"project": project,
"github_publisher_form": view.github_publisher_form,
"google_publisher_form": view.google_publisher_form,
"activestate_publisher_form": view.activestate_publisher_form,
}

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(
Expand Down Expand Up @@ -5930,6 +5939,27 @@ def test_manage_project_oidc_publishers_admin_disabled(
sub=pretend.stub(data=publisher.sub),
),
),
(
"add_activestate_oidc_publisher",
pretend.stub(
id="fakeid",
publisher_name="ActiveState",
publisher_url=(
lambda x=None: "https://platform.activestate.com/some-org/some-project" # noqa
),
organization="some-org",
activestate_project_name="some-project",
actor="some-user",
actor_id="some-user-id",
),
lambda publisher: pretend.stub(
validate=pretend.call_recorder(lambda: True),
organization=pretend.stub(data=publisher.organization),
project=pretend.stub(data=publisher.activestate_project_name),
actor=pretend.stub(data=publisher.actor),
actor_id="some-user-id",
),
),
],
)
def test_add_oidc_publisher_preexisting(
Expand Down Expand Up @@ -5973,6 +6003,7 @@ def test_add_oidc_publisher_preexisting(
publisher_form_cls = pretend.call_recorder(lambda *a, **kw: publisher_form_obj)
monkeypatch.setattr(views, "GitHubPublisherForm", publisher_form_cls)
monkeypatch.setattr(views, "GooglePublisherForm", publisher_form_cls)
monkeypatch.setattr(views, "ActiveStatePublisherForm", publisher_form_cls)

view = views.ManageOIDCPublisherViews(project, request)
monkeypatch.setattr(
Expand Down Expand Up @@ -6048,6 +6079,20 @@ def test_add_oidc_publisher_preexisting(
),
"Google",
),
(
"add_activestate_oidc_publisher",
pretend.stub(
validate=pretend.call_recorder(lambda: True),
id="fakeid",
publisher_name="ActiveState",
publisher_url=lambda x=None: None,
organization=pretend.stub(data="fake-org"),
project=pretend.stub(data="fake-project"),
actor=pretend.stub(data="fake-actor"),
actor_id="some-user-id",
),
"ActiveState",
),
],
)
def test_add_oidc_publisher_created(
Expand Down Expand Up @@ -6088,6 +6133,7 @@ def test_add_oidc_publisher_created(
publisher_form_cls = pretend.call_recorder(lambda *a, **kw: publisher_form_obj)
monkeypatch.setattr(views, "GitHubPublisherForm", publisher_form_cls)
monkeypatch.setattr(views, "GooglePublisherForm", publisher_form_cls)
monkeypatch.setattr(views, "ActiveStatePublisherForm", publisher_form_cls)
monkeypatch.setattr(
views,
"send_trusted_publisher_added_email",
Expand Down Expand Up @@ -6191,6 +6237,23 @@ def test_add_oidc_publisher_created(
}
),
),
(
"add_activestate_oidc_publisher",
"ActiveState",
ActiveStatePublisher(
organization="some-org",
activestate_project_name="some-project",
actor="some-user",
actor_id="some-user-id",
),
MultiDict(
{
"organization": "some-org",
"project": "some-project",
"actor": "some-user",
}
),
),
],
)
def test_add_oidc_publisher_already_registered_with_project(
Expand Down Expand Up @@ -6227,6 +6290,18 @@ def test_add_oidc_publisher_already_registered_with_project(
lambda *a: {"login": "some-owner", "id": "some-owner-id"},
)

monkeypatch.setattr(
views.ActiveStatePublisherForm,
"_lookup_organization",
lambda *a: None,
)

monkeypatch.setattr(
views.ActiveStatePublisherForm,
"_lookup_actor",
lambda *a: {"user_id": "some-user-id"},
)

monkeypatch.setattr(
view, "_hit_ratelimits", pretend.call_recorder(lambda: None)
)
Expand All @@ -6235,10 +6310,11 @@ def test_add_oidc_publisher_already_registered_with_project(
)

assert getattr(view, view_name)() == {
"disabled": {"GitHub": False, "Google": False},
"disabled": {"GitHub": False, "Google": False, "ActiveState": False},
"project": project,
"github_publisher_form": view.github_publisher_form,
"google_publisher_form": view.google_publisher_form,
"activestate_publisher_form": view.activestate_publisher_form,
}
assert view.metrics.increment.calls == [
pretend.call(
Expand All @@ -6259,6 +6335,7 @@ def test_add_oidc_publisher_already_registered_with_project(
[
("add_github_oidc_publisher", "GitHub"),
("add_google_oidc_publisher", "Google"),
("add_activestate_oidc_publisher", "ActiveState"),
],
)
def test_add_oidc_publisher_ratelimited(
Expand Down Expand Up @@ -6307,6 +6384,7 @@ def test_add_oidc_publisher_ratelimited(
[
("add_github_oidc_publisher", "GitHub"),
("add_google_oidc_publisher", "Google"),
("add_activestate_oidc_publisher", "ActiveState"),
],
)
def test_add_oidc_publisher_admin_disabled(
Expand Down Expand Up @@ -6348,6 +6426,7 @@ def test_add_oidc_publisher_admin_disabled(
[
("add_github_oidc_publisher", "GitHub"),
("add_google_oidc_publisher", "Google"),
("add_activestate_oidc_publisher", "ActiveState"),
],
)
def test_add_oidc_publisher_invalid_form(
Expand All @@ -6372,11 +6451,13 @@ def test_add_oidc_publisher_invalid_form(
publisher_form_cls = pretend.call_recorder(lambda *a, **kw: publisher_form_obj)
monkeypatch.setattr(views, "GitHubPublisherForm", publisher_form_cls)
monkeypatch.setattr(views, "GooglePublisherForm", publisher_form_cls)
monkeypatch.setattr(views, "ActiveStatePublisherForm", publisher_form_cls)

view = views.ManageOIDCPublisherViews(project, request)
default_response = {
"github_publisher_form": publisher_form_obj,
"google_publisher_form": publisher_form_obj,
"activestate_publisher_form": publisher_form_obj,
}
monkeypatch.setattr(
views.ManageOIDCPublisherViews, "default_response", default_response
Expand Down Expand Up @@ -6413,6 +6494,12 @@ def test_add_oidc_publisher_invalid_form(
email="[email protected]",
sub="some-sub",
),
ActiveStatePublisher(
organization="some-org",
activestate_project_name="some-project",
actor="some-user",
actor_id="some-user-id",
),
],
)
def test_delete_oidc_publisher_registered_to_multiple_projects(
Expand Down Expand Up @@ -6515,6 +6602,12 @@ def test_delete_oidc_publisher_registered_to_multiple_projects(
email="[email protected]",
sub="some-sub",
),
ActiveStatePublisher(
organization="some-org",
activestate_project_name="some-project",
actor="some-user",
actor_id="some-user-id",
),
],
)
def test_delete_oidc_publisher_entirely(self, monkeypatch, db_request, publisher):
Expand Down
Loading