Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merge Prod Release into Master #75

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,7 @@ reports
*.map

# npm audit results
npm-audit.html
npm-audit.html

# Idea specific hidden files
.idea/**
2 changes: 1 addition & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"editor.defaultFormatter": "esbenp.prettier-vscode",
"eslint.packageManager": "npm",
"editor.codeActionsOnSave": {
"source.fixAll.eslint": true
"source.fixAll.eslint": "explicit"
},
"[javascript]": {
"editor.formatOnSave": true
Expand Down
1,911 changes: 1,763 additions & 148 deletions package-lock.json

Large diffs are not rendered by default.

8 changes: 6 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,14 @@
"tools-setup": "echo 'nothing to do for now'"
},
"dependencies": {
"@aws-sdk/client-eventbridge": "3.556.0",
"@aws-sdk/client-secrets-manager": "3.556.0",
"@aws-sdk/client-signer": "3.556.0",
"@aws-sdk/rds-signer": "3.556.0",
"aws-lambda": "1.0.6",
"aws-sdk": "2.1354.0",
"dateformat": "4.6.3",
"knex": "2.4.0",
"mysql2": "2.3.3",
"mysql2": "3.9.7",
"source-map-support": "0.5.19",
"winston": "3.3.3"
},
Expand All @@ -67,6 +70,7 @@
"@types/supertest": "2.0.10",
"@typescript-eslint/eslint-plugin": "5.3.1",
"@typescript-eslint/parser": "5.3.1",
"aws-sdk-client-mock": "4.0.0",
"commitlint-plugin-function-rules": "1.1.20",
"concurrently": "6.3.0",
"cross-env": "7.0.3",
Expand Down
4 changes: 2 additions & 2 deletions src/eventbridge/send.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { EventBridge } from 'aws-sdk';
import { EventBridge } from '@aws-sdk/client-eventbridge';
import { EventEntry } from './EventEntry';
import { Entries } from './Entries';
import { SendResponse } from './SendResponse';
Expand Down Expand Up @@ -34,7 +34,7 @@ const sendEvents = async (schedules: FacillitySchedules[]): Promise<SendResponse
logger.debug(`event about to be sent: ${JSON.stringify(params)}`);
// TODO Make the putEvents run in parallel?
// eslint-disable-next-line no-await-in-loop
const result = await eventbridge.putEvents(params).promise();
const result = await eventbridge.putEvents(params);
logger.info(`${result.Entries.length} ${result.Entries.length === 1 ? 'event' : 'events'} sent to eventbridge.`);
sendResponse.SuccessCount++;
} catch (error) {
Expand Down
4 changes: 2 additions & 2 deletions src/filterUtils.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { SecretsManager } from 'aws-sdk';
import { SecretsManager } from '@aws-sdk/client-secrets-manager';

async function getSecret(secretName: string): Promise<string[]> {
const secretsManager = new SecretsManager();
const secretValue = await secretsManager.getSecretValue({ SecretId: secretName }).promise();
const secretValue = await secretsManager.getSecretValue({ SecretId: secretName });
return secretValue.SecretString.split(',');
}
export { getSecret };
6 changes: 3 additions & 3 deletions src/wms/Database.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* eslint-disable @typescript-eslint/no-unsafe-assignment */
/* eslint-disable @typescript-eslint/no-unsafe-call */
import { Signer } from 'aws-sdk/clients/rds';
import { Signer } from '@aws-sdk/rds-signer';
import dateformat from 'dateformat';
import { knex, Knex } from 'knex';
import { getSecret } from '../filterUtils';
Expand Down Expand Up @@ -33,13 +33,13 @@ export class Database {
if (process.env.WMS_PASSWORD) {
config.password = process.env.WMS_PASSWORD;
} else {
const signer = new Signer();
const token = signer.getAuthToken({
const signer = new Signer({
region: process.env.AWS_REGION,
hostname: process.env.WMS_HOST,
port: parseInt(process.env.WMS_PORT, 10),
username: process.env.WMS_USER,
});
const token = signer.getAuthToken();

config.authPlugins = {
mysql_clear_password: () => () => token,
Expand Down
6 changes: 3 additions & 3 deletions tests/unit/database.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { knex, Knex } from 'knex';
import { mocked } from 'ts-jest/utils';
import { StaffSchedule } from '../../src/wms/Interfaces/StaffSchedule';
import { Database } from '../../src/wms/Database';
import { getSecret } from '../../src/filterUtils';
import { Database } from '../../src/wms/Database';
import { StaffSchedule } from '../../src/wms/Interfaces/StaffSchedule';

jest.mock('aws-sdk/clients/rds', () => {
jest.mock('@aws-sdk/rds-signer', () => {
const mSignerInstance = {
getAuthToken: jest.fn().mockReturnValue('I am a token!'),
};
Expand Down
59 changes: 22 additions & 37 deletions tests/unit/send.test.ts
Original file line number Diff line number Diff line change
@@ -1,61 +1,46 @@
import { EventBridge, Request } from 'aws-sdk';
import { mocked } from 'ts-jest/utils';
import { PutEventsResponse, PutEventsRequest, PutEventsResultEntry } from 'aws-sdk/clients/eventbridge';
import { mockClient } from 'aws-sdk-client-mock';
import {
EventBridgeClient, PutEventsCommand, PutEventsResultEntry,
} from '@aws-sdk/client-eventbridge';
import { SendResponse } from '../../src/eventbridge/SendResponse';
import { sendEvents } from '../../src/eventbridge/send';
import { FacillitySchedules } from '../../src/wms/Interfaces/DynamicsCE';
import { SendResponse } from '../../src/eventbridge/SendResponse';

jest.mock('aws-sdk', () => {
const mEventBridgeInstance = {
putEvents: jest.fn(),
const eventBridgeMock = mockClient(EventBridgeClient);
const mockPut = (params: { Entries: [{ Detail }] }) => {
const mPutEventsResponse = {
FailedEntryCount: 0,
Entries: Array<PutEventsResultEntry>(params.Entries.length),
};
const mRequestInstance = {
promise: jest.fn(),
};
const mEventBridge = jest.fn(() => mEventBridgeInstance);
const mRequest = jest.fn(() => mRequestInstance);

return { EventBridge: mEventBridge, Request: mRequest };
});

type PutEventsWithParams = (params: PutEventsRequest) => AWS.Request<PutEventsResponse, AWS.AWSError>;

const mEventBridgeInstance = new EventBridge();
const mResultInstance = new Request<PutEventsResponse, AWS.AWSError>(null, null);
// eslint-disable-next-line @typescript-eslint/unbound-method
mocked(mEventBridgeInstance.putEvents as PutEventsWithParams).mockImplementation(
(params: PutEventsRequest): AWS.Request<PutEventsResponse, AWS.AWSError> => {
const mPutEventsResponse: PutEventsResponse = {
FailedEntryCount: 0,
Entries: Array<PutEventsResultEntry>(params.Entries.length),
};
if (params.Entries[0].Detail === '{ "schedule": "{\\"testfacilityid\\":\\"Error\\",\\"eventdate\\":\\"Now\\"}" }') {
mResultInstance.promise = jest.fn().mockReturnValue(Promise.reject(new Error('Oh no!')));
} else {
mResultInstance.promise = jest.fn().mockReturnValue(Promise.resolve(mPutEventsResponse));
}
return mResultInstance;
},
);
if (params.Entries[0].Detail === '{ "schedule": "{\\"testfacilityid\\":\\"Error\\",\\"eventdate\\":\\"Now\\"}" }') {
return Promise.reject(new Error('Oh no!'));
}
return Promise.resolve(mPutEventsResponse);
};

describe('Send events', () => {
describe('Events sent', () => {
beforeEach(() => {
eventBridgeMock.reset();
});
it('GIVEN one event to send WHEN sent THEN one event is returned.', async () => {
const mFacillitySchedules = Array<FacillitySchedules>(1);
const mSendResponse: SendResponse = { SuccessCount: 1, FailCount: 0 };
eventBridgeMock.on(PutEventsCommand).callsFake((params: { Entries: [{ Detail }] }) => mockPut(params));
await expect(sendEvents(mFacillitySchedules)).resolves.toEqual(mSendResponse);
});

it('GIVEN two events to send WHEN sent THEN two events are returned.', async () => {
const mFacillitySchedules = Array<FacillitySchedules>(2);
const mSendResponse: SendResponse = { SuccessCount: 2, FailCount: 0 };
eventBridgeMock.on(PutEventsCommand).callsFake((params: { Entries: [{ Detail }] }) => mockPut(params));
await expect(sendEvents(mFacillitySchedules)).resolves.toEqual(mSendResponse);
});

it('GIVEN an issue with eventbridge WHEN 6 events are sent and 1 fails THEN the failure is in the response.', async () => {
const mFacillitySchedules = Array<FacillitySchedules>(6);
const errorFacillitySchedules: FacillitySchedules = { testfacilityid: 'Error', eventdate: 'Now' };
mFacillitySchedules[0] = errorFacillitySchedules;
mFacillitySchedules[0] = { testfacilityid: 'Error', eventdate: 'Now' };
eventBridgeMock.on(PutEventsCommand).callsFake((params: { Entries: [{ Detail }] }) => mockPut(params));
const mSendResponse: SendResponse = { SuccessCount: 5, FailCount: 1 };
await expect(sendEvents(mFacillitySchedules)).resolves.toEqual(mSendResponse);
});
Expand Down
Loading