Skip to content

Commit

Permalink
feat: Add head method to UserPsychoPassView for checking user access
Browse files Browse the repository at this point in the history
  • Loading branch information
dragonejt committed Jun 25, 2024
1 parent 07a31e1 commit 0f74e38
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
17 changes: 17 additions & 0 deletions psychopass/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,23 @@ def test_get(self) -> None:
)
self.assertEqual(response.status_code, status.HTTP_200_OK)

def test_head_ok(self) -> None:
response = self.client.head(f"{self.url}?id={self.psycho_pass.user_id}")

self.assertEqual(response.status_code, status.HTTP_200_OK)

def test_head_forbidden(self) -> None:
user = get_user_model().objects.create(
username=get_random_string(10),
email=get_random_string(10),
password=get_random_string(10),
)
psycho_pass = UserPsychoPass.objects.create(
platform=user, user_id=get_random_string(20)
)
response = self.client.head(f"{self.url}?id={psycho_pass.user_id}")
self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)

def test_post(self) -> None:
user_id = get_random_string(20)
response = self.client.post(self.url, data={"userID": user_id}, format="json")
Expand Down
14 changes: 14 additions & 0 deletions psychopass/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,20 @@ def get(self, request: Request) -> Response:
return Response(
UserPsychoPassSerializer(psycho_pass).data, status=status.HTTP_200_OK
)

def head(self, request: Request) -> Response:
set_user(
{
"id": request.query_params.get("id"),
"username": f"{request.user.username}/{request.query_params.get("id")}",
}
)
psycho_pass = get_object_or_404(
UserPsychoPass, user_id=request.query_params.get("id")
)
if request.user == psycho_pass.platform:
return Response(status=status.HTTP_200_OK)
return Response(status=status.HTTP_403_FORBIDDEN)

def post(self, request: Request) -> Response:
set_user(
Expand Down

0 comments on commit 0f74e38

Please sign in to comment.