Skip to content

Commit

Permalink
Adds user group update form
Browse files Browse the repository at this point in the history
  • Loading branch information
jmsmkn committed Aug 13, 2019
1 parent 3e57f00 commit 563c9dc
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 21 deletions.
4 changes: 4 additions & 0 deletions app/grandchallenge/workstations/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,3 +101,7 @@ def add_or_remove_user(self, *, workstation):

class EditorsForm(UserGroupForm):
role = "editor"


class UsersForm(UserGroupForm):
role = "user"
10 changes: 8 additions & 2 deletions app/grandchallenge/workstations/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
WorkstationImageUpdate,
WorkstationList,
WorkstationUpdate,
WorkstationUserAutocomplete,
WorkstationUsersAutocomplete,
WorkstationUsersUpdate,
)

app_name = "workstations"
Expand All @@ -24,7 +25,7 @@
path("", WorkstationList.as_view(), name="list"),
path(
"users-autocomplete/",
WorkstationUserAutocomplete.as_view(),
WorkstationUsersAutocomplete.as_view(),
name="users-autocomplete",
),
path("create/", WorkstationCreate.as_view(), name="create"),
Expand All @@ -46,6 +47,11 @@
WorkstationEditorsUpdate.as_view(),
name="editors-update",
),
path(
"<slug>/users/update/",
WorkstationUsersUpdate.as_view(),
name="users-update",
),
path("<slug>/", WorkstationDetail.as_view(), name="detail"),
path("<slug>/update/", WorkstationUpdate.as_view(), name="update"),
path(
Expand Down
8 changes: 7 additions & 1 deletion app/grandchallenge/workstations/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
WorkstationForm,
WorkstationImageForm,
EditorsForm,
UsersForm,
)
from grandchallenge.workstations.models import (
Workstation,
Expand Down Expand Up @@ -94,7 +95,7 @@ class WorkstationUpdate(
raise_exception = True


class WorkstationUserAutocomplete(
class WorkstationUsersAutocomplete(
LoginRequiredMixin, UserPassesTestMixin, autocomplete.Select2QuerySetView
):
def test_func(self):
Expand Down Expand Up @@ -158,6 +159,11 @@ class WorkstationEditorsUpdate(WorkstationGroupUpdateMixin):
success_message = "Editors successfully updated"


class WorkstationUsersUpdate(WorkstationGroupUpdateMixin):
form_class = UsersForm
success_message = "Users successfully updated"


class WorkstationImageCreate(
LoginRequiredMixin, ObjectPermissionRequiredMixin, CreateView
):
Expand Down
1 change: 1 addition & 0 deletions app/tests/workstations_tests/test_permissions.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ def test_user_autocomplete_permissions(client, two_workstation_sets):
"workstations:image-detail",
"workstations:image-update",
"workstations:editors-update",
"workstations:users-update",
],
)
def test_workstation_editor_permissions(
Expand Down
40 changes: 22 additions & 18 deletions app/tests/workstations_tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -351,42 +351,46 @@ def test_workstation_proxy(client):


@pytest.mark.django_db
def test_group_update(client, two_workstation_sets):
new_editor = UserFactory()
@pytest.mark.parametrize("new_user", [False, True])
def test_workstation_group_update(client, two_workstation_sets, new_user):
new_editor = not new_user # Only tests for editors and users groups
group = "users" if new_user else "editors"

assert not two_workstation_sets.ws1.workstation.is_editor(user=new_editor)
assert not two_workstation_sets.ws1.workstation.is_user(user=new_editor)
assert not two_workstation_sets.ws2.workstation.is_editor(user=new_editor)
assert not two_workstation_sets.ws2.workstation.is_user(user=new_editor)
u = UserFactory()

assert not two_workstation_sets.ws1.workstation.is_editor(user=u)
assert not two_workstation_sets.ws1.workstation.is_user(user=u)
assert not two_workstation_sets.ws2.workstation.is_editor(user=u)
assert not two_workstation_sets.ws2.workstation.is_user(user=u)

response = get_view_for_user(
client=client,
viewname="workstations:editors-update",
viewname=f"workstations:{group}-update",
method=client.post,
reverse_kwargs={"slug": two_workstation_sets.ws1.workstation.slug},
user=two_workstation_sets.ws1.editor,
data={"action": "ADD", "user": new_editor.pk},
data={"action": "ADD", "user": u.pk},
follow=True,
)
assert response.status_code == 200

assert two_workstation_sets.ws1.workstation.is_editor(user=new_editor)
assert not two_workstation_sets.ws1.workstation.is_user(user=new_editor)
assert not two_workstation_sets.ws2.workstation.is_editor(user=new_editor)
assert not two_workstation_sets.ws2.workstation.is_user(user=new_editor)
assert two_workstation_sets.ws1.workstation.is_editor(user=u) == new_editor
assert two_workstation_sets.ws1.workstation.is_user(user=u) == new_user
assert not two_workstation_sets.ws2.workstation.is_editor(user=u)
assert not two_workstation_sets.ws2.workstation.is_user(user=u)

response = get_view_for_user(
client=client,
viewname="workstations:editors-update",
viewname=f"workstations:{group}-update",
method=client.post,
reverse_kwargs={"slug": two_workstation_sets.ws1.workstation.slug},
user=two_workstation_sets.ws1.editor,
data={"action": "REMOVE", "user": new_editor.pk},
data={"action": "REMOVE", "user": u.pk},
follow=True,
)
assert response.status_code == 200

assert not two_workstation_sets.ws1.workstation.is_editor(user=new_editor)
assert not two_workstation_sets.ws1.workstation.is_user(user=new_editor)
assert not two_workstation_sets.ws2.workstation.is_editor(user=new_editor)
assert not two_workstation_sets.ws2.workstation.is_user(user=new_editor)
assert not two_workstation_sets.ws1.workstation.is_editor(user=u)
assert not two_workstation_sets.ws1.workstation.is_user(user=u)
assert not two_workstation_sets.ws2.workstation.is_editor(user=u)
assert not two_workstation_sets.ws2.workstation.is_user(user=u)

0 comments on commit 563c9dc

Please sign in to comment.