-
Notifications
You must be signed in to change notification settings - Fork 28
Added api/AuditLog.js and a test case to frontend.test.js #1864
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
Open
Pikalot
wants to merge
3
commits into
dev
Choose a base branch
from
devTA_audit_log
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,158 @@ | ||
| process.env.NODE_ENV = 'test'; | ||
|
|
||
| const User = require('../../api/main_endpoints/models/User.js'); | ||
| const AuditLog = require('../../api/main_endpoints/models/AuditLog.js'); | ||
|
|
||
| const chai = require('chai'); | ||
|
|
||
| const chaiHttp = require('chai-http'); | ||
| const { | ||
| UNAUTHORIZED, | ||
| } = require('../../api/util/constants.js').STATUS_CODES; | ||
| const SceApiTester = require('../util/tools/SceApiTester.js'); | ||
|
|
||
| let app = null; | ||
| let test = null; | ||
| const expect = chai.expect; | ||
| const tools = require('../util/tools/tools.js'); | ||
| const { | ||
| setTokenStatus, | ||
| resetTokenMock, | ||
| restoreTokenMock, | ||
| initializeTokenMock | ||
| } = require('../util/mocks/TokenValidFunctions.js'); | ||
| const { | ||
| setDiscordAPIStatus, | ||
| resetDiscordAPIMock, | ||
| restoreDiscordAPIMock, | ||
| initializeDiscordAPIMock | ||
| } = require('../util/mocks/DiscordApiFunction.js'); | ||
| const { MEMBERSHIP_STATE } = require('../../api/util/constants.js'); | ||
| const AuditLogActions = require('../../api/main_endpoints/util/auditLogActions.js'); | ||
|
|
||
| chai.should(); | ||
| chai.use(chaiHttp); | ||
|
|
||
| describe('AuditLog', () => { | ||
| before(done => { | ||
| initializeTokenMock(); | ||
| initializeDiscordAPIMock(); | ||
| app = tools.initializeServer([ | ||
| __dirname + '/../../api/main_endpoints/routes/AuditLog.js' | ||
| ]); | ||
| test = new SceApiTester(app); | ||
|
|
||
| tools.emptySchema(User); | ||
| tools.emptySchema(AuditLog); | ||
| const testUser = new User({ | ||
| email: '[email protected]', | ||
| password: 'Passw0rd', | ||
| firstName: 'firstName', | ||
| lastName: 'lastName', | ||
| major: 'Software Engineering', | ||
| }); | ||
| testUser.save(); | ||
| const testLog = new AuditLog({ | ||
| userId: testUser._id, | ||
| action: AuditLogActions.LOG_IN, | ||
| documentId: testUser._id, | ||
| details: {email: testUser.email }, | ||
| }); | ||
| testLog.save(); | ||
| done(); | ||
| }); | ||
|
|
||
| after(done => { | ||
| resetTokenMock(); | ||
| restoreDiscordAPIMock(); | ||
| tools.terminateServer(done); | ||
| }); | ||
|
|
||
| beforeEach(() => { | ||
| setTokenStatus(false); | ||
| setDiscordAPIStatus(false); | ||
| }); | ||
|
|
||
| afterEach(async () => { | ||
| restoreTokenMock(); | ||
| resetTokenMock(); | ||
| restoreDiscordAPIMock(); | ||
| resetDiscordAPIMock(); | ||
| }); | ||
|
|
||
| const token = ''; | ||
|
|
||
| describe('GET /getAuditLogs', () => { | ||
| const url = '/api/AuditLog/getAuditLogs/'; | ||
|
|
||
| it('Should return status code 401 if no token is passed through', async () => { | ||
| setTokenStatus(false); | ||
| const result = await test.sendGetRequest(url); | ||
| expect(result).to.have.status(UNAUTHORIZED); | ||
| }); | ||
|
|
||
| it('Should return status code 401 if access level is invalid', async () => { | ||
| setTokenStatus(false, { accessLevel: MEMBERSHIP_STATE.MEMBER }); | ||
| const result = await test.sendGetRequestWithToken(token, url); | ||
| expect(result).to.have.status(UNAUTHORIZED); | ||
| }); | ||
|
|
||
| it('Should return at most 50 records when query is an empty string and access level is valid', async () => { | ||
| setTokenStatus(true, { accessLevel: MEMBERSHIP_STATE.OFFICER}); | ||
|
|
||
| before(async () => { | ||
| const newUser = new User({ | ||
| email: '[email protected]', | ||
| password: 'Passw0rd', | ||
| firstName: 'first name', | ||
| lastName: 'last name', | ||
| major: 'Software Engineering', | ||
| }); | ||
| newUser.save(); | ||
|
|
||
| for (let i = 0; i < 3; i++) { | ||
| await AuditLog.create({ | ||
| userId: newUser._id, | ||
| action: AuditLogActions.RESET_PW, | ||
| documentId: newUser._id, | ||
| details: {email: newUser.email }, | ||
| }); | ||
| } | ||
|
|
||
| for (let i = 0; i < 60; i++) { | ||
| await AuditLog.create({ | ||
| userId: newUser._id, | ||
| action: AuditLogActions.EMAIL_SENT, | ||
| documentId: newUser._id, | ||
| details: {email: newUser.email }, | ||
| }); | ||
| } | ||
| }); | ||
| const result = await test.sendGetRequestWithToken(token, url); | ||
| expect(result.body.items).that.is.an('array'); | ||
| expect(result.body.items.length).at.most(50); | ||
| }); | ||
|
|
||
| it('Should return the testUser when query is "[email protected]" and access level is OFFICER', async () => { | ||
| setTokenStatus(true, { accessLevel: MEMBERSHIP_STATE.OFFICER}); | ||
| const search = '[email protected]'; | ||
| const fullUrl = `/api/AuditLog/getAuditLogs?search=${encodeURIComponent(search)}`; | ||
| const result = await test.sendGetRequestWithToken(token, fullUrl); | ||
| expect(result.body.items).that.is.an('array').to.have.lengthOf(1); | ||
| expect(result.body.items[0].userId.email).to.eql('[email protected]'); | ||
| }); | ||
|
|
||
| it('Should return an empty array when the query matches no record: "[email protected]" and access level is ADMIN', async () => { | ||
| setTokenStatus(true, { accessLevel: MEMBERSHIP_STATE.ADMIN}); | ||
| const search = '[email protected]'; | ||
| const fullUrl = `/api/AuditLog/getAuditLogs?search=${encodeURIComponent(search)}`; | ||
| const result = await test.sendGetRequestWithToken(token, fullUrl); | ||
| expect(result.body.items).that.is.an('array').that.is.empty; | ||
| }); | ||
|
|
||
| after(async () => { | ||
| await User.deleteMany({}); | ||
| await AuditLog.deleteMany({}); | ||
| }); | ||
| }); | ||
| }); | ||
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
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.
what are these loops for?
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.
The loops create 63 records. The test checks that, when the query is empty, the route returns at most 50 records from the first page.