Skip to content

Commit

Permalink
chore: testing
Browse files Browse the repository at this point in the history
  • Loading branch information
xyb committed Mar 8, 2023
1 parent a382aaa commit 58f91eb
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 2 deletions.
66 changes: 66 additions & 0 deletions drf_passwordless_jwt/tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import os
from unittest.mock import patch

from django.core import mail
from django.test import override_settings
from django.urls import reverse
from rest_framework import status
from rest_framework.test import APITestCase


class TaskTest(APITestCase):
@override_settings(EMAIL_BACKEND="django.core.mail.backends.locmem.EmailBackend")
def test_token_email(self):
response = self.client.post(
reverse("auth_email_token"),
{"email": "[email protected]"},
format="json",
)

self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(
response.json(),
{"detail": "A login token has been sent to your email."},
)
self.assertEqual(len(mail.outbox), 1)
msg = mail.outbox[0]
self.assertEqual(msg.subject, 'Your Login Token')
self.assertTrue(msg.body.startswith('Enter this token to sign in:'))
token = msg.body.split()[-1]
self.assertEqual(len(token), 6)
self.assertTrue(token.isdigit())
self.assertEqual(msg.from_email, '[email protected]')
self.assertEqual(msg.to, ['[email protected]'])

@patch.dict(os.environ, {"EMAIL_TEST_ACCOUNT_a_at_a_com": "123456"})
def test_auth_jwt_token(self):
response = self.client.post(
reverse("auth_jwt_token"),
{"email": "[email protected]", "token": "123456"},
format="json",
)

self.assertEqual(response.status_code, status.HTTP_200_OK)
json = response.json()
self.assertEqual(list(json.keys()), ['email', 'token'])
self.assertEqual(json['email'], '[email protected]')

@patch.dict(os.environ, {"EMAIL_TEST_ACCOUNT_a_at_a_com": "123456"})
def test_verify_jwt_token(self):
response = self.client.post(
reverse("auth_jwt_token"),
{"email": "[email protected]", "token": "123456"},
format="json",
)
token = response.json()['token']

response = self.client.post(
reverse("verify_jwt_token"),
{"token": token},
format="json",
)

self.assertEqual(response.status_code, status.HTTP_200_OK)
json = response.json()
self.assertEqual(list(json.keys()), ['email', 'exp'])
self.assertEqual(json['email'], '[email protected]')
4 changes: 2 additions & 2 deletions drf_passwordless_jwt/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ class VerifyJWTView(APIView):
serializer_class = JWTSerializer

def post(self, request, *args, **kwargs):
email = request.data['email']
if exists_test_account(email):
email = request.data.get('email')
if email and exists_test_account(email):
return Response({
'email': email,
'exp': '9999-12-31T23:59:59',
Expand Down

0 comments on commit 58f91eb

Please sign in to comment.