You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

shareSpec.js 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551
  1. /*
  2. * Copyright (c) 2014 Vincent Petry <pvince81@owncloud.com>
  3. *
  4. * This file is licensed under the Affero General Public License version 3
  5. * or later.
  6. *
  7. * See the COPYING-README file.
  8. *
  9. */
  10. describe('OCA.Sharing.Util tests', function() {
  11. var fileList;
  12. var testFiles;
  13. function getImageUrl($el) {
  14. // might be slightly different cross-browser
  15. var url = $el.css('background-image');
  16. var r = url.match(/url\(['"]?([^'")]*)['"]?\)/);
  17. if (!r) {
  18. return url;
  19. }
  20. return r[1];
  21. }
  22. beforeEach(function() {
  23. var $content = $('<div id="content"></div>');
  24. $('#testArea').append($content);
  25. // dummy file list
  26. var $div = $(
  27. '<div id="listContainer">' +
  28. '<table id="filestable">' +
  29. '<thead></thead>' +
  30. '<tbody id="fileList"></tbody>' +
  31. '</table>' +
  32. '</div>');
  33. $('#content').append($div);
  34. var fileActions = new OCA.Files.FileActions();
  35. fileList = new OCA.Files.FileList(
  36. $div, {
  37. fileActions : fileActions
  38. }
  39. );
  40. OCA.Sharing.Util.attach(fileList);
  41. testFiles = [{
  42. id: 1,
  43. type: 'file',
  44. name: 'One.txt',
  45. path: '/subdir',
  46. mimetype: 'text/plain',
  47. size: 12,
  48. permissions: OC.PERMISSION_ALL,
  49. etag: 'abc',
  50. shareOwner: 'User One',
  51. isShareMountPoint: false,
  52. shareTypes: [OC.Share.SHARE_TYPE_USER]
  53. }];
  54. });
  55. afterEach(function() {
  56. delete OCA.Sharing.sharesLoaded;
  57. delete OC.Share.droppedDown;
  58. fileList.destroy();
  59. fileList = null;
  60. });
  61. describe('Sharing data in table row', function() {
  62. // TODO: test data-permissions, data-share-owner, etc
  63. });
  64. describe('Share action icon', function() {
  65. it('do not shows share text when not shared', function() {
  66. var $action, $tr;
  67. OC.Share.statuses = {};
  68. fileList.setFiles([{
  69. id: 1,
  70. type: 'dir',
  71. name: 'One',
  72. path: '/subdir',
  73. mimetype: 'httpd/unix-directory',
  74. size: 12,
  75. permissions: OC.PERMISSION_ALL,
  76. etag: 'abc',
  77. shareTypes: []
  78. }]);
  79. $tr = fileList.$el.find('tbody tr:first');
  80. $action = $tr.find('.action-share');
  81. expect($action.find('.icon').hasClass('icon-shared')).toEqual(true);
  82. expect($action.find('.icon').hasClass('icon-public')).toEqual(false);
  83. expect(OC.basename(getImageUrl($tr.find('.filename .thumbnail')))).toEqual('folder.svg');
  84. });
  85. it('shows simple share text with share icon', function() {
  86. var $action, $tr;
  87. fileList.setFiles([{
  88. id: 1,
  89. type: 'dir',
  90. name: 'One',
  91. path: '/subdir',
  92. mimetype: 'text/plain',
  93. size: 12,
  94. permissions: OC.PERMISSION_ALL,
  95. etag: 'abc',
  96. shareTypes: [OC.Share.SHARE_TYPE_USER]
  97. }]);
  98. $tr = fileList.$el.find('tbody tr:first');
  99. $action = $tr.find('.action-share');
  100. expect($action.find('>span').text().trim()).toEqual('Shared');
  101. expect($action.find('.icon').hasClass('icon-shared')).toEqual(true);
  102. expect($action.find('.icon').hasClass('icon-public')).toEqual(false);
  103. expect(OC.basename(getImageUrl($tr.find('.filename .thumbnail')))).toEqual('folder-shared.svg');
  104. });
  105. it('shows simple share text with public icon when shared with link', function() {
  106. var $action, $tr;
  107. OC.Share.statuses = {1: {link: true, path: '/subdir'}};
  108. fileList.setFiles([{
  109. id: 1,
  110. type: 'dir',
  111. name: 'One',
  112. path: '/subdir',
  113. mimetype: 'text/plain',
  114. size: 12,
  115. permissions: OC.PERMISSION_ALL,
  116. etag: 'abc',
  117. shareTypes: [OC.Share.SHARE_TYPE_LINK]
  118. }]);
  119. $tr = fileList.$el.find('tbody tr:first');
  120. $action = $tr.find('.action-share');
  121. expect($action.find('>span').text().trim()).toEqual('Shared');
  122. expect($action.find('.icon').hasClass('icon-shared')).toEqual(false);
  123. expect($action.find('.icon').hasClass('icon-public')).toEqual(true);
  124. expect(OC.basename(getImageUrl($tr.find('.filename .thumbnail')))).toEqual('folder-public.svg');
  125. });
  126. it('shows owner name when owner is available', function() {
  127. var $action, $tr;
  128. fileList.setFiles([{
  129. id: 1,
  130. type: 'dir',
  131. name: 'One.txt',
  132. path: '/subdir',
  133. mimetype: 'text/plain',
  134. size: 12,
  135. permissions: OC.PERMISSION_ALL,
  136. shareOwner: 'User One',
  137. etag: 'abc',
  138. shareTypes: []
  139. }]);
  140. $tr = fileList.$el.find('tbody tr:first');
  141. $action = $tr.find('.action-share');
  142. expect($action.find('>span').text().trim()).toEqual('Shared by User One');
  143. expect($action.find('.icon').hasClass('icon-shared')).toEqual(true);
  144. expect($action.find('.icon').hasClass('icon-public')).toEqual(false);
  145. expect(OC.basename(getImageUrl($tr.find('.filename .thumbnail')))).toEqual('folder-shared.svg');
  146. });
  147. it('shows recipients when recipients are available', function() {
  148. var $action, $tr;
  149. fileList.setFiles([{
  150. id: 1,
  151. type: 'dir',
  152. name: 'One.txt',
  153. path: '/subdir',
  154. mimetype: 'text/plain',
  155. size: 12,
  156. permissions: OC.PERMISSION_ALL,
  157. recipientsDisplayName: 'User One, User Two',
  158. etag: 'abc',
  159. shareTypes: [OC.Share.SHARE_TYPE_USER]
  160. }]);
  161. $tr = fileList.$el.find('tbody tr:first');
  162. $action = $tr.find('.action-share');
  163. expect($action.text().trim()).toEqual('Shared with User One Shared with User Two');
  164. expect($action.find('.icon').hasClass('icon-shared')).toEqual(true);
  165. expect($action.find('.icon').hasClass('icon-public')).toEqual(false);
  166. expect(OC.basename(getImageUrl($tr.find('.filename .thumbnail')))).toEqual('folder-shared.svg');
  167. });
  168. it('shows share action when shared with user who has no share permission', function() {
  169. var $action, $tr;
  170. fileList.setFiles([{
  171. id: 1,
  172. type: 'dir',
  173. name: 'One',
  174. path: '/subdir',
  175. mimetype: 'text/plain',
  176. size: 12,
  177. permissions: OC.PERMISSION_CREATE,
  178. etag: 'abc',
  179. shareOwner: 'User One'
  180. }]);
  181. $tr = fileList.$el.find('tbody tr:first');
  182. expect($tr.find('.action-share').length).toEqual(1);
  183. });
  184. it('do not show share action when share exists but neither permission nor owner is available', function() {
  185. var $action, $tr;
  186. fileList.setFiles([{
  187. id: 1,
  188. type: 'dir',
  189. name: 'One',
  190. path: '/subdir',
  191. mimetype: 'text/plain',
  192. size: 12,
  193. permissions: OC.PERMISSION_CREATE,
  194. etag: 'abc'
  195. }]);
  196. $tr = fileList.$el.find('tbody tr:first');
  197. expect($tr.find('.action-share').length).toEqual(0);
  198. });
  199. });
  200. describe('Share action', function() {
  201. var shareTab;
  202. function makeDummyShareItem(displayName) {
  203. return {
  204. share_with_displayname: displayName
  205. };
  206. }
  207. beforeEach(function() {
  208. // make it look like not the "All files" list
  209. fileList.id = 'test';
  210. shareTab = fileList._detailsView._tabViews[0];
  211. });
  212. afterEach(function() {
  213. shareTab = null;
  214. });
  215. it('clicking share action opens sidebar and share tab', function() {
  216. var showDetailsViewStub = sinon.stub(fileList, 'showDetailsView');
  217. fileList.setFiles([{
  218. id: 1,
  219. type: 'file',
  220. name: 'One.txt',
  221. path: '/subdir',
  222. mimetype: 'text/plain',
  223. size: 12,
  224. permissions: OC.PERMISSION_ALL,
  225. etag: 'abc'
  226. }]);
  227. var $tr = fileList.$el.find('tr:first');
  228. $tr.find('.action-share').click();
  229. expect(showDetailsViewStub.calledOnce).toEqual(true);
  230. expect(showDetailsViewStub.getCall(0).args[0]).toEqual('One.txt');
  231. expect(showDetailsViewStub.getCall(0).args[1]).toEqual('shareTabView');
  232. showDetailsViewStub.restore();
  233. });
  234. it('adds share icon after sharing a non-shared file', function() {
  235. var $action, $tr;
  236. OC.Share.statuses = {};
  237. fileList.setFiles([{
  238. id: 1,
  239. type: 'file',
  240. name: 'One.txt',
  241. path: '/subdir',
  242. mimetype: 'text/plain',
  243. size: 12,
  244. permissions: OC.PERMISSION_ALL,
  245. etag: 'abc'
  246. }]);
  247. $action = fileList.$el.find('tbody tr:first .action-share');
  248. $tr = fileList.$el.find('tr:first');
  249. $tr.find('.action-share').click();
  250. // simulate updating shares
  251. shareTab._dialog.model.set({
  252. shares: [
  253. {share_with_displayname: 'User One'},
  254. {share_with_displayname: 'User Two'},
  255. {share_with_displayname: 'Group One'},
  256. {share_with_displayname: 'Group Two'}
  257. ]
  258. });
  259. expect($tr.attr('data-share-recipients')).toEqual('Group One, Group Two, User One, User Two');
  260. expect($action.text().trim()).toEqual('Shared with Group One Shared with Group Two Shared with User One Shared with User Two');
  261. expect($action.find('.icon').hasClass('icon-shared')).toEqual(true);
  262. expect($action.find('.icon').hasClass('icon-public')).toEqual(false);
  263. });
  264. it('updates share icon after updating shares of a file', function() {
  265. var $action, $tr;
  266. OC.Share.statuses = {1: {link: false, path: '/subdir'}};
  267. fileList.setFiles([{
  268. id: 1,
  269. type: 'file',
  270. name: 'One.txt',
  271. path: '/subdir',
  272. mimetype: 'text/plain',
  273. size: 12,
  274. permissions: OC.PERMISSION_ALL,
  275. etag: 'abc'
  276. }]);
  277. $action = fileList.$el.find('tbody tr:first .action-share');
  278. $tr = fileList.$el.find('tr:first');
  279. $tr.find('.action-share').click();
  280. // simulate updating shares
  281. shareTab._dialog.model.set({
  282. shares: [
  283. {share_with_displayname: 'User One'},
  284. {share_with_displayname: 'User Two'},
  285. {share_with_displayname: 'User Three'}
  286. ]
  287. });
  288. expect($tr.attr('data-share-recipients')).toEqual('User One, User Three, User Two');
  289. expect($action.text().trim()).toEqual('Shared with User One Shared with User Three Shared with User Two');
  290. expect($action.find('.icon').hasClass('icon-shared')).toEqual(true);
  291. expect($action.find('.icon').hasClass('icon-public')).toEqual(false);
  292. });
  293. it('removes share icon after removing all shares from a file', function() {
  294. var $action, $tr;
  295. OC.Share.statuses = {1: {link: false, path: '/subdir'}};
  296. fileList.setFiles([{
  297. id: 1,
  298. type: 'file',
  299. name: 'One.txt',
  300. path: '/subdir',
  301. mimetype: 'text/plain',
  302. size: 12,
  303. permissions: OC.PERMISSION_ALL,
  304. etag: 'abc',
  305. recipients: 'User One, User Two'
  306. }]);
  307. $action = fileList.$el.find('tbody tr:first .action-share');
  308. $tr = fileList.$el.find('tr:first');
  309. $tr.find('.action-share').click();
  310. // simulate updating shares
  311. shareTab._dialog.model.set({
  312. shares: []
  313. });
  314. expect($tr.attr('data-share-recipients')).not.toBeDefined();
  315. });
  316. it('keep share text after updating reshare', function() {
  317. var $action, $tr;
  318. OC.Share.statuses = {1: {link: false, path: '/subdir'}};
  319. fileList.setFiles([{
  320. id: 1,
  321. type: 'file',
  322. name: 'One.txt',
  323. path: '/subdir',
  324. mimetype: 'text/plain',
  325. size: 12,
  326. permissions: OC.PERMISSION_ALL,
  327. etag: 'abc',
  328. shareOwner: 'User One'
  329. }]);
  330. $action = fileList.$el.find('tbody tr:first .action-share');
  331. $tr = fileList.$el.find('tr:first');
  332. $tr.find('.action-share').click();
  333. // simulate updating shares
  334. shareTab._dialog.model.set({
  335. shares: [{share_with_displayname: 'User Two'}]
  336. });
  337. expect($tr.attr('data-share-recipients')).toEqual('User Two');
  338. expect($action.find('>span').text().trim()).toEqual('Shared by User One');
  339. expect($action.find('.icon').hasClass('icon-shared')).toEqual(true);
  340. expect($action.find('.icon').hasClass('icon-public')).toEqual(false);
  341. });
  342. it('keep share text after unsharing reshare', function() {
  343. var $action, $tr;
  344. OC.Share.statuses = {1: {link: false, path: '/subdir'}};
  345. fileList.setFiles([{
  346. id: 1,
  347. type: 'file',
  348. name: 'One.txt',
  349. path: '/subdir',
  350. mimetype: 'text/plain',
  351. size: 12,
  352. permissions: OC.PERMISSION_ALL,
  353. etag: 'abc',
  354. shareOwner: 'User One',
  355. recipients: 'User Two'
  356. }]);
  357. $action = fileList.$el.find('tbody tr:first .action-share');
  358. $tr = fileList.$el.find('tr:first');
  359. $tr.find('.action-share').click();
  360. // simulate updating shares
  361. shareTab._dialog.model.set({
  362. shares: []
  363. });
  364. expect($tr.attr('data-share-recipients')).not.toBeDefined();
  365. expect($action.find('>span').text().trim()).toEqual('Shared by User One');
  366. expect($action.find('.icon').hasClass('icon-shared')).toEqual(true);
  367. expect($action.find('.icon').hasClass('icon-public')).toEqual(false);
  368. });
  369. });
  370. describe('formatRecipients', function() {
  371. it('returns a single recipient when one passed', function() {
  372. expect(OCA.Sharing.Util.formatRecipients(['User one']))
  373. .toEqual('User one');
  374. });
  375. it('returns two recipients when two passed', function() {
  376. expect(OCA.Sharing.Util.formatRecipients(['User one', 'User two']))
  377. .toEqual('User one, User two');
  378. });
  379. it('returns four recipients with plus when five passed', function() {
  380. var recipients = [
  381. 'User one',
  382. 'User two',
  383. 'User three',
  384. 'User four',
  385. 'User five'
  386. ];
  387. expect(OCA.Sharing.Util.formatRecipients(recipients))
  388. .toEqual('User four, User one, User three, User two, +1');
  389. });
  390. it('returns four recipients with plus when ten passed', function() {
  391. var recipients = [
  392. 'User one',
  393. 'User two',
  394. 'User three',
  395. 'User four',
  396. 'User five',
  397. 'User six',
  398. 'User seven',
  399. 'User eight',
  400. 'User nine',
  401. 'User ten'
  402. ];
  403. expect(OCA.Sharing.Util.formatRecipients(recipients))
  404. .toEqual('User four, User one, User three, User two, +6');
  405. });
  406. it('returns four recipients with plus when four passed with counter', function() {
  407. var recipients = [
  408. 'User one',
  409. 'User two',
  410. 'User three',
  411. 'User four'
  412. ];
  413. expect(OCA.Sharing.Util.formatRecipients(recipients, 10))
  414. .toEqual('User four, User one, User three, User two, +6');
  415. });
  416. });
  417. describe('Excluded lists', function() {
  418. function createListThenAttach(listId) {
  419. var fileActions = new OCA.Files.FileActions();
  420. fileList.destroy();
  421. fileList = new OCA.Files.FileList(
  422. $('#listContainer'), {
  423. id: listId,
  424. fileActions: fileActions
  425. }
  426. );
  427. OCA.Sharing.Util.attach(fileList);
  428. fileList.setFiles(testFiles);
  429. return fileList;
  430. }
  431. it('does not attach to trashbin or public file lists', function() {
  432. createListThenAttach('trashbin');
  433. expect($('.action-share').length).toEqual(0);
  434. expect($('[data-share-recipient]').length).toEqual(0);
  435. createListThenAttach('files.public');
  436. expect($('.action-share').length).toEqual(0);
  437. expect($('[data-share-recipient]').length).toEqual(0);
  438. });
  439. });
  440. describe('ShareTabView interaction', function() {
  441. var shareTabSpy;
  442. var fileInfoModel;
  443. var configModel;
  444. var shareModel;
  445. beforeEach(function() {
  446. shareTabSpy = sinon.spy(OCA.Sharing, 'ShareTabView');
  447. var attributes = {
  448. itemType: 'file',
  449. itemSource: 123,
  450. possiblePermissions: 31,
  451. permissions: 31
  452. };
  453. fileInfoModel = new OCA.Files.FileInfoModel(testFiles[0]);
  454. configModel = new OC.Share.ShareConfigModel({
  455. enforcePasswordForPublicLink: false,
  456. isResharingAllowed: true,
  457. isDefaultExpireDateEnabled: false,
  458. isDefaultExpireDateEnforced: false,
  459. defaultExpireDate: 7
  460. });
  461. shareModel = new OC.Share.ShareItemModel(attributes, {
  462. configModel: configModel,
  463. fileInfoModel: fileInfoModel
  464. });
  465. /* jshint camelcase: false */
  466. shareModel.set({
  467. reshare: {},
  468. shares: [{
  469. id: 100,
  470. item_source: 1,
  471. permissions: 31,
  472. share_type: OC.Share.SHARE_TYPE_USER,
  473. share_with: 'user1',
  474. share_with_displayname: 'User One'
  475. }, {
  476. id: 102,
  477. item_source: 1,
  478. permissions: 31,
  479. share_type: OC.Share.SHARE_TYPE_REMOTE,
  480. share_with: 'foo@bar.com/baz',
  481. share_with_displayname: 'foo@bar.com/baz'
  482. }]
  483. }, {parse: true});
  484. fileList.destroy();
  485. fileList = new OCA.Files.FileList(
  486. $('#listContainer'), {
  487. id: 'files',
  488. fileActions: new OCA.Files.FileActions()
  489. }
  490. );
  491. OCA.Sharing.Util.attach(fileList);
  492. fileList.setFiles(testFiles);
  493. });
  494. afterEach(function() {
  495. shareTabSpy.restore();
  496. });
  497. it('updates fileInfoModel when shares changed', function() {
  498. var changeHandler = sinon.stub();
  499. fileInfoModel.on('change', changeHandler);
  500. shareTabSpy.getCall(0).thisValue.trigger('sharesChanged', shareModel);
  501. expect(changeHandler.calledOnce).toEqual(true);
  502. expect(changeHandler.getCall(0).args[0].changed).toEqual({
  503. shareTypes: [
  504. OC.Share.SHARE_TYPE_USER,
  505. OC.Share.SHARE_TYPE_REMOTE
  506. ]
  507. });
  508. });
  509. });
  510. });