|
| 1 | +""" |
| 2 | +Tests for the storage API to check that temporary files are properly marked for deletion. |
| 3 | +""" |
| 4 | + |
| 5 | + |
| 6 | +import datetime as dt |
| 7 | +import json |
| 8 | + |
| 9 | +import pytest |
| 10 | +import pytz |
| 11 | +from aiohttp import FormData |
| 12 | +from configmanager import Config |
| 13 | + |
| 14 | +from aleph.model import ScheduledDeletion |
| 15 | +from aleph.model.hashes import get_value as read_gridfs_file |
| 16 | +from aleph.web import create_app |
| 17 | + |
| 18 | + |
| 19 | +async def check_scheduled_deletion( |
| 20 | + config: Config, file_hash: str, post_datetime: dt.datetime |
| 21 | +): |
| 22 | + scheduled_deletion = await ScheduledDeletion.collection.find_one( |
| 23 | + {"filename": file_hash} |
| 24 | + ) |
| 25 | + |
| 26 | + assert scheduled_deletion is not None |
| 27 | + |
| 28 | + # Check that the file is scheduled for deletion at least after |
| 29 | + # the expected interval. |
| 30 | + delete_interval = config.storage.delete_interval.value |
| 31 | + delete_by = scheduled_deletion["delete_by"] |
| 32 | + assert delete_by >= post_datetime + dt.timedelta(seconds=delete_interval) |
| 33 | + |
| 34 | + |
| 35 | +@pytest.mark.asyncio |
| 36 | +async def test_store_temporary_file(mock_config, test_db, aiohttp_client): |
| 37 | + """ |
| 38 | + Checks that the garbage collector schedules temporary files uploaded |
| 39 | + with /storage/add_file for deletion. |
| 40 | + """ |
| 41 | + |
| 42 | + app = create_app() |
| 43 | + app["config"] = mock_config |
| 44 | + client = await aiohttp_client(app) |
| 45 | + |
| 46 | + file_content = b"Some file I'd like to upload" |
| 47 | + |
| 48 | + data = FormData() |
| 49 | + data.add_field("file", file_content) |
| 50 | + |
| 51 | + post_datetime = pytz.utc.localize(dt.datetime.utcnow()) |
| 52 | + response = await client.post(f"/api/v0/storage/add_file", data=data) |
| 53 | + assert response.status == 200, await response.text() |
| 54 | + |
| 55 | + data = await response.json() |
| 56 | + assert data["status"] == "success" |
| 57 | + file_hash = data["hash"] |
| 58 | + |
| 59 | + db_content = await read_gridfs_file(file_hash) |
| 60 | + assert db_content == file_content |
| 61 | + |
| 62 | + await check_scheduled_deletion(mock_config, file_hash, post_datetime) |
| 63 | + |
| 64 | + |
| 65 | +@pytest.mark.asyncio |
| 66 | +async def test_store_temporary_json(mock_config, test_db, aiohttp_client): |
| 67 | + """ |
| 68 | + Checks that the garbage collector schedules temporary JSON files uploaded |
| 69 | + with /storage/add_json for deletion. |
| 70 | + """ |
| 71 | + |
| 72 | + app = create_app() |
| 73 | + app["config"] = mock_config |
| 74 | + client = await aiohttp_client(app) |
| 75 | + |
| 76 | + json_content = { |
| 77 | + "title": "A garbage collector for CCNs", |
| 78 | + "body": "Discover the new GC for Aleph CCNs. Deletes all the files, even useful ones!", |
| 79 | + } |
| 80 | + |
| 81 | + post_datetime = pytz.utc.localize(dt.datetime.utcnow()) |
| 82 | + response = await client.post(f"/api/v0/storage/add_json", json=json_content) |
| 83 | + assert response.status == 200, await response.text() |
| 84 | + |
| 85 | + data = await response.json() |
| 86 | + assert data["status"] == "success" |
| 87 | + file_hash = data["hash"] |
| 88 | + |
| 89 | + db_content = await read_gridfs_file(file_hash) |
| 90 | + assert json.loads(db_content) == json_content |
| 91 | + |
| 92 | + await check_scheduled_deletion(mock_config, file_hash, post_datetime) |
0 commit comments