-
Notifications
You must be signed in to change notification settings - Fork 72
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(authentication-service): filtering option in activity logs (#2049)
* chore(authentication-service): rebase gh-1517 * feat(authentication-service): rebase gh-1517 * feat(authentication-service): add test cases for login activity controller gh-1517 * feat(authentication-service): test cases for login activity controller gh-1517
- Loading branch information
1 parent
d849562
commit aa60b14
Showing
12 changed files
with
367 additions
and
11 deletions.
There are no files selected for viewing
This file contains 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 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
155 changes: 155 additions & 0 deletions
155
...s/authentication-service/src/__tests__/acceptance/login-activity.controller.acceptance.ts
This file contains 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,155 @@ | ||
import {Client, expect} from '@loopback/testlab'; | ||
import {LoginType} from '../../enums'; | ||
import {LoginActivityRepository} from '../../repositories'; | ||
import {TestingApplication} from '../fixtures/application'; | ||
import {token} from '../fixtures/datasources/userCredsAndPermission'; | ||
import {setupApplication} from './test-helper'; | ||
|
||
describe('', () => { | ||
let app: TestingApplication; | ||
let client: Client; | ||
let loginActivityRepo: LoginActivityRepository; | ||
before('setupApplication', async () => { | ||
({app, client} = await setupApplication()); | ||
}); | ||
after(async () => app.stop()); | ||
before(givenLoginActivityRepository); | ||
before(setMockData); | ||
after(deleteMockData); | ||
afterEach(() => { | ||
delete process.env.JWT_ISSUER; | ||
delete process.env.JWT_SECRET; | ||
delete process.env.ENCRYPTION_KEY; | ||
}); | ||
const basePath = '/login-activity'; | ||
const range = | ||
'startDate=2024-03-01T00:00:00.596Z&endDate=2024-04-30T23:59:59.596Z'; | ||
const rangeWithFilter = | ||
'startDate=2024-03-01T00:00:00.596Z&endDate=2024-04-30T23:59:59.596Z&filter={"userIdentity":"actorId","inclusion":true,"userIdentifier":["user1"]}'; | ||
|
||
it('throws error when token is not passed', async () => { | ||
const response = await client.get(`/login-activity/count`).send(); | ||
expect(response.error).to.have.property('status').to.be.equal(401); | ||
}); | ||
|
||
it('returns the count with status code 200', async () => { | ||
await client | ||
.get(`${basePath}/count`) | ||
.set('authorization', `Bearer ${token}`) | ||
.send() | ||
.expect(200); | ||
}); | ||
|
||
it('returns all the data when no filter passed', async () => { | ||
const response = await client | ||
.get(`${basePath}`) | ||
.set('authorization', `Bearer ${token}`) | ||
.send() | ||
.expect(200); | ||
expect(response.body.length).to.be.greaterThan(0); | ||
}); | ||
|
||
it('returns single entry matching the id passed in filter', async () => { | ||
const response = await client | ||
.get(`${basePath}/1`) | ||
.set('authorization', `Bearer ${token}`) | ||
.send() | ||
.expect(200); | ||
expect(response.body).to.have.property('id').to.be.equal(`1`); | ||
}); | ||
|
||
it('returns all the daily active users when no filter is passed ', async () => { | ||
await client | ||
.get(`/active-users/daily?${range}`) | ||
.set('authorization', `Bearer ${token}`) | ||
.send() | ||
.expect(200); | ||
}); | ||
|
||
it('returns all the monthly active users when no filter is passed ', async () => { | ||
await client | ||
.get(`/active-users/monthly?${range}`) | ||
.set('authorization', `Bearer ${token}`) | ||
.send() | ||
.expect(200); | ||
}); | ||
|
||
it('returns only those daily active users that satisfy the passed filter', async () => { | ||
await client | ||
.get(`/active-users/daily?${rangeWithFilter}`) | ||
.set('authorization', `Bearer ${token}`) | ||
.send() | ||
.expect(200); | ||
}); | ||
|
||
it('returns only those monthly active users that satisfy the passed filter', async () => { | ||
await client | ||
.get(`/active-users/monthly?${rangeWithFilter}`) | ||
.set('authorization', `Bearer ${token}`) | ||
.send() | ||
.expect(200); | ||
}); | ||
|
||
async function givenLoginActivityRepository() { | ||
loginActivityRepo = await app.getRepository(LoginActivityRepository); | ||
} | ||
|
||
async function deleteMockData() { | ||
await loginActivityRepo.deleteAll(); | ||
} | ||
async function setMockData() { | ||
const tokenPayload = 'encrypted payload'; | ||
await loginActivityRepo.createAll([ | ||
{ | ||
id: '1', | ||
actor: 'user1', | ||
tenantId: 'tenant1', | ||
loginTime: new Date('2024-03-22T13:28:51.596Z'), | ||
tokenPayload, | ||
loginType: LoginType.ACCESS, | ||
deviceInfo: 'chrome', | ||
ipAddress: 'ipaddress', | ||
}, | ||
{ | ||
id: '2', | ||
actor: 'user1', | ||
tenantId: 'tenant1', | ||
loginTime: new Date('2024-03-22T14:28:51.596Z'), | ||
tokenPayload, | ||
loginType: LoginType.RELOGIN, | ||
deviceInfo: 'chrome', | ||
ipAddress: 'ipaddress', | ||
}, | ||
{ | ||
id: '3', | ||
actor: 'user1', | ||
tenantId: 'tenant1', | ||
loginTime: new Date('2024-03-22T15:28:51.596Z'), | ||
tokenPayload, | ||
loginType: LoginType.LOGOUT, | ||
deviceInfo: 'chrome', | ||
ipAddress: 'ipaddress', | ||
}, | ||
{ | ||
id: '4', | ||
actor: 'user1', | ||
tenantId: 'tenant1', | ||
loginTime: new Date('2024-03-23T13:28:51.596Z'), | ||
tokenPayload, | ||
loginType: LoginType.ACCESS, | ||
deviceInfo: 'chrome', | ||
ipAddress: 'ipaddress', | ||
}, | ||
{ | ||
id: '5', | ||
actor: 'user1', | ||
tenantId: 'tenant1', | ||
loginTime: new Date('2024-04-22T13:28:51.596Z'), | ||
tokenPayload, | ||
loginType: LoginType.ACCESS, | ||
deviceInfo: 'chrome', | ||
ipAddress: 'ipaddress', | ||
}, | ||
]); | ||
} | ||
}); |
24 changes: 24 additions & 0 deletions
24
services/authentication-service/src/__tests__/fixtures/datasources/userCredsAndPermission.ts
This file contains 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,24 @@ | ||
import * as jwt from 'jsonwebtoken'; | ||
import {PermissionKey} from '../../../enums'; | ||
|
||
process.env.JWT_ISSUER = 'test'; | ||
process.env.JWT_SECRET = 'test'; | ||
//User Creds | ||
const User = { | ||
id: 1, | ||
username: 'test_user', | ||
password: 'test_password', | ||
}; | ||
export const testUserPayload = { | ||
...User, | ||
role: 1, | ||
authClientId: 2, | ||
authClientIds: [2], | ||
deleted: false, | ||
userLocked: false, | ||
permissions: [PermissionKey.ViewLoginActivity], | ||
}; | ||
export const token = jwt.sign(testUserPayload, process.env.JWT_SECRET, { | ||
expiresIn: 180000, | ||
issuer: process.env.JWT_ISSUER, | ||
}); |
This file contains 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 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 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 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
Oops, something went wrong.