Add offline clip caching
This commit is contained in:
@@ -108,6 +108,112 @@ def test_teamsnap_token_returns_proxy_api_root() -> None:
|
||||
|
||||
assert response.status_code == 200
|
||||
assert response.json()["api_root"] == "https://kif.local.ascorrea.com/api/teamsnap"
|
||||
assert response.headers["cache-control"] == "no-store"
|
||||
|
||||
|
||||
def test_session_and_clip_reads_use_cache_validators() -> None:
|
||||
login = client.post("/auth/admin/login", json={"username": "admin", "password": "admin"})
|
||||
assert login.status_code == 200
|
||||
|
||||
session_response = client.get("/auth/session")
|
||||
assert session_response.status_code == 200
|
||||
assert session_response.headers["cache-control"] == "no-store"
|
||||
|
||||
db = SessionLocal()
|
||||
asset = AudioAsset(
|
||||
external_team_id="team-cache",
|
||||
owner_external_player_id="player-cache",
|
||||
title="Cache Song",
|
||||
original_filename="cache-song.mp3",
|
||||
mime_type="audio/mpeg",
|
||||
size_bytes=123,
|
||||
storage_path="uploads/cache-song.mp3",
|
||||
)
|
||||
db.add(asset)
|
||||
db.flush()
|
||||
clip = AudioClip(
|
||||
asset_id=asset.id,
|
||||
label="Cache clip",
|
||||
start_ms=0,
|
||||
end_ms=10000,
|
||||
normalization_status="ready",
|
||||
normalized_path="normalized/cache-clip.mp3",
|
||||
)
|
||||
db.add(clip)
|
||||
db.commit()
|
||||
db.refresh(clip)
|
||||
db.close()
|
||||
|
||||
clips = client.get("/media/clips", params={"external_team_id": "team-cache", "owner_external_player_id": "player-cache"})
|
||||
assert clips.status_code == 200
|
||||
assert clips.headers["etag"]
|
||||
assert clips.headers["cache-control"] == "private, max-age=0, must-revalidate"
|
||||
assert clips.headers["vary"] == "Cookie, Authorization"
|
||||
|
||||
revalidated = client.get(
|
||||
"/media/clips",
|
||||
params={"external_team_id": "team-cache", "owner_external_player_id": "player-cache"},
|
||||
headers={"if-none-match": clips.headers["etag"]},
|
||||
)
|
||||
assert revalidated.status_code == 304
|
||||
|
||||
|
||||
def test_game_prep_uses_stable_etag_for_cached_assignments() -> None:
|
||||
login = client.post("/auth/admin/login", json={"username": "admin", "password": "admin"})
|
||||
assert login.status_code == 200
|
||||
|
||||
db = SessionLocal()
|
||||
asset = AudioAsset(
|
||||
external_team_id="team-prep",
|
||||
owner_external_player_id="player-prep",
|
||||
title="Prep Song",
|
||||
original_filename="prep-song.mp3",
|
||||
mime_type="audio/mpeg",
|
||||
size_bytes=123,
|
||||
storage_path="uploads/prep-song.mp3",
|
||||
)
|
||||
db.add(asset)
|
||||
db.flush()
|
||||
clip = AudioClip(
|
||||
asset_id=asset.id,
|
||||
label="Prep clip",
|
||||
start_ms=0,
|
||||
end_ms=10000,
|
||||
normalization_status="ready",
|
||||
normalized_path="normalized/prep-clip.mp3",
|
||||
)
|
||||
db.add(clip)
|
||||
db.flush()
|
||||
assignment = GameAssignment(
|
||||
external_team_id="team-prep",
|
||||
external_game_id="game-prep",
|
||||
external_player_id="player-prep",
|
||||
clip_id=clip.id,
|
||||
batting_slot=3,
|
||||
status="ready",
|
||||
)
|
||||
db.add(assignment)
|
||||
db.commit()
|
||||
db.close()
|
||||
|
||||
prep = client.get("/games/game-prep/prep")
|
||||
assert prep.status_code == 200
|
||||
assert prep.headers["etag"]
|
||||
|
||||
revalidated = client.get("/games/game-prep/prep", headers={"if-none-match": prep.headers["etag"]})
|
||||
assert revalidated.status_code == 304
|
||||
|
||||
|
||||
def test_normalized_media_files_are_cacheable() -> None:
|
||||
media_file = settings.media_root / "normalized" / "cacheable.mp3"
|
||||
media_file.parent.mkdir(parents=True, exist_ok=True)
|
||||
media_file.write_bytes(make_test_wav_bytes())
|
||||
|
||||
response = client.get("/media/files/normalized/cacheable.mp3")
|
||||
|
||||
assert response.status_code == 200
|
||||
assert response.headers["cache-control"] == "public, max-age=31536000, immutable"
|
||||
assert response.headers["etag"]
|
||||
|
||||
|
||||
def test_walkup_session_selection_is_persisted_in_session() -> None:
|
||||
|
||||
Reference in New Issue
Block a user