From 4a97878941af14566921fd99ee743aeb35f7a5a8 Mon Sep 17 00:00:00 2001 From: Mingze Date: Wed, 6 May 2020 16:48:46 -0700 Subject: [PATCH] fix(annotations): Fix annotations control shows if no permission (#1204) * fix(annotations): Fix annotations control shows if no permission * fix(annotations): Fix tests --- src/lib/viewers/BaseViewer.js | 6 +++++- src/lib/viewers/__tests__/BaseViewer-test.js | 20 +++++++++++++++++--- 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/src/lib/viewers/BaseViewer.js b/src/lib/viewers/BaseViewer.js index 5662986c4..74e1318c8 100644 --- a/src/lib/viewers/BaseViewer.js +++ b/src/lib/viewers/BaseViewer.js @@ -1051,7 +1051,11 @@ class BaseViewer extends EventEmitter { */ areNewAnnotationsEnabled() { const { showAnnotationsControls, file } = this.options; - const { extension } = file || {}; + const { permissions, extension } = file || {}; + + if (!this.hasAnnotationPermissions(permissions)) { + return false; + } // Disable new annotations for Excel and iWork formats if (EXCEL_EXTENSIONS.includes(extension) || IWORK_EXTENSIONS.includes(extension)) { diff --git a/src/lib/viewers/__tests__/BaseViewer-test.js b/src/lib/viewers/__tests__/BaseViewer-test.js index 7a938b222..0c6b642ab 100644 --- a/src/lib/viewers/__tests__/BaseViewer-test.js +++ b/src/lib/viewers/__tests__/BaseViewer-test.js @@ -542,7 +542,7 @@ describe('lib/viewers/BaseViewer', () => { base.annotationControls = { resetControls: sandbox.mock(), }; - base.options.showAnnotationsControls = true; + sandbox.stub(base, 'areNewAnnotationsEnabled').returns(true); base.handleFullscreenEnter(); @@ -558,7 +558,7 @@ describe('lib/viewers/BaseViewer', () => { base.annotator = { emit: sandbox.mock(), }; - base.options.showAnnotationsControls = true; + sandbox.stub(base, 'areNewAnnotationsEnabled').returns(true); base.handleFullscreenExit(); @@ -1116,7 +1116,6 @@ describe('lib/viewers/BaseViewer', () => { location: { locale: 'en-US', }, - showAnnotationsControls: true, }; base.scale = 1.5; base.annotator = { @@ -1129,6 +1128,7 @@ describe('lib/viewers/BaseViewer', () => { base.annotationControls = { resetControls: sandbox.stub(), }; + sandbox.stub(base, 'areNewAnnotationsEnabled').returns(true); }); it('should initialize the annotator', () => { @@ -1263,6 +1263,20 @@ describe('lib/viewers/BaseViewer', () => { }); describe('areNewAnnotationsEnabled()', () => { + beforeEach(() => { + stubs.hasPermissions = sandbox.stub(base, 'hasAnnotationPermissions').returns(true); + base.options.file = { + permissions: { + can_annotate: true, + }, + }; + }); + + it('should return false if the user cannot create/view annotations', () => { + stubs.hasPermissions.returns(false); + expect(base.areNewAnnotationsEnabled()).to.be.false; + }); + EXCEL_EXTENSIONS.concat(IWORK_EXTENSIONS).forEach(extension => { it(`should return false if the file is ${extension} format`, () => { base.options.file.extension = extension;