From 9b13404ce74c8953432759e83395a1bffcf4e42d Mon Sep 17 00:00:00 2001 From: Tom Hacohen Date: Tue, 14 Apr 2020 16:21:51 +0300 Subject: [PATCH] Add a reset view for tests. --- django_etesync/views.py | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/django_etesync/views.py b/django_etesync/views.py index 8de7313..a4b8108 100644 --- a/django_etesync/views.py +++ b/django_etesync/views.py @@ -12,9 +12,10 @@ # You should have received a copy of the GNU General Public License # along with this program. If not, see . +from django.conf import settings from django.contrib.auth import get_user_model from django.db import IntegrityError -from django.http import Http404 +from django.http import HttpResponseBadRequest, HttpResponse, Http404 from django.shortcuts import get_object_or_404 from rest_framework import status @@ -206,3 +207,26 @@ class CollectionItemChunkViewSet(viewsets.ViewSet): # FIXME: DO NOT USE! Use django-send file or etc instead. return serve(request, basename, dirname) + + +class ResetViewSet(BaseViewSet): + allowed_methods = ['POST'] + + def post(self, request, *args, **kwargs): + # Only run when in DEBUG mode! It's only used for tests + if not settings.DEBUG: + return HttpResponseBadRequest("Only allowed in debug mode.") + + # Only allow local users, for extra safety + if not getattr(request.user, User.USERNAME_FIELD).endswith('@localhost'): + return HttpResponseBadRequest("Endpoint not allowed for user.") + + # Delete all of the journal data for this user for a clear test env + request.user.collection_set.all().delete() + + # FIXME: also delete chunk files!!! + + return HttpResponse() + + +reset = ResetViewSet.as_view({'post': 'post'})