Files
privatestudio/privatestudio/studio/tests/test_admin.py
T
Shaun KrugerandClaude Opus 5.5 38f3a0da2f
Build and publish / test (pull_request) Failing after 40s
Build and publish / cleanup (pull_request) Skipped
Build and publish / build (pull_request) Skipped
Build and publish / pypi (pull_request) Skipped
Add Docker image, Gitea CI, PyPI publishing and BSD-3-Clause license
- Dockerfile builds the npm bundle and serves the app with gunicorn
- Gitea workflow runs tests, tags releases on main, pushes the image to
  the Gitea registry, and publishes to PyPI when PYPI_TOKEN is set
- MANIFEST.in includes the built static bundle in distributions
- License the project under BSD-3-Clause

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-12-01 23:42:20 -07:00

65 lines
2.5 KiB
Python

import re
from django.contrib.auth.models import User
from django.urls import reverse
from privatestudio.studio.auth import hash_api_key, user_for_api_key
from privatestudio.studio.models import APIKey
from privatestudio.studio.tests.base import StudioTestCase
ADD_URL = reverse("admin:studio_apikey_add")
LIST_URL = reverse("admin:studio_apikey_changelist")
TOKEN_RE = re.compile(r"<code>([^<]+)</code>")
class APIKeyAdminTests(StudioTestCase):
def setUp(self):
super().setUp()
self.client.force_login(User.objects.create_superuser("admin"))
self.existing = APIKey.objects.get()
self.change_url = reverse("admin:studio_apikey_change", args=[self.existing.pk])
def form_data(self, **extra):
return {
"user": self.user.pk,
"created_at_0": "2026-01-01",
"created_at_1": "12:00:00",
"is_active": "on",
**extra,
}
def add_key(self):
"""Submit the add form and return the token shown to the admin."""
response = self.client.post(ADD_URL, self.form_data(), follow=True)
self.assertRedirects(response, LIST_URL)
return TOKEN_RE.search(response.content.decode()).group(1)
def test_add_shows_working_token_once(self):
token = self.add_key()
self.assertEqual(APIKey.objects.get(key_hash=hash_api_key(token)).user, self.user)
self.assertEqual(user_for_api_key(token), self.user)
self.assertNotContains(self.client.get(LIST_URL), token)
def test_each_key_is_unique(self):
self.assertNotEqual(self.add_key(), self.add_key())
self.assertEqual(APIKey.objects.count(), 3)
def test_submitted_key_hash_is_ignored(self):
self.client.post(ADD_URL, self.form_data(key_hash=hash_api_key("chosen")))
self.assertIsNone(user_for_api_key("chosen"))
def test_change_keeps_hash_and_shows_no_token(self):
data = self.form_data()
del data["is_active"]
response = self.client.post(self.change_url, data, follow=True)
self.assertRedirects(response, LIST_URL)
self.assertIsNone(TOKEN_RE.search(response.content.decode()))
original_hash = self.existing.key_hash
self.existing.refresh_from_db()
self.assertEqual(self.existing.key_hash, original_hash)
self.assertFalse(self.existing.is_active)
def test_key_hash_only_on_change_form(self):
self.assertNotContains(self.client.get(ADD_URL), "field-key_hash")
self.assertContains(self.client.get(self.change_url), self.existing.key_hash)