Skip to content

Commit

Permalink
chore: testing
Browse files Browse the repository at this point in the history
  • Loading branch information
xyb committed Mar 7, 2023
1 parent a382aaa commit 4f69000
Showing 1 changed file with 52 additions and 0 deletions.
52 changes: 52 additions & 0 deletions drf_passwordless_jwt/tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
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):
url = reverse("auth_email_token")
data = {"email": "[email protected]"}

response = self.client.post(url, data, 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]'])

@override_settings(EMAIL_BACKEND="django.core.mail.backends.locmem.EmailBackend")
@patch.dict(
os.environ,
{
"EMAIL_TEST_ACCOUNT_a_at_a_com": "123456",
}
)
def test_test_account(self):
url = reverse("auth_email_token")
data = {"email": "[email protected]"}

response = self.client.post(url, data, format="json")

self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(
response.json(),
{"detail": "test account email '[email protected]' available"},
)
self.assertEqual(len(mail.outbox), 0)

0 comments on commit 4f69000

Please sign in to comment.