Skip to content
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

tests, warehouse: per-provider OIDC admin flags #13871

Merged
merged 6 commits into from
Jun 7, 2023
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
72 changes: 40 additions & 32 deletions tests/unit/accounts/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -3097,7 +3097,9 @@ def test_manage_publishing(self, monkeypatch):
}
),
find_service=pretend.call_recorder(lambda *a, **kw: metrics),
flags=pretend.stub(enabled=pretend.call_recorder(lambda f: False)),
flags=pretend.stub(
disallow_oidc=pretend.call_recorder(lambda f=None: False)
),
POST=pretend.stub(),
)

Expand All @@ -3120,9 +3122,7 @@ def test_manage_publishing(self, monkeypatch):
"pending_github_publisher_form": pending_github_publisher_form_obj,
}

assert request.flags.enabled.calls == [
pretend.call(AdminFlagValue.DISALLOW_OIDC)
]
assert request.flags.disallow_oidc.calls == [pretend.call()]
assert project_factory_cls.calls == [pretend.call(request)]
assert pending_github_publisher_form_cls.calls == [
pretend.call(
Expand Down Expand Up @@ -3152,7 +3152,7 @@ def test_manage_publishing_admin_disabled(self, monkeypatch, pyramid_request):
}
)
pyramid_request.flags = pretend.stub(
enabled=pretend.call_recorder(lambda f: True)
disallow_oidc=pretend.call_recorder(lambda f=None: True)
)
pyramid_request.session = pretend.stub(
flash=pretend.call_recorder(lambda *a, **kw: None)
Expand All @@ -3177,13 +3177,11 @@ def test_manage_publishing_admin_disabled(self, monkeypatch, pyramid_request):
"pending_github_publisher_form": pending_github_publisher_form_obj,
}

