54 lines
1.5 KiB
Python
54 lines
1.5 KiB
Python
import os
|
|
import tempfile
|
|
from web.app import app
|
|
|
|
|
|
def test_cached_route_serves_file(monkeypatch):
|
|
# Create a temporary file in the configured cache dir
|
|
cache_dir = os.path.abspath(os.path.join(
|
|
os.path.dirname(__file__), '..', 'cache'))
|
|
os.makedirs(cache_dir, exist_ok=True)
|
|
fd, tmp_path = tempfile.mkstemp(
|
|
prefix='test_cached_', suffix='.html', dir=cache_dir)
|
|
os.close(fd)
|
|
with open(tmp_path, 'w', encoding='utf-8') as f:
|
|
f.write('<html><body>cached</body></html>')
|
|
|
|
# Fake job record returned by get_job_by_id
|
|
fake_job = {
|
|
'id': 'fake123',
|
|
'job_id': 'fake123',
|
|
'file_path': os.path.relpath(tmp_path, cache_dir),
|
|
'file_path_abs': tmp_path,
|
|
}
|
|
|
|
def fake_get_job_by_id(jid):
|
|
if str(jid) in ('fake123',):
|
|
return fake_job
|
|
return {}
|
|
|
|
# Patch the symbol imported into web.app
|
|
monkeypatch.setattr('web.app.get_job_by_id', fake_get_job_by_id)
|
|
|
|
# Request route
|
|
client = app.test_client()
|
|
res = client.get('/cached/fake123')
|
|
assert res.status_code == 200
|
|
assert b'cached' in res.data
|
|
|
|
# Cleanup
|
|
try:
|
|
os.remove(tmp_path)
|
|
except Exception:
|
|
pass
|
|
|
|
|
|
def test_cached_route_missing(monkeypatch):
|
|
def fake_get_job_by_id(jid):
|
|
return {}
|
|
|
|
monkeypatch.setattr('web.app.get_job_by_id', fake_get_job_by_id)
|
|
client = app.test_client()
|
|
res = client.get('/cached/nope')
|
|
assert res.status_code == 404
|