From 4d176548121b35ebd062892ede011d27eab610da Mon Sep 17 00:00:00 2001 From: Matt Bevilacqua Date: Mon, 1 Jul 2024 12:28:33 -0400 Subject: [PATCH] handle exceptions in goal policy --- src/policies/goals.js | 4 ++++ src/policies/goals.test.js | 48 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+) 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); + }); + }); });