Skip to content

Commit

Permalink
fixes api unit tests
Browse files Browse the repository at this point in the history
Signed-off-by: Diana Barsan <barsan@medic.org>
  • Loading branch information
dianabarsan committed Jan 16, 2025
1 parent fb696b2 commit 9d255ca
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 41 deletions.
3 changes: 2 additions & 1 deletion api/src/services/africas-talking.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ const getRecipient = res => {
res.SMSMessageData.Recipients[0];
};

const getStatus = recipient => recipient && STATUS_MAP[recipient.status];
const getStatus = recipient => recipient && STATUS_MAP[recipient.statusCode];

const generateStateChange = (message, res) => {
const recipient = getRecipient(res);
Expand Down Expand Up @@ -88,6 +88,7 @@ const sendMessage = (credentials, message) => {
return request
.post({
url: url,
json: false,
form: {
username: credentials.username,
from: credentials.from,
Expand Down
6 changes: 3 additions & 3 deletions api/tests/mocha/auth.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ describe('Auth', () => {
describe('check', () => {

it('returns error when not logged in', () => {
const get = sinon.stub(request, 'get').rejects({ statusCode: 401 });
const get = sinon.stub(request, 'get').rejects({ status: 401 });
return auth.check({ }).catch(err => {
chai.expect(get.callCount).to.equal(1);
chai.expect(get.args[0][0].url).to.equal('http://abc.com/_session');
Expand Down Expand Up @@ -168,12 +168,12 @@ describe('Auth', () => {
});

it('should throw a custom 401 error', async () => {
sinon.stub(request, 'get').rejects({ statusCode: 401, error: 'not logged in' });
sinon.stub(request, 'get').rejects({ status: 401, error: 'not logged in' });

await chai.expect(auth.getUserCtx(req)).to.be.rejected.and.eventually.deep.equal({
code: 401,
message: 'Not logged in',
err: { statusCode: 401, error: 'not logged in' }
err: { status: 401, error: 'not logged in' }
});

chai.expect(request.get.callCount).to.equal(1);
Expand Down
59 changes: 30 additions & 29 deletions api/tests/mocha/controllers/login.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,8 @@ describe('login controller', () => {
sinon.stub(tokenLogin, 'getUserByToken').resolves('userId');
sinon.stub(tokenLogin, 'resetPassword').resolves({ user: 'user_name', password: 'secret' });
sinon.stub(tokenLogin, 'deactivateTokenLogin').resolves();
sinon.stub(request, 'post').resolves({ statusCode: 200, headers: { 'set-cookie': [ 'AuthSession=abc;' ] } });
sinon.stub(request, 'post')
.resolves({ status: 200, headers: new Headers({ 'set-cookie': [ 'AuthSession=abc;' ] }) });
sinon.stub(res, 'status').returns(res);
sinon.stub(res, 'send').returns(res);
sinon.stub(res, 'cookie');
Expand Down Expand Up @@ -421,11 +422,11 @@ describe('login controller', () => {
sinon.stub(tokenLogin, 'resetPassword').resolves({ user: 'user_name', password: 'secret' });
sinon.stub(tokenLogin, 'deactivateTokenLogin').resolves();
sinon.stub(request, 'post')
.onCall(0).resolves({ statusCode: 401 })
.onCall(1).resolves({ statusCode: 401 })
.onCall(2).resolves({ statusCode: 401 })
.onCall(3).resolves({ statusCode: 401 })
.resolves({ statusCode: 200, headers: { 'set-cookie': [ 'AuthSession=cde;' ] } });
.onCall(0).resolves({ status: 401 })
.onCall(1).resolves({ status: 401 })
.onCall(2).resolves({ status: 401 })
.onCall(3).resolves({ status: 401 })
.resolves({ status: 200, headers: new Headers({ 'set-cookie': [ 'AuthSession=cde;' ] }) });

sinon.stub(res, 'status').returns(res);
sinon.stub(res, 'cookie');
Expand Down Expand Up @@ -497,7 +498,7 @@ describe('login controller', () => {

it('returns invalid credentials', () => {
req.body = { user: 'sharon', password: 'p4ss' };
const post = sinon.stub(request, 'post').resolves({ statusCode: 401 });
const post = sinon.stub(request, 'post').resolves({ status: 401 });
const status = sinon.stub(res, 'status').returns(res);
const json = sinon.stub(res, 'json').returns(res);
return controller.post(req, res).then(() => {
Expand All @@ -523,8 +524,8 @@ describe('login controller', () => {
it('should retry getting userCtx 10 times', async () => {
req.body = { user: 'sharon', password: 'p4ss', locale: 'fr' };
const postResponse = {
statusCode: 200,
headers: { 'set-cookie': [ 'AuthSession=abc;' ] }
status: 200,
headers: new Headers({ 'set-cookie': [ 'AuthSession=abc;' ] }),
};
sinon.stub(request, 'post').resolves(postResponse);
sinon.stub(res, 'status').returns(res);
Expand All @@ -539,8 +540,8 @@ describe('login controller', () => {
chai.expect(request.post.args[0][0].url).to.equal('http://test.com:1234/_session');
chai.expect(request.post.args[0][0].body.name).to.equal('sharon');
chai.expect(request.post.args[0][0].body.password).to.equal('p4ss');
chai.expect(request.post.args[0][0].auth.user).to.equal('sharon');
chai.expect(request.post.args[0][0].auth.pass).to.equal('p4ss');
chai.expect(request.post.args[0][0].auth.username).to.equal('sharon');
chai.expect(request.post.args[0][0].auth.password).to.equal('p4ss');
chai.expect(auth.getUserCtx.callCount).to.equal(10);
chai.expect(auth.getUserCtx.args[0][0].headers.Cookie).to.equal('AuthSession=abc;');
chai.expect(res.status.callCount).to.equal(1);
Expand All @@ -561,8 +562,8 @@ describe('login controller', () => {
it('returns errors from auth after 10 retries', () => {
req.body = { user: 'sharon', password: 'p4ss' };
const postResponse = {
statusCode: 200,
headers: { 'set-cookie': [ 'AuthSession=abc;' ] }
status: 200,
headers: new Headers({ 'set-cookie': [ 'AuthSession=abc;' ] })
};
const post = sinon.stub(request, 'post').resolves(postResponse);
const status = sinon.stub(res, 'status').returns(res);
Expand All @@ -581,8 +582,8 @@ describe('login controller', () => {
it('returns errors immediately from auth if code is not 401', () => {
req.body = { user: 'sharon', password: 'p4ss' };
const postResponse = {
statusCode: 200,
headers: { 'set-cookie': [ 'AuthSession=abc;' ] }
status: 200,
headers: new Headers({ 'set-cookie': [ 'AuthSession=abc;' ] })
};
const post = sinon.stub(request, 'post').resolves(postResponse);
const status = sinon.stub(res, 'status').returns(res);
Expand All @@ -601,8 +602,8 @@ describe('login controller', () => {
it('logs in successfully', () => {
req.body = { user: 'sharon', password: 'p4ss', locale: 'es' };
const postResponse = {
statusCode: 200,
headers: { 'set-cookie': [ 'AuthSession=abc;' ] }
status: 200,
headers: new Headers({ 'set-cookie': [ 'AuthSession=abc;' ] })
};
const post = sinon.stub(request, 'post').resolves(postResponse);
const send = sinon.stub(res, 'send');
Expand All @@ -617,8 +618,8 @@ describe('login controller', () => {
chai.expect(post.args[0][0].url).to.equal('http://test.com:1234/_session');
chai.expect(post.args[0][0].body.name).to.equal('sharon');
chai.expect(post.args[0][0].body.password).to.equal('p4ss');
chai.expect(post.args[0][0].auth.user).to.equal('sharon');
chai.expect(post.args[0][0].auth.pass).to.equal('p4ss');
chai.expect(post.args[0][0].auth.username).to.equal('sharon');
chai.expect(post.args[0][0].auth.password).to.equal('p4ss');
chai.expect(getUserCtx.callCount).to.equal(1);
chai.expect(getUserCtx.args[0][0].headers.Cookie).to.equal('AuthSession=abc;');
chai.expect(status.callCount).to.equal(1);
Expand All @@ -642,8 +643,8 @@ describe('login controller', () => {
it('sets user settings and cookie to default when no locale selected', () => {
req.body = { user: 'sharon', password: 'p4ss' };
const postResponse = {
statusCode: 200,
headers: { 'set-cookie': [ 'AuthSession=abc;' ] }
status: 200,
headers: new Headers({ 'set-cookie': [ 'AuthSession=abc;' ] }),
};
sinon.stub(request, 'post').resolves(postResponse);
sinon.stub(res, 'send');
Expand All @@ -667,8 +668,8 @@ describe('login controller', () => {
it('does not set locale when not changed', () => {
req.body = { user: 'sharon', password: 'p4ss' };
const postResponse = {
statusCode: 200,
headers: { 'set-cookie': [ 'AuthSession=abc;' ] }
status: 200,
headers: new Headers({ 'set-cookie': [ 'AuthSession=abc;' ] })
};
sinon.stub(request, 'post').resolves(postResponse);
sinon.stub(res, 'send');
Expand All @@ -693,8 +694,8 @@ describe('login controller', () => {
it('redirect offline admin user to webapp after successful login - #5785', () => {
req.body = { user: 'sharon', password: 'p4ss' };
const postResponse = {
statusCode: 200,
headers: { 'set-cookie': [ 'AuthSession=abc;' ] }
status: 200,
headers: new Headers({ 'set-cookie': [ 'AuthSession=abc;' ] })
};
const post = sinon.stub(request, 'post').resolves(postResponse);
const send = sinon.stub(res, 'send');
Expand All @@ -717,8 +718,8 @@ describe('login controller', () => {
it('redirect admin users to admin app after successful login', () => {
req.body = { user: 'sharon', password: 'p4ss' };
const postResponse = {
statusCode: 200,
headers: { 'set-cookie': [ 'AuthSession=abc;' ] }
status: 200,
headers: new Headers({ 'set-cookie': [ 'AuthSession=abc;' ] })
};
const post = sinon.stub(request, 'post').resolves(postResponse);
const send = sinon.stub(res, 'send');
Expand Down Expand Up @@ -749,8 +750,8 @@ describe('login controller', () => {
it('should not return a 401 when an admin without user-settings logs in', () => {
req.body = { user: 'shazza', password: 'p4ss' };
const postResponse = {
statusCode: 200,
headers: { 'set-cookie': [ 'AuthSession=abc;' ] }
status: 200,
headers: new Headers({ 'set-cookie': [ 'AuthSession=abc;' ] })
};
sinon.stub(request, 'post').resolves(postResponse);
sinon.stub(res, 'send');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ describe('restrict-access-to-audit-db', () => {
sinon.stub(environment, 'db').returns('database');
const url = `${environment.protocol}//${environment.host}:${environment.port}/${environment.db}-audit/_security`;
const auth = {
user: environment.username,
pass: environment.password
username: environment.username,
password: environment.password
};
const body = {
admins: { names: [], roles: [ 'audit-writer' ] },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ describe('restrict-access-to-sentinel-db migration', () => {
sinon.stub(environment, 'db').returns('database');
const url = `${environment.protocol}//${environment.host}:${environment.port}/${environment.db}-sentinel/_security`;
const auth = {
user: environment.username,
pass: environment.password
username: environment.username,
password: environment.password
};
const body = {
admins: { names: [], roles: [ 'sentinel' ] },
Expand Down
6 changes: 3 additions & 3 deletions api/tests/mocha/services/rapidpro.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ describe('RapidPro SMS Gateway', () => {
.withArgs(sinon.match({ body: { text: 'message2' } })).resolves()
.withArgs(sinon.match({ body: { text: 'message3' } })).rejects({ some: 'error' })
.withArgs(sinon.match({ body: { text: 'message4' } })).resolves({ id: 'broadcast4', status: 'queued' })
.withArgs(sinon.match({ body: { text: 'message5' } })).rejects({ statusCode: 400 })
.withArgs(sinon.match({ body: { text: 'message5' } })).rejects({ status: 400 })
.withArgs(sinon.match({ body: { text: 'message6' } })).resolves({ id: 'broadcast6' });

return service.send(messages).then((result) => {
Expand Down Expand Up @@ -582,7 +582,7 @@ describe('RapidPro SMS Gateway', () => {
.onCall(2).resolves({ rows: [] });
sinon.stub(request, 'get')
.resolves({ results: [{ status: 'delivered' }] })
.onCall(10).rejects({ statusCode: 429, error: 'Request was throttled' });
.onCall(10).rejects({ status: 429, error: 'Request was throttled' });

return service
.poll()
Expand Down Expand Up @@ -620,7 +620,7 @@ describe('RapidPro SMS Gateway', () => {
.onCall(2).resolves({ rows: [] });
sinon.stub(request, 'get')
.resolves({ results: [{ status: 'delivered' }] })
.onCall(10).rejects({ statusCode: 429, error: 'Request was throttled' });
.onCall(10).rejects({ status: 429, error: 'Request was throttled' });

return service
.poll()
Expand Down
2 changes: 1 addition & 1 deletion api/tests/mocha/services/user-db.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ describe('User DB service', () => {
chai.expect(get.callCount).to.equal(1);
const requestParams = requestPut.args[0][0];
chai.expect(requestParams.url).to.equal(`http://localhost:7357/${environment.db}-user-gareth-meta/_security`);
chai.expect(requestParams.auth).to.deep.equal({ user: 'auser', pass: 'apass' });
chai.expect(requestParams.auth).to.deep.equal({ username: 'auser', password: 'apass' });
chai.expect(requestParams.json).to.equal(true);
chai.expect(requestParams.body.admins.names[0]).to.equal('gareth');
chai.expect(requestParams.body.members.names[0]).to.equal('gareth');
Expand Down
2 changes: 2 additions & 0 deletions shared-libs/couch-request/src/couch-request.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,8 @@ const request = async (options = {}) => {
throw err;
};

// todo add jsdoc for this!!!

module.exports = {
initialize: (store, header) => {
asyncLocalStorage = store;
Expand Down

0 comments on commit 9d255ca

Please sign in to comment.