Skip to content
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
3 changes: 3 additions & 0 deletions documentcloud/projects/rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,15 @@ def is_admin(user, project):

can_add_remove = is_authenticated & is_edit_collaborator

can_view_collaborators = is_authenticated & is_collaborator

add_perm("projects.view_project", can_view)
add_perm("projects.add_project", is_authenticated)
add_perm("projects.change_project_all", can_change)
add_perm("projects.change_project", can_view)
add_perm("projects.add_remove_project", can_add_remove)
add_perm("projects.delete_project", can_change)
add_perm("projects.view_collaborators", can_view_collaborators)


# These predicates are for project memberships and collaborators
Expand Down
25 changes: 16 additions & 9 deletions documentcloud/projects/tests/test_rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ def test_project_rules():
admin_collaborator = UserFactory()
edit_collaborator = UserFactory()
view_collaborator = UserFactory()
non_collaborator = UserFactory()

public_project = ProjectFactory(
admin_collaborators=[admin_collaborator],
Expand All @@ -29,16 +30,22 @@ def test_project_rules():
private=True,
)

for user, project, can_view, can_change in [
(anonymous, public_project, True, False),
(anonymous, private_project, False, False),
(admin_collaborator, public_project, True, True),
(admin_collaborator, private_project, True, True),
(edit_collaborator, public_project, True, False),
(edit_collaborator, private_project, True, False),
(view_collaborator, public_project, True, False),
(view_collaborator, private_project, True, False),
for user, project, can_view, can_change, can_view_collaborators in [
(anonymous, public_project, True, False, False),
(anonymous, private_project, False, False, False),
(admin_collaborator, public_project, True, True, True),
(admin_collaborator, private_project, True, True, True),
(edit_collaborator, public_project, True, False, True),
(edit_collaborator, private_project, True, False, True),
(view_collaborator, public_project, True, False, True),
(view_collaborator, private_project, True, False, True),
(non_collaborator, public_project, True, False, False),
(non_collaborator, private_project, False, False, False),
]:
assert user.has_perm("projects.view_project", project) is can_view
assert user.has_perm("projects.change_project_all", project) is can_change
assert user.has_perm("projects.delete_project", project) is can_change
assert (
user.has_perm("projects.view_collaborators", project)
is can_view_collaborators
)
63 changes: 63 additions & 0 deletions documentcloud/projects/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -651,6 +651,32 @@ def test_list(self, client):
# add one for the projects owner
assert len(response_json["results"]) == size + 1

@pytest.mark.parametrize("access", ["edit_collaborators", "collaborators"])
def test_list_non_admin_collaborator(self, client, user, access):
"""Edit and view collaborators can see who else is on the project"""
other = UserFactory()
project = ProjectFactory(**{access: [user, other]})
client.force_authenticate(user=user)
response = client.get(f"/api/projects/{project.pk}/users/")
assert response.status_code == status.HTTP_200_OK
user_ids = {c["user"] for c in response.json()["results"]}
assert user_ids == {project.user.pk, user.pk, other.pk}

def test_list_non_collaborator_public(self, client, user):
"""Non-collaborators cannot see collaborators on a public project"""
project = ProjectFactory(private=False, collaborators=[UserFactory()])
client.force_authenticate(user=user)
response = client.get(f"/api/projects/{project.pk}/users/")
assert response.status_code == status.HTTP_200_OK
assert response.json()["results"] == []

def test_list_anonymous_public(self, client):
"""Anonymous users cannot see collaborators on a public project"""
project = ProjectFactory(private=False, collaborators=[UserFactory()])
response = client.get(f"/api/projects/{project.pk}/users/")
assert response.status_code == status.HTTP_200_OK
assert response.json()["results"] == []

@pytest.mark.parametrize("expand", ["", "~all", "user.organization"])
@override_settings(DEBUG=True)
def test_list_queries(self, client, expand):
Expand Down Expand Up @@ -745,6 +771,43 @@ def test_update(self, client, user):
collaborator = project.collaboration_set.get(user=user)
assert collaborator.access == CollaboratorAccess.admin

def test_retrieve_view_collaborator(self, client, user):
"""View collaborators can retrieve another collaborator"""
project = ProjectFactory(collaborators=[user])
client.force_authenticate(user=user)
response = client.get(f"/api/projects/{project.pk}/users/{project.user.pk}/")
assert response.status_code == status.HTTP_200_OK

def test_retrieve_non_collaborator_public(self, client, user):
"""Non-collaborators cannot retrieve a collaborator on a public project"""
project = ProjectFactory(private=False)
client.force_authenticate(user=user)
response = client.get(f"/api/projects/{project.pk}/users/{project.user.pk}/")
assert response.status_code == status.HTTP_404_NOT_FOUND

@pytest.mark.parametrize("access", ["edit_collaborators", "collaborators"])
def test_update_non_admin_collaborator(self, client, user, access):
"""Seeing collaborators does not let a non-admin change their access"""
other = UserFactory()
project = ProjectFactory(**{access: [user, other]})
client.force_authenticate(user=user)
response = client.patch(
f"/api/projects/{project.pk}/users/{other.pk}/", {"access": "admin"}
)
assert response.status_code == status.HTTP_403_FORBIDDEN
assert (
project.collaboration_set.get(user=other).access != CollaboratorAccess.admin
)

@pytest.mark.parametrize("access", ["edit_collaborators", "collaborators"])
def test_delete_non_admin_collaborator(self, client, user, access):
"""Seeing collaborators does not let a non-admin remove them"""
project = ProjectFactory(**{access: [user]})
client.force_authenticate(user=user)
response = client.delete(f"/api/projects/{project.pk}/users/{project.user.pk}/")
assert response.status_code == status.HTTP_403_FORBIDDEN
assert project.collaborators.filter(pk=project.user.pk).exists()

def test_update_bad_user(self, client, user):
"""You may not change a user on a Collaboration"""
project = ProjectFactory(collaborators=[user])
Expand Down
2 changes: 1 addition & 1 deletion documentcloud/projects/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -672,7 +672,7 @@ def get_queryset(self):
Project.objects.get_viewable(self.request.user),
pk=self.kwargs["project_pk"],
)
if self.request.user.has_perm("projects.change_project_all", project):
if self.request.user.has_perm("projects.view_collaborators", project):
queryset = project.collaboration_set.all()
else:
queryset = project.collaboration_set.none()
Expand Down
Loading