Skip to content

Commit

Permalink
add integration test
Browse files Browse the repository at this point in the history
  • Loading branch information
goemen committed May 29, 2024
1 parent 56a4607 commit 3033a26
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 11 deletions.
25 changes: 25 additions & 0 deletions backend-external/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions backend-external/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
"version": "1.0.0",
"main": "src/server.ts",
"dependencies": {
"@js-joda/core": "^5.6.2",
"@js-joda/locale_en": "^4.11.0",
"axios": "^1.6.7",
"dotenv": "^16.3.1",
"express": "^4.18.2",
Expand Down
24 changes: 24 additions & 0 deletions backend-external/tests/integration/integration.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import { agent } from 'supertest';
import { config } from '../../src/config';
import {
DateTimeFormatter,
LocalDate,
} from '@js-joda/core';
import { Locale } from '@js-joda/locale_en';

const request = agent(config.get('server:baseURL'));

Expand Down Expand Up @@ -36,5 +41,24 @@ describe('/v1/pay-transparency/ GET', () => {
.expect(({ body }) => {
expect(body).toHaveProperty('records');
});
});
it('should not allow users to enter end date later than current - 1 day', () => {
//note: this test requires both backend-external and backend to be running.
const dateFormat = DateTimeFormatter.ofPattern(
'YYYY-MM-dd',
).withLocale(Locale.ENGLISH);
const endDate = LocalDate.now().format(dateFormat);

return request
.get('/v1/pay-transparency/reports?pageSize=1')
.query({startDate: '2015-01-01', endDate})
.set('x-api-key', config.get('server:apiKey'))
.retry(3)
.expect(400)
.expect(({ body }) => {
expect(body.error).toBe(
'End date cannot be later than current - 1 days.',
);
});
});
});
6 changes: 3 additions & 3 deletions backend/src/v1/services/external-consumer-service.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { faker } from '@faker-js/faker';
import { externalConsumerService } from './external-consumer-service';
import { DateTimeFormatter, LocalDate } from '@js-joda/core';
import { LocalDate } from '@js-joda/core';
import { JSON_REPORT_DATE_FORMAT } from '../../constants';

const mockReportsViewFindMany = jest.fn();
Expand Down Expand Up @@ -134,7 +134,7 @@ describe('external-consumer-service', () => {
expect(error.message).toBe('Start date must be before the end date.');
}
});
it('should fail when endDate after yesterday', async () => {
it('should fail when endDate is later than current date - 1 days', async () => {
mockReportsViewFindMany.mockReturnValue([testData]);

const endDate = LocalDate.now().format(JSON_REPORT_DATE_FORMAT);
Expand All @@ -147,7 +147,7 @@ describe('external-consumer-service', () => {
);
} catch (error) {
expect(error.message).toBe(
'End date must always be before the current date.',
'End date cannot be later than current - 1 days.',
);
}
});
Expand Down
22 changes: 14 additions & 8 deletions backend/src/v1/services/external-consumer-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ const externalConsumerService = {
* if limit is greater than 50, it will default to 50.
* calling this endpoint with no limit will default to 50.
* calling this endpoint with no offset will default to 0.
* calling this endpoint with no start date will default to yesterday.
* calling this endpoint with no start date will default to -1 days.
* calling this endpoint with no end date will default to today.
* consumer is responsible for making the api call in a loop to get all the records.
* @param startDate from when records needs to be fetched
Expand Down Expand Up @@ -61,10 +61,21 @@ const externalConsumerService = {

if (endDate) {
const date = convert(LocalDate.parse(endDate)).toDate();
endDt = withEndOfDay(LocalDateTime.from(nativeJs(date, ZoneId.UTC)));
endDt = withEndOfDay(
LocalDateTime.from(nativeJs(date, ZoneId.UTC))
);

if (!endDt.isBefore(withStartOfDay(currentTime))) {
throw new PayTransparencyUserError(
'End date cannot be later than current - 1 days.',
);
}
}
} catch (error) {
console.log(error);
if (error instanceof PayTransparencyUserError) {
throw error;
}

throw new PayTransparencyUserError(
'Failed to parse dates. Please use date format YYYY-MM-dd',
);
Expand All @@ -76,11 +87,6 @@ const externalConsumerService = {
);
}

if (endDt.isAfter(withStartOfDay(currentTime))) {
throw new PayTransparencyUserError(
'End date must always be before the current date.',
);
}
const whereClause: Prisma.reports_viewWhereInput = {
update_date: {
gte: convert(startDt).toDate(),
Expand Down

0 comments on commit 3033a26

Please sign in to comment.