new apps.lyric.authenticate model PasswordlessAuthenticationBackend and authenticate() function help determine Token authenticity, login to existing accounts, or create new ones; associated .tests.test_authentication class asserts this functionality; apps.lyric.models & .tests.models now handles token uid instead of id, completing the transition away from email-as-pk
This commit is contained in:
44
src/apps/lyric/tests/test_authentication.py
Normal file
44
src/apps/lyric/tests/test_authentication.py
Normal file
@@ -0,0 +1,44 @@
|
||||
import uuid
|
||||
from django.http import HttpRequest
|
||||
from django.test import TestCase
|
||||
|
||||
from ..authentication import PasswordlessAuthenticationBackend
|
||||
from ..models import Token, User
|
||||
|
||||
|
||||
class AuthenticateTest(TestCase):
|
||||
def test_returns_None_if_token_is_invalid_uuid(self):
|
||||
result = PasswordlessAuthenticationBackend().authenticate(
|
||||
HttpRequest(), "no-such-token"
|
||||
)
|
||||
self.assertIsNone(result)
|
||||
|
||||
def test_returns_None_if_token_uuid_not_found(self):
|
||||
uid = uuid.uuid4()
|
||||
result = PasswordlessAuthenticationBackend().authenticate(
|
||||
HttpRequest(), uid
|
||||
)
|
||||
self.assertIsNone(result)
|
||||
|
||||
def test_returns_new_user_with_correct_email_if_token_exists(self):
|
||||
email = "discoman@example.com"
|
||||
token = Token.objects.create(email=email)
|
||||
user = PasswordlessAuthenticationBackend().authenticate(
|
||||
HttpRequest(), token.uid
|
||||
)
|
||||
new_user = User.objects.get(email=email)
|
||||
self.assertEqual(user, new_user)
|
||||
|
||||
def test_returns_existing_user_with_correct_email_if_token_exists(self):
|
||||
email = "discoman@example.com"
|
||||
existing_user = User.objects.create(email=email)
|
||||
token = Token.objects.create(email=email)
|
||||
user = PasswordlessAuthenticationBackend().authenticate(
|
||||
HttpRequest(), token.uid
|
||||
)
|
||||
self.assertEqual(user, existing_user)
|
||||
|
||||
def test_can_retrieve_token_by_uuid(self):
|
||||
token = Token.objects.create(email="a@b.cde")
|
||||
fetched = Token.objects.get(pk=token.uid)
|
||||
self.assertEqual(fetched, token)
|
||||
@@ -1,3 +1,4 @@
|
||||
import uuid
|
||||
from django.contrib import auth
|
||||
from django.test import TestCase
|
||||
from ..models import Token, User
|
||||
@@ -16,6 +17,7 @@ class UserModelTest(TestCase):
|
||||
|
||||
class TokenModelTest(TestCase):
|
||||
def test_links_user_with_autogen_uid(self):
|
||||
token1 = Token.objects.create(id="123")
|
||||
token2 = Token.objects.create(id="124")
|
||||
self.assertNotEqual(token1.uid, token2.uid)
|
||||
token1 = Token.objects.create(email="a@b.cde")
|
||||
token2 = Token.objects.create(email="v@w.xyz")
|
||||
self.assertNotEqual(token1.pk, token2.pk)
|
||||
self.assertIsInstance(token1.pk, uuid.UUID)
|
||||
|
||||
Reference in New Issue
Block a user