add: delete and edit accounts page

This commit is contained in:
2023-12-01 01:29:56 +01:00
parent c2b6dbb989
commit 25f315c24f
12 changed files with 232 additions and 23 deletions

View File

@ -1,4 +1,5 @@
from .register import *
from .login import *
from .logout import *
from .edit import *
from .delete import *

View File

@ -21,12 +21,17 @@ class DeleteTest(TestCase):
def test_normal_delete(self):
response: HttpResponse = self.client.delete(self.url)
response: HttpResponse = self.client.delete(self.url, {"password": self.password}, content_type='application/json')
response_text: str = response.content.decode("utf-8")
self.assertEqual(response_text, '"user deleted"')
def test_wrong_pass(self):
response: HttpResponse = self.client.delete(self.url, {"password": "cacaman a frapper"}, content_type='application/json')
errors: dict = eval(response.content)
self.assertDictEqual(errors, {"password": ["Password wrong."]})
def test_no_logged(self):
self.client.logout()
response: HttpResponse = self.client.post(self.url)
response: HttpResponse = self.client.delete(self.url, {"password": self.password}, content_type='application/json')
errors: dict = eval(response.content)
self.assertDictEqual(errors, {"detail":"Authentication credentials were not provided."})

View File

@ -8,5 +8,12 @@ class DeleteView(APIView):
permission_classes = (permissions.IsAuthenticated,)
authentication_classes = (SessionAuthentication,)
def delete(self, request: HttpRequest):
data: dict = request.data
password: str = data["password"]
if (password is None):
return Response({"password": ["This field may not be blank."]})
if (request.user.check_password(password) == False):
return Response({"password": ["Password wrong."]})
request.user.delete()
return Response("user deleted", status=status.HTTP_200_OK)

View File

@ -12,6 +12,9 @@ class EditView(APIView):
permission_classes = (permissions.IsAuthenticated,)
authentication_classes = (SessionAuthentication,)
def get(self, request: HttpRequest):
return Response({"username": request.user.username})
def patch(self, request: HttpRequest):
data: dict = request.data