UNPKG

@itwin/object-storage-tests-backend

Version:

Tests for generic storage packages

358 lines 17.2 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.testMultipartUploadWithMetadata = exports.testMultipartUploadWithRelativeDir = exports.testMultipartUpload = exports.testMultipartUploadWithMetadataFromStream = exports.testMultipartUploadWithRelativeDirFromStream = exports.testMultipartUploadFromStream = exports.testUploadWithMetadataWithConfig = exports.testUploadWithRelativeDirWithConfig = exports.testUploadWithConfig = exports.testUploadWithMetadataFromStreamWithConfig = exports.testUploadWithMetadataFromBufferWithConfig = exports.testUploadWithRelativeDirFromStreamWithConfig = exports.testUploadWithRelativeDirFromBufferWithConfig = exports.testUploadFromStreamWithConfig = exports.testUploadFromBufferWithConfig = exports.testUploadToUrlWithMetadata = exports.testUploadToUrlWithRelativeDir = exports.testUploadToUrl = exports.testUploadWithMetadataFromStreamToUrl = exports.testUploadWithMetadataFromBufferToUrl = exports.testUploadWithRelativeDirFromStreamToUrl = exports.testUploadWithRelativeDirFromBufferToUrl = exports.testUploadFromStreamToUrl = exports.testUploadFromBufferToUrl = void 0; /*--------------------------------------------------------------------------------------------- * Copyright (c) Bentley Systems, Incorporated. All rights reserved. * See LICENSE.md in the project root for license terms and full copyright notice. *--------------------------------------------------------------------------------------------*/ const fs_1 = require("fs"); const object_storage_core_1 = require("@itwin/object-storage-core"); const Config_1 = require("../Config"); const Global_test_1 = require("../Global.test"); const utils_1 = require("../utils"); const { serverStorage } = Config_1.config; async function getTestStream(data) { // TODO: there are more simple ways to create a test stream than to write the // data to a file and then read from it. Currently the `streamToBuffer` method // does not work with streams created any other way. const path = await Global_test_1.testLocalFileManager.createAndWriteFile("temp-stream.txt", Buffer.from(data)); return fs_1.createReadStream(path); } async function testUploadFromBufferToUrl(storageUnderTest) { const buffer = Buffer.from(`${storageUnderTest.constructor.name}-test-upload-from-buffer-to-url`); return testUploadToUrl({ storageUnderTest, dataToUpload: buffer, dataToAssert: buffer, }); } exports.testUploadFromBufferToUrl = testUploadFromBufferToUrl; async function testUploadFromStreamToUrl(storageUnderTest) { const data = `${storageUnderTest.constructor.name}-test-upload-from-stream-to-url`; const stream = await getTestStream(data); return testUploadToUrl({ storageUnderTest, dataToUpload: stream, dataToAssert: Buffer.from(data), }); } exports.testUploadFromStreamToUrl = testUploadFromStreamToUrl; async function testUploadWithRelativeDirFromBufferToUrl(storageUnderTest) { const buffer = Buffer.from(`${storageUnderTest.constructor.name}-test-upload-from-buffer-to-url-relative-dir`); return testUploadToUrlWithRelativeDir({ storageUnderTest, dataToUpload: buffer, dataToAssert: buffer, }); } exports.testUploadWithRelativeDirFromBufferToUrl = testUploadWithRelativeDirFromBufferToUrl; async function testUploadWithRelativeDirFromStreamToUrl(storageUnderTest) { const data = `${storageUnderTest.constructor.name}-test-upload-from-stream-to-url-relative-dir`; const stream = await getTestStream(data); return testUploadToUrlWithRelativeDir({ storageUnderTest, dataToUpload: stream, dataToAssert: Buffer.from(data), }); } exports.testUploadWithRelativeDirFromStreamToUrl = testUploadWithRelativeDirFromStreamToUrl; async function testUploadWithMetadataFromBufferToUrl(storageUnderTest) { const buffer = Buffer.from(`${storageUnderTest.constructor.name}-test-upload-from-buffer-to-url-metadata`); return testUploadToUrlWithMetadata({ storageUnderTest, dataToUpload: buffer, dataToAssert: buffer, }); } exports.testUploadWithMetadataFromBufferToUrl = testUploadWithMetadataFromBufferToUrl; async function testUploadWithMetadataFromStreamToUrl(storageUnderTest) { const data = `${storageUnderTest.constructor.name}-test-upload-from-stream-to-url-metadata`; const stream = await getTestStream(data); return testUploadToUrlWithMetadata({ storageUnderTest, dataToUpload: stream, dataToAssert: Buffer.from(data), }); } exports.testUploadWithMetadataFromStreamToUrl = testUploadWithMetadataFromStreamToUrl; async function testUploadToUrl(params) { const testBaseDirectory = (await Global_test_1.testDirectoryManager.createNew()).baseDirectory; const reference = { baseDirectory: testBaseDirectory.baseDirectory, objectName: "test-upload-to-url", }; const uploadUrl = await serverStorage.getUploadUrl(reference); await callUrlUpload(params, uploadUrl, undefined); await utils_1.checkUploadedFileValidity(reference, params.dataToAssert); } exports.testUploadToUrl = testUploadToUrl; async function testUploadToUrlWithRelativeDir(params) { const testBaseDirectory = (await Global_test_1.testDirectoryManager.createNew()).baseDirectory; const reference = { baseDirectory: testBaseDirectory.baseDirectory, relativeDirectory: "relative-1/relative-2", objectName: "test-upload-to-url-relative-dir", }; const uploadUrl = await serverStorage.getUploadUrl(reference); await callUrlUpload(params, uploadUrl, undefined); await utils_1.checkUploadedFileValidity(reference, params.dataToAssert); } exports.testUploadToUrlWithRelativeDir = testUploadToUrlWithRelativeDir; async function testUploadToUrlWithMetadata(params) { const testDirectory = await Global_test_1.testDirectoryManager.createNew(); const reference = { baseDirectory: testDirectory.baseDirectory.baseDirectory, objectName: "test-upload-to-url-metadata", }; const metadata = { test: "test-metadata", }; const uploadUrl = await serverStorage.getUploadUrl(reference); await callUrlUpload(params, uploadUrl, metadata); await utils_1.checkUploadedFileValidity(reference, params.dataToAssert); await utils_1.queryAndAssertMetadata(reference, metadata); } exports.testUploadToUrlWithMetadata = testUploadToUrlWithMetadata; async function testUploadFromBufferWithConfig(storageUnderTest) { const buffer = Buffer.from(`${storageUnderTest.constructor.name}-test-upload-from-buffer-with-config`); return testUploadWithConfig({ storageUnderTest, dataToUpload: buffer, dataToAssert: buffer, }); } exports.testUploadFromBufferWithConfig = testUploadFromBufferWithConfig; async function testUploadFromStreamWithConfig(storageUnderTest) { const data = `${storageUnderTest.constructor.name}-test-upload-from-stream-with-config`; const stream = await getTestStream(data); return testUploadWithConfig({ storageUnderTest, dataToUpload: stream, dataToAssert: Buffer.from(data), }); } exports.testUploadFromStreamWithConfig = testUploadFromStreamWithConfig; async function testUploadWithRelativeDirFromBufferWithConfig(storageUnderTest) { const buffer = Buffer.from(`${storageUnderTest.constructor.name}-test-upload-with-relative-dir-from-buffer-with-config`); return testUploadWithRelativeDirWithConfig({ storageUnderTest, dataToUpload: buffer, dataToAssert: buffer, }); } exports.testUploadWithRelativeDirFromBufferWithConfig = testUploadWithRelativeDirFromBufferWithConfig; async function testUploadWithRelativeDirFromStreamWithConfig(storageUnderTest) { const data = `${storageUnderTest.constructor.name}-test-upload-with-relative-dir-from-stream-with-config`; const stream = await getTestStream(data); return testUploadWithRelativeDirWithConfig({ storageUnderTest, dataToUpload: stream, dataToAssert: Buffer.from(data), }); } exports.testUploadWithRelativeDirFromStreamWithConfig = testUploadWithRelativeDirFromStreamWithConfig; async function testUploadWithMetadataFromBufferWithConfig(storageUnderTest) { const buffer = Buffer.from(`${storageUnderTest.constructor.name}-test-upload-with-metadata-from-buffer-with-config`); return testUploadWithMetadataWithConfig({ storageUnderTest, dataToUpload: buffer, dataToAssert: buffer, }); } exports.testUploadWithMetadataFromBufferWithConfig = testUploadWithMetadataFromBufferWithConfig; async function testUploadWithMetadataFromStreamWithConfig(storageUnderTest) { const data = `${storageUnderTest.constructor.name}-test-upload-with-metadata-from-stream-with-config`; const stream = await getTestStream(data); return testUploadWithMetadataWithConfig({ storageUnderTest, dataToUpload: stream, dataToAssert: Buffer.from(data), }); } exports.testUploadWithMetadataFromStreamWithConfig = testUploadWithMetadataFromStreamWithConfig; async function testUploadWithConfig(params) { const testBaseDirectory = (await Global_test_1.testDirectoryManager.createNew()).baseDirectory; const reference = { baseDirectory: testBaseDirectory.baseDirectory, objectName: "test-upload-with-config", }; const uploadConfig = await serverStorage.getUploadConfig({ baseDirectory: testBaseDirectory.baseDirectory, }); await callConfigUpload(params, reference, uploadConfig, undefined); await utils_1.checkUploadedFileValidity(reference, params.dataToAssert); } exports.testUploadWithConfig = testUploadWithConfig; async function testUploadWithRelativeDirWithConfig(params) { const testBaseDirectory = (await Global_test_1.testDirectoryManager.createNew()).baseDirectory; const reference = { baseDirectory: testBaseDirectory.baseDirectory, relativeDirectory: "relative-1/relative-2", objectName: "test-upload-with-relative-dir-with-config", }; const uploadConfig = await serverStorage.getUploadConfig({ baseDirectory: testBaseDirectory.baseDirectory, }); await callConfigUpload(params, reference, uploadConfig, undefined); await utils_1.checkUploadedFileValidity(reference, params.dataToAssert); } exports.testUploadWithRelativeDirWithConfig = testUploadWithRelativeDirWithConfig; async function testUploadWithMetadataWithConfig(params) { const testBaseDirectory = (await Global_test_1.testDirectoryManager.createNew()).baseDirectory; const reference = { baseDirectory: testBaseDirectory.baseDirectory, objectName: "test-upload-with-metadata-with-config", }; const metadata = { test: "test-metadata", }; const uploadConfig = await serverStorage.getUploadConfig({ baseDirectory: testBaseDirectory.baseDirectory, }); await callConfigUpload(params, reference, uploadConfig, metadata); await utils_1.checkUploadedFileValidity(reference, params.dataToAssert); await utils_1.queryAndAssertMetadata(reference, metadata); } exports.testUploadWithMetadataWithConfig = testUploadWithMetadataWithConfig; async function testMultipartUploadFromStream(storageUnderTest) { const data = `${storageUnderTest.constructor.name}-test-upload-with-relative-dir-from-stream-with-config`; const stream = await getTestStream(data); return testMultipartUpload({ storageUnderTest, dataToUpload: stream, dataToAssert: Buffer.from(data), }); } exports.testMultipartUploadFromStream = testMultipartUploadFromStream; async function testMultipartUploadWithRelativeDirFromStream(storageUnderTest) { const data = `${storageUnderTest.constructor.name}-test-upload-with-relative-dir-from-stream-with-config`; const stream = await getTestStream(data); return testMultipartUploadWithRelativeDir({ storageUnderTest, dataToUpload: stream, dataToAssert: Buffer.from(data), }); } exports.testMultipartUploadWithRelativeDirFromStream = testMultipartUploadWithRelativeDirFromStream; async function testMultipartUploadWithMetadataFromStream(storageUnderTest) { const data = `${storageUnderTest.constructor.name}-test-upload-with-relative-dir-from-stream-with-config`; const stream = await getTestStream(data); return testMultipartUploadWithMetadata({ storageUnderTest, dataToUpload: stream, dataToAssert: Buffer.from(data), }); } exports.testMultipartUploadWithMetadataFromStream = testMultipartUploadWithMetadataFromStream; async function testMultipartUpload(params) { const testBaseDirectory = (await Global_test_1.testDirectoryManager.createNew()).baseDirectory; const reference = { baseDirectory: testBaseDirectory.baseDirectory, objectName: "test-multipart-upload", }; const uploadConfig = await serverStorage.getUploadConfig({ baseDirectory: testBaseDirectory.baseDirectory, }); await callUploadInMultipleParts(params, reference, uploadConfig, undefined); await utils_1.checkUploadedFileValidity(reference, params.dataToAssert); } exports.testMultipartUpload = testMultipartUpload; async function testMultipartUploadWithRelativeDir(params) { const testBaseDirectory = (await Global_test_1.testDirectoryManager.createNew()).baseDirectory; const reference = { baseDirectory: testBaseDirectory.baseDirectory, relativeDirectory: "relative-1/relative-2", objectName: "test-multipart-upload-with-relative-dir", }; const uploadConfig = await serverStorage.getUploadConfig({ baseDirectory: testBaseDirectory.baseDirectory, }); await callUploadInMultipleParts(params, reference, uploadConfig, undefined); await utils_1.checkUploadedFileValidity(reference, params.dataToAssert); } exports.testMultipartUploadWithRelativeDir = testMultipartUploadWithRelativeDir; async function testMultipartUploadWithMetadata(params) { const testBaseDirectory = (await Global_test_1.testDirectoryManager.createNew()).baseDirectory; const reference = { baseDirectory: testBaseDirectory.baseDirectory, objectName: "test-multipart-upload-with-metadata", }; const metadata = { test: "test-metadata", }; const uploadConfig = await serverStorage.getUploadConfig({ baseDirectory: testBaseDirectory.baseDirectory, }); await callUploadInMultipleParts(params, reference, uploadConfig, metadata); await utils_1.checkUploadedFileValidity(reference, params.dataToAssert); await utils_1.queryAndAssertMetadata(reference, metadata); } exports.testMultipartUploadWithMetadata = testMultipartUploadWithMetadata; function isClientTestCase(testCase) { return testCase.storageUnderTest instanceof object_storage_core_1.ClientStorage; } function isFrontendTestCase(testCase) { return testCase.storageUnderTest instanceof object_storage_core_1.FrontendStorage; } async function callUrlUpload(params, url, metadata) { if (isClientTestCase(params)) { return params.storageUnderTest.upload({ data: params.dataToUpload, url, metadata, }); } else if (isFrontendTestCase(params)) { return params.storageUnderTest.upload({ data: params.dataToUpload, url, metadata, }); } else { throw new Error("Invalid test case type."); } } async function callConfigUpload(params, reference, transferConfig, metadata) { if (isClientTestCase(params)) { return params.storageUnderTest.upload({ data: params.dataToUpload, reference, transferConfig, metadata, }); } else if (isFrontendTestCase(params)) { return params.storageUnderTest.upload({ data: params.dataToUpload, reference, transferConfig, metadata, }); } else { throw new Error("Invalid test case type."); } } async function callUploadInMultipleParts(params, reference, transferConfig, metadata) { if (isClientTestCase(params)) { return params.storageUnderTest.uploadInMultipleParts({ data: params.dataToUpload, reference, transferConfig, options: { metadata }, }); } else if (isFrontendTestCase(params)) { return params.storageUnderTest.uploadInMultipleParts({ data: params.dataToUpload, reference, transferConfig, options: { metadata }, }); } else { throw new Error("Invalid test case type."); } } //# sourceMappingURL=UploadTests.js.map