-
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
Showing
10 changed files
with
77 additions
and
26 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
3 changes: 3 additions & 0 deletions
3
api/src/modules/auth/commands/request-password-recovery.command.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,3 @@ | ||
export class RequestPasswordRecoveryCommand { | ||
constructor(public readonly email: string) {} | ||
} |
32 changes: 32 additions & 0 deletions
32
api/src/modules/auth/commands/request-password-recovery.handler.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,32 @@ | ||
import { CommandHandler, EventBus, ICommandHandler } from '@nestjs/cqrs'; | ||
import { AuthMailer } from '@api/modules/auth/services/auth.mailer'; | ||
import { RequestPasswordRecoveryCommand } from '@api/modules/auth/commands/request-password-recovery.command'; | ||
import { UsersService } from '@api/modules/users/users.service'; | ||
import { PasswordRecoveryRequestedEvent } from '@api/modules/events/user-events/password-recovery-requested.event'; | ||
import { UnauthorizedException } from '@nestjs/common'; | ||
|
||
@CommandHandler(RequestPasswordRecoveryCommand) | ||
export class RequestPasswordRecoveryHandler | ||
implements ICommandHandler<RequestPasswordRecoveryCommand> | ||
{ | ||
constructor( | ||
private readonly users: UsersService, | ||
private readonly authMailer: AuthMailer, | ||
private readonly eventBus: EventBus, | ||
) {} | ||
|
||
async execute(command: RequestPasswordRecoveryCommand): Promise<void> { | ||
const { email } = command; | ||
const user = await this.users.findByEmail(email); | ||
if (!user) { | ||
this.eventBus.publish(new PasswordRecoveryRequestedEvent(email, null)); | ||
throw new UnauthorizedException(); | ||
} | ||
await this.authMailer.sendPasswordRecoveryEmail({ | ||
user, | ||
// TODO: Origin must come from env vars | ||
origin: 'http://localhost:3000', | ||
}); | ||
this.eventBus.publish(new PasswordRecoveryRequestedEvent(email, user.id)); | ||
} | ||
} |
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 |
---|---|---|
@@ -1,22 +1,20 @@ | ||
import { TestManager } from '../../utils/test-manager'; | ||
import { HttpStatus } from '@nestjs/common'; | ||
import { ApiConfigService } from '@api/modules/config/app-config.service'; | ||
import { JwtService } from '@nestjs/jwt'; | ||
import { TOKEN_TYPE_ENUM } from '@shared/schemas/auth/token-type.schema'; | ||
import { authContract } from '@shared/contracts/auth.contract'; | ||
import { ROLES } from '@api/modules/auth/roles.enum'; | ||
import { JwtManager } from '@api/modules/auth/services/jwt.manager'; | ||
|
||
//create-user.feature | ||
|
||
describe('Create Users', () => { | ||
let testManager: TestManager; | ||
let apiConfig: ApiConfigService; | ||
let jwtService: JwtService; | ||
let jwtManager: JwtManager; | ||
|
||
beforeAll(async () => { | ||
testManager = await TestManager.createTestManager(); | ||
apiConfig = testManager.getModule<ApiConfigService>(ApiConfigService); | ||
jwtService = testManager.getModule<JwtService>(JwtService); | ||
jwtManager = testManager.getModule<JwtManager>(JwtManager); | ||
}); | ||
|
||
afterEach(async () => { | ||
|
@@ -30,17 +28,13 @@ describe('Create Users', () => { | |
test('A sign-up token should not be valid if the user bound to that token has already been activated', async () => { | ||
// Given a user exists with valid credentials | ||
// But the user has the role partner | ||
|
||
const user = await testManager.mocks().createUser({ | ||
role: ROLES.PARTNER, | ||
email: '[email protected]', | ||
isActive: true, | ||
}); | ||
const { secret, expiresIn } = apiConfig.getJWTConfigByType( | ||
TOKEN_TYPE_ENUM.SIGN_UP, | ||
); | ||
|
||
const token = jwtService.sign({ id: user.id }, { secret, expiresIn }); | ||
const token = jwtManager.signSignUpToken(user.id); | ||
|
||
// When the user creates a new user | ||
|
||
|
@@ -52,4 +46,13 @@ describe('Create Users', () => { | |
|
||
expect(response.status).toBe(HttpStatus.UNAUTHORIZED); | ||
}); | ||
|
||
test('Sign up should fail if the current password is incorrect', async () => { | ||
const user = await testManager.mocks().createUser({ | ||
role: ROLES.PARTNER, | ||
email: '[email protected]', | ||
isActive: true, | ||
}); | ||
const token = await jwtManager.signSignUpToken(user.id); | ||
}); | ||
}); |