Skip to content

Commit

Permalink
fix unstable API tests (#4657)
Browse files Browse the repository at this point in the history
  • Loading branch information
bn-pass authored Dec 19, 2023
1 parent a8d50bc commit 2a7b5ae
Showing 1 changed file with 86 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,46 @@ import { DeletionRequestEntity } from '../../entity';

const baseRouteName = '/deletionRequests';

const getMinDeletionPlannedAt = (creationDate: Date, diffInMinutes: number, toleranceInSeconds: number): Date => {
const minDeletionPlannedAt = new Date(creationDate.getTime());

minDeletionPlannedAt.setMinutes(minDeletionPlannedAt.getMinutes() + diffInMinutes);
minDeletionPlannedAt.setSeconds(minDeletionPlannedAt.getSeconds() - toleranceInSeconds);

return minDeletionPlannedAt;
};

const getMaxDeletionPlannedAt = (creationDate: Date, diffInMinutes: number, toleranceInSeconds: number): Date => {
const maxDeletionPlannedAt = new Date(creationDate.getTime());

maxDeletionPlannedAt.setMinutes(maxDeletionPlannedAt.getMinutes() + diffInMinutes);
maxDeletionPlannedAt.setSeconds(maxDeletionPlannedAt.getSeconds() + toleranceInSeconds);

return maxDeletionPlannedAt;
};

const getMinAndMaxDeletionPlannedAt = (creationDate: Date, diffInMinutes: number, toleranceInSeconds: number) => {
const minDeletionPlannedAt = getMinDeletionPlannedAt(creationDate, diffInMinutes, toleranceInSeconds);
const maxDeletionPlannedAt = getMaxDeletionPlannedAt(creationDate, diffInMinutes, toleranceInSeconds);

return { minDeletionPlannedAt, maxDeletionPlannedAt };
};

const isDeletionPlannedWithinAcceptableRange = (
creationDate: Date,
deletionPlannedAt: Date,
diffInMinutes: number,
toleranceInSeconds: number
) => {
const { minDeletionPlannedAt, maxDeletionPlannedAt } = getMinAndMaxDeletionPlannedAt(
creationDate,
diffInMinutes,
toleranceInSeconds
);

return deletionPlannedAt >= minDeletionPlannedAt && deletionPlannedAt <= maxDeletionPlannedAt;
};

describe(`deletionRequest create (api)`, () => {
let app: INestApplication;
let em: EntityManager;
Expand Down Expand Up @@ -42,7 +82,7 @@ describe(`deletionRequest create (api)`, () => {
});

describe('createDeletionRequests', () => {
describe('when create deletionRequest', () => {
describe('when called', () => {
const setup = () => {
const deletionRequestToCreate: DeletionRequestBodyProps = {
targetRef: {
Expand All @@ -59,7 +99,16 @@ describe(`deletionRequest create (api)`, () => {
deleteInMinutes: 0,
};

return { deletionRequestToCreate, deletionRequestToImmediateRemoval };
const defaultDeleteInMinutes = 43200;

const operationalTimeToleranceInSeconds = 30;

return {
deletionRequestToCreate,
deletionRequestToImmediateRemoval,
defaultDeleteInMinutes,
operationalTimeToleranceInSeconds,
};
};

it('should return status 202', async () => {
Expand All @@ -79,33 +128,52 @@ describe(`deletionRequest create (api)`, () => {
expect(result.requestId).toBeDefined();
});

it('should create deletionRequest with default deletion time (add 43200 minutes to current time) ', async () => {
const { deletionRequestToCreate } = setup();
describe('when the "delete in minutes" param has not been provided', () => {
it(
'should set the "deletion planned at" date to the date after the default "delete in minutes" value ' +
'(43200 minutes which is 30 days), with some operational time tolerance',
async () => {
const { deletionRequestToCreate, defaultDeleteInMinutes, operationalTimeToleranceInSeconds } = setup();

const response = await testXApiKeyClient.post('', deletionRequestToCreate);
const response = await testXApiKeyClient.post('', deletionRequestToCreate);

const result = response.body as DeletionRequestResponse;
const createdDeletionRequestId = result.requestId;
const result = response.body as DeletionRequestResponse;
const createdDeletionRequestId = result.requestId;

const createdItem = await em.findOneOrFail(DeletionRequestEntity, createdDeletionRequestId);
const createdItem = await em.findOneOrFail(DeletionRequestEntity, createdDeletionRequestId);

const deletionPlannedAt = createdItem.createdAt;
deletionPlannedAt.setMinutes(deletionPlannedAt.getMinutes() + 43200);
const isDeletionPlannedAtDateCorrect = isDeletionPlannedWithinAcceptableRange(
createdItem.createdAt,
createdItem.deleteAfter,
defaultDeleteInMinutes,
operationalTimeToleranceInSeconds
);

expect(createdItem.deleteAfter).toEqual(deletionPlannedAt);
expect(isDeletionPlannedAtDateCorrect).toEqual(true);
}
);
});

it('should create deletionRequest with deletion time (0 minutes to current time) ', async () => {
const { deletionRequestToImmediateRemoval } = setup();
describe('when the "delete in minutes" param has been set to 0', () => {
it('should set the "deletion planned at" date to now, with some operational time tolerance', async () => {
const { deletionRequestToImmediateRemoval, operationalTimeToleranceInSeconds } = setup();

const response = await testXApiKeyClient.post('', deletionRequestToImmediateRemoval);
const response = await testXApiKeyClient.post('', deletionRequestToImmediateRemoval);

const result = response.body as DeletionRequestResponse;
const createdDeletionRequestId = result.requestId;
const result = response.body as DeletionRequestResponse;
const createdDeletionRequestId = result.requestId;

const createdItem = await em.findOneOrFail(DeletionRequestEntity, createdDeletionRequestId);

const createdItem = await em.findOneOrFail(DeletionRequestEntity, createdDeletionRequestId);
const isDeletionPlannedAtDateCorrect = isDeletionPlannedWithinAcceptableRange(
createdItem.createdAt,
createdItem.deleteAfter,
0,
operationalTimeToleranceInSeconds
);

expect(createdItem.createdAt).toEqual(createdItem.deleteAfter);
expect(isDeletionPlannedAtDateCorrect).toEqual(true);
});
});
});
});
Expand Down

0 comments on commit 2a7b5ae

Please sign in to comment.