Skip to content

Commit

Permalink
test signup flow
Browse files Browse the repository at this point in the history
  • Loading branch information
alexeh committed Sep 26, 2024
1 parent 768b5e7 commit 142b20c
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 55 deletions.
7 changes: 1 addition & 6 deletions api/src/modules/auth/auth.module.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { Module } from '@nestjs/common';
import { PasswordRecoveryService } from '@api/modules/auth/services/password-recovery.service';
import { AuthMailer } from '@api/modules/auth/services/auth.mailer';
import { NotificationsModule } from '@api/modules/notifications/notifications.module';
import { AuthenticationController } from '@api/modules/auth/authentication.controller';
Expand All @@ -9,11 +8,7 @@ import { RequestPasswordRecoveryHandler } from '@api/modules/auth/commands/reque
@Module({
imports: [AuthenticationModule, NotificationsModule],
controllers: [AuthenticationController],
providers: [
PasswordRecoveryService,
AuthMailer,
RequestPasswordRecoveryHandler,
],
providers: [AuthMailer, RequestPasswordRecoveryHandler],
exports: [AuthenticationModule, AuthMailer],
})
export class AuthModule {}
7 changes: 2 additions & 5 deletions api/src/modules/auth/authentication.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,12 @@ import { User } from '@shared/entities/users/user.entity';
import { LocalAuthGuard } from '@api/modules/auth/guards/local-auth.guard';
import { GetUser } from '@api/modules/auth/decorators/get-user.decorator';
import { Public } from '@api/modules/auth/decorators/is-public.decorator';
import { PasswordRecoveryService } from '@api/modules/auth/services/password-recovery.service';
import { tsRestHandler, TsRestHandler } from '@ts-rest/nest';
import { ControllerResponse } from '@api/types/controller-response.type';
import { AuthGuard } from '@nestjs/passport';
import { ResetPassword } from '@api/modules/auth/strategies/reset-password.strategy';
import { authContract } from '@shared/contracts/auth.contract';
import { AuthenticationService } from '@api/modules/auth/authentication.service';
import { JwtAuthGuard } from '@api/modules/auth/guards/jwt-auth.guard';
import { SignUp } from '@api/modules/auth/strategies/sign-up.strategy';
import { CommandBus } from '@nestjs/cqrs';
import { RequestPasswordRecoveryCommand } from '@api/modules/auth/commands/request-password-recovery.command';
Expand All @@ -27,7 +25,6 @@ import { RequestPasswordRecoveryCommand } from '@api/modules/auth/commands/reque
export class AuthenticationController {
constructor(
private authService: AuthenticationService,
private readonly passwordRecovery: PasswordRecoveryService,
private readonly commandBus: CommandBus,
) {}

Expand All @@ -44,10 +41,10 @@ export class AuthenticationController {
});
}

@UseGuards(JwtAuthGuard, AuthGuard(SignUp))
@UseGuards(AuthGuard(SignUp))
@TsRestHandler(authContract.signUp)
async signUp(@GetUser() user: User): Promise<ControllerResponse> {
return tsRestHandler(authContract.login, async ({ body }) => {
return tsRestHandler(authContract.signUp, async ({ body }) => {
await this.authService.signUp(user, body);
return {
body: null,
Expand Down
8 changes: 8 additions & 0 deletions api/src/modules/auth/authentication.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { JwtStrategy } from '@api/modules/auth/strategies/jwt.strategy';
import { TOKEN_TYPE_ENUM } from '@shared/schemas/auth/token-type.schema';
import { ResetPasswordJwtStrategy } from '@api/modules/auth/strategies/reset-password.strategy';
import { JwtManager } from '@api/modules/auth/services/jwt.manager';
import { SignUpStrategy } from '@api/modules/auth/strategies/sign-up.strategy';

@Module({
imports: [
Expand Down Expand Up @@ -46,6 +47,13 @@ import { JwtManager } from '@api/modules/auth/services/jwt.manager';
},
inject: [UsersService, ApiConfigService],
},
{
provide: SignUpStrategy,
useFactory: (users: UsersService, config: ApiConfigService) => {
return new SignUpStrategy(users, config);
},
inject: [UsersService, ApiConfigService],
},
],
exports: [UsersModule, AuthenticationService, JwtManager],
})
Expand Down
38 changes: 0 additions & 38 deletions api/src/modules/auth/services/password-recovery.service.ts

This file was deleted.

51 changes: 45 additions & 6 deletions api/test/integration/auth/sign-up.spec.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { TestManager } from '../../utils/test-manager';
import { HttpStatus } from '@nestjs/common';
import { ApiConfigService } from '@api/modules/config/app-config.service';
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';
import { User } from '@shared/entities/users/user.entity';

//create-user.feature

Expand Down Expand Up @@ -33,25 +33,64 @@ describe('Create Users', () => {
email: '[email protected]',
});

const token = jwtManager.signSignUpToken(user.id);
const { signUpToken } = await jwtManager.signSignUpToken(user.id);

// When the user creates a new user

const response = await testManager
.request()
.get(authContract.validateToken.path)
.set('Authorization', `Bearer ${token}`)
.set('Authorization', `Bearer ${signUpToken}`)
.query({ tokenType: TOKEN_TYPE_ENUM.SIGN_UP });

expect(response.status).toBe(HttpStatus.UNAUTHORIZED);
});

test('Sign up should fail if the current password is incorrect', async () => {
test('Sign up should fail if the auto-generated password is incorrect', async () => {
const user = await testManager.mocks().createUser({
role: ROLES.PARTNER,
email: '[email protected]',
isActive: true,
isActive: false,
});
const token = await jwtManager.signSignUpToken(user.id);
const { signUpToken } = await jwtManager.signSignUpToken(user.id);

const response = await testManager
.request()
.post(authContract.signUp.path)
.set('Authorization', `Bearer ${signUpToken}`)
.query({ tokenType: TOKEN_TYPE_ENUM.SIGN_UP })
.send({ password: 'wrongpassword', newPassword: 'newpassword' });

expect(response.status).toBe(HttpStatus.UNAUTHORIZED);
});

test('Sign up should succeed if the auto-generated password is correct and the user should be activated and allowed to get a access token', async () => {
const user = await testManager.mocks().createUser({
role: ROLES.PARTNER,
email: '[email protected]',
isActive: false,
});
const { signUpToken } = await jwtManager.signSignUpToken(user.id);

const response = await testManager
.request()
.post(authContract.signUp.path)
.set('Authorization', `Bearer ${signUpToken}`)
.send({ password: user.password, newPassword: 'newpassword' });

expect(response.status).toBe(HttpStatus.CREATED);
const foundUser = await testManager
.getDataSource()
.getRepository(User)
.findOneBy({ id: user.id });

expect(foundUser.isActive).toBe(true);

const login = await testManager
.request()
.post(authContract.login.path)
.send({ email: user.email, password: 'newpassword' });

expect(login.body.accessToken).toBeDefined();
});
});

0 comments on commit 142b20c

Please sign in to comment.