-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
12d910d
commit 184fdf6
Showing
10 changed files
with
163 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import { Test, TestingModule } from '@nestjs/testing'; | ||
import { Response, Request } from 'express'; | ||
import { Model } from 'mongoose'; | ||
import { getModelToken } from '@nestjs/mongoose'; | ||
|
||
import { User } from '~/schemas/user.schema'; | ||
import { Entity } from '~/types'; | ||
import { UserService } from '~/app/user/user.service'; | ||
import { UserController } from '~/app/user/user.controller'; | ||
import { UpdateUserProfileDto } from '~/app/user/user.dto'; | ||
|
||
describe('UserController', () => { | ||
let userController: UserController; | ||
let userService: UserService; | ||
|
||
const res = { | ||
status: jest.fn().mockReturnThis(), | ||
json: jest.fn(data => data), | ||
} as unknown as Response; | ||
const req = { | ||
user: { | ||
user: { _id: expect.anything() }, | ||
}, | ||
} as unknown as Request; | ||
const body = {} as unknown as UpdateUserProfileDto; | ||
|
||
beforeEach(async () => { | ||
const app: TestingModule = await Test.createTestingModule({ | ||
controllers: [UserController], | ||
providers: [ | ||
UserService, | ||
{ | ||
provide: getModelToken(User.name), | ||
useValue: Model, | ||
}, | ||
], | ||
}).compile(); | ||
|
||
userController = app.get<UserController>(UserController); | ||
userService = app.get<UserService>(UserService); | ||
}); | ||
|
||
afterEach(() => jest.clearAllMocks()); | ||
|
||
describe('update', () => { | ||
const user = {} as Entity<User>; | ||
|
||
it('must return user, access token and refresh token', async () => { | ||
jest.spyOn(userService, 'getByIdUpdate').mockResolvedValue(user); | ||
|
||
const data = await userController.update(body, req, res); | ||
|
||
expect(userService.getByIdUpdate).toHaveBeenCalledTimes(1); | ||
expect(data).toBe(user); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { | ||
Body, | ||
Controller, | ||
HttpStatus, | ||
Put, | ||
Req, | ||
Res, | ||
UseGuards, | ||
} from '@nestjs/common'; | ||
import { ApiResponse, ApiTags } from '@nestjs/swagger'; | ||
import { Request, Response } from 'express'; | ||
|
||
import { UserService } from '~/app/user/user.service'; | ||
import { JwtAuthGuard } from '~/app/auth/auth.guard'; | ||
import { UpdateUserProfileDto } from '~/app/user/user.dto'; | ||
import { Session } from '~/schemas/session.schema'; | ||
import { Entity } from '~/types'; | ||
import { User } from '~/schemas/user.schema'; | ||
|
||
@ApiTags('User') | ||
@Controller('user') | ||
export class UserController { | ||
constructor(private readonly userService: UserService) {} | ||
|
||
@Put('update') | ||
@UseGuards(JwtAuthGuard) | ||
@ApiResponse({ | ||
status: 200, | ||
description: 'Update user profile data', | ||
}) | ||
async update( | ||
@Body() body: UpdateUserProfileDto, | ||
@Req() req: Request, | ||
@Res() res: Response, | ||
) { | ||
const session = req.user as Session; | ||
const user = session.user as Entity<User>; | ||
|
||
const data = await this.userService.getByIdUpdate(user._id, body); | ||
|
||
return res.status(HttpStatus.OK).json(data); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { ApiProperty } from '@nestjs/swagger'; | ||
import { IsNotEmpty, IsOptional } from 'class-validator'; | ||
|
||
export class UpdateUserProfileDto { | ||
@IsOptional() | ||
@IsNotEmpty() | ||
@ApiProperty() | ||
name: string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { MongooseModule } from '@nestjs/mongoose'; | ||
|
||
import { UserController } from '~/app/user/user.controller'; | ||
import { UserService } from '~/app/user/user.service'; | ||
import { User, UserSchema } from '~/schemas/user.schema'; | ||
|
||
@Module({ | ||
imports: [ | ||
MongooseModule.forFeature([{ name: User.name, schema: UserSchema }]), | ||
], | ||
controllers: [UserController], | ||
providers: [UserService], | ||
}) | ||
export class UserModule {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters