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

Allow marking projects as "archived" #17005

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
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
14 changes: 14 additions & 0 deletions tests/unit/admin/test_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,20 @@ def test_includeme():
traverse="/{project_name}",
domain=warehouse,
),
pretend.call(
"admin.project.archive",
"/admin/projects/{project_name}/archive/",
factory="warehouse.packaging.models:ProjectFactory",
traverse="/{project_name}",
domain=warehouse,
),
pretend.call(
"admin.project.unarchive",
"/admin/projects/{project_name}/unarchive/",
factory="warehouse.packaging.models:ProjectFactory",
traverse="/{project_name}",
domain=warehouse,
),
pretend.call("admin.journals.list", "/admin/journals/", domain=warehouse),
pretend.call(
"admin.prohibited_project_names.list",
Expand Down
95 changes: 94 additions & 1 deletion tests/unit/admin/views/test_projects.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
from tests.common.db.oidc import GitHubPublisherFactory
from warehouse.admin.views import projects as views
from warehouse.observations.models import ObservationKind
from warehouse.packaging.models import Project, Role
from warehouse.packaging.models import LifecycleStatus, Project, Role
from warehouse.packaging.tasks import update_release_description
from warehouse.search.tasks import reindex_project
from warehouse.utils.paginate import paginate_url_factory
Expand Down Expand Up @@ -952,3 +952,96 @@ def test_reindexes_project(self, db_request):
assert db_request.session.flash.calls == [
pretend.call("Task sent to reindex the project 'foo'", queue="success")
]


class TestProjectArchival:
def test_archive(self, db_request):
project = ProjectFactory.create(name="foo")
user = UserFactory.create(username="testuser")

db_request.route_path = pretend.call_recorder(lambda *a, **kw: "/the-redirect")
db_request.method = "POST"
db_request.user = user
db_request.session = pretend.stub(
flash=pretend.call_recorder(lambda *a, **kw: None)
)

result = views.archive_project_view(project, db_request)

assert isinstance(result, HTTPSeeOther)
assert result.headers["Location"] == "/the-redirect"
assert project.lifecycle_status == LifecycleStatus.Archived
assert db_request.route_path.calls == [
pretend.call("admin.project.detail", project_name=project.name)
]

def test_unarchive_project(self, db_request):
project = ProjectFactory.create(
name="foo", lifecycle_status=LifecycleStatus.Archived
)
user = UserFactory.create(username="testuser")

db_request.route_path = pretend.call_recorder(lambda *a, **kw: "/the-redirect")
db_request.method = "POST"
db_request.user = user
db_request.session = pretend.stub(
flash=pretend.call_recorder(lambda *a, **kw: None)
)

result = views.unarchive_project_view(project, db_request)

assert isinstance(result, HTTPSeeOther)
assert result.headers["Location"] == "/the-redirect"
assert db_request.route_path.calls == [
pretend.call("admin.project.detail", project_name=project.name)
]
assert project.lifecycle_status is None

def test_disallowed_archive(self, db_request):
project = ProjectFactory.create(name="foo", lifecycle_status="quarantine-enter")
user = UserFactory.create(username="testuser")

db_request.route_path = pretend.call_recorder(lambda *a, **kw: "/the-redirect")
db_request.method = "POST"
db_request.user = user
db_request.session = pretend.stub(
flash=pretend.call_recorder(lambda *a, **kw: None)
)

result = views.archive_project_view(project, db_request)

assert isinstance(result, HTTPSeeOther)
assert result.headers["Location"] == "/the-redirect"
assert db_request.session.flash.calls == [
pretend.call(
f"Cannot archive project with status {project.lifecycle_status}",
queue="error",
)
]
assert db_request.route_path.calls == [
pretend.call("admin.project.detail", project_name="foo")
]
assert project.lifecycle_status == "quarantine-enter"

def test_disallowed_unarchive(self, db_request):
project = ProjectFactory.create(name="foo", lifecycle_status="quarantine-enter")
user = UserFactory.create(username="testuser")

db_request.route_path = pretend.call_recorder(lambda *a, **kw: "/the-redirect")
db_request.method = "POST"
db_request.user = user
db_request.session = pretend.stub(
flash=pretend.call_recorder(lambda *a, **kw: None)
)

result = views.unarchive_project_view(project, db_request)

assert isinstance(result, HTTPSeeOther)
assert result.headers["Location"] == "/the-redirect"
assert db_request.session.flash.calls == [
pretend.call("Can only unarchive an archived project", queue="error")
]
assert db_request.route_path.calls == [
pretend.call("admin.project.detail", project_name="foo")
]
assert project.lifecycle_status == "quarantine-enter"
101 changes: 97 additions & 4 deletions tests/unit/manage/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
from warehouse.packaging.models import (
File,
JournalEntry,
LifecycleStatus,
Project,
Release,
Role,
Expand Down Expand Up @@ -96,7 +97,6 @@


class TestManageUnverifiedAccount:

def test_manage_account(self, monkeypatch):
user_service = pretend.stub()
name = pretend.stub()
Expand Down Expand Up @@ -2603,7 +2603,7 @@ class TestManageProjectSettings:
@pytest.mark.parametrize("enabled", [False, True])
def test_manage_project_settings(self, enabled, monkeypatch):
request = pretend.stub(organization_access=enabled)
project = pretend.stub(organization=None)
project = pretend.stub(organization=None, lifecycle_status=None)
view = views.ManageProjectSettingsViews(project, request)
form = pretend.stub()
view.transfer_organization_project_form_class = lambda *a, **kw: form
Expand All @@ -2630,7 +2630,7 @@ def test_manage_project_settings_in_organization_managed(self, monkeypatch):
request = pretend.stub(organization_access=True)
organization_managed = pretend.stub(name="managed-org", is_active=True)
organization_owned = pretend.stub(name="owned-org", is_active=True)
project = pretend.stub(organization=organization_managed)
project = pretend.stub(organization=organization_managed, lifecycle_status=None)
view = views.ManageProjectSettingsViews(project, request)
form = pretend.stub()
view.transfer_organization_project_form_class = pretend.call_recorder(
Expand Down Expand Up @@ -2662,7 +2662,7 @@ def test_manage_project_settings_in_organization_owned(self, monkeypatch):
request = pretend.stub(organization_access=True)
organization_managed = pretend.stub(name="managed-org", is_active=True)
organization_owned = pretend.stub(name="owned-org", is_active=True)
project = pretend.stub(organization=organization_owned)
project = pretend.stub(organization=organization_owned, lifecycle_status=None)
view = views.ManageProjectSettingsViews(project, request)
form = pretend.stub()
view.transfer_organization_project_form_class = pretend.call_recorder(
Expand Down Expand Up @@ -7467,3 +7467,96 @@ def test_delete_oidc_publisher_admin_disabled(self, monkeypatch):
queue="error",
)
]


class TestArchiveProject:
def test_archive(self, db_request):
project = ProjectFactory.create(name="foo")
user = UserFactory.create(username="testuser")

db_request.route_path = pretend.call_recorder(lambda *a, **kw: "/the-redirect")
db_request.method = "POST"
db_request.user = user
db_request.session = pretend.stub(
flash=pretend.call_recorder(lambda *a, **kw: None)
)

result = views.archive_project_view(project, db_request)

assert isinstance(result, HTTPSeeOther)
assert result.headers["Location"] == "/the-redirect"
assert project.lifecycle_status == LifecycleStatus.Archived
assert db_request.route_path.calls == [
pretend.call("manage.project.settings", project_name=project.name)
]

def test_unarchive_project(self, db_request):
project = ProjectFactory.create(
name="foo", lifecycle_status=LifecycleStatus.Archived
)
user = UserFactory.create(username="testuser")

db_request.route_path = pretend.call_recorder(lambda *a, **kw: "/the-redirect")
db_request.method = "POST"
db_request.user = user
db_request.session = pretend.stub(
flash=pretend.call_recorder(lambda *a, **kw: None)
)

result = views.unarchive_project_view(project, db_request)

assert isinstance(result, HTTPSeeOther)
assert result.headers["Location"] == "/the-redirect"
assert db_request.route_path.calls == [
pretend.call("manage.project.settings", project_name=project.name)
]
assert project.lifecycle_status is None

def test_disallowed_archive(self, db_request):
project = ProjectFactory.create(name="foo", lifecycle_status="quarantine-enter")
user = UserFactory.create(username="testuser")

db_request.route_path = pretend.call_recorder(lambda *a, **kw: "/the-redirect")
db_request.method = "POST"
db_request.user = user
db_request.session = pretend.stub(
flash=pretend.call_recorder(lambda *a, **kw: None)
)

result = views.archive_project_view(project, db_request)

assert isinstance(result, HTTPSeeOther)
assert result.headers["Location"] == "/the-redirect"
assert db_request.session.flash.calls == [
pretend.call(
f"Cannot archive project with status {project.lifecycle_status}",
queue="error",
)
]
assert db_request.route_path.calls == [
pretend.call("manage.project.settings", project_name="foo")
]
assert project.lifecycle_status == "quarantine-enter"

def test_disallowed_unarchive(self, db_request):
project = ProjectFactory.create(name="foo", lifecycle_status="quarantine-enter")
user = UserFactory.create(username="testuser")

db_request.route_path = pretend.call_recorder(lambda *a, **kw: "/the-redirect")
db_request.method = "POST"
db_request.user = user
db_request.session = pretend.stub(
flash=pretend.call_recorder(lambda *a, **kw: None)
)

result = views.unarchive_project_view(project, db_request)

assert isinstance(result, HTTPSeeOther)
assert result.headers["Location"] == "/the-redirect"
assert db_request.session.flash.calls == [
pretend.call("Can only unarchive an archived project", queue="error")
]
assert db_request.route_path.calls == [
pretend.call("manage.project.settings", project_name="foo")
]
assert project.lifecycle_status == "quarantine-enter"
94 changes: 94 additions & 0 deletions tests/unit/packaging/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,100 @@ def test_acl_for_quarantined_project(self, db_session):
key=lambda x: x[1],
)

def test_acl_for_archived_project(self, db_session):
"""
If a Project is archived, the Project ACL should disallow uploads.
"""
project = DBProjectFactory.create(lifecycle_status="archived")
owner1 = DBRoleFactory.create(project=project)
owner2 = DBRoleFactory.create(project=project)

# Maintainers should not appear in the ACLs, since they only have
# upload permissions, and anchived projects don't allow upload
DBRoleFactory.create(project=project, role_name="Maintainer")
DBRoleFactory.create(project=project, role_name="Maintainer")

organization = DBOrganizationFactory.create()
owner3 = DBOrganizationRoleFactory.create(organization=organization)
DBOrganizationProjectFactory.create(organization=organization, project=project)

team = DBTeamFactory.create()
owner4 = DBTeamRoleFactory.create(team=team)
DBTeamProjectRoleFactory.create(
team=team, project=project, role_name=TeamProjectRoleType.Owner
)

# Publishers should not appear in the ACLs, since they only have upload
# permissions, and archived projects don't allow upload
GitHubPublisherFactory.create(projects=[project])

acls = []
for location in lineage(project):
try:
acl = location.__acl__
except AttributeError:
continue

if acl and callable(acl):
acl = acl()

acls.extend(acl)

_perms_read_and_write = [
Permissions.ProjectsRead,
Permissions.ProjectsWrite,
]
assert acls == [
(
Allow,
"group:admins",
(
Permissions.AdminDashboardSidebarRead,
Permissions.AdminObservationsRead,
Permissions.AdminObservationsWrite,
Permissions.AdminProhibitedProjectsWrite,
Permissions.AdminProhibitedUsernameWrite,
Permissions.AdminProjectsDelete,
Permissions.AdminProjectsRead,
Permissions.AdminProjectsSetLimit,
Permissions.AdminProjectsWrite,
Permissions.AdminRoleAdd,
Permissions.AdminRoleDelete,
),
),
(
Allow,
"group:moderators",
(
Permissions.AdminDashboardSidebarRead,
Permissions.AdminObservationsRead,
Permissions.AdminObservationsWrite,
Permissions.AdminProjectsRead,
Permissions.AdminProjectsSetLimit,
Permissions.AdminRoleAdd,
Permissions.AdminRoleDelete,
),
),
(
Allow,
"group:observers",
Permissions.APIObservationsAdd,
),
(
Allow,
Authenticated,
Permissions.SubmitMalwareObservation,
),
] + sorted(
[
(Allow, f"user:{owner1.user.id}", _perms_read_and_write),
(Allow, f"user:{owner2.user.id}", _perms_read_and_write),
(Allow, f"user:{owner3.user.id}", _perms_read_and_write),
(Allow, f"user:{owner4.user.id}", _perms_read_and_write),
],
key=lambda x: x[1],
)

def test_repr(self, db_request):
project = DBProjectFactory()
assert isinstance(repr(project), str)
Expand Down
14 changes: 14 additions & 0 deletions tests/unit/test_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -492,6 +492,20 @@ def add_redirect_rule(*args, **kwargs):
traverse="/{project_name}",
domain=warehouse,
),
pretend.call(
"manage.project.archive",
"/manage/project/{project_name}/archive/",
factory="warehouse.packaging.models:ProjectFactory",
traverse="/{project_name}",
domain=warehouse,
),
pretend.call(
"manage.project.unarchive",
"/manage/project/{project_name}/unarchive/",
factory="warehouse.packaging.models:ProjectFactory",
traverse="/{project_name}",
domain=warehouse,
),
pretend.call(
"manage.project.history",
"/manage/project/{project_name}/history/",
Expand Down
Loading