User context covers authentication, registration, getCurrentUser and updateUser.
All tests are located in the /test directory.
E2E tests are located with the name like: user.e2e-spec.ts.
We can test e2e test cases with npm run test:e2e.
E2E tests use sqlite in-memory database for speed. The config is located in /config/typeorm.config.ts.
Unit tests are located in the directory named after the contexts. (ex: /test/user/user.controller.spec.ts)
We can test unit test cases with npm run test.
Authenticates the user via JWT using @nestjs/jwt.
The config is located in /config/jwt.config.ts.
You can verify the token using AuthMiddleware andAuthGuard. Check user.module.ts.
You can access the value of the token via user.decorator.ts.
Validates the received body via ValidatorPipe in app.pipe.ts and class-validator.
If the received data is not valid, the pipe throws the HTTP exception with UnprocessableEntity status code.
Validation rules are defined via class-validator. Check user.dto.ts.
Todo
Better error design.
UserService returns Result<T, E> where E is HttpException. And UserController just throws the errors.
Is there any better design for it?
Better strategies for tests.
The test in this PR does not test whether the UserService hashes the given password before creating.
Better validation.
To validate the received body with class-validator for DTOs requires a lot of duplicated conditions. For example, the password field of UserCreateDto and UserUpdateDto should have the same validating rules, but the codes are just copied.
Context
refer: #1, #2.
Goal
Detailed
/test
directory.user.e2e-spec.ts
.npm run test:e2e
./config/typeorm.config.ts
./test/user/user.controller.spec.ts
)npm run test
.@nestjs/jwt
./config/jwt.config.ts
.AuthMiddleware
andAuthGuard
. Checkuser.module.ts
.user.decorator.ts
.ValidatorPipe
inapp.pipe.ts
andclass-validator
.class-validator
. Checkuser.dto.ts
.Todo
UserService
returnsResult<T, E>
whereE
isHttpException
. AndUserController
just throws the errors. Is there any better design for it?class-validator
for DTOs requires a lot of duplicated conditions. For example, the password field ofUserCreateDto
andUserUpdateDto
should have the same validating rules, but the codes are just copied.