Skip to content

Commit b252c3c

Browse files
committed
auth module all tests ok
1 parent 6acf83b commit b252c3c

File tree

11 files changed

+213
-202
lines changed

11 files changed

+213
-202
lines changed

Diff for: .env.development

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ DB_TYPE=postgres
77
DB_HOST=localhost
88
DB_PORT=5432
99
DB_USERNAME=postgres
10-
DB_PASSWORD=postgres
10+
DB_PASSWORD=example
1111
DB_NAME=movie_management
1212
DB_SYNCHRONIZE=true
1313
DB_LOGGING=true

Diff for: :memory

16 KB
Binary file not shown.

Diff for: drafts.ts

-180
This file was deleted.

Diff for: package.json

+4-1
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,9 @@
8787
"**/*.(t|j)s"
8888
],
8989
"coverageDirectory": "../coverage",
90-
"testEnvironment": "node"
90+
"testEnvironment": "node",
91+
"moduleNameMapper": {
92+
"^src/(.*)$": "<rootDir>/$1"
93+
}
9194
}
9295
}

Diff for: src/app.module.ts

+16-12
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,21 @@ import { TimeoutInterceptor } from './common/filters/timeout.interceptor';
2121
TypeOrmModule.forRootAsync({
2222
imports: [ConfigModule],
2323
inject: [ConfigService],
24-
useFactory: (configService: ConfigService) => ({
25-
type: configService.get('DB_TYPE', 'postgres') as any,
26-
host: configService.get('DB_HOST', 'localhost'),
27-
port: configService.get('DB_PORT', 5432),
28-
username: configService.get('DB_USERNAME', 'postgres'),
29-
password: configService.get('DB_PASSWORD', 'postgres'),
30-
databse: configService.get('DB_NAME', 'movie_management'),
31-
entities: [__dirname + '/**/*.typeorm-entity{.ts}'],
32-
synchronize: configService.get<boolean>('DB_SYNCHRONIZE', false),
33-
logging: configService.get<boolean>('DB_LOGGING', false)
34-
})
24+
useFactory: (configService: ConfigService) => {
25+
const dbConfig = {
26+
type: configService.get<string>('DB_TYPE', 'postgres') as 'postgres',
27+
host: configService.get<string>('DB_HOST', 'localhost'),
28+
port: configService.get<number>('DB_PORT', 5432),
29+
username: configService.get<string>('DB_USERNAME', 'postgres'),
30+
password: configService.get<string>('DB_PASSWORD', 'example'),
31+
database: configService.get<string>('DB_NAME', 'movie_management'),
32+
entities: [__dirname + '/**/*.typeorm-entity.{ts,js}'],
33+
synchronize: configService.get<boolean>('DB_SYNCHRONIZE', false),
34+
logging: configService.get<boolean>('DB_LOGGING', false),
35+
};
36+
37+
return dbConfig;
38+
},
3539
}),
3640

3741
// Domain modules
@@ -59,7 +63,7 @@ import { TimeoutInterceptor } from './common/filters/timeout.interceptor';
5963
}
6064
],
6165
})
62-
export class AppModule {}
66+
export class AppModule { }
6367

6468

6569
/**

Diff for: src/modules/auth/adapters/primary/rest/user.controller.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import { Roles } from "../../secondary/security/utils/decorators/roles.decorator
1212
export class UserController {
1313
constructor(private readonly userService: Userservice) {}
1414

15-
@Get('id')
15+
@Get(':id')
1616
@Roles('manager') // This attaches metadata('roles': ['manager']) to the method
1717
@ApiOperation({summary: 'Get user by ID (managers only)'})
1818
@ApiResponse({
@@ -31,12 +31,12 @@ export class UserController {
3131
@ApiResponse({status: 401, description: 'Unauthorized'})
3232
@ApiResponse({status: 403, description: 'Forbidden - Requires manager role'})
3333
@ApiResponse({status: 404, description: 'User not found'})
34-
async getUserById(@Param('id') id: 'string') {
34+
async getUserById(@Param('id') id: string) {
3535
const user = await this.userService.getUserById(id);
3636
return {
3737
id: user.getId(),
3838
username: user.getUsername(),
39-
role: user.getRole(),
39+
role: user.getRole().getValue(),
4040
age: user.getAge()
4141
};
4242
}

Diff for: src/modules/auth/adapters/secondary/security/jwt-adapter/jwt.strategy.ts

+1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ export class JwtStrategy extends PassportStrategy(Strategy) {
2727
async validate(payload: any) {
2828
try {
2929
const user = await this.authService.validateUser(payload.sub);
30+
// The returned user object is attached to the request as req.user
3031
return {
3132
id: user.getId(),
3233
username: user.getUsername(),

Diff for: src/modules/auth/adapters/secondary/security/utils/guards/roles.guard.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { CanActivate, ExecutionContext } from "@nestjs/common";
1+
import { CanActivate, ExecutionContext, Injectable } from "@nestjs/common";
22
import { Reflector } from "@nestjs/core";
33
import { ROLES_KEY } from "../decorators/roles.decorator";
44

@@ -9,6 +9,7 @@ import { ROLES_KEY } from "../decorators/roles.decorator";
99
* @see [1] Reflection metadata system
1010
* @see [2] Execution context types
1111
*/
12+
@Injectable()
1213
export class RolesGuard implements CanActivate {
1314
constructor(private reflector: Reflector) { }
1415

Diff for: src/modules/auth/application/services/user.service.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import { NotFoundException } from "@nestjs/common";
1+
import { Injectable, NotFoundException } from "@nestjs/common";
22
import { IUserRepository } from "../ports/user-repository.interface";
33
import { User } from "../../domain/models/user.entity";
4-
import { use } from "passport";
54

5+
@Injectable()
66
export class Userservice {
77
constructor(private readonly userRepository: IUserRepository) {}
88

Diff for: src/modules/auth/auth.module.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import { UserMapper } from "./adapters/secondary/persistence/user.mapper";
2222
import { UserCreatedHandler } from "./application/event-handlers/user-created.handler";
2323
import { LoginFailedHandler } from "./application/event-handlers/login-failed.handler";
2424
import { JwtStrategy } from "./adapters/secondary/security/jwt-adapter/jwt.strategy";
25+
import { RolesGuard } from "./adapters/secondary/security/utils/guards/roles.guard";
2526

2627
const handlers = [UserCreatedHandler, LoginFailedHandler];
2728

@@ -66,7 +67,10 @@ const handlers = [UserCreatedHandler, LoginFailedHandler];
6667
JwtStrategy,
6768

6869
// Event handlers
69-
...handlers
70+
...handlers,
71+
72+
// Guard
73+
//RolesGuard
7074
],
7175
exports: [AuthService, Userservice]
7276
})

0 commit comments

Comments
 (0)