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

warehouse: Allow maintainers to craft project tokens #6301

Merged
48 changes: 41 additions & 7 deletions tests/unit/manage/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -1363,6 +1363,41 @@ def test_default_response(self, monkeypatch):
"delete_macaroon_form": delete_macaroon_obj,
}

def test_project_names(self, db_request):
user = UserFactory.create()
another_user = UserFactory.create()

db_request.user = user
db_request.find_service = lambda *a, **kw: pretend.stub()

# A project with a sole owner that is the user
with_sole_owner = ProjectFactory.create(name="foo")
RoleFactory.create(user=user, project=with_sole_owner, role_name="Owner")
RoleFactory.create(
user=another_user, project=with_sole_owner, role_name="Maintainer"
)

# A project with multiple owners, including the user
with_multiple_owners = ProjectFactory.create(name="bar")
RoleFactory.create(user=user, project=with_multiple_owners, role_name="Owner")
RoleFactory.create(
user=another_user, project=with_multiple_owners, role_name="Owner"
)

# A project with a sole owner that is not the user
not_an_owner = ProjectFactory.create(name="baz")
RoleFactory.create(user=user, project=not_an_owner, role_name="Maintainer")
RoleFactory.create(user=another_user, project=not_an_owner, role_name="Owner")

# A project that the user is neither owner nor maintainer of
neither_owner_nor_maintainer = ProjectFactory.create(name="quux")
RoleFactory.create(
user=another_user, project=neither_owner_nor_maintainer, role_name="Owner"
)

view = views.ProvisionMacaroonViews(db_request)
assert set(view.project_names) == {"foo", "bar", "baz"}

def test_manage_macaroons(self, monkeypatch):
request = pretend.stub(find_service=lambda *a, **kw: pretend.stub())

Expand Down Expand Up @@ -1412,10 +1447,10 @@ def test_create_macaroon_invalid_form(self, monkeypatch):
)
monkeypatch.setattr(views, "CreateMacaroonForm", create_macaroon_cls)

user_projects = pretend.call_recorder(
lambda r: {"projects_owned": [pretend.stub(name=pretend.stub())]}
project_names = [pretend.stub()]
monkeypatch.setattr(
views.ProvisionMacaroonViews, "project_names", project_names
)
monkeypatch.setattr(views, "user_projects", user_projects)

default_response = {"default": "response"}
monkeypatch.setattr(
Expand Down Expand Up @@ -1458,11 +1493,10 @@ def test_create_macaroon(self, monkeypatch):
)
monkeypatch.setattr(views, "CreateMacaroonForm", create_macaroon_cls)

project_name = pretend.stub()
user_projects = pretend.call_recorder(
lambda r: {"projects_owned": [pretend.stub(name=project_name)]}
project_names = [pretend.stub()]
monkeypatch.setattr(
views.ProvisionMacaroonViews, "project_names", project_names
)
monkeypatch.setattr(views, "user_projects", user_projects)

default_response = {"default": "response"}
monkeypatch.setattr(
Expand Down
3 changes: 1 addition & 2 deletions warehouse/manage/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -559,8 +559,7 @@ def __init__(self, request):

@property
def project_names(self):
projects = user_projects(self.request)["projects_owned"]
return [project.name for project in projects]
return sorted(project.name for project in self.request.user.projects)

@property
def default_response(self):
Expand Down
2 changes: 1 addition & 1 deletion warehouse/templates/manage/account.html
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@
All projects
{% else %}
{% for project in macaroon.caveats.get("permissions")['projects'] %}
<a href="{{ request.route_path('manage.project.releases', project_name=project) }}">{{ project }}</a>
<a href="{{ request.route_path('packaging.project', name=project) }}">{{ project }}</a>
{% endfor %}
{% endif %}
</td>
Expand Down
1 change: 1 addition & 0 deletions warehouse/templates/manage/token.html
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ <h2>Add another token</h2>
<label for="token_scope" class="form-group__label">Scope</label>
<select name="token_scope" id="token_scope" class="form-group__input" aria-describedby="token_scope-errors">
<option disabled selected value="scope:unspecified">Select scope...</option>
<option value="scope:user">Entire account (all projects)</option>
{% for project in project_names %}
<option value="scope:project:{{ project }}">Project: {{ project }}</option>
{% endfor %}
Expand Down