Skip to content

Commit ff8f154

Browse files
authored
Add Legacy DeleteBillLicenceRequest (#826)
https://eaflood.atlassian.net/browse/WATER-4410 > Part of a series of changes to add support for removing a bill licence back into the billing views We're not ready to implement removing a bill licence from a bill run as it depends on sending requests to the [Charging Module API](https://github.com/DEFRA/sroc-charging-module-api) and running the legacy refresh job to update everything on our side. But to trigger all that we need to send a DELETE request to [water-abstraction-service](https://github.com/DEFRA/water-abstraction-service). This adds the legacy request that will deal with sending that request.
1 parent 0ab6d8c commit ff8f154

File tree

2 files changed

+141
-0
lines changed

2 files changed

+141
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
'use strict'
2+
3+
/**
4+
* Connects with the water-abstraction-service to delete a bill licence
5+
* @module DeleteBillLicenceRequest
6+
*/
7+
8+
const LegacyRequest = require('../legacy.request.js')
9+
10+
/**
11+
* Send a request to the legacy water-abstraction-service to delete a bill licence
12+
*
13+
* Users can remove a licence from a bill run. This requires sending a request to the charging module, then after it has
14+
* regenerated the bill run refreshing the data on our side.
15+
*
16+
* Currently, this is all handled by the water-abstraction-service and we're not ready to migrate this until we have
17+
* migrated the refresh functionality (needed by a number of operations)
18+
*
19+
* So, as this is handled by the legacy service we need to send the request to it once a user confirms they wish to
20+
* remove the licence.
21+
*
22+
* @param {string} billLicenceId - UUID of the bill licence to be removed
23+
* @param {module:UserModel} user - Instance representing the user that originated the request
24+
*
25+
* @returns {Promise<Object>} The result of the request; whether it succeeded and the response or error returned
26+
*/
27+
async function send (billLicenceId, user) {
28+
const { id: userId } = user
29+
const path = `billing/invoice-licences/${billLicenceId}`
30+
31+
return LegacyRequest.delete('water', path, userId)
32+
}
33+
34+
module.exports = {
35+
send
36+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
'use strict'
2+
3+
// Test framework dependencies
4+
const Lab = require('@hapi/lab')
5+
const Code = require('@hapi/code')
6+
const Sinon = require('sinon')
7+
8+
const { describe, it, beforeEach, afterEach } = exports.lab = Lab.script()
9+
const { expect } = Code
10+
11+
// Things we need to stub
12+
const LegacyRequest = require('../../../app/requests/legacy.request.js')
13+
14+
// Thing under test
15+
const DeleteBillLicenceRequest = require('../../../app/requests/legacy/delete-bill-licence.request.js')
16+
17+
describe('Legacy Delete Bill Licence request', () => {
18+
const billLicenceId = '8feaf2c1-f7cd-47f1-93b9-0d2218d20d56'
19+
const user = { id: '1c4ce580-9053-4531-ba23-d0cf0caf0562', username: '[email protected]' }
20+
21+
afterEach(() => {
22+
Sinon.restore()
23+
})
24+
25+
describe('when the request can delete a bill licence', () => {
26+
beforeEach(async () => {
27+
Sinon.stub(LegacyRequest, 'delete').resolves({
28+
succeeded: true,
29+
response: {
30+
statusCode: 204,
31+
body: null
32+
}
33+
})
34+
})
35+
36+
it('returns a `true` success status', async () => {
37+
const result = await DeleteBillLicenceRequest.send(billLicenceId, user)
38+
39+
expect(result.succeeded).to.be.true()
40+
})
41+
42+
it('returns a 204 - ok', async () => {
43+
const result = await DeleteBillLicenceRequest.send(billLicenceId, user)
44+
45+
expect(result.response.statusCode).to.equal(204)
46+
expect(result.response.body).to.be.null()
47+
})
48+
})
49+
50+
describe('when the request cannot delete a bill licence', () => {
51+
describe('because the request did not return a 2xx/3xx response', () => {
52+
beforeEach(async () => {
53+
Sinon.stub(LegacyRequest, 'delete').resolves({
54+
succeeded: false,
55+
response: {
56+
statusCode: 401,
57+
body: {
58+
statusCode: 401,
59+
error: 'Unauthorized',
60+
message: 'Invalid JWT: Token format not valid',
61+
attributes: { error: 'Invalid JWT: Token format not valid' }
62+
}
63+
}
64+
})
65+
})
66+
67+
it('returns a `false` success status', async () => {
68+
const result = await DeleteBillLicenceRequest.send(billLicenceId, user)
69+
70+
expect(result.succeeded).to.be.false()
71+
})
72+
73+
it('returns the error in the `response`', async () => {
74+
const result = await DeleteBillLicenceRequest.send(billLicenceId, user)
75+
76+
expect(result.response.body.statusCode).to.equal(401)
77+
expect(result.response.body.error).to.equal('Unauthorized')
78+
expect(result.response.body.message).to.equal('Invalid JWT: Token format not valid')
79+
})
80+
})
81+
82+
describe('because the request attempt returned an error, for example, TimeoutError', () => {
83+
beforeEach(async () => {
84+
Sinon.stub(LegacyRequest, 'delete').resolves({
85+
succeeded: false,
86+
response: new Error("Timeout awaiting 'request' for 5000ms")
87+
})
88+
})
89+
90+
it('returns a `false` success status', async () => {
91+
const result = await DeleteBillLicenceRequest.send(billLicenceId, user)
92+
93+
expect(result.succeeded).to.be.false()
94+
})
95+
96+
it('returns the error in the `response`', async () => {
97+
const result = await DeleteBillLicenceRequest.send(billLicenceId, user)
98+
99+
expect(result.response.statusCode).not.to.exist()
100+
expect(result.response.body).not.to.exist()
101+
expect(result.response.message).to.equal("Timeout awaiting 'request' for 5000ms")
102+
})
103+
})
104+
})
105+
})

0 commit comments

Comments
 (0)