Skip to content

Commit

Permalink
test: add tests to get board service (#1224)
Browse files Browse the repository at this point in the history
  • Loading branch information
CatiaAntunes96 authored Mar 13, 2023
1 parent ae400aa commit 32499c2
Show file tree
Hide file tree
Showing 13 changed files with 650 additions and 152 deletions.
6 changes: 3 additions & 3 deletions backend/src/libs/guards/getBoardPermissions.guard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,13 @@ export class GetBoardGuard implements CanActivate {

try {
const { isPublic, team } = await this.getBoardService.getBoardData(boardId);
const boardUserFound = await this.getBoardService.getBoardUsers(boardId, userId);
const boardUserFound = await this.getBoardService.getBoardUser(boardId, userId);

if (isPublic || boardUserFound.length || isSAdmin) {
if (isPublic || boardUserFound || isSAdmin) {
return true;
}

if (!boardUserFound.length) {
if (!boardUserFound) {
const { role: teamRole } = (team as Team).users.find(
(teamUser: TeamUser) => (teamUser.user as User)._id.toString() === userId.toString()
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const userId = faker.datatype.uuid();

const mockBoardData = () => {
return {
_id: faker.datatype.uuid(),
_id: faker.database.mongodbObjectId(),
title: faker.lorem.words(),
columns: ColumnFactory.createMany(3),
isPublic: faker.datatype.boolean(),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import faker from '@faker-js/faker';
import { buildTestFactory } from './generic-factory.mock';
import BoardUser from 'src/modules/boards/entities/board.user.schema';
import { BoardRoles } from 'src/libs/enum/board.roles';

const mockBoardUserData = () => {
return {
_id: faker.database.mongodbObjectId(),
role: faker.helpers.arrayElement([BoardRoles.MEMBER, BoardRoles.RESPONSIBLE]),
user: faker.database.mongodbObjectId(),
board: faker.database.mongodbObjectId(),
votesCount: 0
};
};

export const BoardUserFactory = buildTestFactory<BoardUser>(() => {
return mockBoardUserData();
});
15 changes: 8 additions & 7 deletions backend/src/libs/test-utils/mocks/factories/card-factory.mock.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,26 @@
import faker from '@faker-js/faker';
import Card from 'src/modules/cards/entities/card.schema';
import { buildTestFactory } from './generic-factory.mock';
import { UserFactory } from './user-factory';

const userId = faker.datatype.uuid();
const cardId = faker.datatype.uuid();
const cardText = faker.lorem.words();
const commentText = faker.lorem.paragraph(1);
const cardText = faker.lorem.words(5);
const commentText = faker.lorem.words(4);
const teamId = faker.datatype.uuid();
const createdAtDate = faker.datatype.datetime();
const user = UserFactory.create();

const mockCardData = (): Card => {
return {
_id: cardId,
text: cardText,
createdBy: userId,
createdBy: user,
createdByTeam: teamId,
createdAt: createdAtDate,
comments: [
{
text: commentText,
createdBy: userId,
createdBy: user,
anonymous: false
}
],
Expand All @@ -29,11 +30,11 @@ const mockCardData = (): Card => {
{
_id: cardId,
text: cardText,
createdBy: userId,
createdBy: user,
comments: [
{
text: commentText,
createdBy: userId,
createdBy: user,
anonymous: false
}
],
Expand Down
21 changes: 21 additions & 0 deletions backend/src/libs/test-utils/mocks/factories/dto/userDto-factory.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import faker from '@faker-js/faker';
import UserDto from 'src/modules/users/dto/user.dto';
import { buildTestFactory } from '../generic-factory.mock';

const mockUserDto = () => {
return {
_id: faker.database.mongodbObjectId(),
firstName: faker.name.firstName(),
lastName: faker.name.lastName(),
email: faker.internet.email(),
strategy: faker.lorem.word(),
isSAdmin: faker.datatype.boolean(),
isAnonimous: faker.datatype.boolean(),
providerAccountCreatedAt: faker.date.past(1),
avatar: faker.internet.avatar()
};
};

export const UserDtoFactory = buildTestFactory<UserDto>(() => {
return mockUserDto();
});
18 changes: 18 additions & 0 deletions backend/src/libs/test-utils/mocks/factories/team-factory.mock.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import faker from '@faker-js/faker';
import { buildTestFactory } from './generic-factory.mock';
import Team from 'src/modules/teams/entities/teams.schema';

const dateCreatedAt = faker.date.past(1);

const mockTeamData = () => {
return {
_id: faker.database.mongodbObjectId(),
name: faker.company.companyName(),
createdAt: dateCreatedAt,
upddatedAt: faker.date.between(dateCreatedAt, Date.now())
};
};

export const TeamFactory = buildTestFactory<Team>(() => {
return mockTeamData();
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import faker from '@faker-js/faker';
import { buildTestFactory } from './generic-factory.mock';
import { TeamRoles } from 'src/libs/enum/team.roles';
import TeamUser from 'src/modules/teams/entities/team.user.schema';

const mockTeamUserData = () => {
return {
_id: faker.database.mongodbObjectId(),
role: faker.helpers.arrayElement([TeamRoles.MEMBER, TeamRoles.ADMIN, TeamRoles.STAKEHOLDER]),
isNewJoiner: faker.datatype.boolean(),
user: faker.database.mongodbObjectId(),
team: faker.database.mongodbObjectId()
};
};

export const TeamUserFactory = buildTestFactory<TeamUser>(() => {
return mockTeamUserData();
});
22 changes: 22 additions & 0 deletions backend/src/libs/test-utils/mocks/factories/user-factory.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import faker from '@faker-js/faker';
import { buildTestFactory } from './generic-factory.mock';
import User from 'src/modules/users/entities/user.schema';

const mockUserData = () => {
return {
_id: faker.database.mongodbObjectId(),
firstName: faker.name.firstName(),
lastName: faker.name.lastName(),
password: faker.internet.password(),
email: faker.internet.email(),
joinedAt: faker.date.past(1),
strategy: faker.lorem.word(),
isSAdmin: faker.datatype.boolean(),
isDeleted: faker.datatype.boolean(),
isAnonymous: faker.datatype.boolean()
};
};

export const UserFactory = buildTestFactory<User>(() => {
return mockUserData();
});
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export interface GetBoardServiceInterface {

getBoardData(boardId: string): Promise<Board>;

getBoardUsers(board: string, user: string): Promise<BoardUser[]>;
getBoardUser(board: string, user: string): Promise<BoardUser>;

getAllMainBoards(): Promise<Board[]>;
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ export interface BoardUserRepositoryInterface extends BaseInterfaceRepository<Bo
getAllBoardsIdsOfUser(userId: string): Promise<BoardUser[]>;
getBoardResponsible(boardId: string): Promise<BoardUser>;
getVotesCount(boardId: string): Promise<BoardUser[]>;
getBoardUsers(board: string, user: string): Promise<BoardUser[]>;
getBoardUser(board: string, user: string): Promise<BoardUser>;
getBoardUserPopulated(board: string, user: string): Promise<BoardUser>;
createBoardUsers(boardUsers: BoardUserDto[]): Promise<BoardUser[]>;
updateBoardUserRole(boardId: string, userId: string, role: string): Promise<BoardUser>;
updateVoteUser(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@ export class BoardUserRepository
return this.findAllWithQuery({ board: boardId }, ['votesCount']);
}

getBoardUsers(board: string, user: string) {
return this.findAllWithQuery({ board, user });
getBoardUser(board: string, user: string) {
return this.findOneByField({ board, user });
}

getBoardUser(board: string, user: string) {
getBoardUserPopulated(board: string, user: string) {
return this.findOneByFieldWithQuery({ board, user }, null, {
path: 'user',
select: '_id firstName lastName '
Expand Down
Loading

0 comments on commit 32499c2

Please sign in to comment.