Add output cleanup policy
This commit is contained in:
@@ -1,4 +1,6 @@
|
||||
import io
|
||||
import json
|
||||
from pathlib import Path
|
||||
|
||||
from app import create_app
|
||||
|
||||
@@ -132,6 +134,95 @@ def test_download_route_returns_generated_file(client):
|
||||
download_response.close()
|
||||
|
||||
|
||||
def test_download_route_can_cleanup_files_after_download(tmp_path):
|
||||
class CleanupAfterDownloadConfig:
|
||||
TESTING = True
|
||||
SECRET_KEY = "test-secret"
|
||||
MAX_CONTENT_LENGTH = 100 * 1024 * 1024
|
||||
PREVIEW_RECORD_LIMIT = 5
|
||||
OUTPUT_DIRECTORY = tmp_path / "download-cleanup-outputs"
|
||||
OUTPUT_RETENTION_HOURS = 24
|
||||
CLEANUP_ON_STARTUP = False
|
||||
CLEANUP_AFTER_DOWNLOAD = True
|
||||
|
||||
app = create_app(CleanupAfterDownloadConfig)
|
||||
client = app.test_client()
|
||||
log_file = io.BytesIO(SAMPLE_LOG.encode("utf-8"))
|
||||
|
||||
convert_response = client.post(
|
||||
"/convert",
|
||||
data={
|
||||
"mode": "vendor",
|
||||
"output_format": "csv",
|
||||
"sort_by": "datetime",
|
||||
"order": "asc",
|
||||
"policy_cs": "",
|
||||
"policy_ci": "",
|
||||
"severity_cs": "",
|
||||
"severity_ci": "",
|
||||
"log_file": (log_file, "sample.log"),
|
||||
},
|
||||
content_type="multipart/form-data",
|
||||
)
|
||||
log_file.close()
|
||||
|
||||
html = convert_response.data.decode("utf-8")
|
||||
marker = "/download/"
|
||||
start = html.index(marker) + len(marker)
|
||||
end = html.index('"', start)
|
||||
result_id = html[start:end]
|
||||
metadata_path = Path(app.config["OUTPUT_DIRECTORY"]) / f"{result_id}.json"
|
||||
|
||||
download_response = client.get(f"/download/{result_id}")
|
||||
download_response.close()
|
||||
convert_response.close()
|
||||
|
||||
assert not metadata_path.exists()
|
||||
|
||||
|
||||
def test_cleanup_on_startup_removes_expired_outputs(tmp_path):
|
||||
output_dir = tmp_path / "startup-cleanup-outputs"
|
||||
output_dir.mkdir(parents=True)
|
||||
result_id = "expired-result"
|
||||
file_path = output_dir / f"{result_id}.csv"
|
||||
metadata_path = output_dir / f"{result_id}.json"
|
||||
file_path.write_text("header\nvalue\n", encoding="utf-8")
|
||||
metadata_path.write_text(
|
||||
json.dumps(
|
||||
{
|
||||
"result_id": result_id,
|
||||
"file_path": str(file_path),
|
||||
"download_name": "waf-report.csv",
|
||||
"mimetype": "text/csv; charset=utf-8",
|
||||
}
|
||||
),
|
||||
encoding="utf-8",
|
||||
)
|
||||
old_timestamp = 946684800
|
||||
file_path.touch()
|
||||
metadata_path.touch()
|
||||
Path(file_path).touch()
|
||||
import os
|
||||
|
||||
os.utime(file_path, (old_timestamp, old_timestamp))
|
||||
os.utime(metadata_path, (old_timestamp, old_timestamp))
|
||||
|
||||
class StartupCleanupConfig:
|
||||
TESTING = True
|
||||
SECRET_KEY = "test-secret"
|
||||
MAX_CONTENT_LENGTH = 100 * 1024 * 1024
|
||||
PREVIEW_RECORD_LIMIT = 5
|
||||
OUTPUT_DIRECTORY = output_dir
|
||||
OUTPUT_RETENTION_HOURS = 1
|
||||
CLEANUP_ON_STARTUP = True
|
||||
CLEANUP_AFTER_DOWNLOAD = False
|
||||
|
||||
create_app(StartupCleanupConfig)
|
||||
|
||||
assert not file_path.exists()
|
||||
assert not metadata_path.exists()
|
||||
|
||||
|
||||
def test_default_upload_limit_is_100_mib(app):
|
||||
assert app.config["MAX_CONTENT_LENGTH"] == 100 * 1024 * 1024
|
||||
|
||||
|
||||
Reference in New Issue
Block a user