-
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 #126 from zhumeisongsong/feature/refactor-auth-module
refactor: ♻️ auth module
- Loading branch information
Showing
8 changed files
with
131 additions
and
130 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
export * from './lib/auth.service'; | ||
export * from './lib/use-cases/sign-in.use-case'; |
This file was deleted.
Oops, something went wrong.
70 changes: 70 additions & 0 deletions
70
libs/auth/application/src/lib/use-cases/sign-in.use-case.spec.ts
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,70 @@ | ||
import { Test } from '@nestjs/testing'; | ||
import { JwtService } from '@nestjs/jwt'; | ||
import { UnauthorizedException } from '@nestjs/common'; | ||
import { AwsCognitoService } from '@shared/infrastructure-aws-cognito'; | ||
import { SignInUseCase } from './sign-in.use-case'; | ||
|
||
describe('SignInUseCase', () => { | ||
let signInUseCase: SignInUseCase; | ||
let awsCognitoService: AwsCognitoService; | ||
let jwtService: JwtService; | ||
|
||
beforeEach(async () => { | ||
const moduleRef = await Test.createTestingModule({ | ||
providers: [ | ||
SignInUseCase, | ||
{ | ||
provide: AwsCognitoService, | ||
useValue: { | ||
signIn: jest.fn(), | ||
}, | ||
}, | ||
{ | ||
provide: JwtService, | ||
useValue: { | ||
signAsync: jest.fn(), | ||
}, | ||
}, | ||
], | ||
}).compile(); | ||
|
||
signInUseCase = moduleRef.get<SignInUseCase>(SignInUseCase); | ||
awsCognitoService = moduleRef.get<AwsCognitoService>(AwsCognitoService); | ||
jwtService = moduleRef.get<JwtService>(JwtService); | ||
}); | ||
|
||
describe('execute', () => { | ||
const email = '[email protected]'; | ||
const password = 'password123'; | ||
const mockAccessToken = 'mock.access.token'; | ||
|
||
it('should successfully sign in and return access token', async () => { | ||
jest.spyOn(awsCognitoService, 'signIn').mockResolvedValue(undefined); | ||
jest.spyOn(jwtService, 'signAsync').mockResolvedValue(mockAccessToken); | ||
|
||
const result = await signInUseCase.execute(email, password); | ||
|
||
expect(awsCognitoService.signIn).toHaveBeenCalledWith(email, password); | ||
expect(jwtService.signAsync).toHaveBeenCalledWith({ email }); | ||
expect(result).toEqual({ accessToken: mockAccessToken }); | ||
}); | ||
|
||
it('should throw UnauthorizedException when AWS Cognito sign in fails', async () => { | ||
const error = new Error('Invalid credentials'); | ||
jest.spyOn(awsCognitoService, 'signIn').mockRejectedValue(error); | ||
|
||
await expect(signInUseCase.execute(email, password)).rejects.toThrow( | ||
UnauthorizedException, | ||
); | ||
}); | ||
|
||
it('should throw UnauthorizedException when JWT signing fails', async () => { | ||
jest.spyOn(awsCognitoService, 'signIn').mockResolvedValue(undefined); | ||
jest.spyOn(jwtService, 'signAsync').mockRejectedValue(new Error()); | ||
|
||
await expect(signInUseCase.execute(email, password)).rejects.toThrow( | ||
UnauthorizedException, | ||
); | ||
}); | ||
}); | ||
}); |
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
61 changes: 41 additions & 20 deletions
61
libs/auth/interface-adapters/src/lib/resolver/auth.resolver.spec.ts
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,50 +1,71 @@ | ||
import { Test, TestingModule } from '@nestjs/testing'; | ||
import { AuthService } from '@auth/application'; | ||
import { SignInUseCase } from '@auth/application'; | ||
import { UnauthorizedException } from '@nestjs/common'; | ||
|
||
import { AuthResolver } from './auth.resolver'; | ||
import { SignInInputDto } from '../dto/sign-in-input.dto'; | ||
|
||
describe('AuthResolver', () => { | ||
let resolver: AuthResolver; | ||
let authService: AuthService; | ||
let signInUseCase: SignInUseCase; | ||
|
||
const mockSignInUseCase = { | ||
execute: jest.fn(), | ||
}; | ||
|
||
beforeEach(async () => { | ||
const module: TestingModule = await Test.createTestingModule({ | ||
providers: [ | ||
AuthResolver, | ||
{ | ||
provide: AuthService, | ||
useValue: { | ||
signIn: jest.fn(), | ||
}, | ||
provide: SignInUseCase, | ||
useValue: mockSignInUseCase, | ||
}, | ||
], | ||
}).compile(); | ||
|
||
resolver = module.get<AuthResolver>(AuthResolver); | ||
authService = module.get<AuthService>(AuthService); | ||
signInUseCase = module.get<SignInUseCase>(SignInUseCase); | ||
}); | ||
|
||
it('should be defined', () => { | ||
expect(resolver).toBeDefined(); | ||
afterEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
describe('signIn', () => { | ||
it('should call authService.signIn with correct parameters', async () => { | ||
const signInInput: SignInInputDto = { | ||
email: '[email protected]', | ||
password: 'password123', | ||
}; | ||
const expectedResult = { accessToken: 'testToken' }; | ||
|
||
jest.spyOn(authService, 'signIn').mockResolvedValue(expectedResult); | ||
const signInInput: SignInInputDto = { | ||
email: '[email protected]', | ||
password: 'password123', | ||
}; | ||
|
||
const mockResponse = { | ||
accessToken: 'mock-access-token', | ||
}; | ||
|
||
it('should successfully sign in a user', async () => { | ||
mockSignInUseCase.execute.mockResolvedValue(mockResponse); | ||
|
||
const result = await resolver.signIn(signInInput); | ||
|
||
expect(authService.signIn).toHaveBeenCalledWith( | ||
expect(result).toEqual(mockResponse); | ||
expect(signInUseCase.execute).toHaveBeenCalledWith( | ||
signInInput.email, | ||
signInInput.password, | ||
); | ||
}); | ||
|
||
it('should throw UnauthorizedException when sign in fails', async () => { | ||
mockSignInUseCase.execute.mockRejectedValue( | ||
new UnauthorizedException('Invalid credentials'), | ||
); | ||
|
||
await expect(resolver.signIn(signInInput)).rejects.toThrow( | ||
UnauthorizedException, | ||
); | ||
expect(signInUseCase.execute).toHaveBeenCalledWith( | ||
signInInput.email, | ||
signInInput.password | ||
signInInput.password, | ||
); | ||
expect(result).toEqual(expectedResult); | ||
}); | ||
}); | ||
}); |
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