-
-
Notifications
You must be signed in to change notification settings - Fork 219
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
Showing
82 changed files
with
2,236 additions
and
280 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
const chaiExclude = require('chai-exclude'); | ||
const chaiAsPromised = require('chai-as-promised'); | ||
const chai = require('chai'); | ||
|
||
chai.use(chaiExclude); | ||
chai.use(chaiAsPromised); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,20 @@ | ||
const { Person, Qualifier } = require('@medic/cht-datasource'); | ||
const ctx = require('../services/data-context'); | ||
const serverUtils = require('../server-utils'); | ||
const auth = require('../auth'); | ||
|
||
const getPerson = (qualifier) => ctx.bind(Person.v1.get)(qualifier); | ||
|
||
module.exports = { | ||
v1: { | ||
get: serverUtils.doOrError(async (req, res) => { | ||
await auth.check(req, 'can_view_contacts'); | ||
const { uuid } = req.params; | ||
const person = await getPerson(Qualifier.byUuid(uuid)); | ||
if (!person) { | ||
return serverUtils.error({ status: 404, message: 'Person not found' }, req, res); | ||
} | ||
return res.json(person); | ||
}) | ||
} | ||
}; |
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,5 @@ | ||
const { getLocalDataContext } = require('@medic/cht-datasource'); | ||
const db = require('../db'); | ||
const config = require('../config'); | ||
|
||
module.exports = getLocalDataContext(config, db); |
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,90 @@ | ||
const sinon = require('sinon'); | ||
const { expect } = require('chai'); | ||
const { Person, Qualifier } = require('@medic/cht-datasource'); | ||
const auth = require('../../../src/auth'); | ||
const controller = require('../../../src/controllers/person'); | ||
const dataContext = require('../../../src/services/data-context'); | ||
const serverUtils = require('../../../src/server-utils'); | ||
|
||
describe('Person Controller', () => { | ||
let authCheck; | ||
let dataContextBind; | ||
let serverUtilsError; | ||
let req; | ||
let res; | ||
|
||
beforeEach(() => { | ||
authCheck = sinon.stub(auth, 'check'); | ||
dataContextBind = sinon.stub(dataContext, 'bind'); | ||
serverUtilsError = sinon.stub(serverUtils, 'error'); | ||
res = { | ||
json: sinon.stub(), | ||
}; | ||
}); | ||
|
||
afterEach(() => sinon.restore()); | ||
|
||
describe('v1', () => { | ||
describe('get', () => { | ||
const qualifier = Object.freeze({ uuid: 'uuid' }); | ||
let byUuid; | ||
let personGet; | ||
|
||
beforeEach(() => { | ||
req = { params: { uuid: 'uuid' } }; | ||
byUuid = sinon | ||
.stub(Qualifier, 'byUuid') | ||
.returns(qualifier); | ||
personGet = sinon.stub(); | ||
dataContextBind | ||
.withArgs(Person.v1.get) | ||
.returns(personGet); | ||
}); | ||
|
||
it('returns a person', async () => { | ||
const person = { name: 'John Doe' }; | ||
personGet.resolves(person); | ||
|
||
await controller.v1.get(req, res); | ||
|
||
expect(authCheck.calledOnceWithExactly(req, 'can_view_contacts')).to.be.true; | ||
expect(dataContextBind.calledOnceWithExactly(Person.v1.get)).to.be.true; | ||
expect(byUuid.calledOnceWithExactly(req.params.uuid)).to.be.true; | ||
expect(personGet.calledOnceWithExactly(qualifier)).to.be.true; | ||
expect(res.json.calledOnceWithExactly(person)).to.be.true; | ||
expect(serverUtilsError.notCalled).to.be.true; | ||
}); | ||
|
||
it('returns a 404 error if person is not found', async () => { | ||
personGet.resolves(null); | ||
|
||
await controller.v1.get(req, res); | ||
|
||
expect(authCheck.calledOnceWithExactly(req, 'can_view_contacts')).to.be.true; | ||
expect(dataContextBind.calledOnceWithExactly(Person.v1.get)).to.be.true; | ||
expect(byUuid.calledOnceWithExactly(req.params.uuid)).to.be.true; | ||
expect(personGet.calledOnceWithExactly(qualifier)).to.be.true; | ||
expect(res.json.notCalled).to.be.true; | ||
expect(serverUtilsError.calledOnceWithExactly( | ||
{ status: 404, message: 'Person not found' }, | ||
req, | ||
res | ||
)).to.be.true; | ||
}); | ||
|
||
it('returns error if user unauthorized', async () => { | ||
const error = new Error('Unauthorized'); | ||
authCheck.rejects(error); | ||
|
||
await controller.v1.get(req, res); | ||
|
||
expect(authCheck.calledOnceWithExactly(req, 'can_view_contacts')).to.be.true; | ||
expect(dataContextBind.notCalled).to.be.true; | ||
expect(byUuid.notCalled).to.be.true; | ||
expect(personGet.notCalled).to.be.true; | ||
expect(res.json.notCalled).to.be.true; | ||
expect(serverUtilsError.calledOnceWithExactly(error, req, res)).to.be.true; | ||
}); | ||
}); | ||
}); | ||
}); |
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,14 @@ | ||
const dataSource = require('@medic/cht-datasource'); | ||
const { expect } = require('chai'); | ||
const db = require('../../../src/db'); | ||
const config = require('../../../src/config'); | ||
const dataContext = require('../../../src/services/data-context'); | ||
|
||
describe('Data context service', () => { | ||
it('is initialized with the methods from the data context', () => { | ||
const expectedDataContext = dataSource.getLocalDataContext(config, db); | ||
|
||
expect(dataContext.bind).is.a('function'); | ||
expect(dataContext).excluding('bind').to.deep.equal(expectedDataContext); | ||
}); | ||
}); |
Oops, something went wrong.