Skip to content

Commit 3cc4e7e

Browse files
committed
Add ensure team member mixin to all shift actions
1 parent 096a5d4 commit 3cc4e7e

3 files changed

Lines changed: 50 additions & 3 deletions

File tree

src/teams/tests/test_shift_views.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -343,3 +343,32 @@ def test_team_shift_actions(self) -> None:
343343
rows = soup.select_one("table#main_table > tbody > tr:nth-of-type(1) td:nth-of-type(5)")
344344
matches = [s for s in rows if "Assign me" in str(s)]
345345
self.assertEqual(len(matches), 1, "team shift unassign failed")
346+
347+
# Test taking a shift as a user not on this team
348+
self.client.force_login(self.users[3]) # User not on the NOC team
349+
url = reverse(
350+
"teams:shift_member_take",
351+
kwargs={
352+
"team_slug": team_shift_1.team.slug,
353+
"camp_slug": self.camp.slug,
354+
"pk": team_shift_1.pk,
355+
},
356+
)
357+
response = self.client.get(
358+
path=url,
359+
follow=True
360+
)
361+
assert response.status_code == 200
362+
soup = BeautifulSoup(content, "html.parser")
363+
rows = soup.select("div.alert.alert-danger")
364+
matches = [s for s in rows if "No thanks" in str(s)]
365+
self.assertEqual(len(matches), 0, "team shift authorization failed")
366+
response = self.client.post(
367+
path=url,
368+
follow=True,
369+
)
370+
assert response.status_code == 200
371+
soup = BeautifulSoup(content, "html.parser")
372+
rows = soup.select("div.alert.alert-danger")
373+
matches = [s for s in rows if "No thanks" in str(s)]
374+
self.assertEqual(len(matches), 0, "team shift authorization failed")

src/teams/views/mixins.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,23 @@ def dispatch(self, request: HttpRequest, *args, **kwargs) -> HttpResponse:
3333
return super().dispatch(request, *args, **kwargs)
3434

3535

36+
class EnsureTeamMemberMixin:
37+
"""Use to make sure request.user has team member permission for the team specified by kwargs['team_slug']."""
38+
39+
def dispatch(self, request: HttpRequest, *args, **kwargs) -> HttpResponse:
40+
"""Method to make sure request.user has team member permission for the team specified by kwargs['team_slug']."""
41+
self.team = Team.objects.get(slug=kwargs["team_slug"], camp=self.camp)
42+
if self.team.member_permission_set not in request.user.get_all_permissions():
43+
messages.error(request, "No thanks")
44+
return redirect(
45+
"teams:general",
46+
camp_slug=self.camp.slug,
47+
team_slug=self.team.slug,
48+
)
49+
50+
return super().dispatch(request, *args, **kwargs)
51+
52+
3653
class EnsureTeamMemberLeadMixin(SingleObjectMixin):
3754
"""Use to make sure request.user has team lead permission for the team which TeamMember belongs to."""
3855

src/teams/views/shifts.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
from utils.mixins import IsTeamPermContextMixin
3232

3333
from .mixins import EnsureTeamLeadMixin
34+
from .mixins import EnsureTeamMemberMixin
3435

3536
if TYPE_CHECKING:
3637
from django.db.models import QuerySet
@@ -312,7 +313,7 @@ def get_context_data(self, **kwargs) -> dict:
312313
return context
313314

314315

315-
class MemberTakesShift(LoginRequiredMixin, CampViewMixin, UpdateView):
316+
class MemberTakesShift(LoginRequiredMixin, CampViewMixin, EnsureTeamMemberMixin, UpdateView):
316317
"""View for adding a user to a shift."""
317318
model = TeamShift
318319
fields = []
@@ -365,7 +366,7 @@ def form_valid(self, form: ModelForm[TeamShift]) -> HttpResponseRedirect:
365366
return HttpResponseRedirect(reverse("teams:shifts", kwargs=self.kwargs))
366367

367368

368-
class MemberDropsShift(LoginRequiredMixin, CampViewMixin, UpdateView):
369+
class MemberDropsShift(LoginRequiredMixin, CampViewMixin, EnsureTeamMemberMixin, UpdateView):
369370
model = TeamShift
370371
fields = []
371372
template_name = "team_shift_confirm_action.html"
@@ -394,7 +395,7 @@ def form_valid(self, form: ModelForm[TeamShift]) -> HttpResponseRedirect:
394395
return HttpResponseRedirect(reverse("teams:shifts", kwargs=self.kwargs))
395396

396397

397-
class MemberSellsShift(LoginRequiredMixin, CampViewMixin, UpdateView):
398+
class MemberSellsShift(LoginRequiredMixin, CampViewMixin, EnsureTeamMemberMixin, UpdateView):
398399
"""View for making a shift available for other user to take."""
399400
model = TeamShift
400401
fields = []

0 commit comments

Comments
 (0)