Skip to content

Commit 7589def

Browse files
fix unit testuser service
1 parent c2db722 commit 7589def

File tree

5 files changed

+129
-12
lines changed

5 files changed

+129
-12
lines changed

src/auth/auth.controller.spec.ts

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import { Test, TestingModule } from "@nestjs/testing";
2+
import { AuthController } from "./auth.controller";
3+
import { AuthService } from "./auth.service";
4+
5+
6+
describe("User Controller", () => {
7+
let controller: AuthController;
8+
let service: AuthService;
9+
const mockUserRequest = {
10+
password: "Sairam1@",
11+
email: "te@test.com"
12+
};
13+
14+
15+
beforeEach(async () => {
16+
const module: TestingModule = await Test.createTestingModule({
17+
controllers: [AuthController],
18+
providers: [
19+
{
20+
provide: AuthService,
21+
useValue: {
22+
signIn: jest.fn().mockResolvedValue(true)
23+
},
24+
},
25+
],
26+
}).compile();
27+
28+
controller = module.get<AuthController>(AuthController);
29+
service = module.get<AuthService>(AuthService);
30+
});
31+
32+
33+
describe("auth controller ()", () => {
34+
35+
it("should create access token", async () => {
36+
let response = { access_token: "s" };
37+
const createSpy = jest
38+
.spyOn(service, "signIn")
39+
.mockResolvedValueOnce(response);
40+
await controller.signIn(mockUserRequest);
41+
expect(createSpy).toHaveBeenCalled();
42+
});
43+
44+
});
45+
});

src/auth/auth.controller.ts

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,6 @@ export class AuthController {
1818
@HttpCode(HttpStatus.OK)
1919
@Post('login')
2020
signIn(@Body() signInDto: Record<string, any>) {
21-
2221
return this.authService.signIn(signInDto.email, signInDto.password);
2322
}
24-
25-
@Get('profile')
26-
getProfile(@Request() req) {
27-
return req.user;
28-
}
2923
}

src/auth/auth.module.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import { AuthService } from './auth.service';
1414
JwtModule.register({
1515
global: true,
1616
secret: jwtConstants.secret,
17-
signOptions: { expiresIn: '60s' },
17+
signOptions: { expiresIn: '60s' }
1818
}),
1919
],
2020
providers: [

src/auth/auth.service.spec.ts

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
import { Test, TestingModule } from '@nestjs/testing';
2+
import { AuthService } from './auth.service';
3+
import { UserService } from '../users/users.service';
4+
import { JwtService } from '@nestjs/jwt';
5+
import * as bcrypt from 'bcrypt';
6+
import { UnauthorizedException } from '@nestjs/common/exceptions';
7+
8+
9+
const mockUser = {
10+
email: 'test@example.com',
11+
_id: '12345',
12+
password: 'mockAccessToken', // Hashing the password
13+
};
14+
const authServiceResult = {
15+
access_token: 'mockAccessToken', // Hashing the password
16+
};
17+
18+
describe('AuthService', () => {
19+
let authService: AuthService;
20+
let userService: UserService;
21+
let jwtService: JwtService;
22+
23+
beforeEach(async () => {
24+
const module: TestingModule = await Test.createTestingModule({
25+
providers: [
26+
AuthService,
27+
{
28+
provide: AuthService,
29+
useValue: {
30+
signIn: jest.fn().mockResolvedValueOnce(authServiceResult)
31+
.mockRejectedValueOnce( new UnauthorizedException()), // Error scenario
32+
}
33+
},
34+
{
35+
provide: UserService,
36+
useValue: {
37+
findOneUser: jest.fn().mockResolvedValue(authServiceResult)
38+
}
39+
},
40+
{
41+
provide: JwtService,
42+
useValue: {
43+
compare: jest.fn().mockRejectedValue(false),
44+
signAsync: jest.fn().mockResolvedValue(true)
45+
}
46+
}
47+
48+
],
49+
}).compile();
50+
51+
authService = module.get<AuthService>(AuthService);
52+
userService = module.get<UserService>(UserService);
53+
jwtService = module.get<JwtService>(JwtService);
54+
});
55+
56+
it('should sign in a user and return an access token', async () => {
57+
58+
jest.spyOn(userService, 'findOneUser').mockResolvedValue(mockUser);
59+
jest.spyOn(bcrypt, 'compare').mockResolvedValue(true);
60+
jest.spyOn(jwtService, 'signAsync').mockResolvedValue('access_token');
61+
62+
const result = await authService.signIn('test@example.com', 'password');
63+
64+
expect(result).toBeDefined();
65+
});
66+
67+
it('should throw UnauthorizedException when passwords do not match', async () => {
68+
const hashedPassword = await bcrypt.hash('password', 10);
69+
const mockUser = {
70+
email: 'test@example.com',
71+
_id: '12345',
72+
password: hashedPassword,
73+
};
74+
75+
jest.spyOn(userService, 'findOneUser').mockResolvedValue(mockUser);
76+
jest.spyOn(bcrypt, 'compare').mockResolvedValue(false); // Passwords do not match
77+
});
78+
});

src/auth/auth.service.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,20 @@ import * as bcrypt from 'bcrypt';
88
export class AuthService {
99
constructor(
1010
private usersService: UserService,
11-
private jwtService: JwtService,
11+
private jwtService: JwtService
1212
) {}
1313

1414
async signIn(email: string, pass: string) {
1515
const user = await this.usersService.findOneUser(email);
1616
const match = await bcrypt.compare(pass, user?.password);
17-
18-
if(match){
17+
18+
if (match) {
1919
const payload = { email: user.email, userId: user._id };
20+
const access_token = await this.jwtService.signAsync(payload);
2021
return {
21-
access_token: await this.jwtService.signAsync(payload),
22+
access_token
2223
};
2324
}
2425
throw new UnauthorizedException();
25-
2626
}
2727
}

0 commit comments

Comments
 (0)