|
3 | 3 | """ |
4 | 4 |
|
5 | 5 | from copy import copy, deepcopy |
6 | | -import json |
| 6 | +from json import dumps as json_dumps |
7 | 7 | from os import environ |
8 | 8 | from unittest import skip, TestCase |
9 | 9 | from uuid import uuid4 |
10 | 10 |
|
11 | 11 | from archivist.archivist import Archivist |
| 12 | +from archivist.assets import BEHAVIOURS |
12 | 13 | from archivist.logger import set_logger |
13 | 14 | from archivist.proof_mechanism import ProofMechanism |
14 | 15 |
|
|
27 | 28 | "some_custom_attribute": "value", |
28 | 29 | } |
29 | 30 |
|
| 31 | +ASSET_NAME = "Telephone with 2 attachments - one bad or not scanned 2022-03-01" |
| 32 | +REQUEST_EXISTS_ATTACHMENTS = { |
| 33 | + "signature": { |
| 34 | + "attributes": { |
| 35 | + "arc_display_name": ASSET_NAME, |
| 36 | + "arc_namespace": "namespace", |
| 37 | + }, |
| 38 | + }, |
| 39 | + "behaviours": BEHAVIOURS, |
| 40 | + "proof_mechanism": ProofMechanism.SIMPLE_HASH.name, |
| 41 | + "attributes": { |
| 42 | + "arc_firmware_version": "1.0", |
| 43 | + "arc_serial_number": "vtl-x4-07", |
| 44 | + "arc_description": "Traffic flow control light at A603 North East", |
| 45 | + "arc_display_type": "Traffic light with violation camera", |
| 46 | + "some_custom_attribute": "value", |
| 47 | + }, |
| 48 | + "attachments": [ |
| 49 | + { |
| 50 | + "filename": "functests/test_resources/telephone.jpg", |
| 51 | + "content_type": "image/jpg", |
| 52 | + }, |
| 53 | + { |
| 54 | + "url": "https://secure.eicar.org/eicarcom2.zip", |
| 55 | + "content_type": "application/zip", |
| 56 | + }, |
| 57 | + ], |
| 58 | +} |
| 59 | + |
30 | 60 |
|
31 | 61 | class TestAssetCreate(TestCase): |
32 | 62 | """ |
@@ -140,7 +170,7 @@ def test_asset_create_event(self): |
140 | 170 | # get identity of first asset |
141 | 171 | identity = None |
142 | 172 | for asset in self.arch.assets.list(): |
143 | | - print("asset", json.dumps(asset, sort_keys=True, indent=4)) |
| 173 | + print("asset", json_dumps(asset, sort_keys=True, indent=4)) |
144 | 174 | identity = asset["identity"] |
145 | 175 | break |
146 | 176 |
|
@@ -176,4 +206,80 @@ def test_asset_create_event(self): |
176 | 206 | event = self.arch.events.create( |
177 | 207 | identity, props=props, attrs=attrs, confirm=True |
178 | 208 | ) |
179 | | - print("event", json.dumps(event, sort_keys=True, indent=4)) |
| 209 | + print("event", json_dumps(event, sort_keys=True, indent=4)) |
| 210 | + |
| 211 | + |
| 212 | +class TestAssetCreateIfNotExists(TestCase): |
| 213 | + """ |
| 214 | + Test Archivist Asset CreateIfNotExists method |
| 215 | + """ |
| 216 | + |
| 217 | + maxDiff = None |
| 218 | + |
| 219 | + def setUp(self): |
| 220 | + with open(environ["TEST_AUTHTOKEN_FILENAME"], encoding="utf-8") as fd: |
| 221 | + auth = fd.read().strip() |
| 222 | + self.arch = Archivist( |
| 223 | + environ["TEST_ARCHIVIST"], auth, verify=False, max_time=300 |
| 224 | + ) |
| 225 | + |
| 226 | + def tearDown(self): |
| 227 | + self.arch = None |
| 228 | + |
| 229 | + def test_asset_create_if_not_exists_with_bad_attachment(self): |
| 230 | + """ |
| 231 | + Test asset creation if not exists - check attachment for scanned status. |
| 232 | +
|
| 233 | + Because we use create_if_not_exists the asset and attachments will persist. |
| 234 | +
|
| 235 | + The test checks the scanned timestamp and checks scanned status. |
| 236 | + The first attachment should return OK after 24 hours and the second attachment |
| 237 | + should return bad after 24 hours. |
| 238 | +
|
| 239 | + """ |
| 240 | + asset, existed = self.arch.assets.create_if_not_exists( |
| 241 | + REQUEST_EXISTS_ATTACHMENTS, |
| 242 | + confirm=True, |
| 243 | + ) |
| 244 | + print("asset", json_dumps(asset, indent=4)) |
| 245 | + print("existed", existed) |
| 246 | + |
| 247 | + # first attachment is ok.... |
| 248 | + attachment_id = asset["attributes"]["arc_attachments"][0][ |
| 249 | + "arc_attachment_identity" |
| 250 | + ] |
| 251 | + info = self.arch.attachments.info( |
| 252 | + attachment_id, |
| 253 | + asset_or_event_id=asset["identity"], |
| 254 | + ) |
| 255 | + print("info attachment1", json_dumps(info, indent=4)) |
| 256 | + timestamp = info["scanned_timestamp"] |
| 257 | + if timestamp: |
| 258 | + print(attachment_id, "scanned last at", timestamp) |
| 259 | + print(attachment_id, "scanned status", info["scanned_status"]) |
| 260 | + print(attachment_id, "scanned reason", info["scanned_reason"]) |
| 261 | + self.assertEqual( |
| 262 | + info["scanned_status"], |
| 263 | + "SCANNED_OK", |
| 264 | + msg="First attachment is not clean", |
| 265 | + ) |
| 266 | + |
| 267 | + # second attachment is bad when scanned.... |
| 268 | + attachment_id = asset["attributes"]["arc_attachments"][1][ |
| 269 | + "arc_attachment_identity" |
| 270 | + ] |
| 271 | + info = self.arch.attachments.info( |
| 272 | + attachment_id, |
| 273 | + asset_or_event_id=asset["identity"], |
| 274 | + ) |
| 275 | + print("info attachment1", json_dumps(info, indent=4)) |
| 276 | + timestamp = info["scanned_timestamp"] |
| 277 | + if timestamp: |
| 278 | + print(attachment_id, "scanned last at", timestamp) |
| 279 | + print(attachment_id, "scanned status", info["scanned_status"]) |
| 280 | + print(attachment_id, "scanned reason", info["scanned_reason"]) |
| 281 | + self.assertEqual( |
| 282 | + info["scanned_status"], |
| 283 | + "SCANNED_BAD", |
| 284 | + msg="First attachment should not be clean", |
| 285 | + ) |
0 commit comments