Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[TTAHUB-3567] Coverage >= 90% src/policies/sessionReport.js #2513

Merged
merged 2 commits into from
Dec 4, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions src/policies/sessionReport.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,5 +106,60 @@ describe('Session Report policies', () => {
const policy = new SessionReport(authorRegion2, reportRegion1);
expect(policy.canRead()).toBe(false);
});

it('is true if the user is an admin', () => {
const reportRegion1 = createReport({ author: authorRegion1, regionId: 1 });
const policy = new SessionReport(admin, reportRegion1);
expect(policy.canRead()).toBe(true);
});
});

describe('canWriteInRegion', () => {
it('is true if the user is an admin', () => {
const reportRegion1 = createReport({ author: authorRegion1, regionId: 1 });
const policy = new SessionReport(admin, reportRegion1);
expect(policy.canWriteInRegion()).toBe(true);
});

it('is true if the user has write permissions in the region', () => {
const reportRegion1 = createReport({ author: authorRegion1, regionId: 1 });
const policy = new SessionReport(authorRegion1, reportRegion1);
expect(policy.canWriteInRegion()).toBe(true);
});

it('is false if the user does not have write permissions in the region', () => {
const reportRegion1 = createReport({ author: authorRegion1, regionId: 1 });
const policy = new SessionReport(authorRegion2, reportRegion1);
expect(policy.canWriteInRegion()).toBe(false);
});

it('is false if the user has no permissions', () => {
const reportRegion1 = createReport({ author: noPerms, regionId: 1 });
const policy = new SessionReport(noPerms, reportRegion1);
expect(policy.canWriteInRegion()).toBe(false);
});

it('is false if the user has read permissions but not write permissions in the region', () => {
const readOnlyUser = createUser({ read: true, regionId: 1 });
const reportRegion1 = createReport({ author: readOnlyUser, regionId: 1 });
const policy = new SessionReport(readOnlyUser, reportRegion1);
expect(policy.canWriteInRegion()).toBe(false);
});

it('is true if the user is not an admin, and provided regionId is not null', () => {
const writeUser = createUser({ write: true, regionId: 1 });
const reportRegion1 = createReport({ author: writeUser, regionId: 1 });
const policy = new SessionReport(writeUser, reportRegion1);
expect(policy.canWriteInRegion(1)).toBe(true);
});
});

describe('permissions default', () => {
it('defaults to an empty array if user has no permissions', () => {
const userWithoutPermissions = { id: 999 };
const reportRegion1 = createReport({ author: userWithoutPermissions, regionId: 1 });
const policy = new SessionReport(userWithoutPermissions, reportRegion1);
expect(policy.permissions).toEqual([]);
});
});
});