def test_store_file(self): db_area = self.create_upload_area() area = UploadArea(uuid=db_area.uuid) filename = "some.json" content_type = 'application/json; dcp-type="metadata/sample"' content = "exquisite corpse" file = area.store_file(filename, content=content, content_type=content_type) s3_key = f"{db_area.uuid}/some.json" s3_etag = "18f17fbfdd21cf869d664731e10d4ffd" o1 = self.upload_bucket.Object(s3_key) o1.load() self.assertEqual( { 'upload_area_id': db_area.uuid, 'name': 'some.json', 'size': 16, 'last_modified': o1.last_modified.isoformat(), 'content_type': 'application/json; dcp-type="metadata/sample"', 'url': f"s3://{self.upload_config.bucket_name}/{db_area.uuid}/some.json", 'checksums': { "crc32c": "FE9ADA52", "s3_etag": s3_etag, "sha1": "b1b101e21cf9cf8a4729da44d7818f935eec0ce8", "sha256": "29f5572dfbe07e1db9422a4c84e3f9e455aab9ac596f0bf3340be17841f26f70" } }, file.info()) obj = self.upload_bucket.Object(f"{db_area.uuid}/some.json") self.assertEqual("exquisite corpse".encode('utf8'), obj.get()['Body'].read()) db_file = self.db.query(DbFile).filter( DbFile.s3_key == s3_key, DbFile.s3_etag == s3_etag).one() self.assertEqual(16, db_file.size) self.assertEqual(db_area.id, db_file.upload_area_id) self.assertEqual("some.json", db_file.name)
def test_format_and_send_notification(self, mock_send_notification): area_uuid = str(uuid.uuid4()) upload_area = UploadArea(area_uuid) upload_area.update_or_create() upload_area._db_load() file = upload_area.store_file("test_file_name", "test_file_content", "application/json; dcp-type=data") ingest_notifier = IngestNotifier("file_uploaded", file_id=file.db_id) test_payload = { 'names': "[test_file_name]", 'upload_area_id': area_uuid } notification_id = ingest_notifier.format_and_send_notification( test_payload) record = UploadDB().get_pg_record("notification", notification_id, column="id") self.assertEqual(record['status'], "DELIVERED") self.assertEqual(record['file_id'], file.db_id) self.assertEqual(record['payload'], test_payload)