Skip to content

Commit c8c1c8e

Browse files
committed
test(team): enhance team member removal tests with access control checks
1 parent bdcd9a8 commit c8c1c8e

File tree

2 files changed

+16
-4
lines changed

2 files changed

+16
-4
lines changed

todo/tests/unit/views/test_team.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -143,8 +143,10 @@ def setUp(self):
143143
self.user_id = "507f1f77bcf86cd799439011"
144144
self.mock_user_id = "507f1f77bcf86cd799439013"
145145

146+
@patch("todo.utils.team_access.has_team_access")
146147
@patch("todo.views.team.TeamService.remove_member_from_team")
147-
def test_remove_member_success(self, mock_remove):
148+
def test_remove_member_success(self, mock_remove, mock_has_team_access):
149+
mock_has_team_access.return_value = True
148150
mock_remove.return_value = True
149151

150152
mock_request = MagicMock()
@@ -157,10 +159,12 @@ def test_remove_member_success(self, mock_remove):
157159
user_id=self.user_id, team_id=self.team_id, removed_by_user_id=self.mock_user_id
158160
)
159161

162+
@patch("todo.utils.team_access.has_team_access")
160163
@patch("todo.views.team.TeamService.remove_member_from_team")
161-
def test_remove_member_not_found(self, mock_remove):
164+
def test_remove_member_not_found(self, mock_remove, mock_has_team_access):
162165
from todo.services.team_service import TeamService
163166

167+
mock_has_team_access.return_value = True
164168
mock_remove.side_effect = TeamService.TeamOrUserNotFound()
165169

166170
mock_request = MagicMock()
@@ -171,8 +175,10 @@ def test_remove_member_not_found(self, mock_remove):
171175
self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)
172176
self.assertIn("not found", response.data["detail"])
173177

178+
@patch("todo.utils.team_access.has_team_access")
174179
@patch("todo.views.team.TeamService.remove_member_from_team")
175-
def test_remove_member_generic_error(self, mock_remove):
180+
def test_remove_member_generic_error(self, mock_remove, mock_has_team_access):
181+
mock_has_team_access.return_value = True
176182
mock_remove.side_effect = Exception("Something went wrong")
177183

178184
mock_request = MagicMock()

todo/utils/team_access.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,13 @@ def has_team_access(user_id: str, team_id: str) -> bool:
3030

3131
def team_access_required(func):
3232
def wrapper(self, request, *args, **kwargs):
33-
if not has_team_access(request.user_id, kwargs["team_id"]):
33+
team_id = kwargs.get("team_id")
34+
if team_id is None and len(args) > 0:
35+
team_id = args[0]
36+
if not team_id:
37+
return Response({"detail": "Team ID is required."}, status=status.HTTP_400_BAD_REQUEST)
38+
39+
if not has_team_access(request.user_id, team_id):
3440
return Response({"detail": "You are not authorized to view this team."}, status=status.HTTP_403_FORBIDDEN)
3541
return func(self, request, *args, **kwargs)
3642

0 commit comments

Comments
 (0)