|
| 1 | +import chai from 'chai'; |
| 2 | +import sinon from 'sinon'; |
| 3 | +import request from 'supertest'; |
| 4 | +import config from 'config'; |
| 5 | +import models from '../../models'; |
| 6 | +import server from '../../app'; |
| 7 | +import testUtil from '../../tests/util'; |
| 8 | +import util from '../../util'; |
| 9 | +import lookerSerivce from '../../services/lookerService'; |
| 10 | + |
| 11 | +const should = chai.should(); |
| 12 | + |
| 13 | +describe('GET embed report', () => { |
| 14 | + let project0; |
| 15 | + let project1; |
| 16 | + beforeEach((done) => { |
| 17 | + testUtil.clearDb() |
| 18 | + .then(() => models.Project.create({ |
| 19 | + type: 'generic', |
| 20 | + directProjectId: 0, |
| 21 | + billingAccountId: 0, |
| 22 | + name: 'test0', |
| 23 | + description: 'test project0', |
| 24 | + status: 'reviewed', |
| 25 | + details: {}, |
| 26 | + createdBy: 1, |
| 27 | + updatedBy: 1, |
| 28 | + lastActivityAt: 1, |
| 29 | + lastActivityUserId: '1', |
| 30 | + })) |
| 31 | + .then((p0) => { |
| 32 | + project0 = p0; |
| 33 | + return models.ProjectMember.create({ |
| 34 | + userId: 40051331, |
| 35 | + projectId: project0.id, |
| 36 | + role: 'customer', |
| 37 | + isPrimary: true, |
| 38 | + createdBy: 1, |
| 39 | + updatedBy: 1, |
| 40 | + }); |
| 41 | + }) |
| 42 | + .then(() => models.ProjectTemplate.create({ |
| 43 | + name: 'template 2', |
| 44 | + key: 'key 2', |
| 45 | + category: 'concrete', |
| 46 | + icon: 'http://example.com/icon1.ico', |
| 47 | + question: 'question 2', |
| 48 | + info: 'info 2', |
| 49 | + aliases: ['key-2', 'key_2'], |
| 50 | + scope: {}, |
| 51 | + phases: {}, |
| 52 | + createdBy: 1, |
| 53 | + updatedBy: 2, |
| 54 | + })) |
| 55 | + .then(temp => models.Project.create({ |
| 56 | + type: 'generic', |
| 57 | + directProjectId: 1, |
| 58 | + billingAccountId: 1, |
| 59 | + name: 'test1', |
| 60 | + description: 'test project1', |
| 61 | + status: 'reviewed', |
| 62 | + details: {}, |
| 63 | + createdBy: 1, |
| 64 | + updatedBy: 1, |
| 65 | + lastActivityAt: 1, |
| 66 | + templateId: temp.id, |
| 67 | + lastActivityUserId: '1', |
| 68 | + })) |
| 69 | + .then((p) => { |
| 70 | + project1 = p; |
| 71 | + // create members |
| 72 | + return models.ProjectMember.create({ |
| 73 | + userId: 40051332, |
| 74 | + projectId: project1.id, |
| 75 | + role: 'copilot', |
| 76 | + isPrimary: true, |
| 77 | + createdBy: 1, |
| 78 | + updatedBy: 1, |
| 79 | + }); |
| 80 | + }) |
| 81 | + .then(() => models.ProjectMember.create({ |
| 82 | + userId: 40051334, |
| 83 | + projectId: project1.id, |
| 84 | + role: 'manager', |
| 85 | + isPrimary: true, |
| 86 | + createdBy: 1, |
| 87 | + updatedBy: 1, |
| 88 | + })) |
| 89 | + .then(() => models.ProjectMember.create({ |
| 90 | + userId: 40051331, |
| 91 | + projectId: project1.id, |
| 92 | + role: 'customer', |
| 93 | + isPrimary: true, |
| 94 | + createdBy: 1, |
| 95 | + updatedBy: 1, |
| 96 | + })) |
| 97 | + .then(() => { |
| 98 | + done(); |
| 99 | + }); |
| 100 | + }); |
| 101 | + |
| 102 | + after((done) => { |
| 103 | + testUtil.clearDb(done); |
| 104 | + }); |
| 105 | + |
| 106 | + describe('GET /projects/{id}/reports/embed', () => { |
| 107 | + let sandbox; |
| 108 | + beforeEach(() => { |
| 109 | + sandbox = sinon.sandbox.create(); |
| 110 | + }); |
| 111 | + afterEach(() => { |
| 112 | + sandbox.restore(); |
| 113 | + }); |
| 114 | + |
| 115 | + it('should return 403 if user does not have permissions', (done) => { |
| 116 | + request(server) |
| 117 | + .get(`/v5/projects/${project1.id}/reports/embed`) |
| 118 | + .set({ |
| 119 | + Authorization: `Bearer ${testUtil.jwts.member2}`, |
| 120 | + }) |
| 121 | + .expect('Content-Type', /json/) |
| 122 | + .expect(403, done); |
| 123 | + }); |
| 124 | + |
| 125 | + it('should return 403 if project not exist', (done) => { |
| 126 | + request(server) |
| 127 | + .get('/v5/projects/100100/reports/embed') |
| 128 | + .set({ |
| 129 | + Authorization: `Bearer ${testUtil.jwts.member}`, |
| 130 | + }) |
| 131 | + .expect('Content-Type', /json/) |
| 132 | + .expect(403, done); |
| 133 | + }); |
| 134 | + |
| 135 | + it('should return 404 when report name not mock and not in EMBED_REPORTS_MAPPING', (done) => { |
| 136 | + const cfg = sinon.stub(config, 'get'); |
| 137 | + cfg.withArgs('lookerConfig.USE_MOCK').returns(false); |
| 138 | + request(server) |
| 139 | + .get(`/v5/projects/${project1.id}/reports/embed?reportName=random`) |
| 140 | + .set({ |
| 141 | + Authorization: `Bearer ${testUtil.jwts.member}`, |
| 142 | + }) |
| 143 | + .expect(404, (err) => { |
| 144 | + cfg.restore(); |
| 145 | + done(err); |
| 146 | + }); |
| 147 | + }); |
| 148 | + |
| 149 | + it('should return 500 when get admin user error', (done) => { |
| 150 | + const cfg = sinon.stub(config, 'get'); |
| 151 | + const gem = sinon.stub(lookerSerivce, 'generateEmbedUrl', () => 'generatedUrl'); |
| 152 | + cfg.withArgs('lookerConfig.USE_MOCK').returns(false); |
| 153 | + cfg.withArgs('lookerConfig.EMBED_REPORTS_MAPPING').returns('{"mock-concrete-customer": "/embed/looks/2"}'); |
| 154 | + request(server) |
| 155 | + .get(`/v5/projects/${project1.id}/reports/embed?reportName=mock`) |
| 156 | + .set({ |
| 157 | + Authorization: `Bearer ${testUtil.jwts.admin}`, |
| 158 | + }) |
| 159 | + .expect(500, (err) => { |
| 160 | + gem.restore(); |
| 161 | + cfg.restore(); |
| 162 | + done(err); |
| 163 | + }); |
| 164 | + }); |
| 165 | + |
| 166 | + it('should return 404 when the project template is not found', (done) => { |
| 167 | + const cfg = sinon.stub(config, 'get'); |
| 168 | + const gem = sinon.stub(lookerSerivce, 'generateEmbedUrl', () => 'generatedUrl'); |
| 169 | + cfg.withArgs('lookerConfig.USE_MOCK').returns(false); |
| 170 | + cfg.withArgs('lookerConfig.EMBED_REPORTS_MAPPING').returns('{"mock-concrete-customer": "/embed/looks/2"}'); |
| 171 | + request(server) |
| 172 | + .get(`/v5/projects/${project0.id}/reports/embed?reportName=mock`) |
| 173 | + .set({ |
| 174 | + Authorization: `Bearer ${testUtil.jwts.member}`, |
| 175 | + }) |
| 176 | + .expect(404, (err) => { |
| 177 | + gem.restore(); |
| 178 | + cfg.restore(); |
| 179 | + done(err); |
| 180 | + }); |
| 181 | + }); |
| 182 | + |
| 183 | + it('should return generate customer url', (done) => { |
| 184 | + const cfg = sinon.stub(config, 'get'); |
| 185 | + const gem = sinon.stub(lookerSerivce, 'generateEmbedUrl', () => 'generatedUrl'); |
| 186 | + cfg.withArgs('lookerConfig.USE_MOCK').returns(false); |
| 187 | + cfg.withArgs('lookerConfig.EMBED_REPORTS_MAPPING') |
| 188 | + .returns('{"mock-concrete-customer": "/customer/embed/looks/2"}'); |
| 189 | + request(server) |
| 190 | + .get(`/v5/projects/${project1.id}/reports/embed?reportName=mock`) |
| 191 | + .set({ |
| 192 | + Authorization: `Bearer ${testUtil.jwts.member}`, |
| 193 | + }) |
| 194 | + .expect('Content-Type', /json/) |
| 195 | + .expect(200) |
| 196 | + .end((err, res) => { |
| 197 | + gem.restore(); |
| 198 | + cfg.restore(); |
| 199 | + if (err) { |
| 200 | + done(err); |
| 201 | + } else { |
| 202 | + const resJson = res.body; |
| 203 | + should.exist(resJson); |
| 204 | + resJson.should.equal('generatedUrl'); |
| 205 | + const [user, project, member, embedUrl] = gem.lastCall.args; |
| 206 | + user.userId.should.equal(40051331); |
| 207 | + project.should.deep.equal({ id: project1.id }); |
| 208 | + member.userId.should.equal(40051331); |
| 209 | + member.role.should.equal('customer'); |
| 210 | + embedUrl.should.equal('/customer/embed/looks/2'); |
| 211 | + done(); |
| 212 | + } |
| 213 | + }); |
| 214 | + }); |
| 215 | + |
| 216 | + it('should return generate admin url', (done) => { |
| 217 | + const cfg = sinon.stub(config, 'get'); |
| 218 | + const gem = sinon.stub(lookerSerivce, 'generateEmbedUrl', () => 'generatedUrl'); |
| 219 | + const getAdmin = sinon.stub(util, 'getTopcoderUser', () => ({ |
| 220 | + firstName: 'fn', |
| 221 | + lastName: 'ln', |
| 222 | + userId: 40051333, |
| 223 | + })); |
| 224 | + cfg.withArgs('lookerConfig.USE_MOCK').returns(false); |
| 225 | + cfg.withArgs('lookerConfig.EMBED_REPORTS_MAPPING').returns('{"mock-concrete-topcoder": "/admin/embed/looks/2"}'); |
| 226 | + request(server) |
| 227 | + .get(`/v5/projects/${project1.id}/reports/embed?reportName=mock`) |
| 228 | + .set({ |
| 229 | + Authorization: `Bearer ${testUtil.jwts.admin}`, |
| 230 | + }) |
| 231 | + .expect('Content-Type', /json/) |
| 232 | + .expect(200) |
| 233 | + .end((err, res) => { |
| 234 | + getAdmin.restore(); |
| 235 | + gem.restore(); |
| 236 | + cfg.restore(); |
| 237 | + if (err) { |
| 238 | + done(err); |
| 239 | + } else { |
| 240 | + const resJson = res.body; |
| 241 | + should.exist(resJson); |
| 242 | + resJson.should.equal('generatedUrl'); |
| 243 | + const [user, project, member, embedUrl] = gem.lastCall.args; |
| 244 | + user.userId.should.equal(40051333); |
| 245 | + project.should.deep.equal({ id: project1.id }); |
| 246 | + member.userId.should.equal(40051333); |
| 247 | + member.firstName.should.equal('fn'); |
| 248 | + member.lastName.should.equal('ln'); |
| 249 | + member.role.should.equal(''); |
| 250 | + embedUrl.should.equal('/admin/embed/looks/2'); |
| 251 | + done(); |
| 252 | + } |
| 253 | + }); |
| 254 | + }); |
| 255 | + |
| 256 | + it('should return generate copilot url', (done) => { |
| 257 | + const cfg = sinon.stub(config, 'get'); |
| 258 | + const gem = sinon.stub(lookerSerivce, 'generateEmbedUrl', () => 'generatedUrl'); |
| 259 | + cfg.withArgs('lookerConfig.USE_MOCK').returns(false); |
| 260 | + cfg.withArgs('lookerConfig.EMBED_REPORTS_MAPPING').returns('{"mock-concrete-copilot": "/copilot/embed/looks/2"}'); |
| 261 | + request(server) |
| 262 | + .get(`/v5/projects/${project1.id}/reports/embed?reportName=mock`) |
| 263 | + .set({ |
| 264 | + Authorization: `Bearer ${testUtil.jwts.copilot}`, |
| 265 | + }) |
| 266 | + .expect('Content-Type', /json/) |
| 267 | + .expect(200) |
| 268 | + .end((err, res) => { |
| 269 | + gem.restore(); |
| 270 | + cfg.restore(); |
| 271 | + if (err) { |
| 272 | + done(err); |
| 273 | + } else { |
| 274 | + const resJson = res.body; |
| 275 | + should.exist(resJson); |
| 276 | + resJson.should.equal('generatedUrl'); |
| 277 | + const [user, project, member, embedUrl] = gem.lastCall.args; |
| 278 | + user.userId.should.equal(40051332); |
| 279 | + project.should.deep.equal({ id: project1.id }); |
| 280 | + member.userId.should.equal(40051332); |
| 281 | + member.role.should.equal('copilot'); |
| 282 | + embedUrl.should.equal('/copilot/embed/looks/2'); |
| 283 | + done(); |
| 284 | + } |
| 285 | + }); |
| 286 | + }); |
| 287 | + }); |
| 288 | +}); |
0 commit comments