Skip to content

armgit / mwmbl

Public Imported from GitHub
0

An open source, non-profit web search engine

Add tests; fix bug

Daoud Clarkedee8f1588c9f
diff --git a/mwmbl/migrations/0019_add_userbilling.py b/mwmbl/migrations/0019_add_userbilling.py
new file mode 100644
index 0000000..9f5b873
--- /dev/null
+++ b/mwmbl/migrations/0019_add_userbilling.py
@@ -0,0 +1,25 @@
+# Generated by Django 5.2.13 on 2026-04-16 21:03
+
+import django.db.models.deletion
+from django.conf import settings
+from django.db import migrations, models
+
+
+class Migration(migrations.Migration):
+
+ dependencies = [
+ ('mwmbl', '0018_usagebucket_remove_monthly_search_count'),
+ ]
+
+ operations = [
+ migrations.CreateModel(
+ name='UserBilling',
+ fields=[
+ ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
+ ('polar_customer_id', models.CharField(blank=True, default='', max_length=100)),
+ ('polar_subscription_id', models.CharField(blank=True, default='', max_length=100)),
+ ('current_period_end', models.DateTimeField(blank=True, null=True)),
+ ('user', models.OneToOneField(on_delete=django.db.models.deletion.CASCADE, related_name='billing', to=settings.AUTH_USER_MODEL)),
+ ],
+ ),
+ ]
diff --git a/mwmbl/settings_common.py b/mwmbl/settings_common.py
index d766a69..a1a32b8 100644
--- a/mwmbl/settings_common.py
+++ b/mwmbl/settings_common.py
@@ -228,8 +228,10 @@ def strip_query_string(event):
del event["request"]["query_string"]
return event
-# Django ninja-jwt settings - custom auth rule
-USER_AUTHENTICATION_RULE = require_email_confirmation
+# Django ninja-jwt settings
+NINJA_JWT = {
+ "USER_AUTHENTICATION_RULE": require_email_confirmation,
+}
# Database configuration (shared across all environments via DATABASE_URL env var)
# Apply PostgreSQL's default: if no database name is given in the URL, fall back to
diff --git a/test/test_auth.py b/test/test_auth.py
new file mode 100644
index 0000000..a675132
--- /dev/null
+++ b/test/test_auth.py
@@ -0,0 +1,112 @@
+import pytest
+from django.contrib.auth import get_user_model
+from allauth.account.models import EmailAddress
+
+User = get_user_model()
+
+TOKEN_URL = "/api/v1/platform/token/pair"
+
+
+def verified_user(db):
+ user = User.objects.create_user(
+ username="swift_falcon_379",
+ password="correctpassword",
+ )
+ EmailAddress.objects.create(
+ user=user, email="[email protected]", verified=True, primary=True
+ )
+ return user
+
+
+def unverified_user(db):
+ user = User.objects.create_user(
+ username="quiet_river_123",
+ password="correctpassword",
+ )
+ EmailAddress.objects.create(
+ user=user, email="[email protected]", verified=False, primary=True
+ )
+ return user
+
+
+def test_login_with_username(client, verified_user):
+ response = client.post(
+ TOKEN_URL,
+ {"username": "swift_falcon_379", "password": "correctpassword"},
+ content_type="application/json",
+ )
+ assert response.status_code == 200
+ data = response.json()
+ assert "access" in data
+ assert "refresh" in data
+
+
+def test_login_with_email(client, verified_user):
+ response = client.post(
+ TOKEN_URL,
+ {"username": "[email protected]", "password": "correctpassword"},
+ content_type="application/json",
+ )
+ assert response.status_code == 200
+ data = response.json()
+ assert "access" in data
+ assert "refresh" in data
+
+
+def test_login_wrong_password(client, verified_user):
+ response = client.post(
+ TOKEN_URL,
+ {"username": "swift_falcon_379", "password": "wrongpassword"},
+ content_type="application/json",
+ )
+ assert response.status_code == 401
+
+
+def test_login_nonexistent_user(client, db):
+ response = client.post(
+ TOKEN_URL,
+ {"username": "nobody", "password": "somepassword"},
+ content_type="application/json",
+ )
+ assert response.status_code == 401
+
+
+def test_login_unverified_email_rejected(client, unverified_user):
+ response = client.post(
+ TOKEN_URL,
+ {"username": "quiet_river_123", "password": "correctpassword"},
+ content_type="application/json",
+ )
+ assert response.status_code == 401
+
+
+def test_login_email_and_username_return_same_user(client, verified_user):
+ by_username = client.post(
+ TOKEN_URL,
+ {"username": "swift_falcon_379", "password": "correctpassword"},
+ content_type="application/json",
+ ).json()
+ by_email = client.post(
+ TOKEN_URL,
+ {"username": "[email protected]", "password": "correctpassword"},
+ content_type="application/json",
+ ).json()
+ # Both tokens are for the same user — decode the subject claim to verify
+ import base64, json as _json
+
+ def subject(token):
+ payload = token.split(".")[1]
+ payload += "=" * (-len(payload) % 4)
+ return _json.loads(base64.urlsafe_b64decode(payload))["user_id"]
+
+ assert subject(by_username["access"]) == subject(by_email["access"])