From 617c9106cc1cb5995e426c40cac6f51494d17bb6 Mon Sep 17 00:00:00 2001 From: Gabriel Indik Date: Wed, 21 Feb 2024 15:04:27 -0500 Subject: [PATCH] Add unit tests for blobs Signed-off-by: Gabriel Indik --- test/handlers/blobs.test.ts | 85 +++++++++++++++++++++++++++++++++++++ 1 file changed, 85 insertions(+) create mode 100644 test/handlers/blobs.test.ts diff --git a/test/handlers/blobs.test.ts b/test/handlers/blobs.test.ts new file mode 100644 index 0000000..bb08a58 --- /dev/null +++ b/test/handlers/blobs.test.ts @@ -0,0 +1,85 @@ +// Copyright © 2024 Kaleido, Inc. +// +// SPDX-License-Identifier: Apache-2.0 +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +import stream from 'stream'; +import { expect } from 'chai'; +import * as blobs from '../../src/handlers/blobs'; + +const SAMPLE_TEXT_FILE_CONTENTS = 'This is the contents of the sample file'; +const SAMPLE_TEXT_FILE_CONTENTS_HASH = '4791654392ed1850e18b594c5731187802451a16f5beeaad19f5efcb708b082a'; +const SAMPLE_TEXT_FILE_PATH = '/other/my_test.txt'; + +describe('blobs', () => { + + it('Store blob', async () => { + let readableStream = new stream.PassThrough(); + readableStream.write(SAMPLE_TEXT_FILE_CONTENTS); + readableStream.end(); + const metadata = await blobs.storeBlob({ + key: 'file', + name: 'test.txt', + readableStream + }, SAMPLE_TEXT_FILE_PATH); + expect(metadata.hash).to.equal(SAMPLE_TEXT_FILE_CONTENTS_HASH); + expect(metadata.size).to.equal(SAMPLE_TEXT_FILE_CONTENTS.length); + }); + + it('Retreive blob', async () => { + const readStream = await blobs.retrieveBlob(SAMPLE_TEXT_FILE_PATH); + await new Promise(resolve => { + let contents = ''; + readStream.on('data', data => { + contents += data; + }).on('end', () => { + expect(contents).to.equal(SAMPLE_TEXT_FILE_CONTENTS); + resolve(); + }); + }); + }); + + it('Retreive blob metadata', async () => { + const metadata = await blobs.retrieveMetadata(SAMPLE_TEXT_FILE_PATH); + expect(metadata.hash).to.equal(SAMPLE_TEXT_FILE_CONTENTS_HASH); + expect(metadata.size).to.equal(SAMPLE_TEXT_FILE_CONTENTS.length); + }); + + it('Delete blob', async () => { + await blobs.deleteBlob(SAMPLE_TEXT_FILE_PATH); + }); + + it('Should not return deleted blob', async () => { + let exceptionHandled = false; + try { + await blobs.retrieveBlob(SAMPLE_TEXT_FILE_PATH); + } catch (err: any) { + expect(err.responseCode).to.equal(404); + exceptionHandled = true; + } + expect(exceptionHandled).to.equal(true); + }); + + it('Should not return deleted blob metadata', async () => { + let exceptionHandled = false; + try { + await blobs.retrieveMetadata(SAMPLE_TEXT_FILE_PATH); + } catch (err: any) { + expect(err.responseCode).to.equal(404); + exceptionHandled = true; + } + expect(exceptionHandled).to.equal(true); + }); + +});