-
Notifications
You must be signed in to change notification settings - Fork 56
Hotfix/user level reports #530
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
Merged
Changes from 7 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
4fa1ed4
Adding new endpoint for user level reports
29b71ea
fix: unit tests
maxceem cc63fe8
Merge branch 'develop' into hotfix/user_level_reports
f929bb9
Temporary deployable feature branch
6a7c6a3
Fixing user Id field access
5d96965
Unit tests for user reports
0bb4bd7
Merge branch 'master' into hotfix/user_level_reports
aae6e8f
Removed temp deployment of feature branch
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
/* eslint-disable no-unused-vars */ | ||
import config from 'config'; | ||
import _ from 'lodash'; | ||
import { middleware as tcMiddleware } from 'tc-core-library-js'; | ||
import util from '../../util'; | ||
import { USER_ROLE, ADMIN_ROLES, MANAGER_ROLES } from '../../constants'; | ||
import lookerSerivce from '../../services/lookerService'; | ||
|
||
const permissions = tcMiddleware.permissions; | ||
|
||
|
||
module.exports = [ | ||
async (req, res, next) => { | ||
const mockReport = config.get('lookerConfig.USE_MOCK') === 'true'; | ||
let reportName = mockReport ? 'mock' : req.query.reportName; | ||
const authUser = req.authUser; | ||
let REPORTS = null; | ||
let allowedUsers = null; | ||
try { | ||
allowedUsers = config.get('lookerConfig.ALLOWED_USERS'); | ||
allowedUsers = allowedUsers ? JSON.parse(allowedUsers) : []; | ||
req.log.trace(allowedUsers, 'allowedUsers'); | ||
REPORTS = JSON.parse(config.get('lookerConfig.EMBED_REPORTS_MAPPING')); | ||
} catch (error) { | ||
req.log.error(error); | ||
req.log.debug('Invalid reports mapping. Should be a valid JSON.'); | ||
} | ||
if (!mockReport && !REPORTS) { | ||
return res.status(404).send('Report not found'); | ||
} | ||
|
||
try { | ||
const isAdmin = util.hasRoles(req, ADMIN_ROLES); | ||
const userDisallowed = allowedUsers.length > 0 && !allowedUsers.includes(authUser.userId); | ||
if (userDisallowed) { | ||
req.log.error(`User whitelisting prevented accessing report ${reportName} to ${authUser.userId}`); | ||
return res.status(403).send('User is not allowed to access the report'); | ||
} | ||
const token = await util.getM2MToken(); | ||
const callerUser = await util.getTopcoderUser(authUser.userId, token, req.log); | ||
req.log.trace(callerUser, 'callerUser'); | ||
const member = { | ||
firstName: callerUser.firstName, | ||
lastName: callerUser.lastName, | ||
userId: authUser.userId, | ||
role: '', | ||
}; | ||
let roleKey = ''; | ||
if (!mockReport) { | ||
if (util.hasRoles(req, [USER_ROLE.COPILOT])) { | ||
roleKey = 'copilot'; | ||
} else if (isAdmin || util.hasRoles(req, MANAGER_ROLES)) { | ||
roleKey = 'topcoder'; | ||
} else { | ||
roleKey = 'customer'; | ||
} | ||
reportName = `${reportName}-${roleKey}`; | ||
} | ||
// pick the report based on its name | ||
let result = {}; | ||
const embedUrl = REPORTS[reportName]; | ||
req.log.trace(`Generating embed URL for ${reportName} report, using ${embedUrl} as embed URL.`); | ||
if (embedUrl) { | ||
result = await lookerSerivce.generateEmbedUrlForUser(req.authUser, member, embedUrl); | ||
} else { | ||
return res.status(404).send('Report not found'); | ||
} | ||
|
||
req.log.trace(result); | ||
return res.status(200).json(result); | ||
} catch (err) { | ||
req.log.error(err); | ||
return res.status(500).send(err.toString()); | ||
} | ||
}, | ||
]; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@vikasrohit I'm not sure how does it work. Here
gem.lastCall.args
is used but a few lines above on line250
it's restored bygem.restore()
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, that is valid argument. However, I just copied the unit tests we already have for project reports route and updated them to meet my requirements. :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And strange thing is that, it is working fine. However, I did find one minor issue with unit tests of project reports route, they are still using magic numbers i.e. user ids are used as numbers instead of using them from the variables/constants and I fixed that in my unit tests.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey, @xxcxy could you please help us to clarify this moment how does it work?
first it restores the stub:
and after it uses it for testing:
See initial code d08f9f3#diff-fa4fefc23d4de0d05b580cf93c44207bR206