Skip to content

Commit

Permalink
chore: fix some test and add more test cases to essential parts
Browse files Browse the repository at this point in the history
  • Loading branch information
apsantiso committed Mar 22, 2024
1 parent fb86905 commit 02c8c2f
Show file tree
Hide file tree
Showing 4 changed files with 218 additions and 38 deletions.
1 change: 0 additions & 1 deletion .env.template
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,5 @@ GATEWAY_PASS=gatewaypass

PAYMENTS_API_URL=http://host.docker.internal:8003

#Workspaces
WORKSPACES_USER_INVITATION_EMAIL_ID=d-de1ed6df4a9947129c0bf592c808b58d
WORKSPACES_GUEST_USER_INVITATION_EMAIL_ID=d-41b4608fc94a41bca65aab7ed6ccad15
192 changes: 192 additions & 0 deletions src/modules/workspaces/repositories/workspaces.repository.spec.ts
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,
});
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -227,4 +227,3 @@ export class SequelizeWorkspaceRepository implements WorkspaceRepository {
return domain?.toJSON();
}
}
export { WorkspaceModel };
62 changes: 26 additions & 36 deletions src/modules/workspaces/workspaces.usecase.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ describe('WorkspacesUsecases', () => {
const module: TestingModule = await Test.createTestingModule({
providers: [WorkspacesUsecases],
})
.useMocker(() => createMock())
.useMocker(createMock)
.compile();

service = module.get<WorkspacesUsecases>(WorkspacesUsecases);
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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));
});
});
});

0 comments on commit 02c8c2f

Please sign in to comment.