|
| 1 | +import { Headers } from 'node-mocks-http'; |
| 2 | +import { NextFunction, Request, Response } from 'express'; |
| 3 | +import { add, format, startOfDay } from 'date-fns'; |
| 4 | +import { HttpStatusCode } from 'axios'; |
| 5 | +import { AnyObject, DEAL_STATUS, DEAL_SUBMISSION_TYPE, ROLES } from '@ukef/dtfs2-common'; |
| 6 | +import { withRoleValidationApiTests } from '../common-tests/role-validation-api-tests'; |
| 7 | +import app from '../../server/createApp'; |
| 8 | +import { createApi } from '../create-api'; |
| 9 | +import api from '../../server/services/api'; |
| 10 | +import * as storage from '../test-helpers/storage/storage'; |
| 11 | +import { MOCK_BASIC_DEAL } from '../../server/utils/mocks/mock-applications'; |
| 12 | +import { MOCK_ISSUED_FACILITY } from '../../server/utils/mocks/mock-facilities'; |
| 13 | +import { PortalFacilityAmendmentWithUkefIdMockBuilder } from '../../test-helpers/mock-amendment'; |
| 14 | +import { PORTAL_AMENDMENT_PAGES } from '../../server/constants/amendments'; |
| 15 | + |
| 16 | +const originalEnv = { ...process.env }; |
| 17 | + |
| 18 | +const { post } = createApi(app); |
| 19 | + |
| 20 | +jest.mock('csurf', () => () => (_req: Request, _res: Response, next: NextFunction) => next()); |
| 21 | +jest.mock('../../server/middleware/csrf', () => ({ |
| 22 | + csrfToken: () => (_req: Request, _res: Response, next: NextFunction) => next(), |
| 23 | +})); |
| 24 | + |
| 25 | +const mockGetFacility = jest.fn(); |
| 26 | +const mockGetApplication = jest.fn(); |
| 27 | +const mockUpdateAmendment = jest.fn(); |
| 28 | + |
| 29 | +const dealId = '123'; |
| 30 | +const facilityId = '111'; |
| 31 | +const amendmentId = '111'; |
| 32 | + |
| 33 | +const mockDeal = { ...MOCK_BASIC_DEAL, submissionType: DEAL_SUBMISSION_TYPE.AIN, status: DEAL_STATUS.UKEF_ACKNOWLEDGED }; |
| 34 | + |
| 35 | +const todayPlusTwoYears = startOfDay(add(new Date(), { years: 2 })); |
| 36 | + |
| 37 | +const url = `/application-details/${dealId}/facilities/${facilityId}/amendments/${amendmentId}/${PORTAL_AMENDMENT_PAGES.BANK_REVIEW_DATE}`; |
| 38 | + |
| 39 | +describe(`POST ${url}`, () => { |
| 40 | + let sessionCookie: string; |
| 41 | + |
| 42 | + beforeEach(async () => { |
| 43 | + await storage.flush(); |
| 44 | + jest.resetAllMocks(); |
| 45 | + |
| 46 | + ({ sessionCookie } = await storage.saveUserSession([ROLES.MAKER])); |
| 47 | + jest.spyOn(api, 'getFacility').mockImplementation(mockGetFacility); |
| 48 | + jest.spyOn(api, 'getApplication').mockImplementation(mockGetApplication); |
| 49 | + jest.spyOn(api, 'updateAmendment').mockImplementation(mockUpdateAmendment); |
| 50 | + |
| 51 | + mockGetFacility.mockResolvedValue(MOCK_ISSUED_FACILITY); |
| 52 | + mockGetApplication.mockResolvedValue(mockDeal); |
| 53 | + mockUpdateAmendment.mockResolvedValue( |
| 54 | + new PortalFacilityAmendmentWithUkefIdMockBuilder() |
| 55 | + .withDealId(dealId) |
| 56 | + .withFacilityId(facilityId) |
| 57 | + .withAmendmentId(amendmentId) |
| 58 | + .withBankReviewDate(todayPlusTwoYears) |
| 59 | + .build(), |
| 60 | + ); |
| 61 | + }); |
| 62 | + |
| 63 | + afterAll(async () => { |
| 64 | + jest.resetAllMocks(); |
| 65 | + await storage.flush(); |
| 66 | + process.env = originalEnv; |
| 67 | + }); |
| 68 | + |
| 69 | + describe('when FF_PORTAL_FACILITY_AMENDMENTS_ENABLED is disabled', () => { |
| 70 | + beforeEach(() => { |
| 71 | + process.env.FF_PORTAL_FACILITY_AMENDMENTS_ENABLED = 'false'; |
| 72 | + }); |
| 73 | + |
| 74 | + it('should redirect to /not-found', async () => { |
| 75 | + // Arrange |
| 76 | + const body = { |
| 77 | + 'bank-review-date-day': format(todayPlusTwoYears, 'd'), |
| 78 | + 'bank-review-date-month': format(todayPlusTwoYears, 'M'), |
| 79 | + 'bank-review-date-year': format(todayPlusTwoYears, 'yyyy'), |
| 80 | + }; |
| 81 | + |
| 82 | + // Act |
| 83 | + const response = await postWithSessionCookie(body, sessionCookie); |
| 84 | + |
| 85 | + // Assert |
| 86 | + expect(response.status).toEqual(HttpStatusCode.Found); |
| 87 | + expect(response.headers.location).toEqual('/not-found'); |
| 88 | + }); |
| 89 | + }); |
| 90 | + |
| 91 | + describe('when FF_PORTAL_FACILITY_AMENDMENTS_ENABLED is not set', () => { |
| 92 | + beforeEach(() => { |
| 93 | + delete process.env.FF_PORTAL_FACILITY_AMENDMENTS_ENABLED; |
| 94 | + }); |
| 95 | + |
| 96 | + it('should redirect to /not-found', async () => { |
| 97 | + // Arrange |
| 98 | + const body = { |
| 99 | + 'bank-review-date-day': format(todayPlusTwoYears, 'd'), |
| 100 | + 'bank-review-date-month': format(todayPlusTwoYears, 'M'), |
| 101 | + 'bank-review-date-year': format(todayPlusTwoYears, 'yyyy'), |
| 102 | + }; |
| 103 | + |
| 104 | + // Act |
| 105 | + const response = await postWithSessionCookie(body, sessionCookie); |
| 106 | + |
| 107 | + // Assert |
| 108 | + expect(response.status).toEqual(HttpStatusCode.Found); |
| 109 | + expect(response.headers.location).toEqual('/not-found'); |
| 110 | + }); |
| 111 | + }); |
| 112 | + |
| 113 | + describe('when FF_PORTAL_FACILITY_AMENDMENTS_ENABLED is enabled', () => { |
| 114 | + beforeEach(() => { |
| 115 | + process.env.FF_PORTAL_FACILITY_AMENDMENTS_ENABLED = 'true'; |
| 116 | + }); |
| 117 | + |
| 118 | + withRoleValidationApiTests({ |
| 119 | + makeRequestWithHeaders: (headers: Headers) => |
| 120 | + post( |
| 121 | + { |
| 122 | + 'bank-review-date-day': format(todayPlusTwoYears, 'd'), |
| 123 | + 'bank-review-date-month': format(todayPlusTwoYears, 'M'), |
| 124 | + 'bank-review-date-year': format(todayPlusTwoYears, 'yyyy'), |
| 125 | + }, |
| 126 | + headers, |
| 127 | + ).to(url), |
| 128 | + whitelistedRoles: [ROLES.MAKER], |
| 129 | + successCode: HttpStatusCode.Found, |
| 130 | + }); |
| 131 | + |
| 132 | + it('should redirect to /not-found when facility not found', async () => { |
| 133 | + // Arrange |
| 134 | + mockGetFacility.mockResolvedValue({ details: undefined }); |
| 135 | + |
| 136 | + const body = { |
| 137 | + 'bank-review-date-day': format(todayPlusTwoYears, 'd'), |
| 138 | + 'bank-review-date-month': format(todayPlusTwoYears, 'M'), |
| 139 | + 'bank-review-date-year': format(todayPlusTwoYears, 'yyyy'), |
| 140 | + }; |
| 141 | + |
| 142 | + // Act |
| 143 | + const response = await postWithSessionCookie(body, sessionCookie); |
| 144 | + // Assert |
| 145 | + expect(response.status).toEqual(HttpStatusCode.Found); |
| 146 | + expect(response.headers.location).toEqual('/not-found'); |
| 147 | + }); |
| 148 | + |
| 149 | + it('should redirect to /not-found when deal not found', async () => { |
| 150 | + // Arrange |
| 151 | + mockGetApplication.mockResolvedValue(undefined); |
| 152 | + |
| 153 | + const body = { |
| 154 | + 'bank-review-date-day': format(todayPlusTwoYears, 'd'), |
| 155 | + 'bank-review-date-month': format(todayPlusTwoYears, 'M'), |
| 156 | + 'bank-review-date-year': format(todayPlusTwoYears, 'yyyy'), |
| 157 | + }; |
| 158 | + |
| 159 | + // Act |
| 160 | + const response = await postWithSessionCookie(body, sessionCookie); |
| 161 | + |
| 162 | + // Assert |
| 163 | + expect(response.status).toEqual(HttpStatusCode.Found); |
| 164 | + expect(response.headers.location).toEqual('/not-found'); |
| 165 | + }); |
| 166 | + |
| 167 | + it('should render bank review date page with errors if bank review date is invalid', async () => { |
| 168 | + // Arrange |
| 169 | + const body = { 'bank-review-date-day': '1000', 'bank-review-date-month': '100', 'bank-review-date-year': '100' }; |
| 170 | + |
| 171 | + // Act |
| 172 | + const response = await postWithSessionCookie(body, sessionCookie); |
| 173 | + |
| 174 | + // Assert |
| 175 | + expect(response.status).toEqual(HttpStatusCode.Ok); |
| 176 | + expect(response.text).toContain('Bank review date'); |
| 177 | + expect(response.text).toContain('Bank review date must be a real date'); |
| 178 | + }); |
| 179 | + |
| 180 | + it('should render bank review date page with errors if bank review date is not provided', async () => { |
| 181 | + // Arrange |
| 182 | + const body = { 'bank-review-date-day': '', 'bank-review-date-month': '', 'bank-review-date-year': '' }; |
| 183 | + |
| 184 | + // Act |
| 185 | + const response = await postWithSessionCookie(body, sessionCookie); |
| 186 | + |
| 187 | + // Assert |
| 188 | + expect(response.status).toEqual(HttpStatusCode.Ok); |
| 189 | + expect(response.text).toContain('Bank review date'); |
| 190 | + expect(response.text).toContain('Enter the bank review date'); |
| 191 | + }); |
| 192 | + |
| 193 | + it('should redirect to the next page if the bank review date is valid', async () => { |
| 194 | + // Arrange |
| 195 | + const body = { |
| 196 | + 'bank-review-date-day': format(todayPlusTwoYears, 'd'), |
| 197 | + 'bank-review-date-month': format(todayPlusTwoYears, 'M'), |
| 198 | + 'bank-review-date-year': format(todayPlusTwoYears, 'yyyy'), |
| 199 | + }; |
| 200 | + |
| 201 | + // Act |
| 202 | + const response = await postWithSessionCookie(body, sessionCookie); |
| 203 | + |
| 204 | + // Assert |
| 205 | + expect(response.status).toEqual(HttpStatusCode.Found); |
| 206 | + expect(response.headers.location).toEqual( |
| 207 | + `/gef/application-details/${dealId}/facilities/${facilityId}/amendments/${amendmentId}/${PORTAL_AMENDMENT_PAGES.ELIGIBILITY}`, |
| 208 | + ); |
| 209 | + }); |
| 210 | + |
| 211 | + it('should render `problem with service` if getApplication throws an error', async () => { |
| 212 | + // Arrange |
| 213 | + mockGetApplication.mockRejectedValue(new Error('test error')); |
| 214 | + |
| 215 | + const body = { |
| 216 | + 'bank-review-date-day': format(todayPlusTwoYears, 'd'), |
| 217 | + 'bank-review-date-month': format(todayPlusTwoYears, 'M'), |
| 218 | + 'bank-review-date-year': format(todayPlusTwoYears, 'yyyy'), |
| 219 | + }; |
| 220 | + |
| 221 | + // Act |
| 222 | + const response = await postWithSessionCookie(body, sessionCookie); |
| 223 | + |
| 224 | + // Assert |
| 225 | + expect(response.status).toEqual(HttpStatusCode.Ok); |
| 226 | + expect(response.text).toContain('Problem with the service'); |
| 227 | + }); |
| 228 | + |
| 229 | + it('should render `problem with service` if getFacility throws an error', async () => { |
| 230 | + // Arrange |
| 231 | + mockGetFacility.mockRejectedValue(new Error('test error')); |
| 232 | + |
| 233 | + const body = { |
| 234 | + 'bank-review-date-day': format(todayPlusTwoYears, 'd'), |
| 235 | + 'bank-review-date-month': format(todayPlusTwoYears, 'M'), |
| 236 | + 'bank-review-date-year': format(todayPlusTwoYears, 'yyyy'), |
| 237 | + }; |
| 238 | + |
| 239 | + // Act |
| 240 | + const response = await postWithSessionCookie(body, sessionCookie); |
| 241 | + |
| 242 | + // Assert |
| 243 | + expect(response.status).toEqual(HttpStatusCode.Ok); |
| 244 | + expect(response.text).toContain('Problem with the service'); |
| 245 | + }); |
| 246 | + |
| 247 | + it('should render `problem with service` if updateAmendment throws an error', async () => { |
| 248 | + // Arrange |
| 249 | + mockUpdateAmendment.mockRejectedValue(new Error('test error')); |
| 250 | + |
| 251 | + const body = { |
| 252 | + 'bank-review-date-day': format(todayPlusTwoYears, 'd'), |
| 253 | + 'bank-review-date-month': format(todayPlusTwoYears, 'M'), |
| 254 | + 'bank-review-date-year': format(todayPlusTwoYears, 'yyyy'), |
| 255 | + }; |
| 256 | + |
| 257 | + // Act |
| 258 | + const response = await postWithSessionCookie(body, sessionCookie); |
| 259 | + |
| 260 | + // Assert |
| 261 | + expect(response.status).toEqual(HttpStatusCode.Ok); |
| 262 | + expect(response.text).toContain('Problem with the service'); |
| 263 | + }); |
| 264 | + }); |
| 265 | +}); |
| 266 | + |
| 267 | +function postWithSessionCookie(body: AnyObject, sessionCookie: string) { |
| 268 | + return post(body, { Cookie: [`dtfs-session=${encodeURIComponent(sessionCookie)}`] }).to(url); |
| 269 | +} |
0 commit comments