349 lines
10 KiB
JavaScript
349 lines
10 KiB
JavaScript
/*
|
|
* Copyright (c) 2014 Vincent Petry <pvince81@owncloud.com>
|
|
*
|
|
* This file is licensed under the Affero General Public License version 3
|
|
* or later.
|
|
*
|
|
* See the COPYING-README file.
|
|
*
|
|
*/
|
|
|
|
describe('OCA.Sharing.Util tests', function() {
|
|
var oldFileListPrototype;
|
|
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
|
|
oldFileListPrototype = _.extend({}, OCA.Files.FileList.prototype);
|
|
|
|
var $content = $('<div id="content"></div>');
|
|
$('#testArea').append($content);
|
|
// dummy file list
|
|
var $div = $(
|
|
'<div>' +
|
|
'<table id="filestable">' +
|
|
'<thead></thead>' +
|
|
'<tbody id="fileList"></tbody>' +
|
|
'</table>' +
|
|
'</div>');
|
|
$('#content').append($div);
|
|
|
|
var fileActions = new OCA.Files.FileActions();
|
|
OCA.Sharing.Util.initialize(fileActions);
|
|
fileList = new OCA.Files.FileList(
|
|
$div, {
|
|
fileActions : fileActions
|
|
}
|
|
);
|
|
|
|
testFiles = [{
|
|
id: 1,
|
|
type: 'file',
|
|
name: 'One.txt',
|
|
path: '/subdir',
|
|
mimetype: 'text/plain',
|
|
size: 12,
|
|
permissions: 31,
|
|
etag: 'abc',
|
|
shareOwner: 'User One',
|
|
isShareMountPoint: false
|
|
}];
|
|
|
|
OCA.Sharing.sharesLoaded = true;
|
|
OC.Share.statuses = {
|
|
1: {link: false, path: '/subdir'}
|
|
};
|
|
});
|
|
afterEach(function() {
|
|
OCA.Files.FileList.prototype = oldFileListPrototype;
|
|
delete OCA.Sharing.sharesLoaded;
|
|
delete OC.Share.droppedDown;
|
|
OC.Share.statuses = {};
|
|
});
|
|
|
|
describe('Sharing data in table row', function() {
|
|
// TODO: test data-permissions, data-share-owner, etc
|
|
});
|
|
describe('Share action icon', function() {
|
|
it('do not shows share text when not shared', function() {
|
|
var $action, $tr;
|
|
OC.Share.statuses = {};
|
|
fileList.setFiles([{
|
|
id: 1,
|
|
type: 'dir',
|
|
name: 'One',
|
|
path: '/subdir',
|
|
mimetype: 'text/plain',
|
|
size: 12,
|
|
permissions: 31,
|
|
etag: 'abc'
|
|
}]);
|
|
$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, $tr;
|
|
fileList.setFiles([{
|
|
id: 1,
|
|
type: 'dir',
|
|
name: 'One',
|
|
path: '/subdir',
|
|
mimetype: 'text/plain',
|
|
size: 12,
|
|
permissions: 31,
|
|
etag: 'abc'
|
|
}]);
|
|
$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, $tr;
|
|
OC.Share.statuses = {1: {link: true, path: '/subdir'}};
|
|
fileList.setFiles([{
|
|
id: 1,
|
|
type: 'dir',
|
|
name: 'One',
|
|
path: '/subdir',
|
|
mimetype: 'text/plain',
|
|
size: 12,
|
|
permissions: 31,
|
|
etag: 'abc'
|
|
}]);
|
|
$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, $tr;
|
|
fileList.setFiles([{
|
|
id: 1,
|
|
type: 'dir',
|
|
name: 'One.txt',
|
|
path: '/subdir',
|
|
mimetype: 'text/plain',
|
|
size: 12,
|
|
permissions: 31,
|
|
shareOwner: 'User One',
|
|
etag: 'abc'
|
|
}]);
|
|
$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, $tr;
|
|
fileList.setFiles([{
|
|
id: 1,
|
|
type: 'dir',
|
|
name: 'One.txt',
|
|
path: '/subdir',
|
|
mimetype: 'text/plain',
|
|
size: 12,
|
|
permissions: 31,
|
|
recipientsDisplayName: 'User One, User Two',
|
|
etag: 'abc'
|
|
}]);
|
|
$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() {
|
|
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() {
|
|
it('returns a single recipient when one passed', function() {
|
|
expect(OCA.Sharing.Util.formatRecipients(['User one']))
|
|
.toEqual('User one');
|
|
});
|
|
it('returns two recipients when two passed', function() {
|
|
expect(OCA.Sharing.Util.formatRecipients(['User one', 'User two']))
|
|
.toEqual('User one, User two');
|
|
});
|
|
it('returns four recipients with plus when five passed', function() {
|
|
var recipients = [
|
|
'User one',
|
|
'User two',
|
|
'User three',
|
|
'User four',
|
|
'User five'
|
|
];
|
|
expect(OCA.Sharing.Util.formatRecipients(recipients))
|
|
.toEqual('User four, User one, User three, User two, +1');
|
|
});
|
|
it('returns four recipients with plus when ten passed', function() {
|
|
var recipients = [
|
|
'User one',
|
|
'User two',
|
|
'User three',
|
|
'User four',
|
|
'User five',
|
|
'User six',
|
|
'User seven',
|
|
'User eight',
|
|
'User nine',
|
|
'User ten'
|
|
];
|
|
expect(OCA.Sharing.Util.formatRecipients(recipients))
|
|
.toEqual('User four, User one, User three, User two, +6');
|
|
});
|
|
it('returns four recipients with plus when four passed with counter', function() {
|
|
var recipients = [
|
|
'User one',
|
|
'User two',
|
|
'User three',
|
|
'User four'
|
|
];
|
|
expect(OCA.Sharing.Util.formatRecipients(recipients, 10))
|
|
.toEqual('User four, User one, User three, User two, +6');
|
|
});
|
|
});
|
|
|
|
});
|