diff options
author | Vincent Petry <pvince81@owncloud.com> | 2014-06-02 17:23:52 +0200 |
---|---|---|
committer | Vincent Petry <pvince81@owncloud.com> | 2014-06-02 18:08:56 +0200 |
commit | 0944565f6033766abfe0ab8987d7116062748c21 (patch) | |
tree | e5b12a01cf6acb8f05e5ef4c35e54af328224a38 /core/js | |
parent | 679d10ca00577f8175fe03ff5c25f4aa41547177 (diff) | |
download | nextcloud-server-0944565f6033766abfe0ab8987d7116062748c21.tar.gz nextcloud-server-0944565f6033766abfe0ab8987d7116062748c21.zip |
More unit tests and fixes for share
Diffstat (limited to 'core/js')
-rw-r--r-- | core/js/share.js | 37 | ||||
-rw-r--r-- | core/js/tests/specs/shareSpec.js | 144 |
2 files changed, 166 insertions, 15 deletions
diff --git a/core/js/share.js b/core/js/share.js index c4ca63908a9..4e12f35d19e 100644 --- a/core/js/share.js +++ b/core/js/share.js @@ -47,6 +47,7 @@ OC.Share={ $fileList = fileList.$fileList; currentDir = fileList.getCurrentDirectory(); } + // TODO: iterating over the files might be more efficient for (item in OC.Share.statuses){ var image = OC.imagePath('core', 'actions/share'); var data = OC.Share.statuses[item]; @@ -63,7 +64,7 @@ OC.Share={ var shareFolder = OC.imagePath('core', 'filetypes/folder-shared'); var img; if (file.length > 0) { - this.markFileAsShared(file, true, image); + this.markFileAsShared(file, true, hasLink); } else { var dir = currentDir; if (dir.length > 1) { @@ -122,7 +123,11 @@ OC.Share={ } else { var $tr = $('tr').filterAttr('data-id', String(itemSource)); if ($tr.length > 0) { - this.markFileAsShared($tr, shares, image); + // it might happen that multiple lists exist in the DOM + // with the same id + $tr.each(function() { + OC.Share.markFileAsShared($(this), shares, link); + }); } } if (shares) { @@ -133,13 +138,14 @@ OC.Share={ } }, /** - * Marks/unmarks a given file as shared + * Marks/unmarks a given file as shared by changing its action icon + * and folder icon. * * @param $tr file element to mark as shared - * @param state true to mark as shared, false to unmark - * @param image image to use for the icon + * @param hasShares whether shares are available + * @param hasLink whether link share is available */ - markFileAsShared: function($tr, state, image) { + markFileAsShared: function($tr, hasShares, hasLink) { var action = $tr.find('.fileactions .action[data-action="Share"]'); var type = $tr.data('type'); var img = action.find('img'); @@ -147,14 +153,22 @@ OC.Share={ var recipients; var owner; var shareFolderIcon; - if (type === 'dir' && state) { - shareFolderIcon = OC.imagePath('core', 'filetypes/folder-shared'); + var image = OC.imagePath('core', 'actions/share'); + // update folder icon + if (type === 'dir' && (hasShares || hasLink)) { + if (hasLink) { + shareFolderIcon = OC.imagePath('core', 'filetypes/folder-public'); + } + else { + shareFolderIcon = OC.imagePath('core', 'filetypes/folder-shared'); + } $tr.children('.filename').css('background-image', 'url(' + shareFolderIcon + ')'); } else if (type === 'dir') { shareFolderIcon = OC.imagePath('core', 'filetypes/folder'); $tr.children('.filename').css('background-image', 'url(' + shareFolderIcon + ')'); } - if (state) { + // update share action text / icon + if (hasShares) { recipients = $tr.attr('data-share-recipients'); owner = $tr.attr('data-share-owner'); @@ -162,7 +176,6 @@ OC.Share={ message = t('core', 'Shared'); if (owner && !recipients) { message = t('files_sharing', 'Shared by {owner}', {owner: owner}); - image = image || OC.imagePath('core', 'actions/share'); } if (recipients) { message = t('core', 'Shared with {recipients}', {recipients: recipients}); @@ -173,6 +186,9 @@ OC.Share={ action.removeClass('permanent'); action.html(' <span>'+ escapeHTML(t('core', 'Share'))+'</span>').prepend(img); } + if (hasLink) { + image = OC.imagePath('core', 'actions/public'); + } img.attr('src', image); }, loadItem:function(itemType, itemSource) { @@ -756,6 +772,7 @@ $(document).ready(function() { if ($('#linkText').val() !== '') { OC.Share.unshare(itemType, itemSource, OC.Share.SHARE_TYPE_LINK, '', function() { OC.Share.itemShares[OC.Share.SHARE_TYPE_LINK] = false; + $('#dropdown').trigger(new $.Event('sharesChanged', {itemShares: OC.Share.itemShares})); OC.Share.updateIcon(itemType, itemSource); if (typeof OC.Share.statuses[itemSource] === 'undefined') { $('#expiration').hide('blind'); diff --git a/core/js/tests/specs/shareSpec.js b/core/js/tests/specs/shareSpec.js index a487b71fdbb..a8d9225649c 100644 --- a/core/js/tests/specs/shareSpec.js +++ b/core/js/tests/specs/shareSpec.js @@ -26,13 +26,17 @@ describe('OC.Share tests', function() { var oldAppConfig; var loadItemStub; var autocompleteStub; + beforeEach(function() { $('#testArea').append($('<div id="shareContainer"></div>')); + // horrible parameters + $('#testArea').append('<input id="allowShareWithLink" type="hidden" value="yes">'); $container = $('#shareContainer'); /* jshint camelcase:false */ - oldAppConfig = oc_appconfig.core; - loadItemStub = sinon.stub(OC.Share, 'loadItem'); + oldAppConfig = _.extend({}, oc_appconfig.core); + oc_appconfig.core.enforcePasswordForPublicLink = false; + loadItemStub = sinon.stub(OC.Share, 'loadItem'); loadItemStub.returns({ reshare: [], shares: [] @@ -89,9 +93,139 @@ describe('OC.Share tests', function() { oc_appconfig.core.defaultExpireDate = ''; // TODO: expect that default date was NOT set }); - // TODO: test password field visibility (whenever enforced or not) - // TODO: check link share field visibility based on whether it is allowed - // TODO: check public upload visibility based on config + describe('Share with link', function() { + // TODO: test ajax calls + // TODO: test password field visibility (whenever enforced or not) + // TODO: check public upload visibility based on config + it('shows share with link checkbox when allowed', function() { + $('#allowShareWithLink').val('yes'); + OC.Share.showDropDown( + 'file', + 123, + $container, + 'http://localhost/dummylink', + 31, + 'shared_file_name.txt' + ); + expect($('#dropdown #linkCheckbox').length).toEqual(1); + }); + it('does not show share with link checkbox when not allowed', function() { + $('#allowShareWithLink').val('no'); + OC.Share.showDropDown( + 'file', + 123, + $container, + 'http://localhost/dummylink', + 31, + 'shared_file_name.txt' + ); + expect($('#dropdown #linkCheckbox').length).toEqual(0); + }); + }); + describe('"sharesChanged" event', function() { + var autocompleteOptions; + var handler; + beforeEach(function() { + handler = sinon.stub(); + loadItemStub.returns({ + reshare: [], + shares: [{ + id: 100, + item_source: 123, + permissions: 31, + share_type: OC.Share.SHARE_TYPE_USER, + share_with: 'user1', + share_with_displayname: 'User One' + }] + }); + OC.Share.showDropDown( + 'file', + 123, + $container, + 'http://localhost/dummylink', + 31, + 'shared_file_name.txt' + ); + $('#dropdown').on('sharesChanged', handler); + autocompleteOptions = autocompleteStub.getCall(0).args[0]; + }); + afterEach(function() { + autocompleteOptions = null; + handler = null; + }); + it('triggers "sharesChanged" event when adding shares', function() { + // simulate autocomplete selection + autocompleteOptions.select(new $.Event('select'), { + item: { + label: 'User Two', + value: { + shareType: OC.Share.SHARE_TYPE_USER, + shareWith: 'user2' + } + } + }); + fakeServer.requests[0].respond( + 200, + { 'Content-Type': 'application/json' }, + JSON.stringify({status: 'success'}) + ); + expect(handler.calledOnce).toEqual(true); + var itemShares = handler.getCall(0).args[0].itemShares; + expect(itemShares).toBeDefined(); + expect(itemShares[OC.Share.SHARE_TYPE_USER]).toEqual(['user1', 'user2']); + expect(itemShares[OC.Share.SHARE_TYPE_GROUP]).not.toBeDefined(); + expect(itemShares[OC.Share.SHARE_TYPE_LINK]).not.toBeDefined(); + }); + it('triggers "sharesChanged" event when deleting shares', function() { + $('#dropdown .unshare:eq(0)').click(); + fakeServer.requests[0].respond( + 200, + { 'Content-Type': 'application/json' }, + JSON.stringify({status: 'success'}) + ); + expect(handler.calledOnce).toEqual(true); + var itemShares = handler.getCall(0).args[0].itemShares; + expect(itemShares).toBeDefined(); + expect(itemShares[OC.Share.SHARE_TYPE_USER]).toEqual([]); + expect(itemShares[OC.Share.SHARE_TYPE_GROUP]).not.toBeDefined(); + expect(itemShares[OC.Share.SHARE_TYPE_LINK]).not.toBeDefined(); + }); + it('triggers "sharesChanged" event when toggling link share', function() { + // simulate autocomplete selection + $('#dropdown #linkCheckbox').click(); + fakeServer.requests[0].respond( + 200, + { 'Content-Type': 'application/json' }, + JSON.stringify({status: 'success', data: { token: 'abc' }}) + ); + expect(handler.calledOnce).toEqual(true); + var itemShares = handler.getCall(0).args[0].itemShares; + expect(itemShares).toBeDefined(); + expect(itemShares[OC.Share.SHARE_TYPE_USER]).toEqual(['user1']); + expect(itemShares[OC.Share.SHARE_TYPE_GROUP]).not.toBeDefined(); + expect(itemShares[OC.Share.SHARE_TYPE_LINK]).toEqual(true); + + handler.reset(); + + // uncheck checkbox + $('#dropdown #linkCheckbox').click(); + fakeServer.requests[1].respond( + 200, + { 'Content-Type': 'application/json' }, + JSON.stringify({status: 'success'}) + ); + + expect(handler.calledOnce).toEqual(true); + itemShares = handler.getCall(0).args[0].itemShares; + expect(itemShares).toBeDefined(); + expect(itemShares[OC.Share.SHARE_TYPE_USER]).toEqual(['user1']); + expect(itemShares[OC.Share.SHARE_TYPE_GROUP]).not.toBeDefined(); + // currently inconsistent, removing share with link sets it to false + // instead of delete + expect(itemShares[OC.Share.SHARE_TYPE_LINK]).toBeFalsy(); + //expect(itemShares[OC.Share.SHARE_TYPE_LINK]).not.toBeDefined(); + }); + }); }); }); |