diff --git a/src/policies/goals.js b/src/policies/goals.js index c66e858e5c..378ec3a68e 100644 --- a/src/policies/goals.js +++ b/src/policies/goals.js @@ -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); } } diff --git a/src/policies/goals.test.js b/src/policies/goals.test.js index 91eeaf3b1c..2f65c26643 100644 --- a/src/policies/goals.test.js +++ b/src/policies/goals.test.js @@ -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); + }); + }); });