Add storage utilities and tests for file handling and tokens
All checks were successful
Build and Push Docker Images / changes (push) Successful in 5s
Build and Push Docker Images / build-backend (push) Successful in 51s
Build and Push Docker Images / build-frontend (push) Has been skipped

Introduced new fixtures and tests for storage functionality, including saving files, generating URLs, and token creation/verification. Refactored `get_storage_provider` into a separate dependency module. Enhanced test coverage for improved reliability.
This commit is contained in:
2025-03-12 18:50:30 +01:00
parent 9879353ac1
commit 38acdb78a1
5 changed files with 144 additions and 8 deletions

View File

@@ -0,0 +1,58 @@
import time
import pytest
from app.utils.security import create_upload_token, verify_upload_token
def test_upload_token_creation():
"""Test that upload tokens can be created with expected fields."""
file_path = "images/test.jpg"
content_type = "image/jpeg"
token = create_upload_token(file_path, content_type)
assert token is not None
assert isinstance(token, str)
assert len(token) > 0
def test_upload_token_verification():
"""Test that created tokens can be verified."""
file_path = "images/test.jpg"
content_type = "image/jpeg"
token = create_upload_token(file_path, content_type)
payload = verify_upload_token(token)
assert payload is not None
assert payload["path"] == file_path
assert payload["content_type"] == content_type
assert payload["exp"] > int(time.time())
def test_upload_token_expiration():
"""Test that expired tokens are rejected."""
file_path = "images/test.jpg"
content_type = "image/jpeg"
# Create a token that expires in 1 second
token = create_upload_token(file_path, content_type, expires_in=1)
# Wait for it to expire
time.sleep(2)
payload = verify_upload_token(token)
assert payload is None
def test_upload_token_tampered():
"""Test that tampered tokens are rejected."""
file_path = "images/test.jpg"
content_type = "image/jpeg"
token = create_upload_token(file_path, content_type)
# Tamper with the token
tampered_token = token[:-5] + "XXXXX"
payload = verify_upload_token(tampered_token)
assert payload is None