|
| 1 | +const cheerio = require('cheerio') |
| 2 | +const expectPhaseBanner = require('../../../utils/phase-banner-expect') |
| 3 | +const { administrator } = require('../../../../app/auth/permissions') |
| 4 | +const getCrumbs = require('../../../utils/get-crumbs') |
| 5 | + |
| 6 | +const applications = require('../../../../app/api/applications') |
| 7 | +jest.mock('../../../../app/api/applications') |
| 8 | + |
| 9 | +const reference = 'AHWR-555A-FD4C' |
| 10 | + |
| 11 | +applications.withdrawApplication = jest.fn().mockResolvedValue(true) |
| 12 | + |
| 13 | +describe('View Application test', () => { |
| 14 | + let crumb |
| 15 | + const url = '/withdraw-application/' |
| 16 | + jest.mock('../../../../app/auth') |
| 17 | + const auth = { strategy: 'session-auth', credentials: { scope: [administrator] } } |
| 18 | + |
| 19 | + beforeEach(async () => { |
| 20 | + crumb = await getCrumbs(global.__SERVER__) |
| 21 | + jest.clearAllMocks() |
| 22 | + }) |
| 23 | + |
| 24 | + describe(`POST ${url} route`, () => { |
| 25 | + test('returns 302 no auth', async () => { |
| 26 | + const options = { |
| 27 | + method: 'POST', |
| 28 | + url |
| 29 | + } |
| 30 | + const res = await global.__SERVER__.inject(options) |
| 31 | + expect(res.statusCode).toBe(302) |
| 32 | + }) |
| 33 | + |
| 34 | + test('returns 403', async () => { |
| 35 | + const options = { |
| 36 | + method: 'POST', |
| 37 | + url, |
| 38 | + auth, |
| 39 | + payload: { |
| 40 | + reference |
| 41 | + } |
| 42 | + } |
| 43 | + const res = await global.__SERVER__.inject(options) |
| 44 | + expect(res.statusCode).toBe(403) |
| 45 | + const $ = cheerio.load(res.payload) |
| 46 | + expect($('h1.govuk-heading-l').text()).toEqual('403 - Forbidden') |
| 47 | + expectPhaseBanner.ok($) |
| 48 | + }) |
| 49 | + |
| 50 | + test('Approve withdraw application', async () => { |
| 51 | + const options = { |
| 52 | + method: 'POST', |
| 53 | + url, |
| 54 | + auth, |
| 55 | + headers: { cookie: `crumb=${crumb}` }, |
| 56 | + payload: { |
| 57 | + reference, |
| 58 | + withdrawConfirmation: 'yes', |
| 59 | + page: 1, |
| 60 | + crumb |
| 61 | + } |
| 62 | + } |
| 63 | + const res = await global.__SERVER__.inject(options) |
| 64 | + expect(applications.withdrawApplication).toHaveBeenCalledTimes(1) |
| 65 | + expect(res.statusCode).toBe(302) |
| 66 | + expect(res.headers.location).toEqual(`/view-application/${reference}?page=1`) |
| 67 | + }) |
| 68 | + |
| 69 | + test('Cancel withdraw application', async () => { |
| 70 | + const options = { |
| 71 | + method: 'POST', |
| 72 | + url, |
| 73 | + auth, |
| 74 | + headers: { cookie: `crumb=${crumb}` }, |
| 75 | + payload: { |
| 76 | + reference, |
| 77 | + withdrawConfirmation: 'no', |
| 78 | + page: 1, |
| 79 | + crumb |
| 80 | + } |
| 81 | + } |
| 82 | + const res = await global.__SERVER__.inject(options) |
| 83 | + expect(applications.withdrawApplication).toHaveBeenCalledTimes(0) |
| 84 | + expect(res.statusCode).toBe(302) |
| 85 | + expect(res.headers.location).toEqual(`/view-application/${reference}?page=1`) |
| 86 | + }) |
| 87 | + }) |
| 88 | +}) |
0 commit comments