generated from xgeekshq/oss-template
-
Notifications
You must be signed in to change notification settings - Fork 11
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
1 parent
e9e09c0
commit 3b96c83
Showing
25 changed files
with
450 additions
and
114 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { CanActivate, ExecutionContext, Injectable } from '@nestjs/common'; | ||
import User from 'src/modules/users/entities/user.schema'; | ||
|
||
@Injectable() | ||
export class DeleteUserGuard implements CanActivate { | ||
async canActivate(context: ExecutionContext) { | ||
const request = context.switchToHttp().getRequest(); | ||
|
||
const user: User = request.user; | ||
const userToDelete: string = request.params.userId; | ||
|
||
// user requesting can't delete itself | ||
return user._id !== userToDelete; | ||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
backend/src/modules/auth/applications/reset-password.use-case.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
79 changes: 79 additions & 0 deletions
79
backend/src/modules/users/applications/delete-user.use-case.spec.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,79 @@ | ||
import { DeepMocked, createMock } from '@golevelup/ts-jest'; | ||
import { UseCase } from 'src/libs/interfaces/use-case.interface'; | ||
import { UserRepositoryInterface } from '../repository/user.repository.interface'; | ||
import { deleteUserUseCase } from '../users.providers'; | ||
import * as Users from 'src/modules/users/interfaces/types'; | ||
import { Test, TestingModule } from '@nestjs/testing'; | ||
import { DeleteTeamUserServiceInterface } from 'src/modules/teamUsers/interfaces/services/delete.team.user.service.interface'; | ||
import { GetTeamUserServiceInterface } from 'src/modules/teamUsers/interfaces/services/get.team.user.service.interface'; | ||
import { UserFactory } from 'src/libs/test-utils/mocks/factories/user-factory'; | ||
import faker from '@faker-js/faker'; | ||
import { DeleteFailedException } from 'src/libs/exceptions/deleteFailedBadRequestException'; | ||
import { DELETE_TEAM_USER_SERVICE, GET_TEAM_USER_SERVICE } from 'src/modules/teamUsers/constants'; | ||
|
||
const userId = faker.datatype.uuid(); | ||
const userDeleted = UserFactory.create({ _id: userId }); | ||
const teamsOfUser = faker.datatype.number(); | ||
|
||
describe('DeleteUserUseCase', () => { | ||
let deleteUser: UseCase<string, boolean>; | ||
let userRepositoryMock: DeepMocked<UserRepositoryInterface>; | ||
let deleteTeamUserServiceMock: DeepMocked<DeleteTeamUserServiceInterface>; | ||
let getTeamUserServiceMock: DeepMocked<GetTeamUserServiceInterface>; | ||
|
||
beforeAll(async () => { | ||
const module: TestingModule = await Test.createTestingModule({ | ||
providers: [ | ||
deleteUserUseCase, | ||
{ | ||
provide: Users.TYPES.repository, | ||
useValue: createMock<UserRepositoryInterface>() | ||
}, | ||
{ | ||
provide: DELETE_TEAM_USER_SERVICE, | ||
useValue: createMock<DeleteTeamUserServiceInterface>() | ||
}, | ||
{ | ||
provide: GET_TEAM_USER_SERVICE, | ||
useValue: createMock<GetTeamUserServiceInterface>() | ||
} | ||
] | ||
}).compile(); | ||
|
||
deleteUser = module.get<UseCase<string, boolean>>(deleteUserUseCase.provide); | ||
|
||
userRepositoryMock = module.get(Users.TYPES.repository); | ||
deleteTeamUserServiceMock = module.get(DELETE_TEAM_USER_SERVICE); | ||
getTeamUserServiceMock = module.get(GET_TEAM_USER_SERVICE); | ||
}); | ||
|
||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
jest.resetAllMocks(); | ||
}); | ||
|
||
it('should be defined', () => { | ||
expect(deleteUser).toBeDefined(); | ||
}); | ||
|
||
describe('execute', () => { | ||
it('should return true', async () => { | ||
userRepositoryMock.deleteUser.mockResolvedValue(userDeleted); | ||
getTeamUserServiceMock.countTeamsOfUser.mockResolvedValue(teamsOfUser); | ||
deleteTeamUserServiceMock.deleteTeamUsersOfUser.mockResolvedValue(teamsOfUser); | ||
await expect(deleteUser.execute(userId)).resolves.toEqual(true); | ||
}); | ||
|
||
it('should throw error when user is not deleted', async () => { | ||
await expect(deleteUser.execute(userId)).rejects.toThrowError(DeleteFailedException); | ||
}); | ||
|
||
it('should throw error when commitTransaction fails', async () => { | ||
userRepositoryMock.deleteUser.mockResolvedValue(userDeleted); | ||
getTeamUserServiceMock.countTeamsOfUser.mockResolvedValue(teamsOfUser); | ||
deleteTeamUserServiceMock.deleteTeamUsersOfUser.mockResolvedValue(teamsOfUser); | ||
userRepositoryMock.commitTransaction.mockRejectedValue(new Error()); | ||
await expect(deleteUser.execute(userId)).rejects.toThrowError(DeleteFailedException); | ||
}); | ||
}); | ||
}); |
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
87 changes: 87 additions & 0 deletions
87
backend/src/modules/users/applications/get-all-users-with-teams.use-case.spec.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,87 @@ | ||
import { DeepMocked, createMock } from '@golevelup/ts-jest'; | ||
import { UseCase } from 'src/libs/interfaces/use-case.interface'; | ||
import { UserRepositoryInterface } from '../repository/user.repository.interface'; | ||
import { getAllUsersWithTeamsUseCase } from '../users.providers'; | ||
import * as Users from 'src/modules/users/interfaces/types'; | ||
import { Test, TestingModule } from '@nestjs/testing'; | ||
import { GetUserServiceInterface } from '../interfaces/services/get.user.service.interface'; | ||
import { GetTeamUserServiceInterface } from 'src/modules/teamUsers/interfaces/services/get.team.user.service.interface'; | ||
import { UserFactory } from 'src/libs/test-utils/mocks/factories/user-factory'; | ||
import { TeamFactory } from 'src/libs/test-utils/mocks/factories/team-factory.mock'; | ||
import { UserWithTeams } from '../interfaces/type-user-with-teams'; | ||
import GetAllUsersWithTeamsUseCaseDto from '../dto/useCase/get-all-users-with-teams.use-case.dto'; | ||
import { GetAllUsersWithTeamsPresenter } from 'src/modules/users/presenter/get-all-users-with-teams.presenter'; | ||
import { sortTeamUserListAlphabetically } from '../utils/sortings'; | ||
import { GET_TEAM_USER_SERVICE } from 'src/modules/teamUsers/constants'; | ||
|
||
const users = UserFactory.createMany(10); | ||
const teams = TeamFactory.createMany(5); | ||
|
||
const usersWithTeams: UserWithTeams[] = users.map((user) => ({ | ||
user, | ||
teams: [...teams.map((team) => team._id)] | ||
})); | ||
|
||
const getAllUsersWithTeamsProps: GetAllUsersWithTeamsUseCaseDto = {}; | ||
|
||
const getAllUsersWithTeamsResult: GetAllUsersWithTeamsPresenter = { | ||
userWithTeams: sortTeamUserListAlphabetically(usersWithTeams), | ||
userAmount: users.length, | ||
hasNextPage: 0 + 1 < Math.ceil(users.length / 15), | ||
page: 0 | ||
}; | ||
|
||
describe('GetAllUsersWithTeamsUseCase', () => { | ||
let getAllUsersWithTeams: UseCase<GetAllUsersWithTeamsUseCaseDto, GetAllUsersWithTeamsPresenter>; | ||
let userRepositoryMock: DeepMocked<UserRepositoryInterface>; | ||
let getUserServiceMock: DeepMocked<GetUserServiceInterface>; | ||
let getTeamUserServiceMock: DeepMocked<GetTeamUserServiceInterface>; | ||
beforeAll(async () => { | ||
const module: TestingModule = await Test.createTestingModule({ | ||
providers: [ | ||
getAllUsersWithTeamsUseCase, | ||
{ | ||
provide: Users.TYPES.repository, | ||
useValue: createMock<UserRepositoryInterface>() | ||
}, | ||
{ | ||
provide: Users.TYPES.services.GetUserService, | ||
useValue: createMock<GetUserServiceInterface>() | ||
}, | ||
{ | ||
provide: GET_TEAM_USER_SERVICE, | ||
useValue: createMock<GetTeamUserServiceInterface>() | ||
} | ||
] | ||
}).compile(); | ||
|
||
getAllUsersWithTeams = module.get< | ||
UseCase<GetAllUsersWithTeamsUseCaseDto, GetAllUsersWithTeamsPresenter> | ||
>(getAllUsersWithTeamsUseCase.provide); | ||
|
||
userRepositoryMock = module.get(Users.TYPES.repository); | ||
getUserServiceMock = module.get(Users.TYPES.services.GetUserService); | ||
getTeamUserServiceMock = module.get(GET_TEAM_USER_SERVICE); | ||
}); | ||
|
||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
jest.resetAllMocks(); | ||
}); | ||
|
||
it('should be defined', () => { | ||
expect(getAllUsersWithTeams).toBeDefined(); | ||
}); | ||
|
||
describe('execute', () => { | ||
it('should return paginated users with teams', async () => { | ||
userRepositoryMock.getAllWithPagination.mockResolvedValue(users); | ||
getUserServiceMock.countUsers.mockResolvedValue(users.length); | ||
getTeamUserServiceMock.getUsersOnlyWithTeams.mockResolvedValue(usersWithTeams); | ||
|
||
await expect(getAllUsersWithTeams.execute(getAllUsersWithTeamsProps)).resolves.toEqual( | ||
getAllUsersWithTeamsResult | ||
); | ||
}); | ||
}); | ||
}); |
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
34 changes: 34 additions & 0 deletions
34
backend/src/modules/users/applications/get-all-users.use-case.spec.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,34 @@ | ||
import { createMock } from '@golevelup/ts-jest'; | ||
import { UseCase } from 'src/libs/interfaces/use-case.interface'; | ||
import { UserRepositoryInterface } from '../repository/user.repository.interface'; | ||
import { getAllUsersUseCase } from '../users.providers'; | ||
import * as Users from 'src/modules/users/interfaces/types'; | ||
import { Test, TestingModule } from '@nestjs/testing'; | ||
import User from '../entities/user.schema'; | ||
|
||
describe('GetAllUsersUseCase', () => { | ||
let getAllUsers: UseCase<void, User[]>; | ||
|
||
beforeAll(async () => { | ||
const module: TestingModule = await Test.createTestingModule({ | ||
providers: [ | ||
getAllUsersUseCase, | ||
{ | ||
provide: Users.TYPES.repository, | ||
useValue: createMock<UserRepositoryInterface>() | ||
} | ||
] | ||
}).compile(); | ||
|
||
getAllUsers = module.get<UseCase<void, User[]>>(getAllUsersUseCase.provide); | ||
}); | ||
|
||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
jest.resetAllMocks(); | ||
}); | ||
|
||
it('should be defined', () => { | ||
expect(getAllUsers).toBeDefined(); | ||
}); | ||
}); |
5 changes: 3 additions & 2 deletions
5
backend/src/modules/users/applications/get-all-users.use-case.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
34 changes: 34 additions & 0 deletions
34
backend/src/modules/users/applications/get-user.use-case.spec.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,34 @@ | ||
import { createMock } from '@golevelup/ts-jest'; | ||
import { UseCase } from 'src/libs/interfaces/use-case.interface'; | ||
import { getUserUseCase } from '../users.providers'; | ||
import * as Users from 'src/modules/users/interfaces/types'; | ||
import { Test, TestingModule } from '@nestjs/testing'; | ||
import { GetUserServiceInterface } from '../interfaces/services/get.user.service.interface'; | ||
import User from '../entities/user.schema'; | ||
|
||
describe('GetUserUseCase', () => { | ||
let getUser: UseCase<string, User>; | ||
|
||
beforeAll(async () => { | ||
const module: TestingModule = await Test.createTestingModule({ | ||
providers: [ | ||
getUserUseCase, | ||
{ | ||
provide: Users.TYPES.services.GetUserService, | ||
useValue: createMock<GetUserServiceInterface>() | ||
} | ||
] | ||
}).compile(); | ||
|
||
getUser = module.get<UseCase<string, User>>(getUserUseCase.provide); | ||
}); | ||
|
||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
jest.resetAllMocks(); | ||
}); | ||
|
||
it('should be defined', () => { | ||
expect(getUser).toBeDefined(); | ||
}); | ||
}); |
Oops, something went wrong.