Skip to content

Commit

Permalink
Chore: Move annotations permissions check into own method
Browse files Browse the repository at this point in the history
  • Loading branch information
pramodsum committed Jan 22, 2018
1 parent ede6866 commit e419b85
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 24 deletions.
22 changes: 16 additions & 6 deletions src/lib/viewers/BaseViewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -763,19 +763,29 @@ class BaseViewer extends EventEmitter {
this.annotator.addListener('annotatorevent', this.handleAnnotatorEvents);
}

/**
* Returns whether or not user has permissions to load annotations on the current file
*
* @param {Object} permissions Permissions on the current file
* @return {boolean} Whether or not user has the correct permissions
*/
hasAnnotationPermissions(permissions) {
if (!permissions) {
return false;
}

const canViewAnnotations = !!(permissions.can_view_annotations_all || permissions.can_view_annotations_self);
return !permissions.can_annotate && !canViewAnnotations;
}

/**
* Returns whether or not annotations are enabled for this viewer.
*
* @return {boolean} Whether or not viewer is annotatable
*/
areAnnotationsEnabled() {
// Do not attempt to fetch annotations if the user cannot create or view annotations
const { permissions } = this.options.file;
if (
permissions &&
!permissions.can_annotate &&
!(permissions.can_view_annotations_all || permissions.can_view_annotations_self)
) {
if (!this.hasAnnotationPermissions(this.options.file)) {
return false;
}

Expand Down
45 changes: 27 additions & 18 deletions src/lib/viewers/__tests__/BaseViewer-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -910,6 +910,33 @@ describe('lib/viewers/BaseViewer', () => {
})
});

describe('hasAnnotationPermissions()', () => {
const permissions = {
can_annotate: false,
can_view_annotations_all: false,
can_view_annotations_self: false
};

it('does nothing if file permissions are undefined', () => {
expect(base.hasAnnotationPermissions()).to.be.falsy;
});

it('should return false if the user can neither annotate nor view all or their own annotations', () => {
expect(base.hasAnnotationPermissions(permissions)).to.be.falsy;
});

it('should return true if the user can at least view all annotations', () => {
permissions.can_view_annotations_all = true;
expect(base.hasAnnotationPermissions(permissions)).to.be.truthy;
});

it('should return true if the user can at least view their own annotations', () => {
permissions.can_view_annotations_all = false;
permissions.can_view_annotations_self = true;
expect(base.hasAnnotationPermissions(permissions)).to.be.truthy;
});
});

describe('areAnnotationsEnabled()', () => {
beforeEach(() => {
stubs.getViewerOption = sandbox.stub(base, 'getViewerOption').withArgs('annotations').returns(false);
Expand All @@ -920,24 +947,6 @@ describe('lib/viewers/BaseViewer', () => {
};
});

it('should return false if the user can neither annotate nor view all or their own annotations', () => {
stubs.getViewerOption.returns(true);

base.options.file.permissions = {
can_annotate: false,
can_view_annotations_all: false,
can_view_annotations_self: false
};
expect(base.areAnnotationsEnabled()).to.equal(false);

base.options.file.permissions.can_view_annotations_all = true;
expect(base.areAnnotationsEnabled()).to.equal(true);

base.options.file.permissions.can_view_annotations_all = false;
base.options.file.permissions.can_view_annotations_self = true;
expect(base.areAnnotationsEnabled()).to.equal(true);
});

it('should return true if viewer option is set to true', () => {
expect(base.areAnnotationsEnabled()).to.equal(false);
stubs.getViewerOption.returns(true);
Expand Down

0 comments on commit e419b85

Please sign in to comment.