From fa5c5060d54a6f8575a7ef1fbd1676543521e6af Mon Sep 17 00:00:00 2001 From: JohannesSetiawan Date: Fri, 23 Feb 2024 17:24:12 +0700 Subject: [PATCH] [RED] Create test and skeleton for SendRecoverPasswordEmail feature --- authentication/tests.py | 34 ++++++++++++++++++++++++++++++++++ authentication/urls.py | 5 +++-- authentication/views.py | 11 ++++++++++- 3 files changed, 47 insertions(+), 3 deletions(-) diff --git a/authentication/tests.py b/authentication/tests.py index 75a61b1..2855f74 100644 --- a/authentication/tests.py +++ b/authentication/tests.py @@ -9,6 +9,7 @@ REGISTER_LINK = reverse('authentication:register') LOGIN_LINK = reverse('authentication:login') EMAIL_VERIFICATION_LINK = reverse('authentication:verify_email') +RECOVER_PASSWORD_LINK = reverse('authentication:recover_password') class RegisterTest(TestCase): @@ -96,3 +97,36 @@ def test_valid_verification_token(self): def test_invalid_verification_token(self): response = self.client.post((EMAIL_VERIFICATION_LINK), {'token': 'invalid token'}) self.assertEqual(response.status_code, 400) + +class SendRecoverPasswordEmailTest(TestCase): + def setUp(self): + self.client = APIClient() + self.user = AppUser.objects.create_user(email='email@email.com',username='testuser',password='test') + + def test_sent_email_recover_password(self): + response = self.client.post((RECOVER_PASSWORD_LINK), {'email':'email@email.com'}) + self.assertEqual(response.status_code, 200) + self.assertEqual(len(mail.outbox), 1) + self.assertEqual(mail.outbox[0].to, ['email@email.com']) + + def test_sent_wrong_email_recover_password(self): + response = self.client.post((RECOVER_PASSWORD_LINK), {'email':'wrong@email.com'}) + self.assertEqual(response.status_code, 400) + + def test_change_password(self): + token = account_token.make_token(self.user) + response = self.client.put((RECOVER_PASSWORD_LINK), + {'email':'email@email.com', 'token': token, 'new_password':'newpass'}) + self.assertEqual(response.status_code, 200) + + def test_change_password_wrong_email(self): + token = account_token.make_token(self.user) + response = self.client.put((RECOVER_PASSWORD_LINK), + {'email':'wrong@email.com', 'token': token, 'new_password':'newpass'}) + self.assertEqual(response.status_code, 400) + + def test_change_password_wrong_token(self): + response = self.client.put((RECOVER_PASSWORD_LINK), + {'email':'email@email.com', 'token': 'wrong token', 'new_password':'newpass'}) + self.assertEqual(response.status_code, 400) + diff --git a/authentication/urls.py b/authentication/urls.py index 6732f1e..365690a 100644 --- a/authentication/urls.py +++ b/authentication/urls.py @@ -1,5 +1,5 @@ from django.urls import path -from authentication.views import RegisterView, LoginView, SendVerificationEmailView +from authentication.views import RegisterView, LoginView, SendVerificationEmailView, SendRecoverPasswordEmailView from rest_framework_simplejwt.views import TokenObtainPairView from rest_framework_simplejwt.views import TokenRefreshView @@ -10,5 +10,6 @@ path('login/', LoginView.as_view(), name='login'), path('token/', TokenObtainPairView.as_view(), name='token_obtain_pair'), path('token/refresh/', TokenRefreshView.as_view(), name='token_refresh'), - path('verify/', SendVerificationEmailView.as_view(), name='verify_email') + path('verify/', SendVerificationEmailView.as_view(), name='verify_email'), + path('recover/', SendRecoverPasswordEmailView.as_view(), name='recover_password'), ] \ No newline at end of file diff --git a/authentication/views.py b/authentication/views.py index fefe39e..5bde4bd 100644 --- a/authentication/views.py +++ b/authentication/views.py @@ -85,4 +85,13 @@ def post(self, request): user.save() return Response({'message': 'Email verified successfully!'}, status=200) else: - return Response({'error': 'Invalid verification token!'}, status=400) \ No newline at end of file + return Response({'error': 'Invalid verification token!'}, status=400) + +class SendRecoverPasswordEmailView(APIView): + permission_classes =() + + def post(self, request): + return 'None' + + def put(self, request): + return 'None'