Skip to content

Commit

Permalink
create user tests
Browse files Browse the repository at this point in the history
  • Loading branch information
alexeh committed Sep 24, 2024
1 parent 01b7b90 commit 3730094
Show file tree
Hide file tree
Showing 8 changed files with 126 additions and 135 deletions.
2 changes: 0 additions & 2 deletions api/src/modules/admin/admin.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { Body, Controller, Post, UseGuards } from '@nestjs/common';
import { RolesGuard } from '@api/modules/auth/guards/roles.guard';
import { AuthenticationService } from '@api/modules/auth/authentication/authentication.service';
import { CreateUserDto } from '@shared/schemas/users/create-user.schema';
import { Public } from '@api/modules/auth/decorators/is-public.decorator';
import { JwtAuthGuard } from '@api/modules/auth/guards/jwt-auth.guard';
import { ROLES } from '@api/modules/auth/authorisation/roles.enum';
import { RequiredRoles } from '@api/modules/auth/decorators/roles.decorator';
Expand All @@ -12,7 +11,6 @@ import { RequiredRoles } from '@api/modules/auth/decorators/roles.decorator';
export class AdminController {
constructor(private readonly auth: AuthenticationService) {}

@Public()
@RequiredRoles(ROLES.ADMIN)
@Post('/users')
async createUser(@Body() createUserDto: CreateUserDto): Promise<void> {
Expand Down
130 changes: 0 additions & 130 deletions api/test/auth/authorization.spec.ts

This file was deleted.

23 changes: 23 additions & 0 deletions api/test/e2e/features/create-user.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
Feature: Create user as Admin

Scenario: A user can not create a user if it is not an admin
Given a user exists with valid credentials
But the user has the role "partner"
When the user creates a new user
Then the user should receive a 403 status code


Scenario: An Admin tries to register a partner with an existing email
Given a admin user exists with valid credentials
When the user creates a new user
But the email is already in use
Then the user should receive a 409 status code
And the user should receive a message containing "Email already exists"


Scenario: An Admin registers a new user
Given a admin user exists with valid credentials
When the user creates a new user
Then the user should receive a 201 status code
And the user should not be active
And an email should be sent
Empty file.
96 changes: 96 additions & 0 deletions api/test/integration/auth/create-user.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
import { ROLES } from '@api/modules/auth/authorisation/roles.enum';
import { TestManager } from '../../utils/test-manager';
import { User } from '@shared/entities/users/user.entity';
import { HttpStatus } from '@nestjs/common';
import { MockEmailService } from '../../utils/mocks/mock-email.service';
import { IEmailServiceToken } from '@api/modules/notifications/email/email-service.interface';

//create-user.feature

describe('Create Users', () => {
let testManager: TestManager;
let testUser: User;
let jwtToken: string;
let mockEmailService: MockEmailService;

beforeAll(async () => {
testManager = await TestManager.createTestManager();
const { user, jwtToken: token } = await testManager.setUpTestUser();
testUser = user;
jwtToken = token;
mockEmailService =
testManager.getModule<MockEmailService>(IEmailServiceToken);
});

afterEach(async () => {
await testManager.clearDatabase();
});

afterAll(async () => {
await testManager.close();
});

test('A user can not create a user if it is not an admin', async () => {
// Given a user exists with valid credentials
// But the user has the role partner

const user = await testManager.mocks().createUser({ role: ROLES.PARTNER });
const { jwtToken } = await testManager.logUserIn(user);

// When the user creates a new user

const response = await testManager
.request()
.post('/admin/users')
.set('Authorization', `Bearer ${jwtToken}`);

expect(response.status).toBe(HttpStatus.FORBIDDEN);
});
test('An Admin tries to register a partner with an existing email', async () => {
// Given a admin user exists with valid credentials
// beforeAll

// When the user creates a new user
// But the email is already in use

const response = await testManager
.request()
.post('/admin/users')
.set('Authorization', `Bearer ${jwtToken}`)
.send({ email: testUser.email, password: '12345678' });

// Then the user should receive a 409 status code
expect(response.status).toBe(HttpStatus.CONFLICT);
// And the user should receive a message containing "Email already exists"
expect(response.body.message).toBe(
`Email ${testUser.email} already exists`,
);
});

test('An Admin registers a new user', async () => {
// Given a admin user exists with valid credentials
// beforeAll
const newUser = {
email: '[email protected]',
password: '12345678',
partnerName: 'test',
};

const response = await testManager
.request()
.post('/admin/users')
.set('Authorization', `Bearer ${jwtToken}`)
.send(newUser);

// Then the user should receive a 201 status code
expect(response.status).toBe(HttpStatus.CREATED);
// And the user should not be active
const createdUser = await testManager
.getDataSource()
.getRepository(User)
.findOne({ where: { email: newUser.email } });

expect(createdUser.isActive).toBe(false);
expect(mockEmailService.sendMail).toHaveBeenCalledTimes(1);
});
});
2 changes: 1 addition & 1 deletion api/test/jest-config.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"rootDir": "../",
"roots": ["<rootDir>/src/", "<rootDir>/test/"],
"testEnvironment": "node",
"testRegex": ".steps.ts$",
"testRegex": "(.steps.ts|.spec.ts)$",
"transform": {
"^.+\\.(t|j)s$": "ts-jest"
},
Expand Down
5 changes: 4 additions & 1 deletion api/test/utils/mocks/mock-email.service.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import { IEmailServiceInterface } from '@api/modules/notifications/email/email-service.interface';
import {
IEmailServiceInterface,
SendMailDTO,
} from '@api/modules/notifications/email/email-service.interface';
import { Logger } from '@nestjs/common';

export class MockEmailService implements IEmailServiceInterface {
Expand Down
3 changes: 2 additions & 1 deletion api/test/utils/test-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { createUser } from './mocks/entity-mocks';
import { User } from '@shared/entities/users/user.entity';
import { IEmailServiceToken } from '@api/modules/notifications/email/email-service.interface';
import { MockEmailService } from './mocks/mock-email.service';
import { ROLES } from '@api/modules/auth/authorisation/roles.enum';

/**
* @description: Abstraction for NestJS testing workflow. For now its a basic implementation to create a test app, but can be extended to encapsulate
Expand Down Expand Up @@ -72,7 +73,7 @@ export class TestManager {
}

async setUpTestUser() {
const user = await createUser(this.getDataSource());
const user = await createUser(this.getDataSource(), { role: ROLES.ADMIN });
return logUserIn(this, user);
}

Expand Down

0 comments on commit 3730094

Please sign in to comment.