Skip to content

Commit

Permalink
[RED] Create test and skeleton for SendRecoverPasswordEmail feature
Browse files Browse the repository at this point in the history
  • Loading branch information
JohannesSetiawan committed Feb 23, 2024
1 parent 902840c commit fa5c506
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 3 deletions.
34 changes: 34 additions & 0 deletions authentication/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):

Expand Down Expand Up @@ -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 protected]',username='testuser',password='test')

def test_sent_email_recover_password(self):
response = self.client.post((RECOVER_PASSWORD_LINK), {'email':'[email protected]'})
self.assertEqual(response.status_code, 200)
self.assertEqual(len(mail.outbox), 1)
self.assertEqual(mail.outbox[0].to, ['[email protected]'])

def test_sent_wrong_email_recover_password(self):
response = self.client.post((RECOVER_PASSWORD_LINK), {'email':'[email protected]'})
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 protected]', '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':'[email protected]', '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 protected]', 'token': 'wrong token', 'new_password':'newpass'})
self.assertEqual(response.status_code, 400)

5 changes: 3 additions & 2 deletions authentication/urls.py
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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'),
]
11 changes: 10 additions & 1 deletion authentication/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
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'

0 comments on commit fa5c506

Please sign in to comment.