Skip to content

Commit

Permalink
handle exceptions in goal policy
Browse files Browse the repository at this point in the history
  • Loading branch information
thewatermethod committed Jul 1, 2024
1 parent cf48d47 commit 4d17654
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/policies/goals.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,11 @@ export default class Goal {
}

canView() {
if (!this.goal || !this.goal.grant) {
return false;
}
const region = this.goal.grant.regionId;

return this.canReadInRegion(region);
}
}
48 changes: 48 additions & 0 deletions src/policies/goals.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -216,4 +216,52 @@ describe('Goals policies', () => {
expect(policy.isOnApprovedActivityReports()).toBe(true);
});
});

describe('canView', () => {
it('returns false if no goal', () => {
const user = {
permissions: [
{
regionId: 2,
scopeId: SCOPES.APPROVE_REPORTS,
},
],
};

const policy = new Goal(user);
expect(policy.canView()).toBe(false);
});

it('returns false if goal has no grant', () => {
const user = {
permissions: [
{
regionId: 2,
scopeId: SCOPES.APPROVE_REPORTS,
},
],
};

const policy = new Goal(user, {});
expect(policy.canView()).toBe(false);
});

it('returns true if user has permissions in that region', () => {
const user = {
permissions: [
{
regionId: 2,
scopeId: SCOPES.APPROVE_REPORTS,
},
],
};

const goal = {
grant: { regionId: 2 },
};

const policy = new Goal(user, goal);
expect(policy.canView()).toBe(true);
});
});
});

0 comments on commit 4d17654

Please sign in to comment.