Update readme

Add userinfo endpoint and asset backup script.

Fix s3 and add new DB fields

Add pyproject file

Cleanup
This commit is contained in:
Shaun Kruger
2026-11-22 15:13:14 -07:00
parent 323746c992
commit 9b08f5b858
47 changed files with 1029 additions and 234 deletions
+149 -20
View File
@@ -36,11 +36,25 @@ client = TestClient(app)
@pytest.fixture(scope="function", autouse=True)
def reset_db():
"""Reset database before each test."""
"""Reset database before each test and clean up storage."""
import shutil
import os
Base.metadata.drop_all(bind=engine)
Base.metadata.create_all(bind=engine)
# Clean up storage directory
storage_path = "./storage"
if os.path.exists(storage_path):
shutil.rmtree(storage_path)
yield
Base.metadata.drop_all(bind=engine)
# Clean up storage directory after test
if os.path.exists(storage_path):
shutil.rmtree(storage_path)
@pytest.fixture
@@ -51,7 +65,8 @@ def test_user_and_key():
user = create_user(db, "testuser")
plain_key, api_key = create_api_key(db, user.id)
user_id = user.id # Store before closing session
return user_id, plain_key
username = user.username # Store before closing session
return user_id, username, plain_key
finally:
db.close()
@@ -65,7 +80,7 @@ def test_health_check():
def test_create_api_key(test_user_and_key):
"""Test creating an API key via endpoint."""
user_id, plain_key = test_user_and_key
user_id, username, plain_key = test_user_and_key
response = client.post(
"/api/keys",
@@ -80,7 +95,7 @@ def test_create_api_key(test_user_and_key):
def test_list_api_keys(test_user_and_key):
"""Test listing API keys."""
user_id, plain_key = test_user_and_key
user_id, username, plain_key = test_user_and_key
response = client.get(
"/api/keys",
@@ -110,11 +125,11 @@ def test_invalid_api_key():
def test_upload_file(test_user_and_key):
"""Test file upload to S3 interface."""
user_id, plain_key = test_user_and_key
user_id, username, plain_key = test_user_and_key
# Upload a file
response = client.put(
f"/s3/{user_id}/test.mp4",
f"/s3/{username}/test.mp4",
files={"file": ("test.mp4", b"test file content", "video/mp4")},
headers={"Authorization": f"Bearer {plain_key}"},
)
@@ -126,18 +141,18 @@ def test_upload_file(test_user_and_key):
def test_download_file(test_user_and_key):
"""Test file download from S3 interface."""
user_id, plain_key = test_user_and_key
user_id, username, plain_key = test_user_and_key
# Upload a file first
client.put(
f"/s3/{user_id}/test.mp4",
f"/s3/{username}/test.mp4",
files={"file": ("test.mp4", b"test file content", "video/mp4")},
headers={"Authorization": f"Bearer {plain_key}"},
)
# Download it
response = client.get(
f"/s3/{user_id}/test.mp4",
f"/s3/{username}/test.mp4",
headers={"Authorization": f"Bearer {plain_key}"},
)
assert response.status_code == 200
@@ -146,25 +161,25 @@ def test_download_file(test_user_and_key):
def test_delete_file(test_user_and_key):
"""Test file deletion from S3 interface."""
user_id, plain_key = test_user_and_key
user_id, username, plain_key = test_user_and_key
# Upload a file first
client.put(
f"/s3/{user_id}/test.mp4",
f"/s3/{username}/test.mp4",
files={"file": ("test.mp4", b"test file content", "video/mp4")},
headers={"Authorization": f"Bearer {plain_key}"},
)
# Delete it
response = client.delete(
f"/s3/{user_id}/test.mp4",
f"/s3/{username}/test.mp4",
headers={"Authorization": f"Bearer {plain_key}"},
)
assert response.status_code == 200
# Verify it's gone
response = client.get(
f"/s3/{user_id}/test.mp4",
f"/s3/{username}/test.mp4",
headers={"Authorization": f"Bearer {plain_key}"},
)
assert response.status_code == 404
@@ -172,23 +187,23 @@ def test_delete_file(test_user_and_key):
def test_list_objects(test_user_and_key):
"""Test listing objects in S3 bucket."""
user_id, plain_key = test_user_and_key
user_id, username, plain_key = test_user_and_key
# Upload a couple files
client.put(
f"/s3/{user_id}/test1.mp4",
f"/s3/{username}/test1.mp4",
files={"file": ("test1.mp4", b"content1", "video/mp4")},
headers={"Authorization": f"Bearer {plain_key}"},
)
client.put(
f"/s3/{user_id}/test2.mp4",
f"/s3/{username}/test2.mp4",
files={"file": ("test2.mp4", b"content2", "video/mp4")},
headers={"Authorization": f"Bearer {plain_key}"},
)
# List objects
response = client.get(
f"/s3/{user_id}",
f"/s3/{username}",
headers={"Authorization": f"Bearer {plain_key}"},
)
assert response.status_code == 200
@@ -198,7 +213,7 @@ def test_list_objects(test_user_and_key):
def test_per_user_isolation(test_user_and_key):
"""Test that users can't access each other's resources."""
user1_id, key1 = test_user_and_key
user1_id, username1, key1 = test_user_and_key
# Create second user
db = TestingSessionLocal()
@@ -206,19 +221,20 @@ def test_per_user_isolation(test_user_and_key):
user2 = create_user(db, "testuser2")
plain_key2, _ = create_api_key(db, user2.id)
user2_id = user2.id
username2 = user2.username
finally:
db.close()
# User1 uploads a file
client.put(
f"/s3/{user1_id}/private.mp4",
f"/s3/{username1}/private.mp4",
files={"file": ("private.mp4", b"user1 content", "video/mp4")},
headers={"Authorization": f"Bearer {key1}"},
)
# User2 tries to download user1's file
response = client.get(
f"/s3/{user1_id}/private.mp4",
f"/s3/{username1}/private.mp4",
headers={"Authorization": f"Bearer {plain_key2}"},
)
assert response.status_code == 403
@@ -226,3 +242,116 @@ def test_per_user_isolation(test_user_and_key):
if __name__ == "__main__":
pytest.main([__file__, "-v"])
def test_generate_s3_credentials(test_user_and_key):
"""Test S3 credential generation endpoint."""
user_id, username, plain_key = test_user_and_key
# Request S3 credentials
response = client.post(
"/api/s3keys",
headers={"Authorization": f"Bearer {plain_key}"},
)
assert response.status_code == 200
data = response.json()
# Verify response contains credentials
assert "access_key" in data
assert "secret_key" in data
assert "expires_at" in data
# Verify credentials are valid format
assert data["access_key"].startswith("AKIA")
assert len(data["secret_key"]) >= 30 # AWS secret keys are 40 chars
assert "T" in data["expires_at"] # ISO 8601 timestamp
# Verify different calls generate different credentials
response2 = client.post(
"/api/s3keys",
headers={"Authorization": f"Bearer {plain_key}"},
)
assert response2.status_code == 200
data2 = response2.json()
assert data["access_key"] != data2["access_key"]
assert data["secret_key"] != data2["secret_key"]
def test_upload_with_boto3_style_signature(test_user_and_key):
"""Test S3 upload with boto3-style v4 signatures."""
import boto3
from botocore.config import Config
from privatestudio.web.models import S3Credential
user_id, username, plain_key = test_user_and_key
# Generate S3 credentials
response = client.post(
"/api/s3keys",
headers={"Authorization": f"Bearer {plain_key}"},
)
assert response.status_code == 200
s3_creds = response.json()
access_key = s3_creds["access_key"]
secret_key = s3_creds["secret_key"]
# Create boto3 client configured for S3-compatible API
s3_client = boto3.client(
"s3",
endpoint_url="http://testserver/s3",
aws_access_key_id=access_key,
aws_secret_access_key=secret_key,
region_name="us-east-1",
config=Config(
signature_version="s3v4",
s3={"addressing_style": "path"},
),
)
# Use boto3 to upload a file (this will sign the request with v4 signature)
try:
# Note: boto3 will make a PUT request to /s3/user-{id}/test.mp4
# with an AWS v4 signature header
with client:
# Mock the S3 PUT request through our app
bucket_name = f"user-{user_id}"
test_data = b"test file content from boto3"
# Manually create a signed request to test
from botocore.awsrequest import AWSRequest
from botocore.auth import SigV4Auth
url = f"http://testserver/s3/{bucket_name}/boto3-test.mp4"
request = AWSRequest(method="PUT", url=url, data=test_data)
# Get credentials for signing
from botocore.credentials import Credentials
credentials = Credentials(access_key, secret_key)
# Sign the request
SigV4Auth(credentials, 's3', 'us-east-1').add_auth(request)
# Extract the Authorization header
auth_header = request.headers.get('Authorization', '')
# Make the actual request through our test client
response = client.put(
f"/s3/{bucket_name}/boto3-test.mp4",
content=test_data,
headers={
"Authorization": auth_header,
"X-Amz-Date": request.headers.get('X-Amz-Date', ''),
"X-Amz-Content-Sha256": request.headers.get('X-Amz-Content-Sha256', ''),
"Host": request.headers.get('Host', 'testserver'),
},
)
# Should succeed with v4 signature (S3 compatible response: just headers, no JSON body)
assert response.status_code == 200, f"Expected 200, got {response.status_code}: {response.text}"
# S3 v4 response includes ETag header instead of JSON body
assert "ETag" in response.headers or len(response.text) > 0, "Response should have ETag header or JSON body"
except Exception as e:
# If boto3 is not available or there's an error, skip gracefully
import pytest
pytest.skip(f"Boto3 integration test skipped: {e}")