Skip to content

Commit

Permalink
Unified errors format (#57)
Browse files Browse the repository at this point in the history
* Switched errors to the format defined in #56

I can't get any other `"msg"` with grep, hope everything is ok.

* 🔨 Fixed a mistake made with a bad sed line 👉👈
  • Loading branch information
Lugrim authored Oct 10, 2023
1 parent 1683d77 commit c8c464b
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 14 deletions.
6 changes: 5 additions & 1 deletion insalan/user/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from django.core import mail
from rest_framework import serializers
from insalan.user.models import User
from django.utils.translation import gettext_lazy as _
import re


Expand Down Expand Up @@ -295,7 +296,10 @@ def send_valid_data(data):
request = self.client.post("/v1/user/login/", data, format="json")

self.assertEquals(request.status_code, 404)
self.assertEquals(request.data["user"][0], "Wrong username or password")
self.assertEquals(
request.data["user"][0],
_("Nom d'utilisateur ou mot de passe incorrect"),
)

send_valid_data(
{
Expand Down
40 changes: 27 additions & 13 deletions insalan/user/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
@require_GET
@ensure_csrf_cookie
def get_csrf(request):
return JsonResponse({"csrf": "Cookie has been set"})
return JsonResponse({"csrf": _("Le cookie a été défini")})


class UserView(generics.RetrieveUpdateDestroyAPIView):
Expand Down Expand Up @@ -82,7 +82,10 @@ def get(self, request, user=None, token=None):
try:
user_object: User = User.objects.get(username=user)
except User.DoesNotExist:
return Response({"msg": error_text}, status=status.HTTP_400_BAD_REQUEST)
return Response(
{"user": [error_text]},
status=status.HTTP_400_BAD_REQUEST,
)

if EmailConfirmationTokenGenerator().check_token(
user_object,
Expand All @@ -93,7 +96,7 @@ def get(self, request, user=None, token=None):
user_object.save()
return Response()

return Response({"msg": error_text}, status=status.HTTP_400_BAD_REQUEST)
return Response({"user": [error_text]}, status=status.HTTP_400_BAD_REQUEST)


class AskForPasswordReset(APIView):
Expand Down Expand Up @@ -123,7 +126,7 @@ def post(self, request):
and "password_confirm" in data
):
return Response(
{"msg": _("Champ manquant dans la ré-initialisation de mot de passe")},
{"user": [_("Champ manquant")]},
status=status.HTTP_400_BAD_REQUEST,
)
try:
Expand All @@ -140,25 +143,26 @@ def post(self, request):
else:
return Response(
{
"msg": _("Mot de passe trop simple ou invalide"),
"user": [_("Mot de passe trop simple ou invalide")],
"errors": validation_errors,
},
status=status.HTTP_400_BAD_REQUEST,
)
else:
return Response(
{"msg": _("Les mots de passe diffèrent")},
{"user": [_("Les mots de passe diffèrent")]},
status=status.HTTP_400_BAD_REQUEST,
)
else:
return Response(
{"msg": _("Jeton de ré-initialisation invalide")},
{"user": [_("Jeton de ré-initialisation invalide")]},
status=status.HTTP_400_BAD_REQUEST,
)

except User.DoesNotExist:
return Response(
{"msg": _("Utilisateur non trouvé")}, status=status.HTTP_400_BAD_REQUEST
{"user": [_("Utilisateur non trouvé")]},
status=status.HTTP_400_BAD_REQUEST,
)

return Response({}, status=status.HTTP_501_NOT_IMPLEMENTED)
Expand All @@ -178,15 +182,24 @@ def post(self, request):
username = request.data.get("username")

if not username:
return Response({"msg": error_text}, status=status.HTTP_400_BAD_REQUEST)
return Response(
{"user": [error_text]},
status=status.HTTP_400_BAD_REQUEST,
)

try:
user_object: User = User.objects.get(username=username)
except User.DoesNotExist:
return Response({"msg": error_text}, status=status.HTTP_400_BAD_REQUEST)
return Response(
{"user": [error_text]},
status=status.HTTP_400_BAD_REQUEST,
)

if user_object.email_active:
return Response({"msg": error_text}, status=status.HTTP_400_BAD_REQUEST)
return Response(
{"user": [error_text]},
status=status.HTTP_400_BAD_REQUEST,
)

UserMailer.send_email_confirmation(user_object)
return Response()
Expand Down Expand Up @@ -217,13 +230,14 @@ def post(self, request):
user = serializer.check_validity(data)
if user is None:
return Response(
{"user": [_("Wrong username or password")]},
{"user": [_("Nom d'utilisateur ou mot de passe incorrect")]},
status=status.HTTP_404_NOT_FOUND,
)
login(request, user)
return Response(status=status.HTTP_200_OK)
return Response(
{"msg": "Invalid data submitted"}, status=status.HTTP_400_BAD_REQUEST
{"user": [_("Format des données soumises invalide")]},
status=status.HTTP_400_BAD_REQUEST,
)


Expand Down

0 comments on commit c8c464b

Please sign in to comment.