Skip to content

Commit da54e17

Browse files
SharpDevOps10thebladehit
authored andcommitted
testing: added tests for user service
1 parent 7e3ffcb commit da54e17

File tree

1 file changed

+58
-0
lines changed

1 file changed

+58
-0
lines changed

src/users/users.service.spec.ts

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,4 +102,62 @@ describe('UsersService', () => {
102102
await expect(usersService.getNotApprovedUsers(req)).rejects.toThrow(ForbiddenException);
103103
});
104104
});
105+
106+
describe('getMyInfo', () => {
107+
it('should return the information of the logged-in user', async () => {
108+
const user = await prismaService.user.create({
109+
data: {
110+
111+
password: 'anon1234',
112+
firstName: 'Alex',
113+
lastName: 'Bond',
114+
admin: false,
115+
approved: true,
116+
},
117+
});
118+
119+
const req = {
120+
user: {
121+
userId: user.id,
122+
},
123+
} as unknown as Request;
124+
125+
const result = await usersService.getMyInfo(req);
126+
expect(result).toStrictEqual(user);
127+
});
128+
});
129+
130+
describe('isAdmin', () => {
131+
it('should return true if the user is an admin', async () => {
132+
const admin = await prismaService.user.create({
133+
data: {
134+
135+
password: '123456',
136+
firstName: 'John',
137+
lastName: 'Doe',
138+
admin: true,
139+
approved: true,
140+
},
141+
});
142+
143+
const result = await usersService.isAdmin(admin.id);
144+
expect(result).toBe(true);
145+
});
146+
147+
it('should return false if the user is not an admin', async () => {
148+
const user = await prismaService.user.create({
149+
data: {
150+
151+
password: 'anon1234',
152+
firstName: 'Alex',
153+
lastName: 'Bond',
154+
admin: false,
155+
approved: true,
156+
},
157+
});
158+
159+
const result = await usersService.isAdmin(user.id);
160+
expect(result).toBe(false);
161+
});
162+
});
105163
});

0 commit comments

Comments
 (0)