2015-09-15 11:27:33 +03:00
|
|
|
/**
|
|
|
|
* 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 16:29:30 +03:00
|
|
|
var oldCurrentUser;
|
2015-09-15 11:27:33 +03:00
|
|
|
|
|
|
|
beforeEach(function() {
|
2015-09-15 16:29:30 +03:00
|
|
|
oldCurrentUser = OC.currentUser;
|
|
|
|
|
2015-09-15 11:27:33 +03:00
|
|
|
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 16:29:30 +03:00
|
|
|
OC.currentUser = oldCurrentUser;
|
2015-09-15 11:27:33 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
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 16:29:30 +03:00
|
|
|
loadItemStub.yields({
|
2015-09-15 11:27:33 +03:00
|
|
|
/* 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 16:29:30 +03: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');
|
2015-09-15 11:27:33 +03:00
|
|
|
|
|
|
|
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 16:29:30 +03:00
|
|
|
loadItemStub.yields({
|
2015-09-15 11:27:33 +03:00
|
|
|
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 16:29:30 +03:00
|
|
|
// remaining share appears in this list
|
|
|
|
expect(shares.length).toEqual(1);
|
2015-09-15 11:27:33 +03:00
|
|
|
|
|
|
|
var linkShare = model.get('linkShare');
|
|
|
|
expect(linkShare.isLinkShare).toEqual(false);
|
|
|
|
});
|
2015-09-15 16:29:30 +03:00
|
|
|
it('parses correct link share when a nested link share exists along with parent one', function() {
|
|
|
|
loadItemStub.yields({
|
2015-09-15 11:27:33 +03:00
|
|
|
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 16:29:30 +03:00
|
|
|
// the parent share remains in the list
|
|
|
|
expect(shares.length).toEqual(1);
|
2015-09-15 11:27:33 +03:00
|
|
|
|
|
|
|
var linkShare = model.get('linkShare');
|
2015-09-15 16:29:30 +03:00
|
|
|
expect(linkShare.isLinkShare).toEqual(true);
|
2015-09-15 11:27:33 +03:00
|
|
|
expect(linkShare.token).toEqual('tehtoken');
|
|
|
|
|
|
|
|
// TODO: check child too
|
|
|
|
});
|
2015-09-15 16:29:30 +03: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({
|
2015-09-23 13:14:09 +03:00
|
|
|
reshare: {},
|
2015-09-15 16:29:30 +03:00
|
|
|
shares: []
|
|
|
|
});
|
|
|
|
|
|
|
|
model.fetch();
|
|
|
|
|
|
|
|
// sharing still allowed
|
|
|
|
expect(model.get('permissions') & OC.PERMISSION_SHARE).toEqual(OC.PERMISSION_SHARE);
|
|
|
|
});
|
2015-09-23 13:14:09 +03:00
|
|
|
it('properly parses integer values when the server is in the mood of returning ints as string', function() {
|
|
|
|
loadItemStub.yields({
|
|
|
|
reshare: {},
|
|
|
|
shares: [{
|
|
|
|
displayname_owner: 'root',
|
|
|
|
expiration: '1403900000',
|
|
|
|
file_source: '123',
|
|
|
|
file_target: '/folder',
|
|
|
|
id: '20',
|
|
|
|
item_source: '123',
|
|
|
|
item_type: 'file',
|
|
|
|
mail_send: '0',
|
|
|
|
parent: '999',
|
|
|
|
path: '/folder',
|
|
|
|
permissions: '' + OC.PERMISSION_READ,
|
|
|
|
share_type: '' + OC.Share.SHARE_TYPE_USER,
|
|
|
|
share_with: 'user1',
|
|
|
|
stime: '1403884258',
|
|
|
|
storage: '1',
|
|
|
|
token: 'tehtoken',
|
|
|
|
uid_owner: 'root'
|
|
|
|
}]
|
|
|
|
});
|
|
|
|
|
|
|
|
model.fetch();
|
|
|
|
|
|
|
|
var shares = model.get('shares');
|
|
|
|
expect(shares.length).toEqual(1);
|
|
|
|
|
|
|
|
var share = shares[0];
|
|
|
|
expect(share.id).toEqual(20);
|
|
|
|
expect(share.file_source).toEqual(123);
|
|
|
|
expect(share.file_target).toEqual('/folder');
|
|
|
|
expect(share.item_source).toEqual(123);
|
|
|
|
expect(share.item_type).toEqual('file');
|
|
|
|
expect(share.displayname_owner).toEqual('root');
|
|
|
|
expect(share.mail_send).toEqual(0);
|
|
|
|
expect(share.parent).toEqual(999);
|
|
|
|
expect(share.path).toEqual('/folder');
|
|
|
|
expect(share.permissions).toEqual(OC.PERMISSION_READ);
|
|
|
|
expect(share.share_type).toEqual(OC.Share.SHARE_TYPE_USER);
|
|
|
|
expect(share.share_with).toEqual('user1');
|
|
|
|
expect(share.stime).toEqual(1403884258);
|
|
|
|
expect(share.expiration).toEqual(1403900000);
|
|
|
|
});
|
2015-09-15 11:27:33 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('Util', function() {
|
|
|
|
it('parseTime should properly parse strings', function() {
|
|
|
|
|
|
|
|
_.each([
|
|
|
|
[ '123456', 123456],
|
|
|
|
[ 123456 , 123456],
|
|
|
|
['0123456', 123456],
|
|
|
|
['abcdefg', null],
|
|
|
|
['0x12345', null],
|
|
|
|
[ '', null],
|
|
|
|
], function(value) {
|
2015-09-15 16:47:45 +03:00
|
|
|
expect(OC.Share.ShareItemModel.prototype._parseTime(value[0])).toEqual(value[1]);
|
2015-09-15 11:27:33 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|