|
| 1 | +/* eslint-disable no-unused-expressions */ |
| 2 | +import chai from 'chai'; |
| 3 | +import request from 'supertest'; |
| 4 | +import sinon from 'sinon'; |
| 5 | + |
| 6 | +import models from '../../models'; |
| 7 | +import server from '../../app'; |
| 8 | +import testUtil from '../../tests/util'; |
| 9 | +import SalesforceService from '../../services/salesforceService'; |
| 10 | + |
| 11 | +chai.should(); |
| 12 | + |
| 13 | +// demo data which might be returned by the `SalesforceService.query` |
| 14 | +const billingAccountsData = [ |
| 15 | + { |
| 16 | + sfBillingAccountId: 123, |
| 17 | + tcBillingAccountId: 123123, |
| 18 | + name: 'Billing Account 1', |
| 19 | + startDate: '2021-02-10T18:51:27Z', |
| 20 | + endDate: '2021-03-10T18:51:27Z', |
| 21 | + }, { |
| 22 | + sfBillingAccountId: 456, |
| 23 | + tcBillingAccountId: 456456, |
| 24 | + name: 'Billing Account 2', |
| 25 | + startDate: '2011-02-10T18:51:27Z', |
| 26 | + endDate: '2011-03-10T18:51:27Z', |
| 27 | + }, |
| 28 | +]; |
| 29 | + |
| 30 | +describe('Project Billing Accounts list', () => { |
| 31 | + let project1; |
| 32 | + let salesforceAuthenticate; |
| 33 | + let salesforceQuery; |
| 34 | + |
| 35 | + beforeEach((done) => { |
| 36 | + testUtil.clearDb() |
| 37 | + .then(() => testUtil.clearES()) |
| 38 | + .then(() => { |
| 39 | + models.Project.create({ |
| 40 | + type: 'generic', |
| 41 | + directProjectId: 1, |
| 42 | + billingAccountId: 1, |
| 43 | + name: 'test1', |
| 44 | + description: 'test project1', |
| 45 | + status: 'draft', |
| 46 | + details: {}, |
| 47 | + createdBy: 1, |
| 48 | + updatedBy: 1, |
| 49 | + lastActivityAt: 1, |
| 50 | + lastActivityUserId: '1', |
| 51 | + }).then((p) => { |
| 52 | + project1 = p; |
| 53 | + // create members |
| 54 | + return models.ProjectMember.create({ |
| 55 | + userId: testUtil.userIds.copilot, |
| 56 | + projectId: project1.id, |
| 57 | + role: 'copilot', |
| 58 | + isPrimary: true, |
| 59 | + createdBy: 1, |
| 60 | + updatedBy: 1, |
| 61 | + }).then(() => models.ProjectMember.create({ |
| 62 | + userId: testUtil.userIds.member, |
| 63 | + projectId: project1.id, |
| 64 | + role: 'customer', |
| 65 | + isPrimary: false, |
| 66 | + createdBy: 1, |
| 67 | + updatedBy: 1, |
| 68 | + })); |
| 69 | + }).then(() => { |
| 70 | + salesforceAuthenticate = sinon.stub(SalesforceService, 'authenticate', () => Promise.resolve({ |
| 71 | + accessToken: 'mock', |
| 72 | + instanceUrl: 'mock_url', |
| 73 | + })); |
| 74 | + salesforceQuery = sinon.stub(SalesforceService, 'query', () => Promise.resolve(billingAccountsData)); |
| 75 | + done(); |
| 76 | + }); |
| 77 | + }); |
| 78 | + }); |
| 79 | + |
| 80 | + afterEach((done) => { |
| 81 | + salesforceAuthenticate.restore(); |
| 82 | + salesforceQuery.restore(); |
| 83 | + done(); |
| 84 | + }); |
| 85 | + |
| 86 | + after((done) => { |
| 87 | + testUtil.clearDb(done); |
| 88 | + }); |
| 89 | + |
| 90 | + describe('List /projects/{id}/billingAccounts', () => { |
| 91 | + it('should return 403 for anonymous user', (done) => { |
| 92 | + request(server) |
| 93 | + .get(`/v5/projects/${project1.id}/billingAccounts`) |
| 94 | + .expect(403, done); |
| 95 | + }); |
| 96 | + |
| 97 | + it('should return 403 for a customer user who is a member of the project', (done) => { |
| 98 | + request(server) |
| 99 | + .get(`/v5/projects/${project1.id}/billingAccounts`) |
| 100 | + .set({ |
| 101 | + Authorization: `Bearer ${testUtil.jwts.member}`, |
| 102 | + }) |
| 103 | + .send() |
| 104 | + .expect(403, done); |
| 105 | + }); |
| 106 | + |
| 107 | + it('should return 403 for a topcoder user who is not a member of the project', (done) => { |
| 108 | + request(server) |
| 109 | + .get(`/v5/projects/${project1.id}/billingAccounts`) |
| 110 | + .set({ |
| 111 | + Authorization: `Bearer ${testUtil.jwts.copilotManager}`, |
| 112 | + }) |
| 113 | + .send() |
| 114 | + .expect(403, done); |
| 115 | + }); |
| 116 | + |
| 117 | + it('should return all billing accounts for a topcoder user who is a member of the project', (done) => { |
| 118 | + request(server) |
| 119 | + .get(`/v5/projects/${project1.id}/billingAccounts`) |
| 120 | + .set({ |
| 121 | + Authorization: `Bearer ${testUtil.jwts.copilot}`, |
| 122 | + }) |
| 123 | + .send() |
| 124 | + .expect(200) |
| 125 | + .end((err, res) => { |
| 126 | + if (err) { |
| 127 | + done(err); |
| 128 | + } else { |
| 129 | + const resJson = res.body; |
| 130 | + resJson.should.have.length(2); |
| 131 | + resJson.should.include(billingAccountsData[0]); |
| 132 | + resJson.should.include(billingAccountsData[1]); |
| 133 | + done(); |
| 134 | + } |
| 135 | + }); |
| 136 | + }); |
| 137 | + |
| 138 | + it('should return all billing accounts to admin', (done) => { |
| 139 | + request(server) |
| 140 | + .get(`/v5/projects/${project1.id}/billingAccounts`) |
| 141 | + .set({ |
| 142 | + Authorization: `Bearer ${testUtil.jwts.admin}`, |
| 143 | + }) |
| 144 | + .send() |
| 145 | + .expect(200) |
| 146 | + .end((err, res) => { |
| 147 | + if (err) { |
| 148 | + done(err); |
| 149 | + } else { |
| 150 | + const resJson = res.body; |
| 151 | + resJson.should.have.length(2); |
| 152 | + resJson.should.have.length(2); |
| 153 | + resJson.should.include(billingAccountsData[0]); |
| 154 | + resJson.should.include(billingAccountsData[1]); |
| 155 | + done(); |
| 156 | + } |
| 157 | + }); |
| 158 | + }); |
| 159 | + |
| 160 | + it('should return all billing accounts using M2M token with "read:user-billing-accounts" scope', (done) => { |
| 161 | + request(server) |
| 162 | + .get(`/v5/projects/${project1.id}/billingAccounts`) |
| 163 | + .set({ |
| 164 | + Authorization: `Bearer ${testUtil.m2m['read:user-billing-accounts']}`, |
| 165 | + }) |
| 166 | + .send() |
| 167 | + .expect(200) |
| 168 | + .end((err, res) => { |
| 169 | + if (err) { |
| 170 | + done(err); |
| 171 | + } else { |
| 172 | + const resJson = res.body; |
| 173 | + resJson.should.have.length(2); |
| 174 | + resJson.should.have.length(2); |
| 175 | + resJson.should.include(billingAccountsData[0]); |
| 176 | + resJson.should.include(billingAccountsData[1]); |
| 177 | + done(); |
| 178 | + } |
| 179 | + }); |
| 180 | + }); |
| 181 | + }); |
| 182 | +}); |
0 commit comments