Skip to content

Commit

Permalink
feat: add more unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
chasewoo committed Aug 25, 2024
1 parent 55e8101 commit 97c7ca1
Show file tree
Hide file tree
Showing 16 changed files with 490 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import { RoleGeneratorService } from '../../../identity/role-generator.service';
import { TokenVerifyService } from '../../../identity/token-verify.service';
import { SystemUserService } from '../../../system-source/system-user.service';

import { AuthorizationService } from './authorization.service';
import {
anonymousUserToken,
invalidOrgNameToken,
Expand All @@ -20,7 +19,8 @@ import {
validCfoUserToken,
validCooUserToken,
validOwnerUserToken,
} from './authorization.service.test-helper';
} from './__mocks__/authorization.service.test-helper';
import { AuthorizationService } from './authorization.service';

describe('AuthorizationService', () => {
let authorizationService: AuthorizationService;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { BadRequestException } from '@nestjs/common';

import { getResourceNodes } from './authorization.utils';

describe('authorization.utils', () => {
Expand All @@ -12,4 +14,30 @@ describe('authorization.utils', () => {
resourceId: undefined,
});
});

it('should throw BadRequestException for invalid method', () => {
const uri =
'daily-build.shukun.local/apis/v1/source/shukun/vehicles?sort=&select=number,title,enabled,area,_id&count=true&filter={"title": "A78787"}';
expect(() => getResourceNodes('OPTIONS', uri)).toThrow(BadRequestException);
});

it('should throw BadRequestException for invalid resource type', () => {
const uri =
'daily-build.shukun.local/apis/v1/invalid-type/shukun/vehicles?sort=&select=number,title,enabled,area,_id&count=true&filter={"title": "A78787"}';
expect(() => getResourceNodes('GET', uri)).toThrow(BadRequestException);
});

it('should return the correct resource nodes', () => {
const uri =
'daily-build.shukun.local/apis/v1/source/shukun/vehicles?sort=&select=number,title,enabled,area,_id&count=true&filter={"title": "A78787"}';
const output = getResourceNodes('GET', uri);
expect(output).toEqual({
method: 'GET',
resourceType: 'source',
orgName: 'shukun',
resourceName: 'vehicles',
resourceId: undefined,
resourceFunction: undefined,
});
});
});
48 changes: 48 additions & 0 deletions apps/server/src/core/role.service.test.ts
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);
});
});
});
72 changes: 72 additions & 0 deletions apps/server/src/identity/role-generator.service.test.ts
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,
);
});
});
});
44 changes: 44 additions & 0 deletions apps/server/src/identity/token-verify.service.test.ts
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 apps/server/src/system-source/system-group.service.test.ts
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 apps/server/src/system-source/system-position.service.test.ts
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);
});
});
});
52 changes: 52 additions & 0 deletions apps/server/src/system-source/system-user.service.test.ts
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);
});
});
});
Loading

0 comments on commit 97c7ca1

Please sign in to comment.