assert pyramid_request.flags.enabled.calls == [
pretend.call(AdminFlagValue.DISALLOW_OIDC)
]
assert pyramid_request.flags.disallow_oidc.calls == [pretend.call()]
assert pyramid_request.session.flash.calls == [
pretend.call(
(
"Trusted publishers are temporarily disabled. "
"Trusted publishing is temporarily disabled. "
"See https://pypi.org/help#admin-intervention for details."
),
queue="error",
Expand Down Expand Up @@ -3219,7 +3217,7 @@ def test_add_pending_github_oidc_publisher_admin_disabled(
}
)
pyramid_request.flags = pretend.stub(
enabled=pretend.call_recorder(lambda f: True)
disallow_oidc=pretend.call_recorder(lambda f=None: True),
)
pyramid_request.session = pretend.stub(
flash=pretend.call_recorder(lambda *a, **kw: None)
Expand All @@ -3244,13 +3242,13 @@ def test_add_pending_github_oidc_publisher_admin_disabled(
"pending_github_publisher_form": pending_github_publisher_form_obj,
}

assert pyramid_request.flags.enabled.calls == [
pretend.call(AdminFlagValue.DISALLOW_OIDC)
assert pyramid_request.flags.disallow_oidc.calls == [
pretend.call(AdminFlagValue.DISALLOW_GITHUB_OIDC)
]
assert pyramid_request.session.flash.calls == [
pretend.call(
(
"Trusted publishers are temporarily disabled. "
"GitHub-based trusted publishing is temporarily disabled. "
"See https://pypi.org/help#admin-intervention for details."
),
queue="error",
Expand All @@ -3277,7 +3275,7 @@ def test_add_pending_github_oidc_publisher_user_cannot_register(
has_primary_verified_email=False,
)
pyramid_request.flags = pretend.stub(
enabled=pretend.call_recorder(lambda f: False)
disallow_oidc=pretend.call_recorder(lambda f=None: False),
)
pyramid_request.session = pretend.stub(
flash=pretend.call_recorder(lambda *a, **kw: None)
Expand All @@ -3302,8 +3300,8 @@ def test_add_pending_github_oidc_publisher_user_cannot_register(
"pending_github_publisher_form": pending_github_publisher_form_obj,
}

assert pyramid_request.flags.enabled.calls == [
pretend.call(AdminFlagValue.DISALLOW_OIDC)
assert pyramid_request.flags.disallow_oidc.calls == [
pretend.call(AdminFlagValue.DISALLOW_GITHUB_OIDC)
]
assert view.metrics.increment.calls == [
pretend.call(
Expand Down Expand Up @@ -3351,7 +3349,9 @@ def test_add_pending_github_oidc_publisher_too_many_already(
"github.token": "fake-api-token",
}
)
db_request.flags = pretend.stub(enabled=pretend.call_recorder(lambda f: False))
db_request.flags = pretend.stub(
disallow_oidc=pretend.call_recorder(lambda f=None: False)
)
db_request.session = pretend.stub(
flash=pretend.call_recorder(lambda *a, **kw: None)
)
Expand All @@ -3372,8 +3372,8 @@ def test_add_pending_github_oidc_publisher_too_many_already(
view = views.ManageAccountPublishingViews(db_request)

assert view.add_pending_github_oidc_publisher() == view.default_response
assert db_request.flags.enabled.calls == [
pretend.call(AdminFlagValue.DISALLOW_OIDC)
assert db_request.flags.disallow_oidc.calls == [
pretend.call(AdminFlagValue.DISALLOW_GITHUB_OIDC)
]
assert view.metrics.increment.calls == [
pretend.call(
Expand Down Expand Up @@ -3406,7 +3406,7 @@ def test_add_pending_github_oidc_publisher_ratelimited(
}
)
pyramid_request.flags = pretend.stub(
enabled=pretend.call_recorder(lambda f: False)
disallow_oidc=pretend.call_recorder(lambda f=None: False)
)
pyramid_request.session = pretend.stub(
flash=pretend.call_recorder(lambda *a, **kw: None)
Expand Down Expand Up @@ -3460,7 +3460,7 @@ def test_add_pending_github_oidc_publisher_invalid_form(
}
)
pyramid_request.flags = pretend.stub(
enabled=pretend.call_recorder(lambda f: False)
disallow_oidc=pretend.call_recorder(lambda f=None: False)
)
pyramid_request.session = pretend.stub(
flash=pretend.call_recorder(lambda *a, **kw: None)
Expand Down Expand Up @@ -3532,7 +3532,9 @@ def test_add_pending_github_oidc_publisher_already_exists(
"github.token": "fake-api-token",
}
)
db_request.flags = pretend.stub(enabled=pretend.call_recorder(lambda f: False))
db_request.flags = pretend.stub(
disallow_oidc=pretend.call_recorder(lambda f=None: False)
)
db_request.session = pretend.stub(
flash=pretend.call_recorder(lambda *a, **kw: None)
)
Expand Down Expand Up @@ -3595,7 +3597,9 @@ def test_add_pending_github_oidc_publisher(self, monkeypatch, db_request):
"github.token": "fake-api-token",
}
)
db_request.flags = pretend.stub(enabled=pretend.call_recorder(lambda f: False))
db_request.flags = pretend.stub(
disallow_oidc=pretend.call_recorder(lambda f=None: False)
)
db_request.session = pretend.stub(
flash=pretend.call_recorder(lambda *a, **kw: None)
)
Expand Down Expand Up @@ -3692,7 +3696,7 @@ def test_delete_pending_oidc_publisher_admin_disabled(
}
)
pyramid_request.flags = pretend.stub(
enabled=pretend.call_recorder(lambda f: True)
disallow_oidc=pretend.call_recorder(lambda f=None: True)
)
pyramid_request.session = pretend.stub(
flash=pretend.call_recorder(lambda *a, **kw: None)
Expand All @@ -3717,13 +3721,11 @@ def test_delete_pending_oidc_publisher_admin_disabled(
"pending_github_publisher_form": pending_github_publisher_form_obj,
}

assert pyramid_request.flags.enabled.calls == [
pretend.call(AdminFlagValue.DISALLOW_OIDC)
]
assert pyramid_request.flags.disallow_oidc.calls == [pretend.call()]
assert pyramid_request.session.flash.calls == [
pretend.call(
(
"Trusted publishers are temporarily disabled. "
"Trusted publishing is temporarily disabled. "
"See https://pypi.org/help#admin-intervention for details."
),
queue="error",
Expand All @@ -3745,7 +3747,7 @@ def test_delete_pending_oidc_publisher_invalid_form(
settings={"warehouse.oidc.enabled": True}
)
pyramid_request.flags = pretend.stub(
enabled=pretend.call_recorder(lambda f: False)
disallow_oidc=pretend.call_recorder(lambda f=None: False)
)
pyramid_request.session = pretend.stub(
flash=pretend.call_recorder(lambda *a, **kw: None)
Expand Down Expand Up @@ -3783,7 +3785,9 @@ def test_delete_pending_oidc_publisher_not_found(self, monkeypatch, db_request):
db_request.db.add(pending_publisher)

db_request.registry = pretend.stub(settings={"warehouse.oidc.enabled": True})
db_request.flags = pretend.stub(enabled=pretend.call_recorder(lambda f: False))
db_request.flags = pretend.stub(
disallow_oidc=pretend.call_recorder(lambda f=None: False)
)
db_request.session = pretend.stub(
flash=pretend.call_recorder(lambda *a, **kw: None)
)
Expand Down Expand Up @@ -3824,7 +3828,9 @@ def test_delete_pending_oidc_publisher_no_access(self, monkeypatch, db_request):

db_request.user = pretend.stub()
db_request.registry = pretend.stub(settings={"warehouse.oidc.enabled": True})
db_request.flags = pretend.stub(enabled=pretend.call_recorder(lambda f: False))
db_request.flags = pretend.stub(
disallow_oidc=pretend.call_recorder(lambda f=None: False)
)
db_request.session = pretend.stub(
flash=pretend.call_recorder(lambda *a, **kw: None)
)
Expand Down Expand Up @@ -3863,7 +3869,9 @@ def test_delete_pending_oidc_publisher(self, monkeypatch, db_request):
db_request.db.flush() # To get the id

db_request.registry = pretend.stub(settings={"warehouse.oidc.enabled": True})
db_request.flags = pretend.stub(enabled=pretend.call_recorder(lambda f: False))
db_request.flags = pretend.stub(
disallow_oidc=pretend.call_recorder(lambda f=None: False)
)
db_request.session = pretend.stub(
flash=pretend.call_recorder(lambda *a, **kw: None)
)
Expand Down
28 changes: 28 additions & 0 deletions tests/unit/admin/test_flags.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@

import enum

import pytest

from warehouse.admin.flags import AdminFlag, AdminFlagValue

from ...common.db.admin import AdminFlagFactory


Expand All @@ -22,6 +26,30 @@ class TestAdminFlagValues(enum.Enum):


class TestAdminFlag:
@pytest.mark.parametrize(
("disallow_oidc", "disallow_github_oidc", "oidc_enabled"),
[
(False, False, True),
(True, False, False),
(False, True, False),
(True, True, False),
],
)
def test_disallow_oidc(
self, db_request, disallow_oidc, disallow_github_oidc, oidc_enabled
):
flag = db_request.db.get(AdminFlag, "disallow-oidc")
flag.enabled = disallow_oidc

flag = db_request.db.get(AdminFlag, "disallow-github-oidc")
flag.enabled = disallow_github_oidc

assert (
not db_request.flags.disallow_oidc(AdminFlagValue.DISALLOW_GITHUB_OIDC)
== oidc_enabled
)
assert db_request.flags.disallow_oidc() == disallow_oidc

def test_default(self, db_request):
assert not db_request.flags.enabled(TestAdminFlagValues.NOT_A_REAL_FLAG)

Expand Down
Loading