Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: testing #8

Merged
merged 1 commit into from
Mar 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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