Skip to content

Commit

Permalink
chore: added fixture for workspaceUser
Browse files Browse the repository at this point in the history
  • Loading branch information
apsantiso committed Mar 19, 2024
1 parent a0feae1 commit 770e554
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 4 deletions.
7 changes: 4 additions & 3 deletions src/modules/workspaces/guards/workspaces.guard.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
newUser,
newWorkspace,
newWorkspaceTeam,
newWorkspaceUser,
} from '../../../../test/fixtures';
import { WorkspaceUser } from '../domains/workspace-user.domain';
import { WorkspaceTeamUser } from '../domains/workspace-team-user.domain';
Expand Down Expand Up @@ -125,7 +126,7 @@ describe('WorkspaceGuard', () => {
});

it('When user is member and required role is member, then grant access', async () => {
const workspaceMember = newUser();
const workspaceMember = newWorkspaceUser();
const workspace = newWorkspace();

jest.spyOn(reflector, 'get').mockReturnValue({
Expand All @@ -135,7 +136,7 @@ describe('WorkspaceGuard', () => {
});
workspaceUseCases.findUserInWorkspace.mockResolvedValue({
workspace,
workspaceUser: {} as WorkspaceUser,
workspaceUser: workspaceMember,
});
const context = createMockExecutionContext(workspaceMember, {
params: { workspaceId: workspace.id },
Expand Down Expand Up @@ -193,7 +194,7 @@ describe('WorkspaceGuard', () => {
);
});

it('Access Granted for Team Member', async () => {
it('When user is team member, then grant access', async () => {
const user = newUser();
const workspace = newWorkspace({ owner: user });
const team = newWorkspaceTeam({
Expand Down
1 change: 0 additions & 1 deletion src/modules/workspaces/guards/workspaces.guard.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// flexible-access.guard.ts
import {
CanActivate,
ExecutionContext,
Expand Down
43 changes: 43 additions & 0 deletions test/fixtures.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -346,4 +346,47 @@ describe('Testing fixtures tests', () => {
expect(typeof team.name).toBe('string');
});
});

describe("WorkspaceUser's fixture", () => {
it('When it generates a workspace user, then the identifier should be random', () => {
const user = fixtures.newWorkspaceUser();
const otherUser = fixtures.newWorkspaceUser();
expect(user.id).toBeTruthy();
expect(user.id).not.toBe(otherUser.id);
});

it('When it generates a workspace user, then the workspaceId should be random', () => {
const user = fixtures.newWorkspaceUser();
const otherUser = fixtures.newWorkspaceUser();
expect(user.workspaceId).toBeTruthy();
expect(user.workspaceId).not.toBe(otherUser.workspaceId);
});

it('When it generates a workspace user with a specified memberId, then the memberId should match', () => {
const memberId = 'anyId';
const user = fixtures.newWorkspaceUser({ memberId });
expect(user.memberId).toBe(memberId);
});

it('When it generates a workspace user, then driveUsage and backupsUsage should not exceed spaceLimit', () => {
const user = fixtures.newWorkspaceUser();
expect(Number(user.driveUsage)).toBeLessThanOrEqual(
BigInt(user.spaceLimit),
);
expect(BigInt(user.backupsUsage)).toBeLessThanOrEqual(
BigInt(user.spaceLimit),
);
});

it('When it generates a workspace user with custom attributes, then those attributes are set correctly', () => {
const customAttributes = {
deactivated: true,
spaceLimit: BigInt(500),
};
const user = fixtures.newWorkspaceUser({ attributes: customAttributes });

expect(user.deactivated).toBe(customAttributes.deactivated);
expect(user.spaceLimit).toBe(customAttributes.spaceLimit);
});
});
});
34 changes: 34 additions & 0 deletions test/fixtures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
import { Limit } from '../src/modules/feature-limit/limit.domain';
import { Workspace } from '../src/modules/workspaces/domains/workspaces.domain';
import { WorkspaceTeam } from '../src/modules/workspaces/domains/workspace-team.domain';
import { WorkspaceUser } from '../src/modules/workspaces/domains/workspace-user.domain';

export const constants = {
BUCKET_ID_LENGTH: 24,
Expand Down Expand Up @@ -307,3 +308,36 @@ export const newWorkspaceTeam = (params?: {

return team;
};

export const newWorkspaceUser = (params?: {
workspaceId?: string;
memberId?: string;
attributes?: Partial<WorkspaceUser>;
}): WorkspaceUser => {
const randomCreatedAt = randomDataGenerator.date();
const spaceLimit = randomDataGenerator.natural({ min: 1, max: 1073741824 });

const workspaceUser = WorkspaceUser.build({
id: v4(),
memberId: params?.memberId || v4(),
key: randomDataGenerator.string({ length: 32 }),
workspaceId: params?.workspaceId || v4(),
spaceLimit: BigInt(spaceLimit),
driveUsage: BigInt(
randomDataGenerator.natural({ min: 1, max: spaceLimit }),
),
backupsUsage: BigInt(
randomDataGenerator.natural({ min: 1, max: spaceLimit }),
),
deactivated: randomDataGenerator.bool(),
createdAt: randomCreatedAt,
updatedAt: new Date(randomDataGenerator.date({ min: randomCreatedAt })),
});

params?.attributes &&
Object.keys(params.attributes).forEach((key) => {
workspaceUser[key] = params.attributes[key];
});

return workspaceUser;
};

0 comments on commit 770e554

Please sign in to comment.