@itwin/object-storage-tests-backend
Version:
Tests for generic storage packages
237 lines • 15.3 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
/*---------------------------------------------------------------------------------------------
* Copyright (c) Bentley Systems, Incorporated. All rights reserved.
* See LICENSE.md in the project root for license terms and full copyright notice.
*--------------------------------------------------------------------------------------------*/
const path = require("path");
const chai_1 = require("chai");
const chaiAsPromised = require("chai-as-promised");
const object_storage_core_1 = require("@itwin/object-storage-core");
const Config_1 = require("./Config");
const Global_test_1 = require("./Global.test");
const DownloadTests_1 = require("./test-templates/DownloadTests");
const UploadTests_1 = require("./test-templates/UploadTests");
const utils_1 = require("./utils");
chai_1.use(chaiAsPromised);
const { clientStorage, serverStorage } = Config_1.config;
describe(`${object_storage_core_1.ClientStorage.name}: ${clientStorage.constructor.name}`, () => {
describe("PresignedUrlProvider", () => {
describe(`${clientStorage.upload.name}() & ${serverStorage.getUploadUrl.name}()`, () => {
it("should fail to upload to URL if specified path contains empty file", async () => {
const emptyFilePath = await Global_test_1.testLocalFileManager.createAndWriteFile("test-empty-file.txt");
const uploadPromise = clientStorage.upload({
url: "test-url",
data: emptyFilePath,
});
await chai_1.expect(uploadPromise).to.eventually.be.rejectedWith(Error, "Provided path is an empty file.");
});
it("should upload a file from buffer to URL", async () => {
await UploadTests_1.testUploadFromBufferToUrl(clientStorage);
});
it("should upload a file from stream to URL", async () => {
await UploadTests_1.testUploadFromStreamToUrl(clientStorage);
});
it("should upload a file from path to URL", async () => {
const contentBuffer = Buffer.from("test-url-upload-content");
const fileToUploadPath = await Global_test_1.testLocalFileManager.createAndWriteFile("test-client-url-upload-metadata.txt", contentBuffer);
await UploadTests_1.testUploadToUrl({
storageUnderTest: clientStorage,
dataToUpload: fileToUploadPath,
dataToAssert: contentBuffer,
});
});
it("should upload a file with relative dir from buffer to URL", async () => {
await UploadTests_1.testUploadWithRelativeDirFromBufferToUrl(clientStorage);
});
it("should upload a file with relative dir from stream to URL", async () => {
await UploadTests_1.testUploadWithRelativeDirFromStreamToUrl(clientStorage);
});
it("should upload a file with relative dir from path to URL", async () => {
const contentBuffer = Buffer.from("test-url-upload-content");
const fileToUploadPath = await Global_test_1.testLocalFileManager.createAndWriteFile("test-client-url-upload-metadata.txt", contentBuffer);
await UploadTests_1.testUploadToUrlWithRelativeDir({
storageUnderTest: clientStorage,
dataToUpload: fileToUploadPath,
dataToAssert: contentBuffer,
});
});
it("should upload a file with metadata from buffer to URL", async () => {
await UploadTests_1.testUploadWithMetadataFromBufferToUrl(clientStorage);
});
it("should upload a file with metadata from stream to URL", async () => {
await UploadTests_1.testUploadWithMetadataFromStreamToUrl(clientStorage);
});
it("should upload a file with metadata dir from path to URL", async () => {
const contentBuffer = Buffer.from("test-url-upload-content");
const fileToUploadPath = await Global_test_1.testLocalFileManager.createAndWriteFile("test-client-url-upload-metadata.txt", contentBuffer);
await UploadTests_1.testUploadToUrlWithMetadata({
storageUnderTest: clientStorage,
dataToUpload: fileToUploadPath,
dataToAssert: contentBuffer,
});
});
});
describe(`${clientStorage.download.name}() & ${serverStorage.getDownloadUrl.name}()`, () => {
it(`should download a file to buffer from URL`, async () => {
await DownloadTests_1.testDownloadFromUrlToBuffer(clientStorage);
});
it(`should download a file to stream from URL`, async () => {
await DownloadTests_1.testDownloadFromUrlToStream(clientStorage);
});
it(`should download a file to path from URL`, async () => {
const contentBuffer = Buffer.from("test-download-from-url-to-path");
const testDownloadPath = await Global_test_1.testLocalFileManager.getDownloadsDir();
const testDirectory = await Global_test_1.testDirectoryManager.createNew();
const uploadedFile = await testDirectory.uploadFile({ objectName: "file-to-download-from-url.txt" }, contentBuffer, undefined);
const downloadUrl = await serverStorage.getDownloadUrl(uploadedFile);
const response = await clientStorage.download({
url: downloadUrl,
transferType: "local",
localPath: `${testDownloadPath}/download-url.txt`,
});
await utils_1.assertLocalFile(response, contentBuffer);
});
});
});
describe("TransferConfigProvider", () => {
describe(`${clientStorage.upload.name}() & ${serverStorage.getUploadConfig.name}()`, () => {
it("should fail to upload with config if specified path contains empty file", async () => {
const emptyFilePath = await Global_test_1.testLocalFileManager.createAndWriteFile("test-empty-file.txt");
const uploadPromise = clientStorage.upload({
data: emptyFilePath,
reference: {
baseDirectory: "test-directory",
objectName: "test-object-name",
},
transferConfig: {
baseUrl: "test-url",
expiration: new Date(),
},
});
await chai_1.expect(uploadPromise).to.eventually.be.rejectedWith(Error, "Provided path is an empty file.");
});
it(`should upload a file from buffer using transfer config`, async () => {
await UploadTests_1.testUploadFromBufferWithConfig(clientStorage);
});
it(`should upload a file from stream using transfer config`, async () => {
await UploadTests_1.testUploadFromStreamWithConfig(clientStorage);
});
it(`should upload a file from path using transfer config`, async () => {
const buffer = Buffer.from(`${clientStorage.constructor.name}-test-upload-with-from-file-with-config`);
const fileToUploadPath = await Global_test_1.testLocalFileManager.createAndWriteFile("test-client-url-upload-metadata.txt", buffer);
await UploadTests_1.testUploadWithConfig({
storageUnderTest: clientStorage,
dataToUpload: fileToUploadPath,
dataToAssert: buffer,
});
});
it(`should upload a file with relative directory from buffer using transfer config`, async () => {
await UploadTests_1.testUploadWithRelativeDirFromBufferWithConfig(clientStorage);
});
it(`should upload a file with relative directory from stream using transfer config`, async () => {
await UploadTests_1.testUploadWithRelativeDirFromStreamWithConfig(clientStorage);
});
it(`should upload a file with relative directory from path using transfer config`, async () => {
const buffer = Buffer.from(`${clientStorage.constructor.name}-test-upload-with-relative-dir-with-from-file-with-config`);
const fileToUploadPath = await Global_test_1.testLocalFileManager.createAndWriteFile("test-client-url-upload-metadata.txt", buffer);
await UploadTests_1.testUploadWithRelativeDirWithConfig({
storageUnderTest: clientStorage,
dataToUpload: fileToUploadPath,
dataToAssert: buffer,
});
});
it(`should upload a file from buffer with metadata using transfer config`, async () => {
await UploadTests_1.testUploadWithMetadataFromBufferWithConfig(clientStorage);
});
it(`should upload a file from stream with metadata using transfer config`, async () => {
await UploadTests_1.testUploadWithMetadataFromStreamWithConfig(clientStorage);
});
it(`should upload a file from path with metadata using transfer config`, async () => {
const buffer = Buffer.from(`${clientStorage.constructor.name}-test-upload-with-metadata-with-from-file-with-config`);
const fileToUploadPath = await Global_test_1.testLocalFileManager.createAndWriteFile("test-client-url-upload-metadata.txt", buffer);
await UploadTests_1.testUploadWithMetadataWithConfig({
storageUnderTest: clientStorage,
dataToUpload: fileToUploadPath,
dataToAssert: buffer,
});
});
});
describe(`${clientStorage.uploadInMultipleParts.name}() & ${serverStorage.getUploadConfig.name}()`, () => {
it("should fail to upload in multiple parts if specified path contains empty file", async () => {
const emptyFilePath = await Global_test_1.testLocalFileManager.createAndWriteFile("test-empty-file.txt");
const uploadPromise = clientStorage.uploadInMultipleParts({
data: emptyFilePath,
reference: {
baseDirectory: "test-directory",
objectName: "test-object-name",
},
transferConfig: {
baseUrl: "test-url",
expiration: new Date(),
},
});
await chai_1.expect(uploadPromise).to.eventually.be.rejectedWith(Error, "Provided path is an empty file.");
});
it(`should upload a file from stream in multiple parts`, async () => {
await UploadTests_1.testMultipartUploadFromStream(clientStorage);
});
it(`should upload a file from path in multiple parts`, async () => {
const buffer = Buffer.from(`${clientStorage.constructor.name}-test-multipart-upload-from-file`);
const fileToUploadPath = await Global_test_1.testLocalFileManager.createAndWriteFile("test-client-config-multipart-upload.txt", buffer);
await UploadTests_1.testMultipartUpload({
storageUnderTest: clientStorage,
dataToUpload: fileToUploadPath,
dataToAssert: buffer,
});
});
it(`should upload a file with relative directory from stream in multiple parts`, async () => {
await UploadTests_1.testMultipartUploadWithRelativeDirFromStream(clientStorage);
});
it(`should upload a file with relative directory from path in multiple parts`, async () => {
const buffer = Buffer.from(`${clientStorage.constructor.name}-test-multipart-upload-with-relative-dir-from-file`);
const fileToUploadPath = await Global_test_1.testLocalFileManager.createAndWriteFile("test-client-config-multipart-upload-relative-dir.txt", buffer);
await UploadTests_1.testMultipartUploadWithRelativeDir({
storageUnderTest: clientStorage,
dataToUpload: fileToUploadPath,
dataToAssert: buffer,
});
});
it(`should upload a file from stream with metadata in multiple parts`, async () => {
await UploadTests_1.testMultipartUploadWithMetadataFromStream(clientStorage);
});
it(`should upload a file from path with metadata in multiple parts`, async () => {
const buffer = Buffer.from(`${clientStorage.constructor.name}-test-multipart-upload-with-metadata-from-file`);
const fileToUploadPath = await Global_test_1.testLocalFileManager.createAndWriteFile("test-client-config-multipart-upload-metadata.txt", buffer);
await UploadTests_1.testMultipartUploadWithMetadata({
storageUnderTest: clientStorage,
dataToUpload: fileToUploadPath,
dataToAssert: buffer,
});
});
});
describe(`${clientStorage.download.name}() & ${serverStorage.getDownloadConfig.name}()`, () => {
it(`should download a file to buffer using transfer config`, async () => {
await DownloadTests_1.testDownloadToBufferWithConfig(clientStorage);
});
it(`should download a file to stream using transfer config`, async () => {
await DownloadTests_1.testDownloadToStreamWithConfig(clientStorage);
});
it(`should download a file to path using transfer config`, async () => {
const contentBuffer = Buffer.from("test-download-to-path-with-config");
const testDownloadPath = await Global_test_1.testLocalFileManager.getDownloadsDir();
const testDirectory = await Global_test_1.testDirectoryManager.createNew();
const uploadedFile = await testDirectory.uploadFile({ objectName: "file-to-download-with-config.txt" }, contentBuffer, undefined);
const downloadConfig = await serverStorage.getDownloadConfig(testDirectory.baseDirectory);
const response = await clientStorage.download({
reference: uploadedFile,
transferConfig: downloadConfig,
transferType: "local",
localPath: path.join(testDownloadPath, "download-config.txt"),
});
await utils_1.assertLocalFile(response, contentBuffer);
});
});
});
});
//# sourceMappingURL=ClientStorage.test.js.map