Skip to content

Commit 5d96965

Browse files
author
Vikas Agarwal
committed
Unit tests for user reports
Fixed and added couple to project reports
1 parent 6a7c6a3 commit 5d96965

File tree

4 files changed

+325
-20
lines changed

4 files changed

+325
-20
lines changed

src/routes/projectReports/getEmbedReport.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ module.exports = [
2020
let REPORTS = null;
2121
let allowedUsers = null;
2222
try {
23-
allowedUsers = JSON.parse(_.get(config, 'lookerConfig.ALLOWED_USERS', '[]'));
23+
allowedUsers = config.get('lookerConfig.ALLOWED_USERS');
24+
allowedUsers = allowedUsers ? JSON.parse(allowedUsers) : [];
2425
req.log.trace(allowedUsers, 'allowedUsers');
2526
REPORTS = JSON.parse(config.get('lookerConfig.EMBED_REPORTS_MAPPING'));
2627
} catch (error) {

src/routes/projectReports/getEmbedReport.spec.js

Lines changed: 70 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ describe('GET embed report', () => {
164164

165165
it('should return 404 when report name not mock and not in EMBED_REPORTS_MAPPING', (done) => {
166166
const cfg = sinon.stub(config, 'get');
167-
cfg.withArgs('lookerConfig.USE_MOCK').returns(false);
167+
cfg.withArgs('lookerConfig.USE_MOCK').returns('false');
168168
request(server)
169169
.get(`/v5/projects/${project1.id}/reports/embed?reportName=random`)
170170
.set({
@@ -176,10 +176,27 @@ describe('GET embed report', () => {
176176
});
177177
});
178178

179+
it('should return 403 when report name not mock and not in EMBED_REPORTS_MAPPING', (done) => {
180+
const cfg = sinon.stub(config, 'get');
181+
cfg.withArgs('lookerConfig.USE_MOCK').returns('false');
182+
// allows only admin user
183+
cfg.withArgs('lookerConfig.ALLOWED_USERS').returns(`[${testUtil.userIds.admin}]`);
184+
cfg.withArgs('lookerConfig.EMBED_REPORTS_MAPPING').returns('{"mock": "/embed/looks/2"}');
185+
request(server)
186+
.get(`/v5/projects/${project1.id}/reports/embed?reportName=random`)
187+
.set({
188+
Authorization: `Bearer ${testUtil.jwts.member}`,
189+
})
190+
.expect(403, (err) => {
191+
cfg.restore();
192+
done(err);
193+
});
194+
});
195+
179196
it('should return 500 when get admin user error', (done) => {
180197
const cfg = sinon.stub(config, 'get');
181198
const gem = sinon.stub(lookerSerivce, 'generateEmbedUrlForProject', () => 'generatedUrl');
182-
cfg.withArgs('lookerConfig.USE_MOCK').returns(false);
199+
cfg.withArgs('lookerConfig.USE_MOCK').returns('false');
183200
cfg.withArgs('lookerConfig.EMBED_REPORTS_MAPPING').returns('{"mock-concrete-customer": "/embed/looks/2"}');
184201
request(server)
185202
.get(`/v5/projects/${project1.id}/reports/embed?reportName=mock`)
@@ -196,7 +213,7 @@ describe('GET embed report', () => {
196213
it('should return 404 when the project template or product template is not found', (done) => {
197214
const cfg = sinon.stub(config, 'get');
198215
const gem = sinon.stub(lookerSerivce, 'generateEmbedUrlForProject', () => 'generatedUrl');
199-
cfg.withArgs('lookerConfig.USE_MOCK').returns(false);
216+
cfg.withArgs('lookerConfig.USE_MOCK').returns('false');
200217
cfg.withArgs('lookerConfig.EMBED_REPORTS_MAPPING').returns('{"mock-concrete-customer": "/embed/looks/2"}');
201218
request(server)
202219
.get(`/v5/projects/${project0.id}/reports/embed?reportName=mock`)
@@ -210,10 +227,48 @@ describe('GET embed report', () => {
210227
});
211228
});
212229

230+
it('should return mock url', (done) => {
231+
const cfg = sinon.stub(config, 'get');
232+
const gem = sinon.stub(lookerSerivce, 'generateEmbedUrlForProject', () => 'generatedUrl');
233+
const getUser = sinon.stub(util, 'getTopcoderUser', () => ({
234+
firstName: 'fn',
235+
lastName: 'ln',
236+
userId: testUtil.userIds.member,
237+
}));
238+
cfg.withArgs('lookerConfig.USE_MOCK').returns('true');
239+
cfg.withArgs('lookerConfig.EMBED_REPORTS_MAPPING')
240+
.returns('{"mock": "/customer/embed/looks/2"}');
241+
request(server)
242+
.get(`/v5/projects/${project1.id}/reports/embed?reportName=mock`)
243+
.set({
244+
Authorization: `Bearer ${testUtil.jwts.member}`,
245+
})
246+
.expect('Content-Type', /json/)
247+
.expect(200)
248+
.end((err, res) => {
249+
getUser.restore();
250+
gem.restore();
251+
cfg.restore();
252+
if (err) {
253+
done(err);
254+
} else {
255+
const resJson = res.body;
256+
should.exist(resJson);
257+
resJson.should.equal('generatedUrl');
258+
const [user, project, member, embedUrl] = gem.lastCall.args;
259+
user.userId.should.equal(testUtil.userIds.member);
260+
project.should.deep.equal({ id: project1.id });
261+
member.userId.should.equal(testUtil.userIds.member);
262+
embedUrl.should.equal('/customer/embed/looks/2');
263+
done();
264+
}
265+
});
266+
});
267+
213268
it('should return customer url', (done) => {
214269
const cfg = sinon.stub(config, 'get');
215270
const gem = sinon.stub(lookerSerivce, 'generateEmbedUrlForProject', () => 'generatedUrl');
216-
cfg.withArgs('lookerConfig.USE_MOCK').returns(false);
271+
cfg.withArgs('lookerConfig.USE_MOCK').returns('false');
217272
cfg.withArgs('lookerConfig.EMBED_REPORTS_MAPPING')
218273
.returns('{"mock-concrete-customer": "/customer/embed/looks/2"}');
219274
request(server)
@@ -233,9 +288,9 @@ describe('GET embed report', () => {
233288
should.exist(resJson);
234289
resJson.should.equal('generatedUrl');
235290
const [user, project, member, embedUrl] = gem.lastCall.args;
236-
user.userId.should.equal(40051331);
291+
user.userId.should.equal(testUtil.userIds.member);
237292
project.should.deep.equal({ id: project1.id });
238-
member.userId.should.equal(40051331);
293+
member.userId.should.equal(testUtil.userIds.member);
239294
member.role.should.equal('customer');
240295
embedUrl.should.equal('/customer/embed/looks/2');
241296
done();
@@ -251,7 +306,7 @@ describe('GET embed report', () => {
251306
lastName: 'ln',
252307
userId: 40051333,
253308
}));
254-
cfg.withArgs('lookerConfig.USE_MOCK').returns(false);
309+
cfg.withArgs('lookerConfig.USE_MOCK').returns('false');
255310
cfg.withArgs('lookerConfig.EMBED_REPORTS_MAPPING').returns('{"mock-concrete-topcoder": "/admin/embed/looks/2"}');
256311
request(server)
257312
.get(`/v5/projects/${project1.id}/reports/embed?reportName=mock`)
@@ -271,9 +326,9 @@ describe('GET embed report', () => {
271326
should.exist(resJson);
272327
resJson.should.equal('generatedUrl');
273328
const [user, project, member, embedUrl] = gem.lastCall.args;
274-
user.userId.should.equal(40051333);
329+
user.userId.should.equal(testUtil.userIds.admin);
275330
project.should.deep.equal({ id: project1.id });
276-
member.userId.should.equal(40051333);
331+
member.userId.should.equal(testUtil.userIds.admin);
277332
member.firstName.should.equal('fn');
278333
member.lastName.should.equal('ln');
279334
member.role.should.equal('');
@@ -286,7 +341,7 @@ describe('GET embed report', () => {
286341
it('should return copilot url', (done) => {
287342
const cfg = sinon.stub(config, 'get');
288343
const gem = sinon.stub(lookerSerivce, 'generateEmbedUrlForProject', () => 'generatedUrl');
289-
cfg.withArgs('lookerConfig.USE_MOCK').returns(false);
344+
cfg.withArgs('lookerConfig.USE_MOCK').returns('false');
290345
cfg.withArgs('lookerConfig.EMBED_REPORTS_MAPPING').returns('{"mock-concrete-copilot": "/copilot/embed/looks/2"}');
291346
request(server)
292347
.get(`/v5/projects/${project1.id}/reports/embed?reportName=mock`)
@@ -305,9 +360,9 @@ describe('GET embed report', () => {
305360
should.exist(resJson);
306361
resJson.should.equal('generatedUrl');
307362
const [user, project, member, embedUrl] = gem.lastCall.args;
308-
user.userId.should.equal(40051332);
363+
user.userId.should.equal(testUtil.userIds.copilot);
309364
project.should.deep.equal({ id: project1.id });
310-
member.userId.should.equal(40051332);
365+
member.userId.should.equal(testUtil.userIds.copilot);
311366
member.role.should.equal('copilot');
312367
embedUrl.should.equal('/copilot/embed/looks/2');
313368
done();
@@ -323,7 +378,7 @@ describe('GET embed report', () => {
323378
lastName: 'ln',
324379
userId: 40051333,
325380
}));
326-
cfg.withArgs('lookerConfig.USE_MOCK').returns(false);
381+
cfg.withArgs('lookerConfig.USE_MOCK').returns('false');
327382
cfg.withArgs('lookerConfig.EMBED_REPORTS_MAPPING')
328383
.returns('{"mock-prodCut-topcoder": "/admin/embed/looks/3"}');
329384
request(server)
@@ -344,9 +399,9 @@ describe('GET embed report', () => {
344399
should.exist(resJson);
345400
resJson.should.equal('generatedUrl');
346401
const [user, project, member, embedUrl] = gem.lastCall.args;
347-
user.userId.should.equal(40051333);
402+
user.userId.should.equal(testUtil.userIds.admin);
348403
project.should.deep.equal({ id: project3.id });
349-
member.userId.should.equal(40051333);
404+
member.userId.should.equal(testUtil.userIds.admin);
350405
member.firstName.should.equal('fn');
351406
member.lastName.should.equal('ln');
352407
member.role.should.equal('');

src/routes/userReports/getEmbedReport.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import _ from 'lodash';
44
import { middleware as tcMiddleware } from 'tc-core-library-js';
55
import util from '../../util';
66
import { USER_ROLE, ADMIN_ROLES, MANAGER_ROLES } from '../../constants';
7-
import models from '../../models';
87
import lookerSerivce from '../../services/lookerService';
98

109
const permissions = tcMiddleware.permissions;
@@ -18,7 +17,8 @@ module.exports = [
1817
let REPORTS = null;
1918
let allowedUsers = null;
2019
try {
21-
allowedUsers = JSON.parse(_.get(config, 'lookerConfig.ALLOWED_USERS', '[]'));
20+
allowedUsers = config.get('lookerConfig.ALLOWED_USERS');
21+
allowedUsers = allowedUsers ? JSON.parse(allowedUsers) : [];
2222
req.log.trace(allowedUsers, 'allowedUsers');
2323
REPORTS = JSON.parse(config.get('lookerConfig.EMBED_REPORTS_MAPPING'));
2424
} catch (error) {
@@ -49,8 +49,7 @@ module.exports = [
4949
if (!mockReport) {
5050
if (util.hasRoles(req, [USER_ROLE.COPILOT])) {
5151
roleKey = 'copilot';
52-
}
53-
if (isAdmin || util.hasRoles(req, MANAGER_ROLES)) {
52+
} else if (isAdmin || util.hasRoles(req, MANAGER_ROLES)) {
5453
roleKey = 'topcoder';
5554
} else {
5655
roleKey = 'customer';

0 commit comments

Comments
 (0)