From fa0979dce17c779321325c6ef173addb47af52f0 Mon Sep 17 00:00:00 2001 From: Tom Hacohen Date: Mon, 28 Dec 2020 16:57:09 +0200 Subject: [PATCH] Test reset: clean reset function. --- etebase_fastapi/test_reset_view.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/etebase_fastapi/test_reset_view.py b/etebase_fastapi/test_reset_view.py index f21fd84..3075290 100644 --- a/etebase_fastapi/test_reset_view.py +++ b/etebase_fastapi/test_reset_view.py @@ -2,31 +2,33 @@ from django.conf import settings from django.contrib.auth import get_user_model from django.db import transaction from django.shortcuts import get_object_or_404 -from fastapi import APIRouter, Request, Response, status +from fastapi import APIRouter, Request, status from django_etebase.utils import get_user_queryset, CallbackContext from etebase_fastapi.authentication import SignupIn, signup_save from etebase_fastapi.msgpack import MsgpackRoute +from etebase_fastapi.exceptions import HttpError test_reset_view_router = APIRouter(route_class=MsgpackRoute, tags=["test helpers"]) User = get_user_model() -@test_reset_view_router.post("/reset/") +@test_reset_view_router.post("/reset/", status_code=status.HTTP_204_NO_CONTENT) def reset(data: SignupIn, request: Request): # Only run when in DEBUG mode! It's only used for tests if not settings.DEBUG: - return Response("Only allowed in debug mode.", status_code=status.HTTP_400_BAD_REQUEST) + raise HttpError(code="generic", detail="Only allowed in debug mode.") with transaction.atomic(): user_queryset = get_user_queryset(User.objects.all(), CallbackContext(request.path_params)) user = get_object_or_404(user_queryset, username=data.user.username) # Only allow test users for extra safety if not getattr(user, User.USERNAME_FIELD).startswith("test_user"): - return Response("Endpoint not allowed for user.", status_code=status.HTTP_400_BAD_REQUEST) + raise HttpError(code="generic", detail="Endpoint not allowed for user.") if hasattr(user, "userinfo"): user.userinfo.delete() + signup_save(data, request) # Delete all of the journal data for this user for a clear test env user.collection_set.all().delete() @@ -34,5 +36,3 @@ def reset(data: SignupIn, request: Request): user.incoming_invitations.all().delete() # FIXME: also delete chunk files!!! - - return Response(status_code=status.HTTP_204_NO_CONTENT)