Commit 1dd1dbca authored by Lyza Danger Gardner's avatar Lyza Danger Gardner

Disallow creation of anonymous annotations and highlights

parent b64ac1f2
......@@ -40,7 +40,7 @@ export function formatAnnot(ann) {
* sidebar.
*/
// @ngInject
export default function FrameSync($rootScope, $window, store, bridge) {
export default function FrameSync($rootScope, $window, flash, store, bridge) {
// Set of tags of annotations that are currently loaded into the frame
const inFrame = new Set();
......@@ -128,8 +128,18 @@ export default function FrameSync($rootScope, $window, store, bridge) {
function setupSyncFromFrame() {
// A new annotation, note or highlight was created in the frame
bridge.on('beforeCreateAnnotation', function(event) {
inFrame.add(event.tag);
const annot = Object.assign({}, event.msg, { $tag: event.tag });
// If user is not logged in, we can't really create a meaningful highlight
// or annotation. Instead, we need to open the sidebar, show an error,
// and delete the (unsaved) annotation so it gets un-selected in the
// target document
if (!store.isLoggedIn()) {
bridge.call('showSidebar');
flash.error('Please log in to create annotations or highlights');
bridge.call('deleteAnnotation', formatAnnot(annot));
return;
}
inFrame.add(event.tag);
$rootScope.$broadcast(events.BEFORE_ANNOTATION_CREATED, annot);
});
......
......@@ -50,6 +50,7 @@ const fixtures = {
describe('sidebar.frame-sync', function() {
let fakeStore;
let fakeBridge;
let fakeFlash;
let frameSync;
let $rootScope;
......@@ -58,6 +59,10 @@ describe('sidebar.frame-sync', function() {
});
beforeEach(function() {
fakeFlash = {
error: sinon.stub(),
};
fakeStore = createFakeStore(
{ annotations: [] },
{
......@@ -66,6 +71,7 @@ describe('sidebar.frame-sync', function() {
findIDsForTags: sinon.stub(),
focusAnnotations: sinon.stub(),
frames: sinon.stub().returns([fixtures.framesListEntry]),
isLoggedIn: sinon.stub().returns(false),
selectAnnotations: sinon.stub(),
selectTab: sinon.stub(),
toggleSelectedAnnotations: sinon.stub(),
......@@ -90,6 +96,7 @@ describe('sidebar.frame-sync', function() {
}
angular.mock.module('app', {
flash: fakeFlash,
store: fakeStore,
bridge: fakeBridge,
});
......@@ -211,7 +218,9 @@ describe('sidebar.frame-sync', function() {
});
context('when a new annotation is created in the frame', function() {
context('when an authenticated user is present', () => {
it('emits a BEFORE_ANNOTATION_CREATED event', function() {
fakeStore.isLoggedIn.returns(true);
const onCreated = sinon.stub();
const ann = { target: [] };
$rootScope.$on(events.BEFORE_ANNOTATION_CREATED, onCreated);
......@@ -229,6 +238,47 @@ describe('sidebar.frame-sync', function() {
});
});
context('when no authenticated user is present', () => {
beforeEach(() => {
fakeStore.isLoggedIn.returns(false);
});
it('should not emit BEFORE_ANNOTATION_CREATED event', () => {
const onCreated = sinon.stub();
const ann = { target: [] };
$rootScope.$on(events.BEFORE_ANNOTATION_CREATED, onCreated);
fakeBridge.emit('beforeCreateAnnotation', { tag: 't1', msg: ann });
assert.notCalled(onCreated);
});
it('should open the sidebar', () => {
const ann = { target: [] };
fakeBridge.emit('beforeCreateAnnotation', { tag: 't1', msg: ann });
assert.calledWith(fakeBridge.call, 'showSidebar');
});
it('should flash an error message', () => {
const ann = { target: [] };
fakeBridge.emit('beforeCreateAnnotation', { tag: 't1', msg: ann });
assert.calledWith(
fakeFlash.error,
'Please log in to create annotations or highlights'
);
});
it('should send a "deleteAnnotation" message to the frame', () => {
const ann = { target: [] };
fakeBridge.emit('beforeCreateAnnotation', { tag: 't1', msg: ann });
assert.calledWith(fakeBridge.call, 'deleteAnnotation');
});
});
});
context('when anchoring completes', function() {
let clock = sinon.stub();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment