Compare commits
9 Commits
64d4ba9542
...
pre-drf
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
4b558020af | ||
|
|
5106b04175 | ||
|
|
025a59938b | ||
|
|
d26196a7f1 | ||
|
|
84fd0554bd | ||
|
|
55f2a043c6 | ||
|
|
a1e7ae8071 | ||
|
|
a06fce26ef | ||
|
|
c41624152a |
@@ -1,6 +1,16 @@
|
|||||||
|
services:
|
||||||
|
- name: postgres
|
||||||
|
image: postgres:16
|
||||||
|
environment:
|
||||||
|
POSTGRES_DB: python_tdd_test
|
||||||
|
POSTGRES_USER: postgres
|
||||||
|
POSTGRES_PASSWORD: postgres
|
||||||
|
|
||||||
steps:
|
steps:
|
||||||
- name: test-UTs
|
- name: test-UTs-n-ITs
|
||||||
image: python:3.13-slim
|
image: python:3.13-slim
|
||||||
|
environment:
|
||||||
|
DATABASE_URL: postgresql://postgres:postgres@postgres/python_tdd_test
|
||||||
commands:
|
commands:
|
||||||
- pip install -r requirements.txt
|
- pip install -r requirements.txt
|
||||||
- cd ./src
|
- cd ./src
|
||||||
|
|||||||
@@ -50,37 +50,6 @@
|
|||||||
- name: Reset ssh connection to allow the user/group change to take effect
|
- name: Reset ssh connection to allow the user/group change to take effect
|
||||||
ansible.builtin.meta: reset_connection
|
ansible.builtin.meta: reset_connection
|
||||||
|
|
||||||
- name: Build container image locally
|
|
||||||
community.docker.docker_image:
|
|
||||||
name: gamearray
|
|
||||||
source: build
|
|
||||||
state: present
|
|
||||||
build:
|
|
||||||
path: /mnt/d/cosmovault/latticework/oreilly/percival/python-tdd
|
|
||||||
platform: linux/amd64
|
|
||||||
force_source: true
|
|
||||||
delegate_to: 127.0.0.1
|
|
||||||
|
|
||||||
- name: Export container image locally
|
|
||||||
community.docker.docker_image:
|
|
||||||
name: gamearray
|
|
||||||
archive_path: /tmp/gamearray-img.tar
|
|
||||||
source: local
|
|
||||||
delegate_to: 127.0.0.1
|
|
||||||
|
|
||||||
- name: Upload image to server
|
|
||||||
ansible.builtin.copy:
|
|
||||||
src: /tmp/gamearray-img.tar
|
|
||||||
dest: /tmp/gamearray-img.tar
|
|
||||||
|
|
||||||
- name: Import container image on server
|
|
||||||
community.docker.docker_image:
|
|
||||||
name: gamearray
|
|
||||||
load_path: /tmp/gamearray-img.tar
|
|
||||||
source: load
|
|
||||||
force_source: true
|
|
||||||
state: present
|
|
||||||
|
|
||||||
- name: Ensure .secret-key files exists
|
- name: Ensure .secret-key files exists
|
||||||
# the intention is that this only happens once per server
|
# the intention is that this only happens once per server
|
||||||
ansible.builtin.copy:
|
ansible.builtin.copy:
|
||||||
@@ -120,27 +89,45 @@
|
|||||||
cmd: docker login gitea.earthmanrpg.me -u discoman -p {{ gitea_registry_token }}
|
cmd: docker login gitea.earthmanrpg.me -u discoman -p {{ gitea_registry_token }}
|
||||||
no_log: true
|
no_log: true
|
||||||
|
|
||||||
- name: Ensure db.sqlite3 file exists outside container
|
- name: Create Docker network
|
||||||
ansible.builtin.file:
|
community.docker.docker_network:
|
||||||
path: "{{ ansible_env.HOME }}/db.sqlite3"
|
name: gamearray_net
|
||||||
state: touch
|
state: present
|
||||||
owner: 1234 # so nonroot user can access it in container
|
|
||||||
become: true # needed for ownership change
|
- name: Create Postgres data volume
|
||||||
|
community.docker.docker_volume:
|
||||||
|
name: gamearray_postgres_data
|
||||||
|
state: present
|
||||||
|
|
||||||
|
- name: Start Postgres container
|
||||||
|
community.docker.docker_container:
|
||||||
|
name: gamearray_postgres
|
||||||
|
image: postgres:16
|
||||||
|
state: started
|
||||||
|
restart_policy: unless-stopped
|
||||||
|
networks:
|
||||||
|
- name: gamearray_net
|
||||||
|
volumes:
|
||||||
|
- gamearray_postgres_data:/var/lib/postgresql/data
|
||||||
|
env:
|
||||||
|
POSTGRES_DB: gamearray
|
||||||
|
POSTGRES_USER: gamearray
|
||||||
|
POSTGRES_PASSWORD: "{{ postgres_password }}"
|
||||||
|
|
||||||
- name: Run container
|
- name: Run container
|
||||||
community.docker.docker_container:
|
community.docker.docker_container:
|
||||||
name: gamearray
|
name: gamearray
|
||||||
image: gamearray
|
image: gitea.earthmanrpg.me/discoman/gamearray:latest
|
||||||
state: started
|
state: started
|
||||||
recreate: true
|
recreate: true
|
||||||
env:
|
env:
|
||||||
DJANGO_DEBUG_FALSE: "1"
|
DJANGO_DEBUG_FALSE: "1"
|
||||||
DJANGO_SECRET_KEY: "{{ secret_key.content | b64decode }}"
|
DJANGO_SECRET_KEY: "{{ secret_key.content | b64decode }}"
|
||||||
DJANGO_ALLOWED_HOST: "{{ django_allowed_host }}"
|
DJANGO_ALLOWED_HOST: "{{ django_allowed_host }}"
|
||||||
DJANGO_DB_PATH: "/home/nonroot/db.sqlite3"
|
DATABASE_URL: "postgresql://gamearray:{{ postgres_password }}@gamearray_postgres/gamearray"
|
||||||
EMAIL_HOST_USER: "{{ email_host_user }}"
|
|
||||||
EMAIL_HOST_PASSWORD: "{{ email_host_password }}"
|
|
||||||
MAILGUN_API_KEY: "{{ mailgun_api_key }}"
|
MAILGUN_API_KEY: "{{ mailgun_api_key }}"
|
||||||
|
networks:
|
||||||
|
- name: gamearray_net
|
||||||
ports:
|
ports:
|
||||||
127.0.0.1:8888:8888
|
127.0.0.1:8888:8888
|
||||||
|
|
||||||
@@ -160,7 +147,7 @@
|
|||||||
- name: Run migration inside container
|
- name: Run migration inside container
|
||||||
community.docker.docker_container_exec:
|
community.docker.docker_container_exec:
|
||||||
container: gamearray
|
container: gamearray
|
||||||
command: ./manage.py migrate
|
command: python manage.py migrate
|
||||||
|
|
||||||
handlers:
|
handlers:
|
||||||
- name: Restart nginx
|
- name: Restart nginx
|
||||||
|
|||||||
@@ -13,6 +13,7 @@ docker rm gamearray 2>/dev/null || true
|
|||||||
echo "==> Starting new container..."
|
echo "==> Starting new container..."
|
||||||
docker run -d --name gamearray \
|
docker run -d --name gamearray \
|
||||||
--env-file /opt/gamearray/gamearray.env \
|
--env-file /opt/gamearray/gamearray.env \
|
||||||
|
--network gamearray_net \
|
||||||
-p 127.0.0.1:8888:8888 \
|
-p 127.0.0.1:8888:8888 \
|
||||||
"$IMAGE"
|
"$IMAGE"
|
||||||
|
|
||||||
|
|||||||
@@ -1,7 +1,5 @@
|
|||||||
DJANGO_DEBUG_FALSE=1
|
DJANGO_DEBUG_FALSE=1
|
||||||
DJANGO_SECRET_KEY={{ secret_key.content | b64decode }}
|
DJANGO_SECRET_KEY={{ secret_key.content | b64decode }}
|
||||||
DJANGO_ALLOWED_HOST={{ django_allowed_host }}
|
DJANGO_ALLOWED_HOST={{ django_allowed_host }}
|
||||||
DJANGO_DB_PATH=/home/nonroot/db.sqlite3
|
DATABASE_URL=postgresql://gamearray:{{ postgres_password }}@gamearray_postgres/gamearray
|
||||||
EMAIL_HOST_USER={{ email_host_user }}
|
MAILGUN_API_KEY={{ mailgun_api_key }}
|
||||||
EMAIL_HOST_PASSWORD={{ email_host_password }}
|
|
||||||
MAILGUN_API_KEY={{ mailgun_api_key }}
|
|
||||||
|
|||||||
@@ -1,28 +1,23 @@
|
|||||||
$ANSIBLE_VAULT;1.1;AES256
|
$ANSIBLE_VAULT;1.1;AES256
|
||||||
36653566363731653435616430626663303038623766663561363231333163336165623863613964
|
33616230376431343735626631623932393166343538653732383533323436326335343463646664
|
||||||
6164383861643530366438623465613565373032396331350a666163636431636663353162383531
|
6565373531623465613661613533376231373837326438300a393665613839646231633737313938
|
||||||
34306534656430653533303530613764336438616536343534663236333665323837636337333334
|
64633035336663313163333634623732323537326363646132313136376131636666636538323066
|
||||||
3432643436636265610a313465396435616263386631353336326464333930613865313934313032
|
3037373930303537320a313062646166353862633836373466316261363939633433663039323866
|
||||||
38353362623937643234333466323063336535623666613366633263623034616638653566666463
|
62333739303662343836306538393734343830366336323265393138343438363533353166383031
|
||||||
66323032653034376663623933306162313832643038653764643864666433376236643163663637
|
32313461313137643039376237346633316466646136353038633861333031663164656233366634
|
||||||
63626334393963343934666665373764393066383866616461333063633664363436613031663036
|
38303363383130376264373861393863623330623733643135643461383132613339376633353031
|
||||||
61343939343633393138666637646137376537393335663032383839306365613764303833323338
|
32313863323039646534633733383661333361313832333830383066633130396239626661643264
|
||||||
33343936333730373362393466373238636666343762373134633962383237623335373634656330
|
65636335303339613432326533343337366261356632313639623634386633383836333733663536
|
||||||
37363039393261313034306166656563333461353034646234323462623631393338383461363961
|
39383361353530646166643531333535356636326535383534326237666638326137616162646261
|
||||||
33356564633637333630663464613265666264393435363238383530333861636365616362316130
|
65316466323335653932636338653565383038313531383638393839313736643739363037353230
|
||||||
38353464343064616463636535316339336430323866303161393065363830356431386430666534
|
35653632353531656435396663316537333133653632366437613339303033333536643937353166
|
||||||
61353961666333313536616661636631643630373337633262653662393863336264636431366634
|
64363037653733303332643931343362303261643432366531326262383465313965633064356338
|
||||||
32323533383963393435343935616135663262633634356631363632396233383839326365396333
|
31336333373665373035656533633864316139303934623030383934393434356334643962666163
|
||||||
64333232626465643438313132323661386235313063303036303631376537353666313532323766
|
33343739366336613263333764306365333566363536616662383733616237396563346132336633
|
||||||
63633834336631633364333334373461333836666630353363343365323033653234356536643939
|
38663239613339376335386233386330396634323033343332366130616162666339393861306336
|
||||||
30316538663230653636316532393931333936613733336366326239633362353666636436636136
|
35383566383831356530633130313732356331616164646132626665646235396635386237313538
|
||||||
64656134663733376630316536616138613234383838316138616433353531396363316462626133
|
38656631336261646530303761643334303937613036363766303637376262373466316431323731
|
||||||
62383431396465333634623066333565643332613935653532613536646632346533383362393330
|
38666462313639353131303134646434646135366136343361353932326165626666306361393431
|
||||||
38646562393762346434663666313431363037636463306435663263386336343461303839346365
|
62646238323265346263386363373462313766616333326366366461346436383064336535376339
|
||||||
63326432643662353830383736613636643866363765366132653563363036316265646531623433
|
31356566356336386262393831616631666233633930393263623563386265343237323133313832
|
||||||
30303131323165653564333331353233373731333539346163613564343331373931633365633631
|
3430363635363332303963316530663765613666306233376463
|
||||||
66396332653436376430626564316639623362383635633134343234626462333162336464656438
|
|
||||||
65393062333631373836303662326436333265373033353339356334633666363065636164343239
|
|
||||||
64623535663633653130643764656539643339633061646437643366376261383137613439323934
|
|
||||||
37343338336130313339356531333038613334393736353365366662313262653737623533616366
|
|
||||||
346430623266646464353639386266313339
|
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
server {
|
server {
|
||||||
listen 80;
|
listen 80;
|
||||||
server_name {{ django_allowed_host }};
|
server_name {{ django_allowed_host | replace(',', ' ')}};
|
||||||
|
|
||||||
location /static/ {
|
location /static/ {
|
||||||
alias /var/www/gamearray/static/;
|
alias /var/www/gamearray/static/;
|
||||||
|
|||||||
@@ -3,7 +3,9 @@ attrs==25.4.0
|
|||||||
certifi==2025.11.12
|
certifi==2025.11.12
|
||||||
cffi==2.0.0
|
cffi==2.0.0
|
||||||
charset-normalizer==3.4.4
|
charset-normalizer==3.4.4
|
||||||
|
coverage
|
||||||
cssselect==1.3.0
|
cssselect==1.3.0
|
||||||
|
dj-database-url
|
||||||
Django==6.0
|
Django==6.0
|
||||||
django-stubs==5.2.8
|
django-stubs==5.2.8
|
||||||
django-stubs-ext==5.2.8
|
django-stubs-ext==5.2.8
|
||||||
|
|||||||
@@ -1,8 +1,10 @@
|
|||||||
cssselect==1.3.0
|
cssselect==1.3.0
|
||||||
Django==6.0
|
Django==6.0
|
||||||
|
dj-database-url
|
||||||
django-stubs==5.2.8
|
django-stubs==5.2.8
|
||||||
django-stubs-ext==5.2.8
|
django-stubs-ext==5.2.8
|
||||||
gunicorn==23.0.0
|
gunicorn==23.0.0
|
||||||
lxml==6.0.2
|
lxml==6.0.2
|
||||||
|
psycopg2-binary
|
||||||
requests==2.31.0
|
requests==2.31.0
|
||||||
whitenoise==6.11.0
|
whitenoise==6.11.0
|
||||||
|
|||||||
8
src/.coveragerc
Normal file
8
src/.coveragerc
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
[run]
|
||||||
|
source = apps
|
||||||
|
omit =
|
||||||
|
*/migrations/*
|
||||||
|
*/tests/*
|
||||||
|
|
||||||
|
[report]
|
||||||
|
show_missing = true
|
||||||
0
src/apps/dashboard/tests/integrated/__init__.py
Normal file
0
src/apps/dashboard/tests/integrated/__init__.py
Normal file
@@ -1,18 +1,15 @@
|
|||||||
from django.test import TestCase
|
from django.test import TestCase
|
||||||
from ..forms import (
|
|
||||||
|
from apps.dashboard.forms import (
|
||||||
DUPLICATE_ITEM_ERROR,
|
DUPLICATE_ITEM_ERROR,
|
||||||
EMPTY_ITEM_ERROR,
|
EMPTY_ITEM_ERROR,
|
||||||
ExistingListItemForm,
|
ExistingListItemForm,
|
||||||
ItemForm,
|
ItemForm,
|
||||||
)
|
)
|
||||||
from ..models import Item, List
|
from apps.dashboard.models import Item, List
|
||||||
|
|
||||||
|
|
||||||
class ItemFormTest(TestCase):
|
class ItemFormTest(TestCase):
|
||||||
def test_form_validation_for_blank_items(self):
|
|
||||||
form = ItemForm(data={"text": ""})
|
|
||||||
self.assertFalse(form.is_valid())
|
|
||||||
self.assertEqual(form.errors["text"], [EMPTY_ITEM_ERROR])
|
|
||||||
|
|
||||||
def test_form_save_handles_saving_to_a_list(self):
|
def test_form_save_handles_saving_to_a_list(self):
|
||||||
mylist = List.objects.create()
|
mylist = List.objects.create()
|
||||||
form = ItemForm(data={"text": "do re mi"})
|
form = ItemForm(data={"text": "do re mi"})
|
||||||
@@ -1,14 +1,12 @@
|
|||||||
from django.core.exceptions import ValidationError
|
from django.core.exceptions import ValidationError
|
||||||
from django.db.utils import IntegrityError
|
from django.db.utils import IntegrityError
|
||||||
from django.test import TestCase
|
from django.test import TestCase
|
||||||
from ..models import Item, List
|
|
||||||
|
from apps.dashboard.models import Item, List
|
||||||
from apps.lyric.models import User
|
from apps.lyric.models import User
|
||||||
|
|
||||||
|
|
||||||
class ItemModelTest(TestCase):
|
class ItemModelTest(TestCase):
|
||||||
def test_default_text(self):
|
|
||||||
item = Item()
|
|
||||||
self.assertEqual(item.text, "")
|
|
||||||
|
|
||||||
def test_item_is_related_to_list(self):
|
def test_item_is_related_to_list(self):
|
||||||
mylist = List.objects.create()
|
mylist = List.objects.create()
|
||||||
item = Item()
|
item = Item()
|
||||||
@@ -42,10 +40,6 @@ class ItemModelTest(TestCase):
|
|||||||
item = Item(list=list2, text="nojk")
|
item = Item(list=list2, text="nojk")
|
||||||
item.full_clean() # should not raise
|
item.full_clean() # should not raise
|
||||||
|
|
||||||
def test_string_representation(self):
|
|
||||||
item = Item(text="sample text")
|
|
||||||
self.assertEqual(str(item), "sample text")
|
|
||||||
|
|
||||||
class ListModelTest(TestCase):
|
class ListModelTest(TestCase):
|
||||||
def test_get_absolute_url(self):
|
def test_get_absolute_url(self):
|
||||||
mylist = List.objects.create()
|
mylist = List.objects.create()
|
||||||
@@ -1,13 +1,14 @@
|
|||||||
import lxml.html
|
import lxml.html
|
||||||
from unittest import skip
|
from unittest import skip
|
||||||
|
|
||||||
from django.test import TestCase
|
from django.test import TestCase
|
||||||
from django.utils import html
|
from django.utils import html
|
||||||
|
|
||||||
from ..forms import (
|
from apps.dashboard.forms import (
|
||||||
DUPLICATE_ITEM_ERROR,
|
DUPLICATE_ITEM_ERROR,
|
||||||
EMPTY_ITEM_ERROR,
|
EMPTY_ITEM_ERROR,
|
||||||
)
|
)
|
||||||
from ..models import Item, List
|
from apps.dashboard.models import Item, List
|
||||||
from apps.lyric.models import User
|
from apps.lyric.models import User
|
||||||
|
|
||||||
|
|
||||||
0
src/apps/dashboard/tests/unit/__init__.py
Normal file
0
src/apps/dashboard/tests/unit/__init__.py
Normal file
13
src/apps/dashboard/tests/unit/test_forms.py
Normal file
13
src/apps/dashboard/tests/unit/test_forms.py
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
from django.test import SimpleTestCase
|
||||||
|
|
||||||
|
from apps.dashboard.forms import (
|
||||||
|
EMPTY_ITEM_ERROR,
|
||||||
|
ItemForm,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
class SimpleItemFormTest(SimpleTestCase):
|
||||||
|
def test_form_validation_for_blank_items(self):
|
||||||
|
form = ItemForm(data={"text": ""})
|
||||||
|
self.assertFalse(form.is_valid())
|
||||||
|
self.assertEqual(form.errors["text"], [EMPTY_ITEM_ERROR])
|
||||||
13
src/apps/dashboard/tests/unit/test_models.py
Normal file
13
src/apps/dashboard/tests/unit/test_models.py
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
from django.test import SimpleTestCase
|
||||||
|
|
||||||
|
from apps.dashboard.models import Item
|
||||||
|
|
||||||
|
|
||||||
|
class SimpleItemModelTest(SimpleTestCase):
|
||||||
|
def test_default_text(self):
|
||||||
|
item = Item()
|
||||||
|
self.assertEqual(item.text, "")
|
||||||
|
|
||||||
|
def test_string_representation(self):
|
||||||
|
item = Item(text="sample text")
|
||||||
|
self.assertEqual(str(item), "sample text")
|
||||||
@@ -1,3 +1,6 @@
|
|||||||
from django.contrib import admin
|
from django.contrib import admin
|
||||||
|
from .models import Token, User
|
||||||
|
|
||||||
# Register your models here.
|
|
||||||
|
admin.site.register(User)
|
||||||
|
admin.site.register(Token)
|
||||||
|
|||||||
@@ -13,7 +13,7 @@ class PasswordlessAuthenticationBackend:
|
|||||||
try:
|
try:
|
||||||
return User.objects.get(email=token.email)
|
return User.objects.get(email=token.email)
|
||||||
except User.DoesNotExist:
|
except User.DoesNotExist:
|
||||||
return User.objects.create(email=token.email)
|
return User.objects.create_user(email=token.email)
|
||||||
|
|
||||||
def get_user(self, user_id):
|
def get_user(self, user_id):
|
||||||
try:
|
try:
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
# Generated by Django 6.0 on 2026-02-08 01:19
|
# Generated by Django 6.0 on 2026-02-20 00:48
|
||||||
|
|
||||||
import uuid
|
import uuid
|
||||||
from django.db import migrations, models
|
from django.db import migrations, models
|
||||||
@@ -15,9 +15,16 @@ class Migration(migrations.Migration):
|
|||||||
migrations.CreateModel(
|
migrations.CreateModel(
|
||||||
name='User',
|
name='User',
|
||||||
fields=[
|
fields=[
|
||||||
|
('password', models.CharField(max_length=128, verbose_name='password')),
|
||||||
|
('last_login', models.DateTimeField(blank=True, null=True, verbose_name='last login')),
|
||||||
('id', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)),
|
('id', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)),
|
||||||
('email', models.EmailField(max_length=254, unique=True)),
|
('email', models.EmailField(max_length=254, unique=True)),
|
||||||
|
('is_staff', models.BooleanField(default=False)),
|
||||||
|
('is_superuser', models.BooleanField(default=False)),
|
||||||
],
|
],
|
||||||
|
options={
|
||||||
|
'abstract': False,
|
||||||
|
},
|
||||||
),
|
),
|
||||||
migrations.CreateModel(
|
migrations.CreateModel(
|
||||||
name='Token',
|
name='Token',
|
||||||
|
|||||||
@@ -1,16 +1,38 @@
|
|||||||
import uuid
|
import uuid
|
||||||
|
|
||||||
|
from django.contrib.auth.base_user import AbstractBaseUser, BaseUserManager
|
||||||
from django.db import models
|
from django.db import models
|
||||||
|
|
||||||
|
|
||||||
|
class UserManager(BaseUserManager):
|
||||||
|
def create_user(self, email):
|
||||||
|
user = self.model(email=email)
|
||||||
|
user.set_unusable_password()
|
||||||
|
user.save(using=self._db)
|
||||||
|
return user
|
||||||
|
|
||||||
|
def create_superuser(self, email, password):
|
||||||
|
user = self.model(email=email, is_staff=True, is_superuser=True)
|
||||||
|
user.set_password(password)
|
||||||
|
user.save(using=self._db)
|
||||||
|
return user
|
||||||
|
|
||||||
class Token(models.Model):
|
class Token(models.Model):
|
||||||
email = models.EmailField()
|
email = models.EmailField()
|
||||||
uid = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
|
uid = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
|
||||||
|
|
||||||
class User(models.Model):
|
class User(AbstractBaseUser):
|
||||||
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
|
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
|
||||||
email = models.EmailField(unique=True)
|
email = models.EmailField(unique=True)
|
||||||
|
is_staff = models.BooleanField(default=False)
|
||||||
|
is_superuser = models.BooleanField(default=False)
|
||||||
|
|
||||||
|
objects = UserManager()
|
||||||
REQUIRED_FIELDS = []
|
REQUIRED_FIELDS = []
|
||||||
USERNAME_FIELD = "email"
|
USERNAME_FIELD = "email"
|
||||||
|
|
||||||
|
def has_perm(self, perm, obj=None):
|
||||||
|
return self.is_superuser
|
||||||
|
|
||||||
is_authenticated = True
|
def has_module_perms(self, app_label):
|
||||||
is_anonymous =False
|
return self.is_superuser
|
||||||
|
|||||||
0
src/apps/lyric/tests/integrated/__init__.py
Normal file
0
src/apps/lyric/tests/integrated/__init__.py
Normal file
@@ -2,17 +2,11 @@ import uuid
|
|||||||
from django.http import HttpRequest
|
from django.http import HttpRequest
|
||||||
from django.test import TestCase
|
from django.test import TestCase
|
||||||
|
|
||||||
from ..authentication import PasswordlessAuthenticationBackend
|
from apps.lyric.authentication import PasswordlessAuthenticationBackend
|
||||||
from ..models import Token, User
|
from apps.lyric.models import Token, User
|
||||||
|
|
||||||
|
|
||||||
class AuthenticateTest(TestCase):
|
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):
|
def test_returns_None_if_token_uuid_not_found(self):
|
||||||
uid = uuid.uuid4()
|
uid = uuid.uuid4()
|
||||||
result = PasswordlessAuthenticationBackend().authenticate(
|
result = PasswordlessAuthenticationBackend().authenticate(
|
||||||
@@ -1,7 +1,9 @@
|
|||||||
import uuid
|
import uuid
|
||||||
from django.contrib import auth
|
from django.contrib import auth
|
||||||
from django.test import TestCase
|
from django.test import TestCase
|
||||||
from ..models import Token, User
|
|
||||||
|
from apps.lyric.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):
|
||||||
@@ -9,6 +11,7 @@ class UserModelTest(TestCase):
|
|||||||
|
|
||||||
def test_user_is_valid_with_email_only(self):
|
def test_user_is_valid_with_email_only(self):
|
||||||
user = User(email="a@b.cde")
|
user = User(email="a@b.cde")
|
||||||
|
user.set_unusable_password()
|
||||||
user.full_clean() # should not raise
|
user.full_clean() # should not raise
|
||||||
|
|
||||||
def test_id_is_primary_key(self):
|
def test_id_is_primary_key(self):
|
||||||
@@ -21,3 +24,19 @@ class TokenModelTest(TestCase):
|
|||||||
token2 = Token.objects.create(email="v@w.xyz")
|
token2 = Token.objects.create(email="v@w.xyz")
|
||||||
self.assertNotEqual(token1.pk, token2.pk)
|
self.assertNotEqual(token1.pk, token2.pk)
|
||||||
self.assertIsInstance(token1.pk, uuid.UUID)
|
self.assertIsInstance(token1.pk, uuid.UUID)
|
||||||
|
|
||||||
|
class UserManagerTest(TestCase):
|
||||||
|
def test_create_superuser_sets_is_staff_and_is_superuser(self):
|
||||||
|
user = User.objects.create_superuser(
|
||||||
|
email="admin@example.com",
|
||||||
|
password="correct-password",
|
||||||
|
)
|
||||||
|
self.assertTrue(user.is_staff)
|
||||||
|
self.assertTrue(user.is_superuser)
|
||||||
|
|
||||||
|
def test_create_superuser_sets_usable_password(self):
|
||||||
|
user = User.objects.create_superuser(
|
||||||
|
email="admin@example.com",
|
||||||
|
password="correct-password",
|
||||||
|
)
|
||||||
|
self.assertTrue(user.check_password("correct-password"))
|
||||||
@@ -1,7 +1,10 @@
|
|||||||
from django.contrib import auth
|
from django.contrib import auth
|
||||||
from django.test import TestCase
|
from django.test import TestCase
|
||||||
from unittest import mock
|
from unittest import mock
|
||||||
from ..models import Token
|
|
||||||
|
from apps.lyric.models import Token
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
class SendLoginEmailViewTest(TestCase):
|
class SendLoginEmailViewTest(TestCase):
|
||||||
def test_redirects_to_home_page(self):
|
def test_redirects_to_home_page(self):
|
||||||
@@ -19,7 +22,7 @@ class SendLoginEmailViewTest(TestCase):
|
|||||||
self.assertEqual(mock_post.called, True)
|
self.assertEqual(mock_post.called, True)
|
||||||
data = mock_post.call_args.kwargs["data"]
|
data = mock_post.call_args.kwargs["data"]
|
||||||
self.assertEqual(data["subject"], "A magic login link to your Dashboard")
|
self.assertEqual(data["subject"], "A magic login link to your Dashboard")
|
||||||
self.assertEqual(data["from"], "adman@howdy.earthmanrpg.me")
|
self.assertEqual(data["from"], "adman@howdy.earthmanrpg.com")
|
||||||
self.assertEqual(data["to"], "discoman@example.com")
|
self.assertEqual(data["to"], "discoman@example.com")
|
||||||
|
|
||||||
def test_adds_success_message(self):
|
def test_adds_success_message(self):
|
||||||
0
src/apps/lyric/tests/unit/__init__.py
Normal file
0
src/apps/lyric/tests/unit/__init__.py
Normal file
31
src/apps/lyric/tests/unit/test_authentication.py
Normal file
31
src/apps/lyric/tests/unit/test_authentication.py
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
from django.http import HttpRequest
|
||||||
|
from django.test import SimpleTestCase
|
||||||
|
|
||||||
|
from apps.lyric.authentication import PasswordlessAuthenticationBackend
|
||||||
|
from apps.lyric.models import User
|
||||||
|
|
||||||
|
|
||||||
|
class SimpleAuthenticateTest(SimpleTestCase):
|
||||||
|
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_no_uuid(self):
|
||||||
|
result = PasswordlessAuthenticationBackend().authenticate(HttpRequest())
|
||||||
|
self.assertIsNone(result)
|
||||||
|
|
||||||
|
class UserPermissionsTest(SimpleTestCase):
|
||||||
|
def test_superuser_has_perm(self):
|
||||||
|
user = User(is_superuser=True)
|
||||||
|
self.assertTrue(user.has_perm("any.permission"))
|
||||||
|
|
||||||
|
def test_superuser_has_module_perms(self):
|
||||||
|
user = User(is_superuser=True)
|
||||||
|
self.assertTrue(user.has_module_perms("any_app"))
|
||||||
|
|
||||||
|
def test_non_superuser_has_no_perm(self):
|
||||||
|
user = User(is_superuser=False)
|
||||||
|
self.assertFalse(user.has_perm("any.permission"))
|
||||||
|
|
||||||
@@ -18,7 +18,7 @@ def send_login_email(request):
|
|||||||
f"https://api.mailgun.net/v3/{settings.MAILGUN_DOMAIN}/messages",
|
f"https://api.mailgun.net/v3/{settings.MAILGUN_DOMAIN}/messages",
|
||||||
auth=("api", settings.MAILGUN_API_KEY),
|
auth=("api", settings.MAILGUN_API_KEY),
|
||||||
data={
|
data={
|
||||||
"from": "adman@howdy.earthmanrpg.me",
|
"from": "adman@howdy.earthmanrpg.com",
|
||||||
"to": email,
|
"to": email,
|
||||||
"subject": "A magic login link to your Dashboard",
|
"subject": "A magic login link to your Dashboard",
|
||||||
"text": message_body,
|
"text": message_body,
|
||||||
|
|||||||
@@ -12,6 +12,7 @@ https://docs.djangoproject.com/en/6.0/ref/settings/
|
|||||||
|
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
import os
|
import os
|
||||||
|
import dj_database_url
|
||||||
|
|
||||||
# Build paths inside the project like this: BASE_DIR / 'subdir'.
|
# Build paths inside the project like this: BASE_DIR / 'subdir'.
|
||||||
BASE_DIR = Path(__file__).resolve().parent.parent
|
BASE_DIR = Path(__file__).resolve().parent.parent
|
||||||
@@ -26,20 +27,25 @@ if 'DJANGO_DEBUG_FALSE' in os.environ:
|
|||||||
DEBUG = False
|
DEBUG = False
|
||||||
SECRET_KEY = os.environ['DJANGO_SECRET_KEY']
|
SECRET_KEY = os.environ['DJANGO_SECRET_KEY']
|
||||||
ALLOWED_HOSTS = [host.strip() for host in os.environ['DJANGO_ALLOWED_HOST'].split(',')]
|
ALLOWED_HOSTS = [host.strip() for host in os.environ['DJANGO_ALLOWED_HOST'].split(',')]
|
||||||
db_path = os.environ['DJANGO_DB_PATH']
|
CSRF_TRUSTED_ORIGINS = [f'https://{host.strip()}' for host in os.environ['DJANGO_ALLOWED_HOST'].split(',')]
|
||||||
|
SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https')
|
||||||
|
SESSION_COOKIE_SECURE = True
|
||||||
|
CSRF_COOKIE_SECURE = True
|
||||||
|
SECURE_HSTS_SECONDS = 60
|
||||||
|
SECURE_HSTS_INCLUDE_SUBDOMAINS = True
|
||||||
|
SECURE_HSTS_PRELOAD = True
|
||||||
else:
|
else:
|
||||||
DEBUG = True
|
DEBUG = True
|
||||||
# SECURITY WARNING: keep the secret key used in production secret!
|
# SECURITY WARNING: keep the secret key used in production secret!
|
||||||
SECRET_KEY = 'django-insecure-&9b_h=qpjy=sshhnsyg98&jp7(t6*v78__y%h2l$b#_@6z$-9r'
|
SECRET_KEY = 'django-insecure-&9b_h=qpjy=sshhnsyg98&jp7(t6*v78__y%h2l$b#_@6z$-9r'
|
||||||
ALLOWED_HOSTS = []
|
ALLOWED_HOSTS = []
|
||||||
db_path = BASE_DIR / 'db.sqlite3'
|
|
||||||
|
|
||||||
|
|
||||||
# Application definition
|
# Application definition
|
||||||
|
|
||||||
INSTALLED_APPS = [
|
INSTALLED_APPS = [
|
||||||
# Django apps
|
# Django apps
|
||||||
# 'django.contrib.admin',
|
'django.contrib.admin',
|
||||||
'django.contrib.auth',
|
'django.contrib.auth',
|
||||||
'django.contrib.contenttypes',
|
'django.contrib.contenttypes',
|
||||||
'django.contrib.sessions',
|
'django.contrib.sessions',
|
||||||
@@ -90,12 +96,16 @@ ASGI_APPLICATION = 'core.asgi.application'
|
|||||||
# Database
|
# Database
|
||||||
# https://docs.djangoproject.com/en/6.0/ref/settings/#databases
|
# https://docs.djangoproject.com/en/6.0/ref/settings/#databases
|
||||||
|
|
||||||
DATABASES = {
|
DATABASE_URL = os.environ.get('DATABASE_URL')
|
||||||
'default': {
|
if DATABASE_URL:
|
||||||
'ENGINE': 'django.db.backends.sqlite3',
|
DATABASES = {'default': dj_database_url.config(conn_max_age=600)}
|
||||||
'NAME': db_path,
|
else:
|
||||||
|
DATABASES = {
|
||||||
|
'default': {
|
||||||
|
'ENGINE': 'django.db.backends.sqlite3',
|
||||||
|
'NAME': BASE_DIR / 'db.sqlite3',
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
# Password validation
|
# Password validation
|
||||||
@@ -119,6 +129,7 @@ AUTH_PASSWORD_VALIDATORS = [
|
|||||||
AUTH_USER_MODEL = "lyric.User"
|
AUTH_USER_MODEL = "lyric.User"
|
||||||
|
|
||||||
AUTHENTICATION_BACKENDS = [
|
AUTHENTICATION_BACKENDS = [
|
||||||
|
"django.contrib.auth.backends.ModelBackend",
|
||||||
"apps.lyric.authentication.PasswordlessAuthenticationBackend",
|
"apps.lyric.authentication.PasswordlessAuthenticationBackend",
|
||||||
]
|
]
|
||||||
|
|
||||||
@@ -155,12 +166,6 @@ LOGGING = {
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
# Email Settings
|
|
||||||
EMAIL_HOST = "smtp.mailgun.org"
|
|
||||||
EMAIL_HOST_USER = os.environ.get("EMAIL_HOST_USER") # switch back to .environ[] when collectstatic moved outside docker build process
|
|
||||||
EMAIL_HOST_PASSWORD = os.environ.get("EMAIL_HOST_PASSWORD") # switch back to .environ[]
|
|
||||||
EMAIL_PORT = 587
|
|
||||||
EMAIL_USE_TLS = True
|
|
||||||
# Mailgun API settings (for HTTP API instead of SMTP)
|
# Mailgun API settings (for HTTP API instead of SMTP)
|
||||||
MAILGUN_API_KEY = os.environ.get("MAILGUN_API_KEY")
|
MAILGUN_API_KEY = os.environ.get("MAILGUN_API_KEY")
|
||||||
MAILGUN_DOMAIN = "howdy.earthmanrpg.me" # Your Mailgun domain
|
MAILGUN_DOMAIN = "howdy.earthmanrpg.com" # Your Mailgun domain
|
||||||
@@ -1,10 +1,10 @@
|
|||||||
# from django.contrib import admin
|
from django.contrib import admin
|
||||||
from django.http import HttpResponse
|
from django.http import HttpResponse
|
||||||
from django.urls import include, path
|
from django.urls import include, path
|
||||||
from apps.dashboard import views as dash_views
|
from apps.dashboard import views as dash_views
|
||||||
|
|
||||||
urlpatterns = [
|
urlpatterns = [
|
||||||
# path('admin/', admin.site.urls),
|
path('admin/', admin.site.urls),
|
||||||
path('', dash_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')),
|
path('apps/lyric/', include('apps.lyric.urls')),
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
import subprocess
|
import subprocess
|
||||||
|
|
||||||
|
|
||||||
USER = "discoman"
|
USER = "discoman"
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,10 @@
|
|||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
from django.contrib.auth import BACKEND_SESSION_KEY, SESSION_KEY, get_user_model
|
from django.contrib.auth import (
|
||||||
|
BACKEND_SESSION_KEY,
|
||||||
|
HASH_SESSION_KEY,
|
||||||
|
SESSION_KEY,
|
||||||
|
get_user_model,
|
||||||
|
)
|
||||||
from django.contrib.sessions.backends.db import SessionStore
|
from django.contrib.sessions.backends.db import SessionStore
|
||||||
from django.core.management.base import BaseCommand
|
from django.core.management.base import BaseCommand
|
||||||
|
|
||||||
@@ -15,9 +20,10 @@ class Command(BaseCommand):
|
|||||||
self.stdout.write(session_key)
|
self.stdout.write(session_key)
|
||||||
|
|
||||||
def create_pre_authenticated_session(email):
|
def create_pre_authenticated_session(email):
|
||||||
user = User.objects.create(email=email)
|
user = User.objects.create_user(email=email)
|
||||||
session = SessionStore()
|
session = SessionStore()
|
||||||
session[SESSION_KEY] = str(user.pk) # Convert UUID to string for JSON serialization
|
session[SESSION_KEY] = str(user.pk) # Convert UUID to string for JSON serialization
|
||||||
session[BACKEND_SESSION_KEY] = settings.AUTHENTICATION_BACKENDS[0]
|
session[BACKEND_SESSION_KEY] = "apps.lyric.authentication.PasswordlessAuthenticationBackend"
|
||||||
|
session[HASH_SESSION_KEY] = user.get_session_auth_hash()
|
||||||
session.save()
|
session.save()
|
||||||
return session.session_key
|
return session.session_key
|
||||||
|
|||||||
28
src/functional_tests/test_admin.py
Normal file
28
src/functional_tests/test_admin.py
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
from selenium.webdriver.common.by import By
|
||||||
|
|
||||||
|
from .base import FunctionalTest
|
||||||
|
from apps.lyric.models import User
|
||||||
|
|
||||||
|
|
||||||
|
class AdminLoginTest(FunctionalTest):
|
||||||
|
def setUp(self):
|
||||||
|
super().setUp()
|
||||||
|
self.superuser = User.objects.create_superuser(
|
||||||
|
email="admin@example.com",
|
||||||
|
password="correct-password",
|
||||||
|
)
|
||||||
|
|
||||||
|
def test_can_access_admin(self):
|
||||||
|
self.browser.get(self.live_server_url + "/admin/")
|
||||||
|
|
||||||
|
self.wait_for(lambda: self.browser.find_element(By.ID, "id_username"))
|
||||||
|
self.browser.find_element(By.ID, "id_username").send_keys("admin@example.com")
|
||||||
|
self.browser.find_element(By.ID, "id_password").send_keys("correct-password")
|
||||||
|
self.browser.find_element(By.CSS_SELECTOR, "input[type=submit]").click()
|
||||||
|
|
||||||
|
body = self.wait_for(
|
||||||
|
lambda: self.browser.find_element(By.TAG_NAME, "body")
|
||||||
|
)
|
||||||
|
self.assertIn("Site administration", body.text)
|
||||||
|
self.assertIn("Users", body.text)
|
||||||
|
self.assertIn("Tokens", body.text)
|
||||||
@@ -4,6 +4,7 @@ from selenium.webdriver.common.keys import Keys
|
|||||||
from .base import FunctionalTest
|
from .base import FunctionalTest
|
||||||
from .list_page import ListPage
|
from .list_page import ListPage
|
||||||
|
|
||||||
|
|
||||||
class LayoutAndStylingTest(FunctionalTest):
|
class LayoutAndStylingTest(FunctionalTest):
|
||||||
def test_layout_and_styling(self):
|
def test_layout_and_styling(self):
|
||||||
self.browser.get(self.live_server_url)
|
self.browser.get(self.live_server_url)
|
||||||
|
|||||||
@@ -2,11 +2,14 @@ import re
|
|||||||
from unittest.mock import patch
|
from unittest.mock import patch
|
||||||
from selenium.webdriver.common.by import By
|
from selenium.webdriver.common.by import By
|
||||||
from selenium.webdriver.common.keys import Keys
|
from selenium.webdriver.common.keys import Keys
|
||||||
|
|
||||||
from .base import FunctionalTest
|
from .base import FunctionalTest
|
||||||
|
|
||||||
|
|
||||||
TEST_EMAIL = "discoman@example.com"
|
TEST_EMAIL = "discoman@example.com"
|
||||||
SUBJECT = "A magic login link to your Dashboard"
|
SUBJECT = "A magic login link to your Dashboard"
|
||||||
|
|
||||||
|
|
||||||
class LoginTest(FunctionalTest):
|
class LoginTest(FunctionalTest):
|
||||||
@patch('apps.lyric.views.requests.post')
|
@patch('apps.lyric.views.requests.post')
|
||||||
def test_login_using_magic_link(self, mock_post):
|
def test_login_using_magic_link(self, mock_post):
|
||||||
|
|||||||
@@ -1,3 +1,5 @@
|
|||||||
|
import os
|
||||||
|
|
||||||
from selenium import webdriver
|
from selenium import webdriver
|
||||||
from selenium.webdriver.common.by import By
|
from selenium.webdriver.common.by import By
|
||||||
|
|
||||||
@@ -21,7 +23,10 @@ class SharingTest(FunctionalTest):
|
|||||||
disco_browser = self.browser
|
disco_browser = self.browser
|
||||||
self.addCleanup(lambda: quit_if_possible(disco_browser))
|
self.addCleanup(lambda: quit_if_possible(disco_browser))
|
||||||
|
|
||||||
ali_browser = webdriver.Firefox()
|
options = webdriver.FirefoxOptions()
|
||||||
|
if os.environ.get("HEADLESS"):
|
||||||
|
options.add_argument("--headless")
|
||||||
|
ali_browser = webdriver.Firefox(options=options)
|
||||||
self.addCleanup(lambda: quit_if_possible(ali_browser))
|
self.addCleanup(lambda: quit_if_possible(ali_browser))
|
||||||
self.browser = ali_browser
|
self.browser = ali_browser
|
||||||
self.create_pre_authenticated_session("alice@example.com")
|
self.create_pre_authenticated_session("alice@example.com")
|
||||||
|
|||||||
Reference in New Issue
Block a user