Skip to content

Commit

Permalink
fix(annotations): Fix annotations control shows if no permission (#1204)
Browse files Browse the repository at this point in the history
* fix(annotations): Fix annotations control shows if no permission

* fix(annotations): Fix tests
  • Loading branch information
Mingze authored May 6, 2020
1 parent 1c8ac70 commit 4a97878
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
6 changes: 5 additions & 1 deletion src/lib/viewers/BaseViewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down
20 changes: 17 additions & 3 deletions src/lib/viewers/__tests__/BaseViewer-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -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();

Expand Down Expand Up @@ -1116,7 +1116,6 @@ describe('lib/viewers/BaseViewer', () => {
location: {
locale: 'en-US',
},
showAnnotationsControls: true,
};
base.scale = 1.5;
base.annotator = {
Expand All @@ -1129,6 +1128,7 @@ describe('lib/viewers/BaseViewer', () => {
base.annotationControls = {
resetControls: sandbox.stub(),
};
sandbox.stub(base, 'areNewAnnotationsEnabled').returns(true);
});

it('should initialize the annotator', () => {
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 4a97878

Please sign in to comment.