Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Display view licence tabs only to permitted users #949

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions app/controllers/licences.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ async function returnsRequired (request, h) {
}

async function view (request, h) {
const { id } = request.params
const { params: { id }, auth } = request
Cruikshanks marked this conversation as resolved.
Show resolved Hide resolved

const data = await ViewLicenceService.go(id)
const data = await ViewLicenceService.go(id, auth)
jonathangoulding marked this conversation as resolved.
Show resolved Hide resolved

return h.view('licences/view.njk', {
activeNavBar: 'search',
Expand Down
13 changes: 11 additions & 2 deletions app/presenters/licences/view-licence.presenter.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const { formatLongDate } = require('../base.presenter.js')
*
* @returns {Object} The data formatted for the view template
*/
function go (licence, licenceAbstractionConditions) {
function go (licence, licenceAbstractionConditions, auth) {
const {
ends,
expiredDate,
Expand Down Expand Up @@ -71,7 +71,8 @@ function go (licence, licenceAbstractionConditions) {
registeredTo,
sourceOfSupply: abstractionDetails.sourceOfSupply,
startDate: formatLongDate(startDate),
warning: _generateWarningMessage(ends)
warning: _generateWarningMessage(ends),
roles: _authRoles(auth)
}
}

Expand Down Expand Up @@ -132,6 +133,14 @@ function _endDate (expiredDate) {
return formatLongDate(expiredDate)
}

function _authRoles (auth) {
const roles = auth?.credentials?.roles?.map((role) => {
return role?.role
})

return roles || null
}

function _generateAbstractionContent (pointDetail) {
let abstractionPoint = null

Expand Down
4 changes: 2 additions & 2 deletions app/services/licences/view-licence.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ const ViewLicencePresenter = require('../../presenters/licences/view-licence.pre
*
* @returns {Promise<Object>} an object representing the `pageData` needed by the licence summary template.
*/
async function go (licenceId) {
async function go (licenceId, auth) {
const licenceData = await FetchLicenceService.go(licenceId)

const currentLicenceVersionId = licenceData?.licenceVersions[0]?.id

const licenceAbstractionConditions = await FetchLicenceAbstractionConditionsService.go(currentLicenceVersionId)

const pageData = ViewLicencePresenter.go(licenceData, licenceAbstractionConditions)
const pageData = ViewLicencePresenter.go(licenceData, licenceAbstractionConditions, auth)

return {
...pageData
Expand Down
4 changes: 2 additions & 2 deletions app/views/licences/view.njk
Original file line number Diff line number Diff line change
Expand Up @@ -87,13 +87,13 @@
panel: {
html: billsTabHtml
}
}, {
} if roles and 'billing' in roles, {
label: "Charge information",
id: "charge-information",
panel: {
html: chargeInformationTabHtml
}
}
} if roles and 'view_charge_versions' in roles
]
}) }}
{% endblock %}
43 changes: 40 additions & 3 deletions test/presenters/licences/view-licence.presenter.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,19 @@ const ViewLicencePresenter = require('../../../app/presenters/licences/view-lice
describe('View Licence presenter', () => {
let licenceAbstractionConditions
let licence
let auth

beforeEach(() => {
licence = _licence()
licenceAbstractionConditions = _abstractionConditions()
auth = undefined
})

describe('when provided with a populated licence', () => {
it('correctly presents the data', () => {
const result = ViewLicencePresenter.go(licence, licenceAbstractionConditions)

expect(result).to.equal({
id: 'f1288f6c-8503-4dc1-b114-75c408a14bd0',
abstractionConditionDetails: {
conditions: ['Derogation clause', 'General conditions', 'Non standard quantities'],
numberOfConditions: 4
Expand All @@ -37,18 +38,20 @@ describe('View Licence presenter', () => {
abstractionQuantities: null,
documentId: '28665d16-eba3-4c9a-aa55-7ab671b0c4fb',
endDate: null,
id: 'f1288f6c-8503-4dc1-b114-75c408a14bd0',
licenceHolder: 'Unregistered licence',
licenceName: 'Unregistered licence',
licenceRef: '01/123',
monitoringStations: [{
gaugingStationId: 'ac075651-4781-4e24-a684-b943b98607ca',
label: 'MEVAGISSEY FIRE STATION'
}],
pageTitle: 'Licence 01/123',
notification: null,
pageTitle: 'Licence 01/123',
purposes: null,
registeredTo: null,
region: 'Narnia',
registeredTo: null,
roles: null,
sourceOfSupply: 'SURFACE WATER SOURCE OF SUPPLY',
startDate: '1 April 2019',
warning: null
Expand Down Expand Up @@ -1120,6 +1123,40 @@ describe('View Licence presenter', () => {
})
})
})

describe("the 'roles' property", () => {
describe('when the authenticated user has roles', () => {
beforeEach(() => {
auth = {
credentials: {
roles: [{
role: 'role 1'
},
{
role: 'role 2'
}]
}
}
})

it('returns the roles in a flat array', () => {
const result = ViewLicencePresenter.go(licence, licenceAbstractionConditions, auth)

expect(result.roles).to.equal(['role 1', 'role 2'])
})
})
describe('when the authenticated user has NO roles', () => {
beforeEach(() => {
auth = undefined
})

it('returns null for the roles', () => {
const result = ViewLicencePresenter.go(licence, licenceAbstractionConditions, auth)

expect(result.roles).to.be.null()
})
})
})
})

function _abstractionConditions () {
Expand Down
7 changes: 4 additions & 3 deletions test/services/licences/view-licence.service.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,20 +58,21 @@ describe('View Licence service', () => {
],
abstractionPointsCaption: 'Point of abstraction',
abstractionQuantities: null,
id: '2c80bd22-a005-4cf4-a2a2-73812a9861de',
documentId: '40306a46-d4ce-4874-9c9e-30ab6469b3fe',
endDate: null,
id: '2c80bd22-a005-4cf4-a2a2-73812a9861de',
licenceHolder: 'Unregistered licence',
licenceName: 'Unregistered licence',
licenceRef: '01/130/R01',
monitoringStations: [],
pageTitle: 'Licence 01/130/R01',
notification: null,
pageTitle: 'Licence 01/130/R01',
purposes: null,
region: 'South West',
registeredTo: null,
startDate: '7 March 2013',
roles: null,
sourceOfSupply: 'SURFACE WATER SOURCE OF SUPPLY',
startDate: '7 March 2013',
warning: null
})
})
Expand Down
Loading