summaryrefslogtreecommitdiffstats
path: root/apps
diff options
context:
space:
mode:
authorVincent Petry <pvince81@owncloud.com>2014-06-02 17:23:52 +0200
committerVincent Petry <pvince81@owncloud.com>2014-06-02 18:08:56 +0200
commit0944565f6033766abfe0ab8987d7116062748c21 (patch)
treee5b12a01cf6acb8f05e5ef4c35e54af328224a38 /apps
parent679d10ca00577f8175fe03ff5c25f4aa41547177 (diff)
downloadnextcloud-server-0944565f6033766abfe0ab8987d7116062748c21.tar.gz
nextcloud-server-0944565f6033766abfe0ab8987d7116062748c21.zip
More unit tests and fixes for share
Diffstat (limited to 'apps')
-rw-r--r--apps/files_sharing/js/share.js201
-rw-r--r--apps/files_sharing/tests/js/shareSpec.js179
2 files changed, 255 insertions, 125 deletions
diff --git a/apps/files_sharing/js/share.js b/apps/files_sharing/js/share.js
index 9f47e785821..66def1fe73f 100644
--- a/apps/files_sharing/js/share.js
+++ b/apps/files_sharing/js/share.js
@@ -14,119 +14,112 @@
}
OCA.Sharing.Util = {
initialize: function(fileActions) {
- if (!_.isUndefined(OC.Share) && !_.isUndefined(OCA.Files)) {
- // TODO: make a separate class for this or a hook or jQuery event ?
- if (OCA.Files.FileList) {
- var oldCreateRow = OCA.Files.FileList.prototype._createRow;
- OCA.Files.FileList.prototype._createRow = function(fileData) {
- var tr = oldCreateRow.apply(this, arguments);
- if (fileData.shareOwner) {
- tr.attr('data-share-owner', fileData.shareOwner);
- // user should always be able to rename a mount point
- if (fileData.isShareMountPoint) {
- tr.attr('data-permissions', fileData.permissions | OC.PERMISSION_UPDATE);
- tr.attr('data-reshare-permissions', fileData.permissions);
- }
+ // TODO: make a separate class for this or a hook or jQuery event ?
+ if (OCA.Files.FileList) {
+ var oldCreateRow = OCA.Files.FileList.prototype._createRow;
+ OCA.Files.FileList.prototype._createRow = function(fileData) {
+ var tr = oldCreateRow.apply(this, arguments);
+ if (fileData.shareOwner) {
+ tr.attr('data-share-owner', fileData.shareOwner);
+ // user should always be able to rename a mount point
+ if (fileData.isShareMountPoint) {
+ tr.attr('data-permissions', fileData.permissions | OC.PERMISSION_UPDATE);
+ tr.attr('data-reshare-permissions', fileData.permissions);
}
- if (fileData.recipientsDisplayName) {
- tr.attr('data-share-recipients', fileData.recipientsDisplayName);
- }
- return tr;
- };
- }
+ }
+ if (fileData.recipientsDisplayName) {
+ tr.attr('data-share-recipients', fileData.recipientsDisplayName);
+ }
+ return tr;
+ };
+ }
- // use delegate to catch the case with multiple file lists
- $('#content').delegate('#fileList', 'fileActionsReady',function(ev){
- // if no share action exists because the admin disabled sharing for this user
- // we create a share notification action to inform the user about files
- // shared with him otherwise we just update the existing share action.
- var fileList = ev.fileList;
- var $fileList = $(this);
- $fileList.find('[data-share-owner]').each(function() {
- var $tr = $(this);
- var permissions = $tr.data('permissions');
- if(permissions & OC.PERMISSION_SHARE) {
- OC.Share.markFileAsShared($tr, true);
- } else {
- // TODO: make this work like/with OC.Share.markFileAsShared()
- var shareNotification = '<a class="action action-share-notification permanent"' +
- ' data-action="Share-Notification" href="#" original-title="">' +
- ' <img class="svg" src="' + OC.imagePath('core', 'actions/share') + '"></img>';
- $tr.find('.fileactions').append(function() {
- var owner = $(this).closest('tr').attr('data-share-owner');
- var shareBy = t('files_sharing', 'Shared by {owner}', {owner: owner});
- var $result = $(shareNotification + '<span> ' + shareBy + '</span></span>');
- $result.on('click', function() {
- return false;
- });
- return $result;
+ // use delegate to catch the case with multiple file lists
+ $('#content').delegate('#fileList', 'fileActionsReady',function(ev){
+ // if no share action exists because the admin disabled sharing for this user
+ // we create a share notification action to inform the user about files
+ // shared with him otherwise we just update the existing share action.
+ var fileList = ev.fileList;
+ var $fileList = $(this);
+ $fileList.find('[data-share-owner]').each(function() {
+ var $tr = $(this);
+ var permissions = $tr.data('permissions');
+ if(permissions & OC.PERMISSION_SHARE) {
+ OC.Share.markFileAsShared($tr, true);
+ } else {
+ // TODO: make this work like/with OC.Share.markFileAsShared()
+ var shareNotification = '<a class="action action-share-notification permanent"' +
+ ' data-action="Share-Notification" href="#" original-title="">' +
+ ' <img class="svg" src="' + OC.imagePath('core', 'actions/share') + '"></img>';
+ $tr.find('.fileactions').append(function() {
+ var owner = $(this).closest('tr').attr('data-share-owner');
+ var shareBy = t('files_sharing', 'Shared by {owner}', {owner: owner});
+ var $result = $(shareNotification + '<span> ' + shareBy + '</span></span>');
+ $result.on('click', function() {
+ return false;
});
- }
- });
-
- if (!OCA.Sharing.sharesLoaded){
- OC.Share.loadIcons('file', fileList);
- // assume that we got all shares, so switching directories
- // will not invalidate that list
- OCA.Sharing.sharesLoaded = true;
- }
- else{
- OC.Share.updateIcons('file', fileList);
+ return $result;
+ });
}
});
- fileActions.register(
- 'all',
- 'Share',
- OC.PERMISSION_SHARE,
- OC.imagePath('core', 'actions/share'),
- function(filename, context) {
+ if (!OCA.Sharing.sharesLoaded){
+ OC.Share.loadIcons('file', fileList);
+ // assume that we got all shares, so switching directories
+ // will not invalidate that list
+ OCA.Sharing.sharesLoaded = true;
+ }
+ else{
+ OC.Share.updateIcons('file', fileList);
+ }
+ });
- var $tr = context.$file;
- var itemType = 'file';
- if ($tr.data('type') === 'dir') {
- itemType = 'folder';
- }
- var possiblePermissions = $tr.data('reshare-permissions');
- if (_.isUndefined(possiblePermissions)) {
- possiblePermissions = $tr.data('permissions');
- }
+ fileActions.register(
+ 'all',
+ 'Share',
+ OC.PERMISSION_SHARE,
+ OC.imagePath('core', 'actions/share'),
+ function(filename, context) {
- var appendTo = $tr.find('td.filename');
- // Check if drop down is already visible for a different file
- if (OC.Share.droppedDown) {
- if ($tr.data('id') !== $('#dropdown').attr('data-item-source')) {
- OC.Share.hideDropDown(function () {
- $tr.addClass('mouseOver');
- OC.Share.showDropDown(itemType, $tr.data('id'), appendTo, true, possiblePermissions, filename);
- });
- } else {
- OC.Share.hideDropDown();
- }
+ var $tr = context.$file;
+ var itemType = 'file';
+ if ($tr.data('type') === 'dir') {
+ itemType = 'folder';
+ }
+ var possiblePermissions = $tr.data('reshare-permissions');
+ if (_.isUndefined(possiblePermissions)) {
+ possiblePermissions = $tr.data('permissions');
+ }
+
+ var appendTo = $tr.find('td.filename');
+ // Check if drop down is already visible for a different file
+ if (OC.Share.droppedDown) {
+ if ($tr.data('id') !== $('#dropdown').attr('data-item-source')) {
+ OC.Share.hideDropDown(function () {
+ $tr.addClass('mouseOver');
+ OC.Share.showDropDown(itemType, $tr.data('id'), appendTo, true, possiblePermissions, filename);
+ });
} else {
- $tr.addClass('mouseOver');
- OC.Share.showDropDown(itemType, $tr.data('id'), appendTo, true, possiblePermissions, filename);
+ OC.Share.hideDropDown();
+ }
+ } else {
+ $tr.addClass('mouseOver');
+ OC.Share.showDropDown(itemType, $tr.data('id'), appendTo, true, possiblePermissions, filename);
+ }
+ $('#dropdown').on('sharesChanged', function(ev) {
+ // note: we only update the data attribute because updateIcon()
+ // is called automatically after this event
+ var userShares = ev.itemShares[OC.Share.SHARE_TYPE_USER] || [];
+ var groupShares = ev.itemShares[OC.Share.SHARE_TYPE_GROUP] || [];
+ var recipients = _.union(userShares, groupShares);
+ if (recipients.length) {
+ $tr.attr('data-share-recipients', OCA.Sharing.Util.formatRecipients(recipients));
+ }
+ else {
+ $tr.removeAttr('data-share-recipients');
}
- $('#dropdown').on('sharesChanged', function(ev) {
- // note: we only update the data attribute because updateIcon()
- // is called automatically after this event
- var userShares = ev.itemShares[OC.Share.SHARE_TYPE_USER] || [];
- var groupShares = ev.itemShares[OC.Share.SHARE_TYPE_GROUP] || [];
- var recipients = _.union(userShares, groupShares);
- // only update the recipients if they existed before
- // (some file lists don't have them)
- if (!_.isUndefined($tr.attr('data-share-recipients'))) {
- // FIXME: use display names from users, we currently only got user ids
- if (recipients.length) {
- $tr.attr('data-share-recipients', OCA.Sharing.Util.formatRecipients(recipients));
- }
- else {
- $tr.attr('data-share-recipients', '');
- }
- }
- });
});
- }
+ });
},
/**
@@ -159,7 +152,9 @@
$(document).ready(function() {
// FIXME: HACK: do not init when running unit tests, need a better way
if (!window.TESTING) {
- OCA.Sharing.Util.initialize(OCA.Files.fileActions);
+ if (!_.isUndefined(OC.Share) && !_.isUndefined(OCA.Files)) {
+ OCA.Sharing.Util.initialize(OCA.Files.fileActions);
+ }
}
});
diff --git a/apps/files_sharing/tests/js/shareSpec.js b/apps/files_sharing/tests/js/shareSpec.js
index 604a4bb2bb6..d2697beeb0b 100644
--- a/apps/files_sharing/tests/js/shareSpec.js
+++ b/apps/files_sharing/tests/js/shareSpec.js
@@ -13,6 +13,16 @@ describe('OCA.Sharing.Util tests', function() {
var fileList;
var testFiles;
+ function getImageUrl($el) {
+ // might be slightly different cross-browser
+ var url = $el.css('background-image');
+ var r = url.match(/url\(['"]?([^'")]*)['"]?\)/);
+ if (!r) {
+ return url;
+ }
+ return r[1];
+ }
+
beforeEach(function() {
// back up prototype, as it will be extended by
// the sharing code
@@ -59,6 +69,7 @@ describe('OCA.Sharing.Util tests', function() {
afterEach(function() {
OCA.Files.FileList.prototype = oldFileListPrototype;
delete OCA.Sharing.sharesLoaded;
+ delete OC.Share.droppedDown;
OC.Share.statuses = {};
});
@@ -67,62 +78,71 @@ describe('OCA.Sharing.Util tests', function() {
});
describe('Share action icon', function() {
it('do not shows share text when not shared', function() {
- var $action;
+ var $action, $tr;
OC.Share.statuses = {};
fileList.setFiles([{
id: 1,
- type: 'file',
- name: 'One.txt',
+ type: 'dir',
+ name: 'One',
path: '/subdir',
mimetype: 'text/plain',
size: 12,
permissions: 31,
etag: 'abc'
}]);
- $action = fileList.$el.find('tbody tr:first .action-share');
+ $tr = fileList.$el.find('tbody tr:first');
+ $action = $tr.find('.action-share');
expect($action.hasClass('permanent')).toEqual(false);
expect(OC.basename($action.find('img').attr('src'))).toEqual('share.svg');
+ expect(OC.basename(getImageUrl($tr.find('.filename')))).toEqual('folder.svg');
+ expect($action.find('img').length).toEqual(1);
});
it('shows simple share text with share icon', function() {
- var $action;
+ var $action, $tr;
fileList.setFiles([{
id: 1,
- type: 'file',
- name: 'One.txt',
+ type: 'dir',
+ name: 'One',
path: '/subdir',
mimetype: 'text/plain',
size: 12,
permissions: 31,
etag: 'abc'
}]);
- $action = fileList.$el.find('tbody tr:first .action-share');
+ $tr = fileList.$el.find('tbody tr:first');
+ $action = $tr.find('.action-share');
expect($action.hasClass('permanent')).toEqual(true);
expect($action.find('>span').text()).toEqual('Shared');
expect(OC.basename($action.find('img').attr('src'))).toEqual('share.svg');
+ expect(OC.basename(getImageUrl($tr.find('.filename')))).toEqual('folder-shared.svg');
+ expect($action.find('img').length).toEqual(1);
});
it('shows simple share text with public icon when shared with link', function() {
- var $action;
+ var $action, $tr;
OC.Share.statuses = {1: {link: true, path: '/subdir'}};
fileList.setFiles([{
id: 1,
- type: 'file',
- name: 'One.txt',
+ type: 'dir',
+ name: 'One',
path: '/subdir',
mimetype: 'text/plain',
size: 12,
permissions: 31,
etag: 'abc'
}]);
- $action = fileList.$el.find('tbody tr:first .action-share');
+ $tr = fileList.$el.find('tbody tr:first');
+ $action = $tr.find('.action-share');
expect($action.hasClass('permanent')).toEqual(true);
expect($action.find('>span').text()).toEqual('Shared');
expect(OC.basename($action.find('img').attr('src'))).toEqual('public.svg');
+ expect(OC.basename(getImageUrl($tr.find('.filename')))).toEqual('folder-public.svg');
+ expect($action.find('img').length).toEqual(1);
});
it('shows owner name when owner is available', function() {
- var $action;
+ var $action, $tr;
fileList.setFiles([{
id: 1,
- type: 'file',
+ type: 'dir',
name: 'One.txt',
path: '/subdir',
mimetype: 'text/plain',
@@ -131,16 +151,18 @@ describe('OCA.Sharing.Util tests', function() {
shareOwner: 'User One',
etag: 'abc'
}]);
- $action = fileList.$el.find('tbody tr:first .action-share');
+ $tr = fileList.$el.find('tbody tr:first');
+ $action = $tr.find('.action-share');
expect($action.hasClass('permanent')).toEqual(true);
expect($action.find('>span').text()).toEqual('Shared by User One');
expect(OC.basename($action.find('img').attr('src'))).toEqual('share.svg');
+ expect(OC.basename(getImageUrl($tr.find('.filename')))).toEqual('folder-shared.svg');
});
it('shows recipients when recipients are available', function() {
- var $action;
+ var $action, $tr;
fileList.setFiles([{
id: 1,
- type: 'file',
+ type: 'dir',
name: 'One.txt',
path: '/subdir',
mimetype: 'text/plain',
@@ -149,17 +171,130 @@ describe('OCA.Sharing.Util tests', function() {
recipientsDisplayName: 'User One, User Two',
etag: 'abc'
}]);
- $action = fileList.$el.find('tbody tr:first .action-share');
+ $tr = fileList.$el.find('tbody tr:first');
+ $action = $tr.find('.action-share');
expect($action.hasClass('permanent')).toEqual(true);
expect($action.find('>span').text()).toEqual('Shared with User One, User Two');
expect(OC.basename($action.find('img').attr('src'))).toEqual('share.svg');
+ expect(OC.basename(getImageUrl($tr.find('.filename')))).toEqual('folder-shared.svg');
+ expect($action.find('img').length).toEqual(1);
});
});
describe('Share action', function() {
- // TODO: test file action / dropdown trigger
- it('updates share icon when shares were changed in dropdown', function() {
- fileList.setFiles(testFiles);
- fileList.$el.find('tr:first .action-share').click();
+ var showDropDownStub;
+
+ beforeEach(function() {
+ showDropDownStub = sinon.stub(OC.Share, 'showDropDown', function() {
+ $('#testArea').append($('<div id="dropdown"></div>'));
+ });
+ });
+ afterEach(function() {
+ showDropDownStub.restore();
+ });
+ it('adds share icon after sharing a non-shared file', function() {
+ var $action, $tr;
+ OC.Share.statuses = {};
+ fileList.setFiles([{
+ id: 1,
+ type: 'file',
+ name: 'One.txt',
+ path: '/subdir',
+ mimetype: 'text/plain',
+ size: 12,
+ permissions: 31,
+ etag: 'abc'
+ }]);
+ $action = fileList.$el.find('tbody tr:first .action-share');
+ $tr = fileList.$el.find('tr:first');
+
+ expect($action.hasClass('permanent')).toEqual(false);
+
+ $tr.find('.action-share').click();
+
+ expect(showDropDownStub.calledOnce).toEqual(true);
+
+ // simulate what the dropdown does
+ var itemShares = {};
+ itemShares[OC.Share.SHARE_TYPE_USER] = ['User One', 'User Two'];
+ itemShares[OC.Share.SHARE_TYPE_GROUP] = ['Group One', 'Group Two'];
+ OC.Share.itemShares = itemShares;
+ $('#dropdown').trigger(new $.Event('sharesChanged', {itemShares: itemShares}));
+
+ expect($tr.attr('data-share-recipients')).toEqual('Group One, Group Two, User One, User Two');
+
+ OC.Share.updateIcon('file', 1);
+ expect($action.hasClass('permanent')).toEqual(true);
+ expect($action.find('>span').text()).toEqual('Shared with Group One, Group Two, User One, User Two');
+ expect(OC.basename($action.find('img').attr('src'))).toEqual('share.svg');
+ });
+ it('removes share icon after unsharing a shared file', function() {
+ var $action, $tr;
+ OC.Share.statuses = {1: {link: false, path: '/subdir'}};
+ fileList.setFiles([{
+ id: 1,
+ type: 'file',
+ name: 'One.txt',
+ path: '/subdir',
+ mimetype: 'text/plain',
+ size: 12,
+ permissions: 31,
+ etag: 'abc'
+ }]);
+ $action = fileList.$el.find('tbody tr:first .action-share');
+ $tr = fileList.$el.find('tr:first');
+
+ expect($action.hasClass('permanent')).toEqual(true);
+
+ $tr.find('.action-share').click();
+
+ expect(showDropDownStub.calledOnce).toEqual(true);
+
+ // simulate what the dropdown does
+ var itemShares = {};
+ itemShares[OC.Share.SHARE_TYPE_USER] = ['User One', 'User Two', 'User Three'];
+ OC.Share.itemShares = itemShares;
+ $('#dropdown').trigger(new $.Event('sharesChanged', {itemShares: itemShares}));
+
+ expect($tr.attr('data-share-recipients')).toEqual('User One, User Three, User Two');
+
+ OC.Share.updateIcon('file', 1);
+
+ expect($action.hasClass('permanent')).toEqual(true);
+ expect($action.find('>span').text()).toEqual('Shared with User One, User Three, User Two');
+ expect(OC.basename($action.find('img').attr('src'))).toEqual('share.svg');
+ });
+ it('updates share icon after modifying shares on a shared file', function() {
+ var $action, $tr;
+ OC.Share.statuses = {1: {link: false, path: '/subdir'}};
+ fileList.setFiles([{
+ id: 1,
+ type: 'file',
+ name: 'One.txt',
+ path: '/subdir',
+ mimetype: 'text/plain',
+ size: 12,
+ permissions: 31,
+ etag: 'abc',
+ recipients: 'User One, User Two'
+ }]);
+ $action = fileList.$el.find('tbody tr:first .action-share');
+ $tr = fileList.$el.find('tr:first');
+
+ expect($action.hasClass('permanent')).toEqual(true);
+
+ $tr.find('.action-share').click();
+
+ expect(showDropDownStub.calledOnce).toEqual(true);
+
+ // simulate what the dropdown does
+ var itemShares = {};
+ OC.Share.itemShares = itemShares;
+ $('#dropdown').trigger(new $.Event('sharesChanged', {itemShares: itemShares}));
+
+ expect($tr.attr('data-share-recipients')).not.toBeDefined();
+
+ OC.Share.updateIcon('file', 1);
+ expect($action.hasClass('permanent')).toEqual(false);
});
});
describe('formatRecipients', function() {