Skip to content

Commit d0f66b3

Browse files
committed
Create unit tests for ReadonlyGuard and CRUD operations in GroupsService.
1 parent 26ed6a4 commit d0f66b3

File tree

2 files changed

+109
-0
lines changed

2 files changed

+109
-0
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import { ReadonlyGuard } from './readonly.guard';
2+
import { ExecutionContext, HttpException } from '@nestjs/common';
3+
4+
describe('ReadonlyGuard', () => {
5+
let guard: ReadonlyGuard;
6+
let mockContext: ExecutionContext;
7+
8+
beforeEach(() => {
9+
guard = new ReadonlyGuard();
10+
mockContext = {} as ExecutionContext;
11+
});
12+
13+
afterEach(() => {
14+
delete process.env.KUBERO_READONLY;
15+
});
16+
17+
it('should allow access when KUBERO_READONLY is not "true"', () => {
18+
process.env.KUBERO_READONLY = 'false';
19+
expect(guard.canActivate(mockContext)).toBe(true);
20+
});
21+
22+
it('should throw HttpException when KUBERO_READONLY is "true"', () => {
23+
process.env.KUBERO_READONLY = 'true';
24+
expect(() => guard.canActivate(mockContext)).toThrow(HttpException);
25+
try {
26+
guard.canActivate(mockContext);
27+
} catch (e) {
28+
expect(e.message).toBe('Kubero is in read-only mode');
29+
expect(e.getStatus()).toBe(202);
30+
}
31+
});
32+
});

server/src/groups/groups.service.spec.ts

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,80 @@ describe('GroupService', () => {
1616
expect(service).toBeDefined();
1717
});
1818
});
19+
20+
describe('GroupsService', () => {
21+
let service: GroupsService;
22+
let prismaMock: any;
23+
24+
beforeEach(() => {
25+
prismaMock = {
26+
userGroup: {
27+
findMany: jest.fn(),
28+
create: jest.fn(),
29+
findUnique: jest.fn(),
30+
update: jest.fn(),
31+
delete: jest.fn(),
32+
},
33+
};
34+
service = new GroupsService();
35+
// @ts-ignore
36+
service['prisma'] = prismaMock;
37+
});
38+
39+
it('should find all groups', async () => {
40+
const mockGroups = [{ id: '1', name: 'group1' }];
41+
prismaMock.userGroup.findMany.mockResolvedValueOnce(mockGroups);
42+
const result = await service.findAll();
43+
expect(result).toBe(mockGroups);
44+
expect(prismaMock.userGroup.findMany).toHaveBeenCalledWith({
45+
select: {
46+
id: true,
47+
name: true,
48+
description: true,
49+
createdAt: true,
50+
updatedAt: true,
51+
},
52+
});
53+
});
54+
55+
it('should create a group', async () => {
56+
const mockGroup = { id: '2', name: 'group2', description: 'desc' };
57+
prismaMock.userGroup.create.mockResolvedValueOnce(mockGroup);
58+
const result = await service.create('group2', 'desc');
59+
expect(result).toBe(mockGroup);
60+
expect(prismaMock.userGroup.create).toHaveBeenCalledWith({
61+
data: { name: 'group2', description: 'desc' },
62+
});
63+
});
64+
65+
it('should find a group by id', async () => {
66+
const mockGroup = { id: '3', name: 'group3' };
67+
prismaMock.userGroup.findUnique.mockResolvedValueOnce(mockGroup);
68+
const result = await service.findById('3');
69+
expect(result).toBe(mockGroup);
70+
expect(prismaMock.userGroup.findUnique).toHaveBeenCalledWith({
71+
where: { id: '3' },
72+
});
73+
});
74+
75+
it('should update a group', async () => {
76+
const mockGroup = { id: '4', name: 'group4', description: 'desc4' };
77+
prismaMock.userGroup.update.mockResolvedValueOnce(mockGroup);
78+
const result = await service.update('4', { name: 'group4', description: 'desc4' });
79+
expect(result).toBe(mockGroup);
80+
expect(prismaMock.userGroup.update).toHaveBeenCalledWith({
81+
where: { id: '4' },
82+
data: { name: 'group4', description: 'desc4' },
83+
});
84+
});
85+
86+
it('should delete a group', async () => {
87+
const mockGroup = { id: '5', name: 'group5' };
88+
prismaMock.userGroup.delete.mockResolvedValueOnce(mockGroup);
89+
const result = await service.delete('5');
90+
expect(result).toBe(mockGroup);
91+
expect(prismaMock.userGroup.delete).toHaveBeenCalledWith({
92+
where: { id: '5' },
93+
});
94+
});
95+
});

0 commit comments

Comments
 (0)