Skip to content

Fix/add unit tests for GET /v5/projects/:projectId/billingAccount #638

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

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
5 changes: 5 additions & 0 deletions src/routes/billingAccounts/get.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ module.exports = [
attributes: ['id', 'billingAccountId'],
raw: true,
});
if (!project) {
const err = new Error(`Project with id "${projectId}" not found`);
err.status = 404;
throw err;
}
const billingAccountId = project.billingAccountId;
if (!billingAccountId) {
const err = new Error('Billing Account not found');
Expand Down
170 changes: 170 additions & 0 deletions src/routes/billingAccounts/get.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
/* eslint-disable no-unused-expressions */
import chai from 'chai';
import request from 'supertest';
import sinon from 'sinon';

import models from '../../models';
import server from '../../app';
import testUtil from '../../tests/util';
import SalesforceService from '../../services/salesforceService';

chai.should();

// demo data which might be returned by the `SalesforceService.query`
const billingAccountData = {
sfBillingAccountId: '123',
tcBillingAccountId: 123123,
name: 'Billing Account 1',
startDate: '2021-02-10T18:51:27Z',
endDate: '2021-03-10T18:51:27Z',
};

describe('Project Billing Accounts list', () => {
let project1;
let project2;
let salesforceAuthenticate;
let salesforceQuery;

beforeEach((done) => {
testUtil.clearDb()
.then(() => testUtil.clearES())
.then(() => models.Project.create({
type: 'generic',
directProjectId: 1,
billingAccountId: 1,
name: 'test1',
description: 'test project1',
status: 'draft',
details: {},
createdBy: 1,
updatedBy: 1,
lastActivityAt: 1,
lastActivityUserId: '1',
}).then((p) => {
project1 = p;
// create members
return models.ProjectMember.create({
userId: testUtil.userIds.copilot,
projectId: project1.id,
role: 'copilot',
isPrimary: true,
createdBy: 1,
updatedBy: 1,
}).then(() => models.ProjectMember.create({
userId: testUtil.userIds.member,
projectId: project1.id,
role: 'customer',
isPrimary: false,
createdBy: 1,
updatedBy: 1,
}));
})).then(() => models.Project.create({
type: 'generic',
directProjectId: 1,
billingAccountId: null, // do not define billingAccountId
name: 'test1',
description: 'test project1',
status: 'draft',
details: {},
createdBy: 1,
updatedBy: 1,
lastActivityAt: 1,
lastActivityUserId: '1',
}).then((p) => {
project2 = p;
// create members
return models.ProjectMember.create({
userId: testUtil.userIds.copilot,
projectId: project2.id,
role: 'copilot',
isPrimary: true,
createdBy: 1,
updatedBy: 1,
}).then(() => models.ProjectMember.create({
userId: testUtil.userIds.member,
projectId: project2.id,
role: 'customer',
isPrimary: false,
createdBy: 1,
updatedBy: 1,
}));
}))
.then(() => {
salesforceAuthenticate = sinon.stub(SalesforceService, 'authenticate', () => Promise.resolve({
accessToken: 'mock',
instanceUrl: 'mock_url',
}));
// eslint-disable-next-line
salesforceQuery = sinon.stub(SalesforceService, 'queryBillingAccount', () => Promise.resolve(billingAccountData));
done();
});
});

afterEach((done) => {
salesforceAuthenticate.restore();
salesforceQuery.restore();
done();
});

after((done) => {
testUtil.clearDb(done);
});

describe('Get /projects/{id}/billingAccounts', () => {
it('should return 403 for anonymous user', (done) => {
request(server)
.get(`/v5/projects/${project1.id}/billingAccount`)
.expect(403, done);
});

it('should return 403 for admin', (done) => {
request(server)
.get(`/v5/projects/${project1.id}/billingAccount`)
.set({
Authorization: `Bearer ${testUtil.jwts.admin}`,
})
.send()
.expect(403, done);
});

it('should return 404 if the project is not found', (done) => {
request(server)
.get('/v5/projects/11223344/billingAccount')
.set({
Authorization: `Bearer ${testUtil.m2m['read:project-billing-account-details']}`,
})
.send()
.expect(404, done);
});

it('should return 404 if billing account is not defined in the project', (done) => {
request(server)
.get(`/v5/projects/${project2.id}/billingAccount`)
.set({
Authorization: `Bearer ${testUtil.m2m['read:project-billing-account-details']}`,
})
.send()
.expect(404, done);
});

it('should return billing account details using M2M token with "read:project-billing-account-details" scope',
(done) => {
request(server)
.get(`/v5/projects/${project1.id}/billingAccount`)
.set({
Authorization: `Bearer ${testUtil.m2m['read:project-billing-account-details']}`,
})
.send()
.expect(200)
.end((err, res) => {
if (err) {
done(err);
} else {
const resJson = res.body;
resJson.should.deep.equal(billingAccountData);
done();
}
});
});
});
});
1 change: 1 addition & 0 deletions src/tests/util.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.