-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: fix some test and add more test cases to essential parts
- Loading branch information
Showing
4 changed files
with
218 additions
and
38 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
192 changes: 192 additions & 0 deletions
192
src/modules/workspaces/repositories/workspaces.repository.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,192 @@ | ||
import { Test, TestingModule } from '@nestjs/testing'; | ||
import { getModelToken } from '@nestjs/sequelize'; | ||
import { SequelizeWorkspaceRepository } from './workspaces.repository'; | ||
import { WorkspaceModel } from '../models/workspace.model'; | ||
import { WorkspaceUserModel } from '../models/workspace-users.model'; | ||
import { WorkspaceInviteModel } from '../models/workspace-invite.model'; | ||
import { createMock } from '@golevelup/ts-jest'; | ||
import { WorkspaceInvite } from '../domains/workspace-invite.domain'; | ||
import { WorkspaceUser } from '../domains/workspace-user.domain'; | ||
import { | ||
newWorkspaceInvite, | ||
newWorkspaceUser, | ||
} from '../../../../test/fixtures'; | ||
import { Workspace } from '../domains/workspaces.domain'; | ||
|
||
describe('SequelizeWorkspaceRepository', () => { | ||
let repository: SequelizeWorkspaceRepository; | ||
let workspaceModel: typeof WorkspaceModel; | ||
let workspaceUserModel: typeof WorkspaceUserModel; | ||
let workspaceInviteModel: typeof WorkspaceInviteModel; | ||
|
||
beforeEach(async () => { | ||
const module: TestingModule = await Test.createTestingModule({ | ||
providers: [SequelizeWorkspaceRepository], | ||
}) | ||
.useMocker(() => createMock()) | ||
.compile(); | ||
|
||
repository = module.get<SequelizeWorkspaceRepository>( | ||
SequelizeWorkspaceRepository, | ||
); | ||
workspaceModel = module.get<typeof WorkspaceModel>( | ||
getModelToken(WorkspaceModel), | ||
); | ||
workspaceUserModel = module.get<typeof WorkspaceUserModel>( | ||
getModelToken(WorkspaceUserModel), | ||
); | ||
workspaceInviteModel = module.get<typeof WorkspaceInviteModel>( | ||
getModelToken(WorkspaceInviteModel), | ||
); | ||
}); | ||
|
||
describe('findInvite', () => { | ||
it('When a workspace invitation is searched and it is found, it should return the respective invitation', async () => { | ||
const mockInvite = newWorkspaceInvite(); | ||
|
||
jest | ||
.spyOn(workspaceInviteModel, 'findOne') | ||
.mockResolvedValueOnce(mockInvite as WorkspaceInviteModel); | ||
|
||
const result = await repository.findInvite({ id: '1' }); | ||
expect(result).toBeInstanceOf(WorkspaceInvite); | ||
expect(result.id).toEqual(mockInvite.id); | ||
}); | ||
|
||
it('When a workspace invitation is searched and it is not found, it should return null', async () => { | ||
jest.spyOn(workspaceInviteModel, 'findOne').mockResolvedValueOnce(null); | ||
|
||
const result = await repository.findInvite({ id: '1' }); | ||
expect(result).toBeNull(); | ||
}); | ||
}); | ||
|
||
describe('getWorkspaceInvitationsCount', () => { | ||
it('When a workspace invitations number is searched, then it should return the respective number', async () => { | ||
jest.spyOn(workspaceInviteModel, 'count').mockResolvedValueOnce(5); | ||
const count = await repository.getWorkspaceInvitationsCount('1'); | ||
expect(count).toEqual(5); | ||
}); | ||
}); | ||
|
||
describe('findWorkspaceUser', () => { | ||
it('When a workspace user is searched and found, it should return the respective user', async () => { | ||
const workspaceUser = newWorkspaceUser(); | ||
const mockWorkspaceUser = { | ||
memberId: workspaceUser.id, | ||
workspaceId: workspaceUser.workspaceId, | ||
...workspaceUser.toJSON(), | ||
}; | ||
|
||
jest.spyOn(workspaceUserModel, 'findOne').mockResolvedValueOnce({ | ||
...mockWorkspaceUser, | ||
toJSON: jest.fn().mockReturnValue(mockWorkspaceUser), | ||
} as any); | ||
|
||
const result = await repository.findWorkspaceUser({ memberId: '1' }); | ||
expect(result).toBeInstanceOf(WorkspaceUser); | ||
expect(result).toEqual( | ||
expect.objectContaining({ | ||
...workspaceUser.toJSON(), | ||
}), | ||
); | ||
}); | ||
|
||
it('When a workspace user is searched and not found, it should return nothing', async () => { | ||
jest.spyOn(workspaceUserModel, 'findOne').mockResolvedValueOnce(null); | ||
const result = await repository.findWorkspaceUser({ memberId: '1' }); | ||
expect(result).toBeNull(); | ||
}); | ||
}); | ||
|
||
describe('getSpaceLimitInInvitations', () => { | ||
it('When the result is null, it should return 0', async () => { | ||
jest.spyOn(workspaceInviteModel, 'sum').mockResolvedValueOnce(null); | ||
|
||
const totalSpace = await repository.getSpaceLimitInInvitations('1'); | ||
expect(totalSpace).toStrictEqual(BigInt(0)); | ||
}); | ||
}); | ||
|
||
describe('getTotalSpaceLimitInWorkspaceUsers', () => { | ||
it('When the total is calculated, the respective space should be returned', async () => { | ||
jest.spyOn(workspaceUserModel, 'sum').mockResolvedValueOnce(10); | ||
|
||
const total = await repository.getTotalSpaceLimitInWorkspaceUsers('1'); | ||
expect(total).toStrictEqual(BigInt(10)); | ||
}); | ||
}); | ||
|
||
describe('findWorkspaceAndUser', () => { | ||
it('When workspace and user in workspace are found, it should return both', async () => { | ||
const userUuid = 'user-uuid'; | ||
const workspaceId = 'workspace-id'; | ||
const mockWorkspaceUser = newWorkspaceUser({ | ||
attributes: { memberId: userUuid, workspaceId }, | ||
}); | ||
const mockWorkspace = { | ||
id: workspaceId, | ||
toJSON: jest.fn().mockReturnValue({ | ||
id: workspaceId, | ||
}), | ||
workspaceUsers: [mockWorkspaceUser], | ||
}; | ||
|
||
jest | ||
.spyOn(workspaceModel, 'findOne') | ||
.mockResolvedValueOnce(mockWorkspace as any); | ||
|
||
const result = await repository.findWorkspaceAndUser( | ||
userUuid, | ||
workspaceId, | ||
); | ||
|
||
expect(result).toEqual({ | ||
workspace: expect.any(Workspace), | ||
workspaceUser: expect.any(WorkspaceUser), | ||
}); | ||
|
||
expect(result.workspace.id).toEqual(workspaceId); | ||
expect(result.workspaceUser.id).toEqual(mockWorkspaceUser.id); | ||
}); | ||
|
||
it('When workspace is not found, it should return null for both values', async () => { | ||
jest.spyOn(workspaceModel, 'findOne').mockResolvedValueOnce(null); | ||
|
||
const result = await repository.findWorkspaceAndUser( | ||
'user-uuid', | ||
'workspace-id', | ||
); | ||
|
||
expect(result).toEqual({ | ||
workspace: null, | ||
workspaceUser: null, | ||
}); | ||
}); | ||
|
||
it('When workspace is found but no user is found, it should return null user', async () => { | ||
const workspaceId = 'workspace-id'; | ||
const mockWorkspace = { | ||
id: workspaceId, | ||
toJSON: jest.fn().mockReturnValue({ | ||
id: workspaceId, | ||
}), | ||
workspaceUsers: [], | ||
}; | ||
|
||
jest | ||
.spyOn(workspaceModel, 'findOne') | ||
.mockResolvedValueOnce(mockWorkspace as any); | ||
|
||
const result = await repository.findWorkspaceAndUser( | ||
'user-uuid', | ||
'workspace-id', | ||
); | ||
|
||
expect(result).toEqual({ | ||
workspace: expect.any(Workspace), | ||
workspaceUser: null, | ||
}); | ||
}); | ||
}); | ||
}); |
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 |
---|---|---|
|
@@ -39,7 +39,7 @@ describe('WorkspacesUsecases', () => { | |
const module: TestingModule = await Test.createTestingModule({ | ||
providers: [WorkspacesUsecases], | ||
}) | ||
.useMocker(() => createMock()) | ||
.useMocker(createMock) | ||
.compile(); | ||
|
||
service = module.get<WorkspacesUsecases>(WorkspacesUsecases); | ||
|
@@ -79,6 +79,23 @@ describe('WorkspacesUsecases', () => { | |
).rejects.toThrow(NotFoundException); | ||
}); | ||
|
||
it('When user is not registered or precreated, then it should throw', async () => { | ||
jest.spyOn(workspaceRepository, 'findById').mockResolvedValueOnce(null); | ||
jest.spyOn(userUsecases, 'findByEmail').mockResolvedValueOnce(null); | ||
jest | ||
.spyOn(userUsecases, 'findPreCreatedByEmail') | ||
.mockResolvedValueOnce(null); | ||
|
||
await expect( | ||
service.inviteUserToWorkspace(user, 'workspace-id', { | ||
invitedUser: '[email protected]', | ||
spaceLimit: BigInt(1024), | ||
encryptionKey: 'Dasdsadas', | ||
encryptionAlgorithm: 'dadads', | ||
}), | ||
).rejects.toThrow(NotFoundException); | ||
}); | ||
|
||
it('When user is precreated, then it should be successfully invited', async () => { | ||
const workspace = newWorkspace(); | ||
jest | ||
|
@@ -160,25 +177,14 @@ describe('WorkspacesUsecases', () => { | |
jest | ||
.spyOn(workspaceRepository, 'findById') | ||
.mockResolvedValueOnce(workspace); | ||
jest.spyOn(service, 'isWorkspaceFull').mockResolvedValueOnce(true); | ||
|
||
await expect( | ||
service.inviteUserToWorkspace(user, workspace.id, { | ||
invitedUser: '[email protected]', | ||
spaceLimit: BigInt(1024), | ||
encryptionKey: 'encryptionKey', | ||
encryptionAlgorithm: 'RSA', | ||
}), | ||
).rejects.toThrow(BadRequestException); | ||
}); | ||
|
||
it('When workspace has no more slots left, then it should throw', async () => { | ||
const workspace = newWorkspace(); | ||
const user = newUser(); | ||
|
||
jest.spyOn(userUsecases, 'findByEmail').mockResolvedValueOnce(user); | ||
jest | ||
.spyOn(workspaceRepository, 'findById') | ||
.mockResolvedValueOnce(workspace); | ||
.spyOn(userUsecases, 'findPreCreatedByEmail') | ||
.mockResolvedValueOnce(null); | ||
jest | ||
.spyOn(workspaceRepository, 'findWorkspaceUser') | ||
.mockResolvedValueOnce(null); | ||
jest.spyOn(workspaceRepository, 'findInvite').mockResolvedValueOnce(null); | ||
jest.spyOn(service, 'isWorkspaceFull').mockResolvedValueOnce(true); | ||
|
||
await expect( | ||
|
@@ -292,7 +298,7 @@ describe('WorkspacesUsecases', () => { | |
const isFull = await service.isWorkspaceFull(workspaceId); | ||
expect(isFull).toBe(false); | ||
}); | ||
it('When workspace has slots left, then workspace is full', async () => { | ||
it('When workspace does not have slots left, then workspace is full', async () => { | ||
jest | ||
.spyOn(workspaceRepository, 'getWorkspaceUsersCount') | ||
.mockResolvedValue(10); | ||
|
@@ -323,21 +329,5 @@ describe('WorkspacesUsecases', () => { | |
); | ||
expect(assignableSpace).toBe(BigInt(300000)); | ||
}); | ||
|
||
it('When there is no space left, then it should return 0', async () => { | ||
jest.spyOn(networkService, 'getLimit').mockResolvedValue(700000); | ||
jest | ||
.spyOn(workspaceRepository, 'getTotalSpaceLimitInWorkspaceUsers') | ||
.mockResolvedValue(BigInt(500000)); | ||
jest | ||
.spyOn(workspaceRepository, 'getSpaceLimitInInvitations') | ||
.mockResolvedValue(BigInt(200000)); | ||
|
||
const assignableSpace = await service.getAssignableSpaceInWorkspace( | ||
workspace, | ||
workspaceDefaultUser, | ||
); | ||
expect(assignableSpace).toBe(BigInt(0)); | ||
}); | ||
}); | ||
}); |