-
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.
Merge pull request #5 from kidp2h/develop
Develop
- Loading branch information
Showing
37 changed files
with
416 additions
and
40 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
pnpm lint-staged | ||
pnpm lint-staged && pnpm test | ||
|
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 |
---|---|---|
|
@@ -82,9 +82,7 @@ | |
"lint-staged": { | ||
"*.ts": [ | ||
"pnpm lint", | ||
"pnpm format", | ||
"pnpm run test", | ||
"pnpm run test:e2e" | ||
"pnpm format" | ||
] | ||
} | ||
} |
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,46 @@ | ||
import { AuthFacadeUsecase } from '@/domain/auth'; | ||
import { AuthorizeUsecase } from '@/domain/auth/usecases'; | ||
import { UserRepository } from '@/domain/user'; | ||
import { AuthController } from '@/presentation/controllers/auth'; | ||
|
||
import { UserModel } from '@/infrastructure/typeorm/models'; | ||
import { UserEntity } from '@/domain/user'; | ||
import { UserRepositoryImpl } from '@/infrastructure/typeorm/repositories'; | ||
import { Module } from '@nestjs/common'; | ||
import { Mapper } from '@/core'; | ||
import { UserMapper } from '@/infrastructure/typeorm/mappers'; | ||
import { CacheService } from '@/infrastructure/redis/cache'; | ||
import { TypeOrmModule } from '@nestjs/typeorm'; | ||
|
||
@Module({}) | ||
export class AuthFacadeModule { | ||
static register() { | ||
return { | ||
module: AuthFacadeModule, | ||
imports: [TypeOrmModule.forFeature([UserModel])], | ||
providers: [ | ||
AuthFacadeUsecase, | ||
AuthorizeUsecase, | ||
|
||
{ | ||
provide: UserRepository, | ||
useClass: UserRepositoryImpl, | ||
}, | ||
|
||
{ | ||
provide: Mapper<UserEntity, UserModel>, | ||
useClass: UserMapper, | ||
}, | ||
], | ||
exports: [AuthFacadeUsecase, AuthorizeUsecase], | ||
}; | ||
} | ||
} | ||
|
||
@Module({ | ||
imports: [AuthFacadeModule.register()], | ||
controllers: [AuthController], | ||
providers: [CacheService], | ||
exports: [], | ||
}) | ||
export class AuthModule {} |
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 |
---|---|---|
@@ -1,3 +1,5 @@ | ||
export { PresentationModule } from './presentation.module'; | ||
export { TypeOrmModule } from './typeorm.module'; | ||
export { AppModule } from './app.module'; | ||
export { AuthModule } from './auth.module'; | ||
export { UserModule } from './user.module'; |
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,9 @@ | ||
import { Length } from 'class-validator'; | ||
|
||
export class AuthorizeDto { | ||
@Length(4, 20) | ||
username: string; | ||
|
||
@Length(6) | ||
password: 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
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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
export abstract class Mapper<D, E> { | ||
abstract toEntity(entity: Partial<E>): Partial<D>; | ||
abstract toModel(domain: Partial<D>): Partial<E>; | ||
export abstract class Mapper<E, M> { | ||
abstract toEntity(entity: Partial<M>): Partial<E>; | ||
abstract toModel(domain: Partial<E>): Partial<M>; | ||
} |
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 |
---|---|---|
@@ -1,3 +1,3 @@ | ||
export abstract class Usecase<T> { | ||
export abstract class Usecase<T = unknown> { | ||
abstract execute(...args: any[]): Promise<Partial<T>>; | ||
} |
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 { Inject, Injectable } from '@nestjs/common'; | ||
import { AuthorizeUsecase } from './usecases'; | ||
import { AuthorizeDto } from '@/application/dtos/auth/authorize.dto'; | ||
|
||
@Injectable() | ||
export class AuthFacadeUsecase { | ||
constructor( | ||
@Inject(AuthorizeUsecase) | ||
private readonly authorizeUsecase: AuthorizeUsecase, | ||
) {} | ||
|
||
authorize(payload: AuthorizeDto) { | ||
return this.authorizeUsecase.execute(payload); | ||
} | ||
} |
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,3 @@ | ||
import { Usecase } from '@/core'; | ||
|
||
export abstract class AuthUsecase extends Usecase {} |
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,2 @@ | ||
export { AuthUsecase } from './auth.usecase'; | ||
export { AuthFacadeUsecase } from './auth.facade.usecase'; |
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,65 @@ | ||
import { Test } from '@nestjs/testing'; | ||
import { AuthUsecase } from '../auth.usecase'; | ||
import { AuthorizeUsecase } from './authorize.usecase'; | ||
import { UserEntity, UserRepository } from '@/domain/user'; | ||
import { faker } from '@faker-js/faker'; | ||
import bcrypt from 'bcryptjs'; | ||
|
||
const mockPayload = { | ||
username: faker.internet.userName(), | ||
password: faker.internet.password(), | ||
}; | ||
const mockUser = { | ||
id: faker.string.uuid(), | ||
username: mockPayload.username, | ||
password: bcrypt.hashSync(mockPayload.password, 10), | ||
createdAt: new Date(), | ||
deletedAt: null, | ||
updatedAt: new Date(), | ||
}; | ||
|
||
describe('AuthorizeUsecase', () => { | ||
let usecase: AuthUsecase; | ||
let repository: UserRepository; | ||
|
||
beforeEach(async () => { | ||
const module = await Test.createTestingModule({ | ||
providers: [ | ||
AuthorizeUsecase, | ||
{ | ||
provide: UserRepository, | ||
useValue: { | ||
findOne: jest.fn(), | ||
}, | ||
}, | ||
], | ||
}).compile(); | ||
usecase = module.get<AuthUsecase>(AuthorizeUsecase); | ||
repository = module.get<UserRepository>(UserRepository); | ||
}); | ||
|
||
it('should be defined', () => { | ||
expect(usecase).toBeDefined(); | ||
expect(repository).toBeDefined(); | ||
}); | ||
it('should return null if user not found', async () => { | ||
jest.spyOn(repository, 'findOne').mockResolvedValue(null); | ||
expect( | ||
await usecase.execute({ username: 'username', password: 'password' }), | ||
).toBeNull(); | ||
}); | ||
|
||
it('should return user if user found', async () => { | ||
jest.spyOn(repository, 'findOne').mockResolvedValue(mockUser); | ||
const result = (await usecase.execute(mockPayload)) as UserEntity; | ||
expect(result.id).toEqual(mockUser.id); | ||
}); | ||
it('should return null if password is invalid', async () => { | ||
jest.spyOn(repository, 'findOne').mockResolvedValue(mockUser); | ||
const result = (await usecase.execute({ | ||
username: 'test', | ||
password: 'wrong password', | ||
})) as UserEntity; | ||
expect(result).toEqual(null); | ||
}); | ||
}); |
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,22 @@ | ||
import { AuthorizeDto } from '@/application/dtos/auth/authorize.dto'; | ||
import { AuthUsecase } from '@/domain/auth'; | ||
import { UserRepository } from '@/domain/user'; | ||
import { Injectable } from '@nestjs/common'; | ||
import bcrypt from 'bcryptjs'; | ||
|
||
@Injectable() | ||
export class AuthorizeUsecase extends AuthUsecase { | ||
constructor(private readonly userRepository: UserRepository) { | ||
super(); | ||
} | ||
|
||
async execute(payload: AuthorizeDto): Promise<Partial<any>> { | ||
const { username, password } = payload; | ||
const user = await this.userRepository.findOne({ username }); | ||
|
||
if (!user) return null; | ||
const isValidPassword = bcrypt.compareSync(password, user.password); | ||
if (!isValidPassword) return null; | ||
return 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 @@ | ||
export { AuthorizeUsecase } from './authorize.usecase'; |
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 |
---|---|---|
@@ -1,16 +1,20 @@ | ||
import { Entity } from '@/core/entity'; | ||
import { Exclude } from 'class-transformer'; | ||
|
||
export class UserEntity extends Entity { | ||
@Exclude() | ||
public password: string; | ||
constructor( | ||
public id: string, | ||
public username: string, | ||
password: string, | ||
|
||
public createdAt: Date, | ||
public updatedAt: Date, | ||
public deletedAt: Date, | ||
|
||
public password?: string, | ||
) { | ||
super(); | ||
|
||
this.password = password; | ||
} | ||
} |
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
Oops, something went wrong.