@studyportals/sp-r2d2
Version:
A framework that contains various components used when developing projects that will be deployed via AWS λ.
355 lines • 15.3 kB
JavaScript
"use strict";
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
const mocha_1 = require("@testdeck/mocha");
const Moq = require("typemoq");
const chai_1 = require("chai");
const s3_adapter_class_1 = require("../../infrastructure/aws-services-adapters/s3-adapter.class");
let S3AdapterTest = class S3AdapterTest {
get s3ClientMock() {
return this._s3ClientMock;
}
get s3Client() {
return this.s3ClientMock.object;
}
get testInstanceMock() {
return this._testInstanceMock;
}
get testInstance() {
return this.testInstanceMock.object;
}
before() {
this._s3ClientMock = Moq.Mock.ofType();
this._testInstanceMock = Moq.Mock.ofType(s3_adapter_class_1.S3Adapter);
this.testInstanceMock.callBase = true;
this.testInstanceMock.setup((_) => _['client']).returns(() => this.s3Client);
}
getBucketName__ProvidedValue() {
const providedValue = 'SomeBucketName';
const testInstance = new s3_adapter_class_1.S3Adapter(providedValue);
chai_1.assert.equal(testInstance['bucketName'], providedValue);
}
getUseAccelerateEndpoint__ProvidedValue() {
const providedValue = 'SomeUseAccelerateEndpoint';
const testInstance = new s3_adapter_class_1.S3Adapter('', providedValue);
chai_1.assert.equal(testInstance['useAccelerateEndpoint'], providedValue);
}
getJSON__CorrectBucketNameSpecified() {
return __awaiter(this, void 0, void 0, function* () {
const bucketName = 'SomeBucketName';
let receivedParameters;
this.testInstanceMock.setup((_) => _['transformToString'](Moq.It.isAny()));
this.s3ClientMock
.setup((_) => _.send(Moq.It.isAny()))
.returns((data) => __awaiter(this, void 0, void 0, function* () {
receivedParameters = data.input;
return this.createValidGetObjectData();
}));
this.testInstanceMock.setup((_) => _['bucketName']).returns(() => bucketName);
yield this.testInstance.getJSON('');
chai_1.assert.equal(receivedParameters['Bucket'], bucketName);
});
}
getJSON__CorrectKeySpecified() {
return __awaiter(this, void 0, void 0, function* () {
const key = 'SomeKey';
let receivedParameters;
this.testInstanceMock.setup((_) => _['transformToString'](Moq.It.isAny()));
this.s3ClientMock
.setup((_) => _.send(Moq.It.isAny()))
.returns((data) => __awaiter(this, void 0, void 0, function* () {
receivedParameters = data.input;
return this.createValidGetObjectData();
}));
yield this.testInstance.getJSON(key);
chai_1.assert.equal(receivedParameters['Key'], key);
});
}
getJSON_Error_Propagated() {
return __awaiter(this, void 0, void 0, function* () {
const error = 'SomeError';
let receivedError;
this.s3ClientMock.setup((_) => _.send(Moq.It.isAny())).throws(error);
try {
yield this.testInstance.getJSON('');
}
catch (e) {
receivedError = e;
}
chai_1.assert.equal(receivedError, error);
});
}
getJSON_DataNotJson_Error() {
return __awaiter(this, void 0, void 0, function* () {
const data = this.createValidGetObjectData();
data['ContentType'] = '';
let errorCaught = false;
this.s3ClientMock
.setup((_) => _.send(Moq.It.isAny()))
.returns(() => __awaiter(this, void 0, void 0, function* () {
return data;
}));
try {
yield this.testInstance.getJSON('');
}
catch (e) {
errorCaught = true;
}
chai_1.assert.isTrue(errorCaught);
});
}
getJSON_DataNotJson_ContentTypeNotEnforced_DataProvided() {
return __awaiter(this, void 0, void 0, function* () {
const body = 'SomeBody';
const data = this.createValidGetObjectData();
data['Body'] = body;
data['ContentType'] = '';
this.testInstanceMock.setup((_) => _['transformToString'](Moq.It.isAny())).returns(() => body);
this.s3ClientMock.setup((_) => _.send(Moq.It.isAny())).returns(() => __awaiter(this, void 0, void 0, function* () { return data; }));
const receivedBody = yield this.testInstance.getJSON('', false);
chai_1.assert.equal(receivedBody, body);
});
}
getJSON_DataJson_BodyReturned() {
return __awaiter(this, void 0, void 0, function* () {
const body = 'SomeBody';
const data = this.createValidGetObjectData();
data['Body'] = body;
this.testInstanceMock.setup((_) => _['transformToString'](Moq.It.isAny())).returns(() => body);
this.s3ClientMock.setup((_) => _.send(Moq.It.isAny())).returns(() => __awaiter(this, void 0, void 0, function* () { return data; }));
const receivedBody = yield this.testInstance.getJSON('');
chai_1.assert.equal(receivedBody, body);
});
}
getJSON_BodyBuffer_StringGiven() {
return __awaiter(this, void 0, void 0, function* () {
const body = 'SomeBody';
const data = this.createValidGetObjectData();
data['Body'] = Buffer.from(body);
this.testInstanceMock.setup((_) => _['transformToString'](Moq.It.isAny())).returns(() => body);
this.s3ClientMock.setup((_) => _.send(Moq.It.isAny())).returns(() => __awaiter(this, void 0, void 0, function* () { return data; }));
const receivedBody = yield this.testInstance.getJSON('');
chai_1.assert.typeOf(receivedBody, 'string');
chai_1.assert.equal(receivedBody, body);
});
}
getJSON_BodyUndefined_EmptyObjectString() {
return __awaiter(this, void 0, void 0, function* () {
const data = this.createValidGetObjectData();
data['Body'] = undefined;
this.s3ClientMock.setup((_) => _.send(Moq.It.isAny())).returns(() => __awaiter(this, void 0, void 0, function* () { return data; }));
const receivedBody = yield this.testInstance.getJSON('');
chai_1.assert.equal(receivedBody, '{}');
});
}
delete__CorrectBucketNameSpecified() {
return __awaiter(this, void 0, void 0, function* () {
const bucketName = 'SomeBucketName';
let receivedParameters;
this.s3ClientMock
.setup((_) => _.send(Moq.It.isAny()))
.returns((data) => __awaiter(this, void 0, void 0, function* () {
receivedParameters = data.input;
return this.createValidGetObjectData();
}));
this.testInstanceMock.setup((_) => _['bucketName']).returns(() => bucketName);
yield this.testInstance.delete('');
chai_1.assert.equal(receivedParameters['Bucket'], bucketName);
});
}
delete__CorrectKeySpecified() {
return __awaiter(this, void 0, void 0, function* () {
const key = 'SomeKey';
let receivedParameters;
this.s3ClientMock
.setup((_) => _.send(Moq.It.isAny()))
.returns((data) => __awaiter(this, void 0, void 0, function* () {
receivedParameters = data.input;
return {};
}));
yield this.testInstance.delete(key);
chai_1.assert.equal(receivedParameters['Key'], key);
});
}
delete_Error_Propagated() {
return __awaiter(this, void 0, void 0, function* () {
const error = 'SomeError';
let receivedError;
this.s3ClientMock.setup((_) => _.send(Moq.It.isAny())).throws(error);
try {
yield this.testInstance.delete('');
}
catch (e) {
receivedError = e;
}
chai_1.assert.equal(receivedError, error);
});
}
putJSON__CorrectBucketNameSpecified() {
return __awaiter(this, void 0, void 0, function* () {
const bucketName = 'SomeBucketName';
let receivedParameters;
this.s3ClientMock
.setup((_) => _.send(Moq.It.isAny()))
.returns((data) => __awaiter(this, void 0, void 0, function* () {
receivedParameters = data.input;
return {};
}));
this.testInstanceMock.setup((_) => _['bucketName']).returns(() => bucketName);
yield this.testInstance.putJSON('', '');
chai_1.assert.equal(receivedParameters['Bucket'], bucketName);
});
}
putJSON__CorrectKeySpecified() {
return __awaiter(this, void 0, void 0, function* () {
const key = 'SomeKey';
let receivedParameters;
this.s3ClientMock
.setup((_) => _.send(Moq.It.isAny()))
.returns((data) => __awaiter(this, void 0, void 0, function* () {
receivedParameters = data.input;
return {};
}));
yield this.testInstance.putJSON(key, '');
chai_1.assert.equal(receivedParameters['Key'], key);
});
}
putJSON__CorrectContentTypeSpecified() {
return __awaiter(this, void 0, void 0, function* () {
const key = 'SomeKey';
let receivedParameters;
this.s3ClientMock
.setup((_) => _.send(Moq.It.isAny()))
.returns((data) => __awaiter(this, void 0, void 0, function* () {
receivedParameters = data.input;
return {};
}));
yield this.testInstance.putJSON(key, '');
chai_1.assert.equal(receivedParameters['ContentType'], 'application/json');
});
}
putJSON__CorrectBodySpecified() {
return __awaiter(this, void 0, void 0, function* () {
const body = 'SomeBody';
let receivedParameters;
this.s3ClientMock
.setup((_) => _.send(Moq.It.isAny()))
.returns((data) => __awaiter(this, void 0, void 0, function* () {
receivedParameters = data.input;
return {};
}));
yield this.testInstance.putJSON('', body);
chai_1.assert.equal(receivedParameters['Body'], body);
});
}
putJSON_Error_Propagated() {
return __awaiter(this, void 0, void 0, function* () {
const error = 'SomeError';
let receivedError;
this.s3ClientMock.setup((_) => _.send(Moq.It.isAny())).throws(error);
try {
yield this.testInstance.putJSON('', '');
}
catch (e) {
receivedError = e;
}
chai_1.assert.equal(receivedError, error);
});
}
createS3__Exists() {
const testInstance = new s3_adapter_class_1.S3Adapter('');
const result = testInstance['client'];
chai_1.assert.exists(result);
}
createS3__UseAccelerateEndpointSpecified() {
const useAccelerateEndpoint = 'UseAccelerateEndpoint';
const testInstance = new s3_adapter_class_1.S3Adapter('', useAccelerateEndpoint);
const result = testInstance['client'];
chai_1.assert.exists(result);
chai_1.assert.equal(result['config'].useAccelerateEndpoint, useAccelerateEndpoint);
}
createValidGetObjectData() {
return {
Body: '{}',
ContentType: 'application/json',
};
}
};
__decorate([
mocha_1.test
], S3AdapterTest.prototype, "getBucketName__ProvidedValue", null);
__decorate([
mocha_1.test
], S3AdapterTest.prototype, "getUseAccelerateEndpoint__ProvidedValue", null);
__decorate([
mocha_1.test
], S3AdapterTest.prototype, "getJSON__CorrectBucketNameSpecified", null);
__decorate([
mocha_1.test
], S3AdapterTest.prototype, "getJSON__CorrectKeySpecified", null);
__decorate([
mocha_1.test
], S3AdapterTest.prototype, "getJSON_Error_Propagated", null);
__decorate([
mocha_1.test
], S3AdapterTest.prototype, "getJSON_DataNotJson_Error", null);
__decorate([
mocha_1.test
], S3AdapterTest.prototype, "getJSON_DataNotJson_ContentTypeNotEnforced_DataProvided", null);
__decorate([
mocha_1.test
], S3AdapterTest.prototype, "getJSON_DataJson_BodyReturned", null);
__decorate([
mocha_1.test
], S3AdapterTest.prototype, "getJSON_BodyBuffer_StringGiven", null);
__decorate([
mocha_1.test
], S3AdapterTest.prototype, "getJSON_BodyUndefined_EmptyObjectString", null);
__decorate([
mocha_1.test
], S3AdapterTest.prototype, "delete__CorrectBucketNameSpecified", null);
__decorate([
mocha_1.test
], S3AdapterTest.prototype, "delete__CorrectKeySpecified", null);
__decorate([
mocha_1.test
], S3AdapterTest.prototype, "delete_Error_Propagated", null);
__decorate([
mocha_1.test
], S3AdapterTest.prototype, "putJSON__CorrectBucketNameSpecified", null);
__decorate([
mocha_1.test
], S3AdapterTest.prototype, "putJSON__CorrectKeySpecified", null);
__decorate([
mocha_1.test
], S3AdapterTest.prototype, "putJSON__CorrectContentTypeSpecified", null);
__decorate([
mocha_1.test
], S3AdapterTest.prototype, "putJSON__CorrectBodySpecified", null);
__decorate([
mocha_1.test
], S3AdapterTest.prototype, "putJSON_Error_Propagated", null);
__decorate([
mocha_1.test
], S3AdapterTest.prototype, "createS3__Exists", null);
__decorate([
mocha_1.test
], S3AdapterTest.prototype, "createS3__UseAccelerateEndpointSpecified", null);
S3AdapterTest = __decorate([
mocha_1.suite
], S3AdapterTest);
//# sourceMappingURL=s3-adapter.test.js.map