new apps.lyric.urls, .tests.test_views files to test & execute mock email sending; apps.lyric.models, .tests.test_models now test & execute unique token autogen to integrate w. magic link login using only email; core.settings updated w. new urlpatterns to accommodate
This commit is contained in:
21
src/apps/lyric/migrations/0002_token.py
Normal file
21
src/apps/lyric/migrations/0002_token.py
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
# Generated by Django 6.0 on 2026-01-30 20:10
|
||||||
|
|
||||||
|
from django.db import migrations, models
|
||||||
|
|
||||||
|
|
||||||
|
class Migration(migrations.Migration):
|
||||||
|
|
||||||
|
dependencies = [
|
||||||
|
('lyric', '0001_initial'),
|
||||||
|
]
|
||||||
|
|
||||||
|
operations = [
|
||||||
|
migrations.CreateModel(
|
||||||
|
name='Token',
|
||||||
|
fields=[
|
||||||
|
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
|
||||||
|
('email', models.EmailField(max_length=254)),
|
||||||
|
('uid', models.UUIDField()),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
]
|
||||||
@@ -1,6 +1,12 @@
|
|||||||
|
import uuid
|
||||||
from django.db import models
|
from django.db import models
|
||||||
|
|
||||||
|
class Token(models.Model):
|
||||||
|
email = models.EmailField()
|
||||||
|
uid = models.UUIDField(default=uuid.uuid4)
|
||||||
|
|
||||||
class User(models.Model):
|
class User(models.Model):
|
||||||
|
id = models.BigAutoField(primary_key=True)
|
||||||
# email = models.EmailField(primary_key=True)
|
# email = models.EmailField(primary_key=True)
|
||||||
email = models.EmailField(unique=True)
|
email = models.EmailField(unique=True)
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
from django.contrib import auth
|
from django.contrib import auth
|
||||||
from django.test import TestCase
|
from django.test import TestCase
|
||||||
from ..models import User
|
from ..models import Token, User
|
||||||
|
|
||||||
class UserModelTest(TestCase):
|
class UserModelTest(TestCase):
|
||||||
def test_model_is_configured_for_django_auth(self):
|
def test_model_is_configured_for_django_auth(self):
|
||||||
@@ -13,3 +13,9 @@ class UserModelTest(TestCase):
|
|||||||
def test_id_is_primary_key(self):
|
def test_id_is_primary_key(self):
|
||||||
user = User(id="123")
|
user = User(id="123")
|
||||||
self.assertEqual(user.pk, "123")
|
self.assertEqual(user.pk, "123")
|
||||||
|
|
||||||
|
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)
|
||||||
|
|||||||
30
src/apps/lyric/tests/test_views.py
Normal file
30
src/apps/lyric/tests/test_views.py
Normal file
@@ -0,0 +1,30 @@
|
|||||||
|
from django.test import TestCase
|
||||||
|
from .. import views as lyric_views
|
||||||
|
|
||||||
|
class SendLoginEmailViewTest(TestCase):
|
||||||
|
def test_redirects_to_home_page(self):
|
||||||
|
response = self.client.post(
|
||||||
|
"/apps/lyric/send_login_email", data={"email": "disco@example.com"}
|
||||||
|
)
|
||||||
|
self.assertRedirects(response, "/")
|
||||||
|
|
||||||
|
def test_sends_mail_to_address_from_post(self):
|
||||||
|
self.send_mail_called = False
|
||||||
|
|
||||||
|
def fake_send_mail(subject, body, from_email, to_list):
|
||||||
|
self.send_mail_called = True
|
||||||
|
self.subject = subject
|
||||||
|
self.body = body
|
||||||
|
self.from_email = from_email
|
||||||
|
self.to_list = to_list
|
||||||
|
|
||||||
|
lyric_views.send_mail = fake_send_mail
|
||||||
|
|
||||||
|
self.client.post(
|
||||||
|
"/apps/lyric/send_login_email", data={"email": "disco@example.com"}
|
||||||
|
)
|
||||||
|
|
||||||
|
self.assertTrue(self.send_mail_called)
|
||||||
|
self.assertEqual(self.subject, "A magic login link for your Dashboard")
|
||||||
|
self.assertEqual(self.from_email, "adman@howdy.earthmanrpg.me")
|
||||||
|
self.assertEqual(self.to_list, ["disco@example.com"])
|
||||||
7
src/apps/lyric/urls.py
Normal file
7
src/apps/lyric/urls.py
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
from django.urls import include, path
|
||||||
|
from . import views
|
||||||
|
|
||||||
|
urlpatterns = [
|
||||||
|
path('send_login_email', views.send_login_email, name='send_login_email'),
|
||||||
|
]
|
||||||
|
|
||||||
@@ -1,3 +1,7 @@
|
|||||||
from django.shortcuts import render
|
from django.core.mail import send_mail
|
||||||
|
from django.shortcuts import redirect
|
||||||
|
|
||||||
# Create your views here.
|
def send_login_email(request):
|
||||||
|
email = request.POST["email"]
|
||||||
|
send_mail("A magic login link for your Dashboard", "magic link sample body", "adman@howdy.earthmanrpg.me", [email])
|
||||||
|
return redirect("/")
|
||||||
|
|||||||
@@ -1,9 +1,10 @@
|
|||||||
# from django.contrib import admin
|
# from django.contrib import admin
|
||||||
from django.urls import include, path
|
from django.urls import include, path
|
||||||
from apps.dashboard import views as list_views
|
from apps.dashboard import views as dash_views
|
||||||
|
|
||||||
urlpatterns = [
|
urlpatterns = [
|
||||||
# path('admin/', admin.site.urls),
|
# path('admin/', admin.site.urls),
|
||||||
path('', list_views.home_page, name='home'),
|
path('', dash_views.home_page, name='home'),
|
||||||
path('apps/dashboard/', include('apps.dashboard.urls')),
|
path('apps/dashboard/', include('apps.dashboard.urls')),
|
||||||
|
path('apps/lyric/', include('apps.lyric.urls')),
|
||||||
]
|
]
|
||||||
|
|||||||
Reference in New Issue
Block a user