/* * @license * Copyright 2018 Brigham Young University * * 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 * as AWS from 'aws-sdk'; import { expect } from 'chai'; import * as fs from 'fs'; import 'mocha'; import * as sinon from 'sinon'; import * as s3Calls from '../../src/aws/s3-calls'; import * as util from '../../src/common/util'; describe('s3 calls module', () => { let sandbox: sinon.SinonSandbox; beforeEach(async () => { sandbox = sinon.sandbox.create(); }); afterEach(() => { sandbox.restore(); }); describe('uploadFileToS3', () => { let uploadStub: sinon.SinonStub; beforeEach(() => { uploadStub = sandbox.stub(); sandbox.stub(AWS, 'S3').returns({ upload: () => ({ promise: uploadStub }) }); }); it('should upload the file to s3', async () => { const createReadStreamStub = sandbox.stub(fs, 'createReadStream').resolves({}); uploadStub.resolves({}); const uploadData = await s3Calls.uploadFileToS3('FakePath', 'FakeBucket', 'FakePrefix', 'FakeFilename'); expect(createReadStreamStub.callCount).to.equal(1); expect(uploadStub.callCount).to.equal(1); expect(uploadData).to.deep.equal({}); }); }); describe('uploadDirectoryToS3', () => { let uploadStub: sinon.SinonStub; beforeEach(() => { uploadStub = sandbox.stub(); sandbox.stub(AWS, 'S3').returns({ upload: () => ({ promise: uploadStub }) }); }); it('should upload the directory to s3', async () => { const zipDirStub = sandbox.stub(util, 'zipDirectoryToFile').resolves(); const createReadStreamStub = sandbox.stub(fs, 'createReadStream').resolves({}); const unlinkSyncStub = sandbox.stub(fs, 'unlinkSync'); uploadStub.resolves({}); const uploadData = await s3Calls.uploadDirectoryToS3('FakePath', 'FakeBucket', 'FakePrefix', 'FakeFilename'); expect(zipDirStub.callCount).to.equal(1); expect(createReadStreamStub.callCount).to.equal(1); expect(uploadStub.callCount).to.equal(1); expect(unlinkSyncStub.callCount).to.equal(1); expect(uploadData).to.deep.equal({}); }); }); describe('deleteFilesByPrefix', () => { let listObjectsStub: sinon.SinonStub; let deleteObjectsStub: sinon.SinonStub; beforeEach(() => { listObjectsStub = sandbox.stub(); deleteObjectsStub = sandbox.stub(); sandbox.stub(AWS, 'S3').returns({ listObjectsV2: () => ({ promise: listObjectsStub }), deleteObjects: () => ({ promise: deleteObjectsStub }) }); }); it('should upload the directory to s3', async () => { listObjectsStub.resolves({ Contents: [ { Key: 'FakeKey1' } ] }); await s3Calls.deleteFilesByPrefix('FakeBucket', 'FakePrefix'); expect(listObjectsStub.callCount).to.equal(1); expect(deleteObjectsStub.callCount).to.equal(1); }); }); });