Skip to content

Commit 714fcbe

Browse files
set cookies in response
1 parent bffb75f commit 714fcbe

File tree

6 files changed

+45
-26
lines changed

6 files changed

+45
-26
lines changed

src/auth/auth.controller.spec.ts

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,15 @@ import { AuthController } from "./auth.controller";
33
import { AuthService } from "./auth.service";
44
import { MockResponse, createResponse } from "node-mocks-http";
55
import { Response } from "express";
6-
6+
import { JwtService } from "@nestjs/jwt";
77

88
describe("User Controller", () => {
99
let controller: AuthController;
1010
let service: AuthService;
1111
const mockUserRequest = {
1212
password: "Sairam1@",
13-
email: "te@test.com"
13+
email: "te@test.com",
1414
};
15-
1615

1716
beforeEach(async () => {
1817
const module: TestingModule = await Test.createTestingModule({
@@ -21,7 +20,16 @@ describe("User Controller", () => {
2120
{
2221
provide: AuthService,
2322
useValue: {
24-
signIn: jest.fn().mockResolvedValue(true)
23+
signIn: jest.fn().mockResolvedValue({
24+
access_token: "eyJhbGciOiJIUzI1NiJ9.sss.aaaaa",
25+
refresh_token: "eyJhbGciOiJIUzI1NiJ9.sss.aaaaa",
26+
}),
27+
},
28+
},
29+
{
30+
provide: JwtService,
31+
useValue: {
32+
signAsync: jest.fn(),
2533
},
2634
},
2735
],
@@ -31,20 +39,20 @@ describe("User Controller", () => {
3139
service = module.get<AuthService>(AuthService);
3240
});
3341

34-
3542
describe("auth controller ()", () => {
36-
3743
it("should create access token", async () => {
38-
let response1: MockResponse<Response> = createResponse();
39-
response1.json = jest.fn();
40-
response1.cookie = jest.fn();
41-
let response = { access_token: "s" };
44+
let mockResponse: MockResponse<Response> = createResponse();
45+
mockResponse.json = jest.fn();
46+
mockResponse.cookie = jest.fn();
47+
const expectedResponse = {
48+
access_token: "eyJhbGciOiJIUzI1NiJ9.sss.aaaaa",
49+
refresh_token: "eyJhbGciOiJIUzI1NiJ9.sss.aaaaa",
50+
};
4251
const createSpy = jest
4352
.spyOn(service, "signIn")
44-
.mockResolvedValueOnce(response);
45-
await controller.signIn(mockUserRequest,response1);
53+
.mockResolvedValueOnce(expectedResponse);
54+
await controller.signIn(mockUserRequest, mockResponse);
4655
expect(createSpy).toHaveBeenCalled();
4756
});
48-
4957
});
5058
});

src/auth/auth.controller.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,10 @@ import {
1111
import { Response,Request } from 'express';
1212
import { AuthService } from './auth.service';
1313
import { Public } from './decorators/public.decorator';
14-
import { GetCurrentUser, GetCurrentUserId } from '../common/decorators';
1514
import { HttpExceptionFilter } from '../utils/http-exception.filter';
1615
import { AuthGuard } from '../common/guards/index';
16+
import { sendResponse } from '../utils/index';
17+
import { statusMessage } from '../constant/statusMessage';
1718

1819
@Controller('auth')
1920
export class AuthController {
@@ -41,7 +42,7 @@ export class AuthController {
4142
secure: true,
4243
});
4344

44-
return token;
45+
return sendResponse(res,HttpStatus.OK,statusMessage[HttpStatus.OK],true,null);
4546
}
4647

4748
// @Public()

src/users/users.controller.spec.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { UserService } from "./users.service";
44
import { CreateUserDto } from "./dto/create-user.dto";
55
import { createResponse, MockResponse } from "node-mocks-http";
66
import { Response } from "express";
7+
import { JwtService } from "@nestjs/jwt";
78

89
describe("User Controller", () => {
910
let controller: UserController;
@@ -64,6 +65,12 @@ describe("User Controller", () => {
6465
findAll: jest.fn().mockResolvedValue(mockResponse),
6566
},
6667
},
68+
{
69+
provide: JwtService,
70+
useValue: {
71+
create: jest.fn().mockResolvedValue(true)
72+
},
73+
},
6774
],
6875
}).compile();
6976

src/users/users.controller.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,8 @@ export class UserController {
2323
const user = await this.userService.create(createCatDto);
2424
return sendResponse(res,HttpStatus.CREATED,statusMessage[HttpStatus.CREATED],true,user);
2525
}
26+
2627
// get user
27-
28-
2928
@UseGuards(AuthGuard)
3029
@Get()
3130
@UseFilters(new HttpExceptionFilter())

src/users/users.module.ts

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
1-
import { Module } from '@nestjs/common';
2-
import { MongooseModule } from '@nestjs/mongoose';
3-
import { UserController } from './users.controller';
4-
import { UserService } from './users.service';
5-
import { User, UserSchema } from './schemas/user.schema';
6-
import { RefresToken, RefresTokenSchema } from './schemas/refreshtoken.schema';
1+
import { Module } from "@nestjs/common";
2+
import { MongooseModule } from "@nestjs/mongoose";
3+
import { UserController } from "./users.controller";
4+
import { UserService } from "./users.service";
5+
import { User, UserSchema } from "./schemas/user.schema";
6+
import { RefresToken, RefresTokenSchema } from "./schemas/refreshtoken.schema";
77

88
@Module({
9-
imports: [MongooseModule.forFeature([{ name: User.name, schema: UserSchema },
10-
{ name: RefresToken.name, schema: RefresTokenSchema }])],
9+
imports: [
10+
MongooseModule.forFeature([
11+
{ name: User.name, schema: UserSchema },
12+
{ name: RefresToken.name, schema: RefresTokenSchema },
13+
]),
14+
],
1115
controllers: [UserController],
1216
providers: [UserService],
1317
exports: [UserService],

src/utils/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { ArgumentsHost, Catch, ExceptionFilter, HttpException } from "@nestjs/common";
1+
import { HttpException } from "@nestjs/common";
22

33
export const errorHandler = (
44
res = null,

0 commit comments

Comments
 (0)