Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Cleanup baseViewer.hasAnnotationPermissions() and tests #685

Merged
merged 6 commits into from
Mar 26, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions src/lib/viewers/BaseViewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -873,8 +873,11 @@ class BaseViewer extends EventEmitter {
return false;
}

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

/**
Expand All @@ -884,7 +887,8 @@ class BaseViewer extends EventEmitter {
*/
areAnnotationsEnabled() {
// Do not attempt to fetch annotations if the user cannot create or view annotations
if (!this.hasAnnotationPermissions(this.options.file)) {
const { permissions } = this.options.file;
if (!this.hasAnnotationPermissions(permissions)) {
return false;
}

Expand Down
50 changes: 29 additions & 21 deletions src/lib/viewers/__tests__/BaseViewer-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ describe('lib/viewers/BaseViewer', () => {
base.isMobile = true;
sandbox.stub(base, 'loadAnnotator');
sandbox.stub(base, 'finishLoadingSetup');
sandbox.stub(base, 'areAnnotationsEnabled').returns(true);

base.setup();

Expand Down Expand Up @@ -499,8 +500,8 @@ describe('lib/viewers/BaseViewer', () => {
sinon.assert.failException;
})
.catch((error) => {
expect(error).equals(sinon.match.error);
expect(error.message).equals('message');
expect(error).to.be.an('error');
expect(error.message).to.equal('message');
});
});
});
Expand Down Expand Up @@ -691,7 +692,7 @@ describe('lib/viewers/BaseViewer', () => {
stubs.isIOS.returns(true);

base.mobileZoomStartHandler(event);
expect(base._scaling).to.equal(true);
expect(base._scaling).to.be.true;
expect(event.stopPropagation).to.be.called;
expect(event.preventDefault).to.be.called;
});
Expand All @@ -700,7 +701,7 @@ describe('lib/viewers/BaseViewer', () => {
stubs.isIOS.returns(false);

base.mobileZoomStartHandler(event);
expect(base._scaling).to.equal(true);
expect(base._scaling).to.be.true;
expect(base._pinchScale).to.not.equal(undefined);
expect(event.stopPropagation).to.be.called;
expect(event.preventDefault).to.be.called;
Expand All @@ -711,7 +712,7 @@ describe('lib/viewers/BaseViewer', () => {
event.touches = [0];

base.mobileZoomStartHandler(event);
expect(base._scaling).to.equal(false);
expect(base._scaling).to.be.false;
expect(base._pinchScale).to.equal(undefined);
expect(event.stopPropagation).to.not.be.called;
expect(event.preventDefault).to.not.be.called;
Expand Down Expand Up @@ -783,7 +784,7 @@ describe('lib/viewers/BaseViewer', () => {

base.mobileZoomEndHandler(event);
expect(base.zoomIn).to.be.called;
expect(base._scaling).to.equal(false);
expect(base._scaling).to.be.false;
expect(base._pincScale).to.equal(undefined);
});

Expand All @@ -807,7 +808,7 @@ describe('lib/viewers/BaseViewer', () => {
base.mobileZoomEndHandler(event);
expect(base.zoomOut).to.be.called;
expect(base.zoomIn).to.not.be.called;
expect(base._scaling).to.equal(false);
expect(base._scaling).to.be.false;
expect(base._pincScale).to.equal(undefined);
});
});
Expand Down Expand Up @@ -1071,55 +1072,62 @@ describe('lib/viewers/BaseViewer', () => {
};

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

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

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;
expect(base.hasAnnotationPermissions(permissions)).to.be.true;
});

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;
expect(base.hasAnnotationPermissions(permissions)).to.be.true;
});
});

describe('areAnnotationsEnabled()', () => {
beforeEach(() => {
stubs.getViewerOption = sandbox.stub(base, 'getViewerOption').withArgs('annotations').returns(false);
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.areAnnotationsEnabled()).to.be.false;
});

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

stubs.getViewerOption.returns(true);
expect(base.areAnnotationsEnabled()).to.equal(true);
expect(base.areAnnotationsEnabled()).to.be.true;
});

it('should use the global showAnnotations boolean if the viewer param is not specified', () => {
stubs.getViewerOption.withArgs('annotations').returns(null);
base.options.showAnnotations = true;
expect(base.areAnnotationsEnabled()).to.equal(true);
expect(base.areAnnotationsEnabled()).to.be.true;

base.options.showAnnotations = false;
expect(base.areAnnotationsEnabled()).to.equal(false);
expect(base.areAnnotationsEnabled()).to.be.false;
});

it('should use BoxAnnotations options if an instance of BoxAnnotations is passed into Preview', () => {
stubs.getViewerOption.withArgs('annotations').returns(null);
base.options.showAnnotations = false;
base.options.boxAnnotations = undefined;
expect(base.areAnnotationsEnabled()).to.equal(false);
expect(base.areAnnotationsEnabled()).to.be.false;

base.options.viewer = { NAME: 'viewerName' };
base.options.boxAnnotations = sinon.createStubInstance(window.BoxAnnotations);
Expand All @@ -1128,29 +1136,29 @@ describe('lib/viewers/BaseViewer', () => {
// No enabled annotators in options
boxAnnotations.options = { 'nope': 'wrong options type' };
boxAnnotations.viewerOptions = undefined;
expect(base.areAnnotationsEnabled()).to.equal(false);
expect(base.areAnnotationsEnabled()).to.be.false;

// All default types enabled
boxAnnotations.viewerOptions = {
'viewerName': { enabled: true }
};
expect(base.areAnnotationsEnabled()).to.equal(true);
expect(base.areAnnotationsEnabled()).to.be.true;

// No specified enabled types
boxAnnotations.viewerOptions = {
'viewerName': { enabledTypes: [] }
};
expect(base.areAnnotationsEnabled()).to.equal(false);
expect(base.areAnnotationsEnabled()).to.be.false;

// Specified types enabled
boxAnnotations.viewerOptions = {
'viewerName': { enabledTypes: [ 'point' ] }
};
expect(base.areAnnotationsEnabled()).to.equal(true);
expect(base.areAnnotationsEnabled()).to.be.true;

// No passed in version of BoxAnnotations
window.BoxAnnotations = undefined;
expect(base.areAnnotationsEnabled()).to.equal(false);
expect(base.areAnnotationsEnabled()).to.be.false;
});
});

Expand Down