-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
490 additions
and
3 deletions.
There are no files selected for viewing
File renamed without changes.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import { CodebaseService } from './codebase.service'; | ||
import { RoleService } from './role.service'; | ||
|
||
describe('RoleService', () => { | ||
let roleService: RoleService; | ||
let codebaseService: CodebaseService; | ||
|
||
beforeEach(() => { | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
codebaseService = new (CodebaseService as any)(); | ||
roleService = new RoleService(codebaseService); | ||
}); | ||
|
||
describe('findAll', () => { | ||
it('should return an empty array if no roles are found', async () => { | ||
// Arrange | ||
const orgName = 'exampleOrg'; | ||
jest | ||
.spyOn(codebaseService, 'findByOrgName') | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
.mockResolvedValueOnce(undefined as any); | ||
|
||
// Act | ||
const result = await roleService.findAll(orgName); | ||
|
||
// Assert | ||
expect(result).toEqual([]); | ||
expect(codebaseService.findByOrgName).toHaveBeenCalledWith(orgName); | ||
}); | ||
|
||
it('should return the roles of the application', async () => { | ||
// Arrange | ||
const orgName = 'exampleOrg'; | ||
const roles = [{ name: 'role1' }, { name: 'role2' }]; | ||
jest | ||
.spyOn(codebaseService, 'findByOrgName') | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
.mockResolvedValueOnce({ roles } as any); | ||
|
||
// Act | ||
const result = await roleService.findAll(orgName); | ||
|
||
// Assert | ||
expect(result).toEqual(roles); | ||
expect(codebaseService.findByOrgName).toHaveBeenCalledWith(orgName); | ||
}); | ||
}); | ||
}); |
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,72 @@ | ||
import { SystemGroupService } from '../system-source/system-group.service'; | ||
import { SystemPositionService } from '../system-source/system-position.service'; | ||
|
||
import { RoleGeneratorService } from './role-generator.service'; | ||
|
||
describe('RoleGeneratorService', () => { | ||
let roleGeneratorService: RoleGeneratorService; | ||
let systemGroupService: SystemGroupService; | ||
let systemPositionService: SystemPositionService; | ||
|
||
beforeEach(() => { | ||
systemGroupService = new (SystemGroupService as any)(); | ||
systemPositionService = new (SystemPositionService as any)(); | ||
roleGeneratorService = new RoleGeneratorService( | ||
systemGroupService, | ||
systemPositionService, | ||
); | ||
}); | ||
|
||
describe('getRoleNames', () => { | ||
it('should return an empty array if no groups and positions are found', async () => { | ||
// Arrange | ||
const orgName = 'exampleOrg'; | ||
const userId = 'exampleUserId'; | ||
jest.spyOn(systemGroupService, 'findAll').mockResolvedValueOnce([]); | ||
jest.spyOn(systemPositionService, 'findAll').mockResolvedValueOnce([]); | ||
|
||
// Act | ||
const result = await roleGeneratorService.getRoleNames(orgName, userId); | ||
|
||
// Assert | ||
expect(result).toEqual([]); | ||
expect(systemGroupService.findAll).toHaveBeenCalledWith(orgName, userId); | ||
expect(systemPositionService.findAll).toHaveBeenCalledWith( | ||
orgName, | ||
userId, | ||
); | ||
}); | ||
|
||
it('should return the combined roles from groups and positions', async () => { | ||
// Arrange | ||
const orgName = 'exampleOrg'; | ||
const userId = 'exampleUserId'; | ||
const groups = [{ roles: ['role1', 'role2'] }, { roles: ['role3'] }]; | ||
const positions = [{ roles: ['role4'] }, { roles: ['role5', 'role6'] }]; | ||
jest | ||
.spyOn(systemGroupService, 'findAll') | ||
.mockResolvedValueOnce(groups as any); | ||
jest | ||
.spyOn(systemPositionService, 'findAll') | ||
.mockResolvedValueOnce(positions as any); | ||
|
||
// Act | ||
const result = await roleGeneratorService.getRoleNames(orgName, userId); | ||
|
||
// Assert | ||
expect(result).toEqual([ | ||
'role1', | ||
'role2', | ||
'role3', | ||
'role4', | ||
'role5', | ||
'role6', | ||
]); | ||
expect(systemGroupService.findAll).toHaveBeenCalledWith(orgName, userId); | ||
expect(systemPositionService.findAll).toHaveBeenCalledWith( | ||
orgName, | ||
userId, | ||
); | ||
}); | ||
}); | ||
}); |
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,44 @@ | ||
import { BadRequestException } from '@nestjs/common'; | ||
import { JwtService } from '@nestjs/jwt'; | ||
|
||
import { TokenVerifyService } from './token-verify.service'; | ||
|
||
describe('TokenVerifyService', () => { | ||
let tokenVerifyService: TokenVerifyService; | ||
let jwtService: JwtService; | ||
|
||
beforeEach(() => { | ||
jwtService = new JwtService({}); | ||
tokenVerifyService = new TokenVerifyService(jwtService); | ||
}); | ||
|
||
describe('parse', () => { | ||
it('should return the parsed token if it is valid', () => { | ||
// Arrange | ||
const token = 'validToken'; | ||
const parsedToken = { userId: 'exampleUserId' }; | ||
jest.spyOn(jwtService, 'verify').mockReturnValueOnce(parsedToken); | ||
|
||
// Act | ||
const result = tokenVerifyService.parse(token); | ||
|
||
// Assert | ||
expect(result).toEqual(parsedToken); | ||
expect(jwtService.verify).toHaveBeenCalledWith(token); | ||
}); | ||
|
||
it('should throw a BadRequestException if the token is not valid', () => { | ||
// Arrange | ||
const token = 'invalidToken'; | ||
jest.spyOn(jwtService, 'verify').mockImplementation(() => { | ||
throw new Error(); | ||
}); | ||
|
||
// Act & Assert | ||
expect(() => tokenVerifyService.parse(token)).toThrow( | ||
BadRequestException, | ||
); | ||
expect(jwtService.verify).toHaveBeenCalledWith(token); | ||
}); | ||
}); | ||
}); |
53 changes: 53 additions & 0 deletions
53
apps/server/src/system-source/system-group.service.test.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,53 @@ | ||
import { SystemGroupModel } from '@shukun/schema'; | ||
|
||
import { SourceService } from '../source/source.service'; | ||
|
||
import { SystemGroupService } from './system-group.service'; | ||
|
||
describe('SystemGroupService', () => { | ||
let systemGroupService: SystemGroupService; | ||
let systemGroupSourceService: SourceService<SystemGroupModel>; | ||
|
||
beforeEach(() => { | ||
systemGroupSourceService = { | ||
query: jest.fn(), | ||
} as any; | ||
systemGroupService = new SystemGroupService(systemGroupSourceService); | ||
}); | ||
|
||
describe('findAll', () => { | ||
it('should call systemGroupSourceService.query with the correct parameters', async () => { | ||
// Arrange | ||
const orgName = 'exampleOrgName'; | ||
const userId = 'exampleUserId'; | ||
|
||
// Act | ||
await systemGroupService.findAll(orgName, userId); | ||
|
||
// Assert | ||
expect(systemGroupSourceService.query).toHaveBeenCalledWith( | ||
orgName, | ||
'system__groups', | ||
{ | ||
filter: { users: userId }, | ||
}, | ||
); | ||
}); | ||
|
||
it('should return the result of systemGroupSourceService.query', async () => { | ||
// Arrange | ||
const orgName = 'exampleOrgName'; | ||
const userId = 'exampleUserId'; | ||
const expectedResult = ['group1', 'group2']; | ||
(systemGroupSourceService.query as any).mockResolvedValueOnce( | ||
expectedResult as any, | ||
); | ||
|
||
// Act | ||
const result = await systemGroupService.findAll(orgName, userId); | ||
|
||
// Assert | ||
expect(result).toEqual(expectedResult); | ||
}); | ||
}); | ||
}); |
55 changes: 55 additions & 0 deletions
55
apps/server/src/system-source/system-position.service.test.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,55 @@ | ||
import { SystemPositionModel } from '@shukun/schema'; | ||
|
||
import { SourceService } from '../source/source.service'; | ||
|
||
import { SystemPositionService } from './system-position.service'; | ||
|
||
describe('SystemPositionService', () => { | ||
let systemPositionService: SystemPositionService; | ||
let systemPositionSourceService: SourceService<SystemPositionModel>; | ||
|
||
beforeEach(() => { | ||
systemPositionSourceService = { | ||
query: jest.fn(), | ||
} as any; | ||
systemPositionService = new SystemPositionService( | ||
systemPositionSourceService, | ||
); | ||
}); | ||
|
||
describe('findAll', () => { | ||
it('should call systemPositionSourceService.query with the correct parameters', async () => { | ||
// Arrange | ||
const orgName = 'exampleOrgName'; | ||
const userId = 'exampleUserId'; | ||
|
||
// Act | ||
await systemPositionService.findAll(orgName, userId); | ||
|
||
// Assert | ||
expect(systemPositionSourceService.query).toHaveBeenCalledWith( | ||
orgName, | ||
'system__positions', | ||
{ | ||
filter: { users: userId }, | ||
}, | ||
); | ||
}); | ||
|
||
it('should return the result of systemPositionSourceService.query', async () => { | ||
// Arrange | ||
const orgName = 'exampleOrgName'; | ||
const userId = 'exampleUserId'; | ||
const expectedResult = ['position1', 'position2']; | ||
(systemPositionSourceService.query as any).mockResolvedValueOnce( | ||
expectedResult as any, | ||
); | ||
|
||
// Act | ||
const result = await systemPositionService.findAll(orgName, userId); | ||
|
||
// Assert | ||
expect(result).toEqual(expectedResult); | ||
}); | ||
}); | ||
}); |
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,52 @@ | ||
import { SourceService } from '../source/source.service'; | ||
import { SystemUserModel } from '../util/schema/models/system-users'; | ||
|
||
import { SystemUserService } from './system-user.service'; | ||
|
||
describe('SystemUserService', () => { | ||
let systemUserService: SystemUserService; | ||
let systemUserSourceService: SourceService<SystemUserModel>; | ||
|
||
beforeEach(() => { | ||
systemUserSourceService = { | ||
findOne: jest.fn(), | ||
} as any; | ||
systemUserService = new SystemUserService(systemUserSourceService); | ||
}); | ||
|
||
describe('findOne', () => { | ||
it('should call systemUserSourceService.findOne with the correct parameters', async () => { | ||
// Arrange | ||
const orgName = 'exampleOrgName'; | ||
const userId = 'exampleUserId'; | ||
|
||
// Act | ||
await systemUserService.findOne(orgName, userId); | ||
|
||
// Assert | ||
expect(systemUserSourceService.findOne).toHaveBeenCalledWith( | ||
orgName, | ||
'system__users', | ||
{ | ||
filter: { _id: userId }, | ||
}, | ||
); | ||
}); | ||
|
||
it('should return the result of systemUserSourceService.findOne', async () => { | ||
// Arrange | ||
const orgName = 'exampleOrgName'; | ||
const userId = 'exampleUserId'; | ||
const expectedResult = { name: 'John Doe' }; | ||
(systemUserSourceService.findOne as any).mockResolvedValueOnce( | ||
expectedResult as any, | ||
); | ||
|
||
// Act | ||
const result = await systemUserService.findOne(orgName, userId); | ||
|
||
// Assert | ||
expect(result).toEqual(expectedResult); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.