Files
privatestudio/privatestudio/studio/tests/test_api.py
T
Shaun KrugerandClaude Opus 5.5 b3b51f6cc5 Convert web service from FastAPI to Django 6.1
Replace the FastAPI/SQLAlchemy/Alembic web service with a Django project
laid out like cumorah-build: manage.py at the root, settings in the
privatestudio.settings package (base/dev/production), and a
privatestudio.studio app.

- Studio model owns a unique bucket_name; access is granted through
  membership in the studio's auth Group. Assets, profiles and jobs are
  scoped to a studio; API keys and S3 credentials stay per user.
- JSON API via Django Ninja under /api/ (profiles and transcode jobs now
  live under /api/studios/{bucket}/).
- S3-compatible interface as plain class-based views under /s3/,
  supporting bearer tokens and AWS v4 signatures.
- create_studio and create_key management commands replace ps-serve.
- Fresh Django initial migration; tests ported to Django TestCase.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-29 21:54:16 -07:00

155 lines
6.7 KiB
Python

from django.contrib.auth.models import User
from privatestudio.studio.auth import create_api_key
from privatestudio.studio.models import TranscodeJob, UploadedAsset
from privatestudio.studio.tests.base import StudioTestCase
PROFILE_YAML = "profiles:\n hd:\n resolution: 1280x720\n bitrate: 2500k\n"
class HealthTests(StudioTestCase):
def test_health_check(self):
response = self.client.get("/health")
self.assertEqual(response.status_code, 200)
self.assertEqual(response.json(), {"status": "ok"})
class APIKeyTests(StudioTestCase):
def test_create_api_key(self):
response = self.client.post("/api/keys", headers=self.auth())
self.assertEqual(response.status_code, 200)
data = response.json()
self.assertIn("id", data)
self.assertIn("key", data)
self.assertIn("created_at", data)
def test_list_api_keys(self):
response = self.client.get("/api/keys", headers=self.auth())
self.assertEqual(response.status_code, 200)
data = response.json()
self.assertEqual(len(data), 1)
self.assertIs(data[0]["is_active"], True)
def test_authentication_required(self):
self.assertEqual(self.client.get("/api/keys").status_code, 401)
def test_invalid_api_key(self):
self.assertEqual(self.client.get("/api/keys", headers=self.auth("invalid_key")).status_code, 401)
def test_revoke_api_key(self):
key_id = self.client.get("/api/keys", headers=self.auth()).json()[0]["id"]
response = self.client.delete(f"/api/keys/{key_id}", headers=self.auth())
self.assertEqual(response.status_code, 200)
self.assertEqual(self.client.get("/api/keys", headers=self.auth()).status_code, 401)
def test_cannot_revoke_other_users_key(self):
other = User.objects.create_user("other")
_, other_key = create_api_key(other)
response = self.client.delete(f"/api/keys/{other_key.id}", headers=self.auth())
self.assertEqual(response.status_code, 403)
class S3KeyTests(StudioTestCase):
def test_generate_s3_credentials(self):
response = self.client.post("/api/s3keys", headers=self.auth())
self.assertEqual(response.status_code, 200)
data = response.json()
self.assertTrue(data["access_key"].startswith("AKIA"))
self.assertGreaterEqual(len(data["secret_key"]), 30)
self.assertIn("T", data["expires_at"])
data2 = self.client.post("/api/s3keys", headers=self.auth()).json()
self.assertNotEqual(data["access_key"], data2["access_key"])
self.assertNotEqual(data["secret_key"], data2["secret_key"])
class UserInfoTests(StudioTestCase):
def test_userinfo_lists_studios(self):
response = self.client.get("/api/userinfo", headers=self.auth())
self.assertEqual(response.status_code, 200)
self.assertEqual(response.json(), {
"user_id": self.user.id,
"username": "testuser",
"studios": [{"name": "Demo", "bucket": "demo"}],
})
class ProfileTests(StudioTestCase):
def create_profile(self, name="hd-profile", body=PROFILE_YAML, key=None):
return self.client.post(
f"/api/studios/demo/profiles?name={name}", data=body,
content_type="application/yaml", headers=self.auth(key),
)
def test_create_list_get_profile(self):
response = self.create_profile()
self.assertEqual(response.status_code, 200, response.content)
profile_id = response.json()["id"]
listing = self.client.get("/api/studios/demo/profiles", headers=self.auth()).json()
self.assertEqual([p["name"] for p in listing], ["hd-profile"])
detail = self.client.get(f"/api/studios/demo/profiles/{profile_id}", headers=self.auth())
self.assertEqual(detail.status_code, 200)
self.assertEqual(detail.json()["profile_definition"], PROFILE_YAML)
def test_duplicate_profile_conflicts(self):
self.create_profile()
self.assertEqual(self.create_profile().status_code, 409)
def test_invalid_profile_yaml(self):
self.assertEqual(self.create_profile(body="not: [valid").status_code, 400)
self.assertEqual(self.create_profile(body="foo: bar\n").status_code, 400)
def test_non_member_forbidden(self):
outsider = User.objects.create_user("outsider")
outsider_key, _ = create_api_key(outsider)
self.assertEqual(self.create_profile(key=outsider_key).status_code, 403)
def test_unknown_studio(self):
response = self.client.get("/api/studios/nope/profiles", headers=self.auth())
self.assertEqual(response.status_code, 404)
class TranscodeTests(StudioTestCase):
def setUp(self):
super().setUp()
self.asset = UploadedAsset.objects.create(
studio=self.studio, uploaded_by=self.user, filename="video.mp4", size=10, s3_path="raw/video.mp4",
)
def request_transcode(self, body):
return self.client.post(
"/api/studios/demo/transcode", data=body, content_type="application/yaml", headers=self.auth(),
)
def test_transcode_with_named_profile(self):
self.client.post(
"/api/studios/demo/profiles?name=hd", data=PROFILE_YAML,
content_type="application/yaml", headers=self.auth(),
)
response = self.request_transcode("asset: raw/video.mp4\nprofile: hd\n")
self.assertEqual(response.status_code, 200, response.content)
data = response.json()
self.assertEqual(data["asset_id"], self.asset.id)
self.assertIsNotNone(data["profile_id"])
self.assertEqual(data["status"], "pending")
job_id = data["id"]
self.assertEqual(self.client.get(f"/api/studios/demo/transcode/{job_id}", headers=self.auth()).status_code, 200)
listing = self.client.get("/api/studios/demo/transcode?status=pending", headers=self.auth()).json()
self.assertEqual([j["id"] for j in listing], [job_id])
def test_transcode_with_inline_profile_by_filename(self):
response = self.request_transcode("asset: video.mp4\nprofile:\n profiles:\n sd:\n bitrate: 800k\n")
self.assertEqual(response.status_code, 200, response.content)
self.assertIsNone(response.json()["profile_id"])
job = TranscodeJob.objects.get()
self.assertIn("800k", job.profile_definition)
def test_transcode_errors(self):
self.assertEqual(self.request_transcode("profile: hd\n").status_code, 400)
self.assertEqual(self.request_transcode("asset: missing.mp4\nprofile: hd\n").status_code, 404)
self.assertEqual(self.request_transcode("asset: video.mp4\nprofile: missing\n").status_code, 404)
self.assertEqual(self.request_transcode("asset: video.mp4\nprofile: [1]\n").status_code, 400)