From b8478ea2c883bb9a8c0faad28b66e09de4b9d2dd Mon Sep 17 00:00:00 2001 From: Sergey G Date: Thu, 11 Jan 2018 17:37:02 +0500 Subject: [PATCH] LIL-71 Use logout --- apps/auth/tests/test_logout.py | 20 ++++++++++++++++++++ apps/auth/urls.py | 3 ++- apps/auth/views.py | 12 +++++++++--- 3 files changed, 31 insertions(+), 4 deletions(-) create mode 100644 apps/auth/tests/test_logout.py diff --git a/apps/auth/tests/test_logout.py b/apps/auth/tests/test_logout.py new file mode 100644 index 00000000..386f8903 --- /dev/null +++ b/apps/auth/tests/test_logout.py @@ -0,0 +1,20 @@ +from django.test import TestCase, Client +from django.contrib.auth import get_user_model +from django.urls import reverse + + +User = get_user_model() + + +class LogoutTest(TestCase): + def setUp(self): + self.url = reverse("lilcity:logout") + self.user = User.objects.create(username='Alice', password='1234') + self.client = Client() + + def test_logout(self): + self.client.login(username=self.user.username, password='1234') + + response = self.client.post(self.url) + + self.assertTrue(response.json()['success']) diff --git a/apps/auth/urls.py b/apps/auth/urls.py index ccf2d689..8ea22d9b 100644 --- a/apps/auth/urls.py +++ b/apps/auth/urls.py @@ -3,5 +3,6 @@ from django.urls import path from . import views urlpatterns = [ - path('registration/learner/', views.LearnerRegistrationView.as_view(), name="registration-learner") + path('registration/learner/', views.LearnerRegistrationView.as_view(), name="registration-learner"), + path('logout/', views.LogoutView.as_view(), name="logout"), ] diff --git a/apps/auth/views.py b/apps/auth/views.py index 5e6d9287..ab6123f6 100644 --- a/apps/auth/views.py +++ b/apps/auth/views.py @@ -1,6 +1,6 @@ -from django.views.generic import FormView +from django.views.generic import FormView, View from django.http import JsonResponse -from django.contrib.auth import get_user_model +from django.contrib.auth import get_user_model, logout from .forms import LearnerRegistrationForm @@ -18,9 +18,15 @@ class LearnerRegistrationView(FormView): email = form.cleaned_data['email'] password = form.cleaned_data['password'] - User.objects.create(username=email, email=email, first_name=first_name, last_name=last_name, password=password) + User.objects.create_user(username=email, email=email, first_name=first_name, last_name=last_name, password=password) return JsonResponse({"success": True}, status=201) def form_invalid(self, form): return JsonResponse(form.errors.get_json_data(escape_html=True), status=400) + + +class LogoutView(View): + def post(self, request, *args, **kwargs): + logout(request) + return JsonResponse({"success": True})