-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4439 from NMDSdevopsServiceAdm/test
Live: Admin parent requests and removing parent status button
- Loading branch information
Showing
55 changed files
with
1,992 additions
and
1,062 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
const models = require('../../../models'); | ||
|
||
const updateStatus = async (req, res) => { | ||
try { | ||
const { uid, status, reviewer, inReview } = req.body; | ||
const establishment = await models.establishment.findByUid(uid); | ||
|
||
if (!establishment) { | ||
return res.status(400).send({ error: 'Workplace could not be found' }); | ||
} | ||
|
||
const approval = await models.Approvals.findbyEstablishmentId(establishment.id, 'BecomeAParent'); | ||
|
||
if (!approval) { | ||
return res.status(400).send({ error: 'Parent request could not be found' }); | ||
} | ||
|
||
if (approval.InReview && reviewer && approval.Reviewer !== reviewer) { | ||
return res.status(400).send({ error: 'This parent request is already being reviewed' }); | ||
} | ||
|
||
approval.Status = status; | ||
approval.Reviewer = reviewer; | ||
approval.InReview = inReview; | ||
await approval.save(); | ||
|
||
res.status(200).send(); | ||
} catch (error) { | ||
console.log(error); | ||
res.status(500).send(); | ||
} | ||
}; | ||
|
||
const router = require('express').Router(); | ||
|
||
router.route('/').post(updateStatus); | ||
|
||
module.exports = router; | ||
module.exports.updateStatus = updateStatus; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
const express = require('express'); | ||
const models = require('../../../models'); | ||
const router = express.Router({ mergeParams: true }); | ||
|
||
const removeParentStatus = async (req, res) => { | ||
try { | ||
const { establishmentId } = req.body; | ||
const workplace = await models.establishment.findByUid(establishmentId); | ||
if (!workplace) { | ||
return res.status(400).json({ message: 'Establishment could not be found' }); | ||
} | ||
await models.establishment.updateEstablishment(workplace.id, { | ||
isParent: false, | ||
}); | ||
res.status(200).send(); | ||
} catch (error) { | ||
console.error(error); | ||
res.status(500).json({ message: 'There was an error removing parent status' }); | ||
} | ||
}; | ||
|
||
router.route('/').post(removeParentStatus); | ||
|
||
module.exports = router; | ||
module.exports.removeParentStatus = removeParentStatus; |
138 changes: 138 additions & 0 deletions
138
server/test/unit/routes/admin/parent-approval/updateStatus.spec.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
const chai = require('chai'); | ||
const expect = chai.expect; | ||
const sinon = require('sinon'); | ||
const sinonChai = require('sinon-chai'); | ||
chai.should(); | ||
chai.use(sinonChai); | ||
const httpMocks = require('node-mocks-http'); | ||
|
||
const models = require('../../../../../models'); | ||
|
||
const { updateStatus } = require('../../../../../routes/admin/parent-approval/updateStatus'); | ||
|
||
describe('updateStatus', () => { | ||
let req; | ||
let res; | ||
|
||
const dummyDetails = { | ||
Status: 'Pending', | ||
UUID: 'bbd54f18-f0bd-4fc2-893d-e492faa9b278', | ||
ID: 1, | ||
InReview: false, | ||
Reviewer: null, | ||
User: { FullNameValue: 'Joe Bloggs', RegistrationID: 1 }, | ||
Establishment: { | ||
id: 1, | ||
uid: 'f61696f7-30fe-441c-9c59-e25dfcb51f59', | ||
nmdsId: 'J111111', | ||
NameValue: 'Workplace 1', | ||
address1: 'Care Home 1', | ||
address2: '31 King Street', | ||
address3: 'Sale', | ||
town: 'Manchester', | ||
county: 'Cheshire', | ||
postcode: 'CA1 2BD', | ||
}, | ||
save() { | ||
return true; | ||
}, | ||
}; | ||
|
||
beforeEach(() => { | ||
const request = { | ||
method: 'POST', | ||
body: { | ||
status: 'In progress', | ||
uid: 'someuid', | ||
reviewer: 'admin', | ||
inReview: true, | ||
}, | ||
}; | ||
|
||
req = httpMocks.createRequest(request); | ||
res = httpMocks.createResponse(); | ||
|
||
sinon.stub(models.Approvals, 'findbyEstablishmentId').returns(dummyDetails); | ||
sinon.stub(models.establishment, 'findByUid').returns({ id: 123 }); | ||
}); | ||
|
||
afterEach(() => { | ||
sinon.restore(); | ||
}); | ||
|
||
it('should return 200 when the pending flag gets updated', async () => { | ||
await updateStatus(req, res); | ||
|
||
expect(res.statusCode).to.deep.equal(200); | ||
}); | ||
|
||
it('should return 200 when the approval is In progress but being reviewed by the person making the request', async () => { | ||
const newDummyDetails = { ...dummyDetails, Status: 'In progress', Reviewer: 'admin', InReview: true }; | ||
|
||
sinon.restore(); | ||
sinon.stub(models.Approvals, 'findbyEstablishmentId').returns(newDummyDetails); | ||
sinon.stub(models.establishment, 'findByUid').returns({ id: 123 }); | ||
|
||
await updateStatus(req, res); | ||
|
||
expect(res.statusCode).to.deep.equal(200); | ||
}); | ||
|
||
it('should return 400 when the establishment cannot be found', async () => { | ||
sinon.restore(); | ||
sinon.stub(models.establishment, 'findByUid').returns(null); | ||
|
||
await updateStatus(req, res); | ||
const expectedResponse = { error: 'Workplace could not be found' }; | ||
|
||
expect(res.statusCode).to.deep.equal(400); | ||
expect(res._getData()).to.deep.equal(expectedResponse); | ||
}); | ||
|
||
it('should return 400 when the approval cannot be found', async () => { | ||
sinon.restore(); | ||
sinon.stub(models.establishment, 'findByUid').returns({ id: 123 }); | ||
sinon.stub(models.Approvals, 'findbyEstablishmentId').returns(null); | ||
|
||
await updateStatus(req, res); | ||
const expectedResponse = { error: 'Parent request could not be found' }; | ||
|
||
expect(res.statusCode).to.deep.equal(400); | ||
expect(res._getData()).to.deep.equal(expectedResponse); | ||
}); | ||
|
||
it('should return 400 if the approval is already being reviewed by someone else', async () => { | ||
sinon.restore(); | ||
sinon.stub(models.establishment, 'findByUid').returns({ id: 123 }); | ||
sinon.stub(models.Approvals, 'findbyEstablishmentId').returns({ | ||
InReview: true, | ||
Reviewer: 'someoneElse', | ||
status: 'In progress', | ||
}); | ||
|
||
await updateStatus(req, res); | ||
const expectedResponse = { error: 'This parent request is already being reviewed' }; | ||
|
||
expect(res.statusCode).to.deep.equal(400); | ||
expect(res._getData()).to.deep.equal(expectedResponse); | ||
}); | ||
|
||
it('should return 500 when an error is thrown getting the establishment', async () => { | ||
sinon.restore(); | ||
sinon.stub(models.establishment, 'findByUid').throws(); | ||
|
||
await updateStatus(req, res); | ||
|
||
expect(res.statusCode).to.deep.equal(500); | ||
}); | ||
|
||
it('should return 500 when an error is thrown getting the approval', async () => { | ||
sinon.restore(); | ||
sinon.stub(models.establishment, 'findByUid').returns({ id: 123 }); | ||
sinon.stub(models.Approvals, 'findbyEstablishmentId').throws(); | ||
|
||
await updateStatus(req, res); | ||
|
||
expect(res.statusCode).to.deep.equal(500); | ||
}); | ||
}); |
69 changes: 69 additions & 0 deletions
69
server/test/unit/routes/admin/remove-parent-status/index.spec.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
const expect = require('chai').expect; | ||
const sinon = require('sinon'); | ||
const httpMocks = require('node-mocks-http'); | ||
const models = require('../../../../../models'); | ||
|
||
const { removeParentStatus } = require('../../../../../routes/admin/remove-parent-status'); | ||
|
||
describe('removeParentStatus', () => { | ||
const establishmentId = 'a131313dasd123325453bac'; | ||
let req; | ||
let res; | ||
|
||
beforeEach(() => { | ||
const request = { | ||
method: 'POST', | ||
url: `/api/establishment/${establishmentId}/remove-parent-status`, | ||
body: { | ||
establishmentId, | ||
}, | ||
}; | ||
|
||
req = httpMocks.createRequest(request); | ||
res = httpMocks.createResponse(); | ||
}); | ||
|
||
afterEach(() => { | ||
sinon.restore(); | ||
}); | ||
|
||
it('should reply with a status of 200', async () => { | ||
sinon.stub(models.establishment, 'findByUid').returns({ id: 1 }); | ||
sinon.stub(models.establishment, 'updateEstablishment'); | ||
|
||
await removeParentStatus(req, res); | ||
expect(res.statusCode).to.deep.equal(200); | ||
}); | ||
|
||
it('should return 400 error code when given an incorrect establishment ID', async () => { | ||
sinon.stub(models.establishment, 'findByUid').returns(null); | ||
|
||
await removeParentStatus(req, res); | ||
const { message } = res._getJSONData(); | ||
|
||
expect(res.statusCode).to.deep.equal(400); | ||
expect(message).to.equal('Establishment could not be found'); | ||
}); | ||
|
||
it('should return 500 when an error is thrown by updateEstablishment', async () => { | ||
sinon.stub(models.establishment, 'findByUid').throws(); | ||
|
||
await removeParentStatus(req, res); | ||
const { message } = res._getJSONData(); | ||
|
||
expect(res.statusCode).to.deep.equal(500); | ||
expect(message).to.equal('There was an error removing parent status'); | ||
}); | ||
|
||
it('should return 500 when an error is thrown by updateEstablishment', async () => { | ||
sinon.stub(models.establishment, 'findByUid').returns({ id: 1 }); | ||
sinon.stub(models.establishment, 'updateEstablishment').throws(); | ||
|
||
await removeParentStatus(req, res); | ||
|
||
const { message } = res._getJSONData(); | ||
|
||
expect(res.statusCode).to.deep.equal(500); | ||
expect(message).to.equal('There was an error removing parent status'); | ||
}); | ||
}); |
Oops, something went wrong.