|
| 1 | +import io |
| 2 | +import zipfile |
| 3 | + |
| 4 | +from checks.remotesettings.attachments_bundles import run |
| 5 | + |
| 6 | + |
| 7 | +COLLECTION_URL = "/buckets/{}/collections/{}" |
| 8 | +RECORDS_URL = "/buckets/{}/collections/{}/records" |
| 9 | +CHANGESET_URL = "/buckets/{}/collections/{}/changeset" |
| 10 | + |
| 11 | + |
| 12 | +def build_zip(num_files=3): |
| 13 | + zip_buffer = io.BytesIO() |
| 14 | + with zipfile.ZipFile(zip_buffer, "w", zipfile.ZIP_DEFLATED) as zip_file: |
| 15 | + for i in range(num_files): |
| 16 | + file_name = f"fake_file_{i}.txt" |
| 17 | + zip_file.writestr(file_name, 1024 * "x") |
| 18 | + return zip_buffer.getvalue() |
| 19 | + |
| 20 | + |
| 21 | +async def test_negative(mock_responses, mock_aioresponses): |
| 22 | + server_url = "http://fake.local/v1" |
| 23 | + mock_responses.get( |
| 24 | + server_url + "/", |
| 25 | + payload={ |
| 26 | + "capabilities": { |
| 27 | + "attachments": {"base_url": "http://cdn/"}, |
| 28 | + "signer": { |
| 29 | + "resources": [ |
| 30 | + { |
| 31 | + "source": {"bucket": "main-workspace", "collection": None}, |
| 32 | + "preview": {"bucket": "main-preview", "collection": None}, |
| 33 | + "destination": {"bucket": "main", "collection": None}, |
| 34 | + } |
| 35 | + ] |
| 36 | + }, |
| 37 | + } |
| 38 | + }, |
| 39 | + ) |
| 40 | + may8_ts = 389664061000 |
| 41 | + may8_http = "Mon, 08 May 1982 00:01:01 GMT" |
| 42 | + may8_iso = "1982-05-08T00:01:01+00:00" |
| 43 | + |
| 44 | + changes_url = server_url + RECORDS_URL.format("monitor", "changes") |
| 45 | + mock_responses.get( |
| 46 | + changes_url, |
| 47 | + payload={ |
| 48 | + "data": [ |
| 49 | + { |
| 50 | + "id": "abc", |
| 51 | + "bucket": "main", |
| 52 | + "collection": "missing", |
| 53 | + "last_modified": may8_ts, |
| 54 | + }, |
| 55 | + { |
| 56 | + "id": "efg", |
| 57 | + "bucket": "main", |
| 58 | + "collection": "ok", |
| 59 | + "last_modified": may8_ts, |
| 60 | + }, |
| 61 | + { |
| 62 | + "id": "hij", |
| 63 | + "bucket": "main", |
| 64 | + "collection": "badzip", |
| 65 | + "last_modified": may8_ts, |
| 66 | + }, |
| 67 | + { |
| 68 | + "id": "klm", |
| 69 | + "bucket": "main", |
| 70 | + "collection": "outdated", |
| 71 | + "last_modified": may8_ts + 24 * 3600 * 1000 + 60 * 1000, |
| 72 | + }, |
| 73 | + { |
| 74 | + "id": "nop", |
| 75 | + "bucket": "main", |
| 76 | + "collection": "late", |
| 77 | + "last_modified": may8_ts + 600 * 1000, |
| 78 | + }, |
| 79 | + { |
| 80 | + "id": "qrs", |
| 81 | + "bucket": "main", |
| 82 | + "collection": "no-bundle", |
| 83 | + "last_modified": may8_ts, |
| 84 | + }, |
| 85 | + ] |
| 86 | + }, |
| 87 | + ) |
| 88 | + |
| 89 | + for cid in ("missing", "ok", "badzip", "outdated", "late", "no-bundle"): |
| 90 | + mock_responses.get( |
| 91 | + server_url + COLLECTION_URL.format("main-workspace", cid), |
| 92 | + payload={ |
| 93 | + "data": { |
| 94 | + "id": cid, |
| 95 | + "bucket": "main-workspace", |
| 96 | + "attachment": {"bundle": cid != "no-bundle"}, |
| 97 | + } |
| 98 | + }, |
| 99 | + ) |
| 100 | + |
| 101 | + mock_aioresponses.get("http://cdn/bundles/main--missing.zip", status=404) |
| 102 | + mock_aioresponses.get( |
| 103 | + "http://cdn/bundles/main--ok.zip", |
| 104 | + body=build_zip(), |
| 105 | + headers={"Last-Modified": may8_http}, |
| 106 | + ) |
| 107 | + mock_aioresponses.get( |
| 108 | + "http://cdn/bundles/main--outdated.zip", |
| 109 | + body=build_zip(num_files=6), |
| 110 | + headers={"Last-Modified": may8_http}, |
| 111 | + ) |
| 112 | + mock_aioresponses.get( |
| 113 | + "http://cdn/bundles/main--late.zip", |
| 114 | + body=build_zip(num_files=6), |
| 115 | + headers={"Last-Modified": may8_http}, |
| 116 | + ) |
| 117 | + mock_aioresponses.get( |
| 118 | + "http://cdn/bundles/main--badzip.zip", |
| 119 | + body=b"boom", |
| 120 | + headers={"Last-Modified": may8_http}, |
| 121 | + ) |
| 122 | + |
| 123 | + status, data = await run(server_url, auth="") |
| 124 | + |
| 125 | + assert status is False |
| 126 | + assert data == { |
| 127 | + "main/badzip": {"status": "bad zip"}, |
| 128 | + "main/missing": {"status": "missing"}, |
| 129 | + "main/ok": { |
| 130 | + "status": "ok", |
| 131 | + "attachments": 3, |
| 132 | + "collection_timestamp": "1982-05-08T00:01:01+00:00", |
| 133 | + "publication_timestamp": may8_iso, |
| 134 | + "size": 373, |
| 135 | + }, |
| 136 | + "main/late": { |
| 137 | + "status": "ok", |
| 138 | + "attachments": 6, |
| 139 | + "collection_timestamp": "1982-05-08T00:11:01+00:00", |
| 140 | + "publication_timestamp": may8_iso, |
| 141 | + "size": 724, |
| 142 | + }, |
| 143 | + "main/outdated": { |
| 144 | + "attachments": 6, |
| 145 | + "collection_timestamp": "1982-05-09T00:02:01+00:00", |
| 146 | + "publication_timestamp": may8_iso, |
| 147 | + "size": 724, |
| 148 | + "status": "outdated", |
| 149 | + }, |
| 150 | + } |
0 commit comments