server/core/js/tests/specs/shareitemmodelSpec.js

284 lines
7.2 KiB
JavaScript
Raw Normal View History

/**
* ownCloud
*
* @author Vincent Petry
* @copyright 2015 Vincent Petry <pvince81@owncloud.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
* License as published by the Free Software Foundation; either
* version 3 of the License, or any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU AFFERO GENERAL PUBLIC LICENSE for more details.
*
* You should have received a copy of the GNU Affero General Public
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
*
*/
/* global oc_appconfig */
describe('OC.Share.ShareItemModel', function() {
var loadItemStub;
var fileInfoModel, configModel, model;
2015-09-15 13:29:30 +00:00
var oldCurrentUser;
beforeEach(function() {
2015-09-15 13:29:30 +00:00
oldCurrentUser = OC.currentUser;
loadItemStub = sinon.stub(OC.Share, 'loadItem');
fileInfoModel = new OCA.Files.FileInfoModel({
id: 123,
name: 'shared_file_name.txt',
path: '/subdir',
size: 100,
mimetype: 'text/plain',
permissions: 31,
sharePermissions: 31
});
var attributes = {
itemType: fileInfoModel.isDirectory() ? 'folder' : 'file',
itemSource: fileInfoModel.get('id'),
possiblePermissions: fileInfoModel.get('sharePermissions')
};
configModel = new OC.Share.ShareConfigModel();
model = new OC.Share.ShareItemModel(attributes, {
configModel: configModel,
fileInfoModel: fileInfoModel
});
});
afterEach(function() {
loadItemStub.restore();
2015-09-15 13:29:30 +00:00
OC.currentUser = oldCurrentUser;
});
describe('Fetching and parsing', function() {
it('fetching calls loadItem with the correct arguments', function() {
model.fetch();
expect(loadItemStub.calledOnce).toEqual(true);
expect(loadItemStub.calledWith('file', 123)).toEqual(true);
});
it('populates attributes with parsed response', function() {
2015-09-15 13:29:30 +00:00
loadItemStub.yields({
/* jshint camelcase: false */
reshare: {
share_type: OC.Share.SHARE_TYPE_USER,
uid_owner: 'owner',
displayname_owner: 'Owner',
permissions: 31
},
shares: [{
id: 100,
item_source: 123,
permissions: 31,
share_type: OC.Share.SHARE_TYPE_USER,
share_with: 'user1',
share_with_displayname: 'User One'
}, {
id: 101,
item_source: 123,
permissions: 31,
share_type: OC.Share.SHARE_TYPE_GROUP,
share_with: 'group',
share_with_displayname: 'group'
}, {
id: 102,
item_source: 123,
permissions: 31,
share_type: OC.Share.SHARE_TYPE_REMOTE,
share_with: 'foo@bar.com/baz',
share_with_displayname: 'foo@bar.com/baz'
}, {
displayname_owner: 'root',
expiration: null,
file_source: 123,
file_target: '/folder',
id: 20,
item_source: '123',
item_type: 'folder',
mail_send: '0',
parent: null,
path: '/folder',
permissions: OC.PERMISSION_READ,
share_type: OC.Share.SHARE_TYPE_LINK,
share_with: null,
stime: 1403884258,
storage: 1,
token: 'tehtoken',
uid_owner: 'root'
}]
});
model.fetch();
var shares = model.get('shares');
expect(shares.length).toEqual(3);
2015-09-15 13:29:30 +00:00
expect(shares[0].id).toEqual(100);
expect(shares[0].permissions).toEqual(31);
expect(shares[0].share_type).toEqual(OC.Share.SHARE_TYPE_USER);
expect(shares[0].share_with).toEqual('user1');
expect(shares[0].share_with_displayname).toEqual('User One');
var linkShare = model.get('linkShare');
expect(linkShare.isLinkShare).toEqual(true);
// TODO: check more attributes
});
it('does not parse link share when for a different file', function() {
2015-09-15 13:29:30 +00:00
loadItemStub.yields({
reshare: [],
/* jshint camelcase: false */
shares: [{
displayname_owner: 'root',
expiration: null,
file_source: 456,
file_target: '/folder',
id: 20,
item_source: '456',
item_type: 'folder',
mail_send: '0',
parent: null,
path: '/folder',
permissions: OC.PERMISSION_READ,
share_type: OC.Share.SHARE_TYPE_LINK,
share_with: null,
stime: 1403884258,
storage: 1,
token: 'tehtoken',
uid_owner: 'root'
}]
});
model.fetch();
var shares = model.get('shares');
2015-09-15 13:29:30 +00:00
// remaining share appears in this list
expect(shares.length).toEqual(1);
var linkShare = model.get('linkShare');
expect(linkShare.isLinkShare).toEqual(false);
});
2015-09-15 13:29:30 +00:00
it('parses correct link share when a nested link share exists along with parent one', function() {
loadItemStub.yields({
reshare: [],
/* jshint camelcase: false */
shares: [{
displayname_owner: 'root',
expiration: 1111,
file_source: 123,
file_target: '/folder',
id: 20,
item_source: '123',
item_type: 'file',
mail_send: '0',
parent: null,
path: '/folder',
permissions: OC.PERMISSION_READ,
share_type: OC.Share.SHARE_TYPE_LINK,
share_with: null,
stime: 1403884258,
storage: 1,
token: 'tehtoken',
uid_owner: 'root'
}, {
displayname_owner: 'root',
expiration: 2222,
file_source: 456,
file_target: '/file_in_folder.txt',
id: 21,
item_source: '456',
item_type: 'file',
mail_send: '0',
parent: null,
path: '/folder/file_in_folder.txt',
permissions: OC.PERMISSION_READ,
share_type: OC.Share.SHARE_TYPE_LINK,
share_with: null,
stime: 1403884509,
storage: 1,
token: 'anothertoken',
uid_owner: 'root'
}]
});
model.fetch();
var shares = model.get('shares');
2015-09-15 13:29:30 +00:00
// the parent share remains in the list
expect(shares.length).toEqual(1);
var linkShare = model.get('linkShare');
2015-09-15 13:29:30 +00:00
expect(linkShare.isLinkShare).toEqual(true);
expect(linkShare.token).toEqual('tehtoken');
// TODO: check child too
});
2015-09-15 13:29:30 +00:00
it('reduces reshare permissions to the ones from the original share', function() {
loadItemStub.yields({
reshare: {
permissions: OC.PERMISSION_READ,
uid_owner: 'user1'
},
shares: []
});
model.fetch();
// no resharing allowed
expect(model.get('permissions')).toEqual(OC.PERMISSION_READ);
});
it('reduces reshare permissions to possible permissions', function() {
loadItemStub.yields({
reshare: {
permissions: OC.PERMISSION_ALL,
uid_owner: 'user1'
},
shares: []
});
model.set('possiblePermissions', OC.PERMISSION_READ);
model.fetch();
// no resharing allowed
expect(model.get('permissions')).toEqual(OC.PERMISSION_READ);
});
it('allows owner to share their own share when they are also the recipient', function() {
OC.currentUser = 'user1';
loadItemStub.yields({
reshare: {
permissions: OC.PERMISSION_READ,
uid_owner: 'user1'
},
shares: []
});
model.fetch();
// sharing still allowed
expect(model.get('permissions') & OC.PERMISSION_SHARE).toEqual(OC.PERMISSION_SHARE);
});
});
describe('Util', function() {
it('parseTime should properly parse strings', function() {
_.each([
[ '123456', 123456],
[ 123456 , 123456],
['0123456', 123456],
['abcdefg', null],
['0x12345', null],
[ '', null],
], function(value) {
expect(OC.Share._parseTime(value[0])).toEqual(value[1]);
});
});
});
});