-
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
14 changed files
with
90 additions
and
32 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 { NotFoundException } 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 NotFoundException(`Email ${email} not found`); | ||
} | ||
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
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 |
---|---|---|
|
@@ -37,9 +37,10 @@ describe('Create Users', () => { | |
// 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]' }); | ||
const user = await testManager.mocks().createUser({ | ||
role: ROLES.PARTNER, | ||
email: '[email protected]', | ||
}); | ||
const { jwtToken } = await testManager.logUserIn(user); | ||
|
||
// When the user creates a new user | ||
|
@@ -72,7 +73,7 @@ describe('Create Users', () => { | |
); | ||
}); | ||
|
||
test('An Admin registers a new user', async () => { | ||
test('An Admin registers a new user ', async () => { | ||
// Given a admin user exists with valid credentials | ||
// beforeAll | ||
const newUser = { | ||
|
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,12 @@ 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 +45,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); | ||
}); | ||
}); |
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 |
---|---|---|
|
@@ -12,6 +12,7 @@ export const createUser = async ( | |
email: '[email protected]', | ||
...additionalData, | ||
password: await hash(usedPassword, salt), | ||
isActive: true, | ||
}; | ||
|
||
await dataSource.getRepository(User).save(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