Skip to content

Commit

Permalink
Merge pull request #2244 from HHS/mb/TTAHUB-3117/better-exception-han…
Browse files Browse the repository at this point in the history
…dling-in-goal-policy

[TTAHUB-3117] handle exceptions in goal policy
  • Loading branch information
thewatermethod authored Jul 2, 2024
2 parents 2bc4294 + 4d17654 commit ff92640
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 ff92640

Please sign in to comment.