shareSpec.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532
  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" class="list-container view-grid">' +
  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 share icon when shared to a room', function() {
  106. var $action, $tr;
  107. fileList.setFiles([{
  108. id: 1,
  109. type: 'dir',
  110. name: 'One',
  111. path: '/subdir',
  112. mimetype: 'text/plain',
  113. size: 12,
  114. permissions: OC.PERMISSION_ALL,
  115. etag: 'abc',
  116. shareTypes: [OC.Share.SHARE_TYPE_ROOM]
  117. }]);
  118. $tr = fileList.$el.find('tbody tr:first');
  119. $action = $tr.find('.action-share');
  120. expect($action.find('>span').text().trim()).toEqual('Shared');
  121. expect($action.find('.icon').hasClass('icon-shared')).toEqual(true);
  122. expect($action.find('.icon').hasClass('icon-public')).toEqual(false);
  123. expect(OC.basename(getImageUrl($tr.find('.filename .thumbnail')))).toEqual('folder-shared.svg');
  124. });
  125. it('shows simple share text with public icon when shared with link', function() {
  126. var $action, $tr;
  127. OC.Share.statuses = {1: {link: true, path: '/subdir'}};
  128. fileList.setFiles([{
  129. id: 1,
  130. type: 'dir',
  131. name: 'One',
  132. path: '/subdir',
  133. mimetype: 'text/plain',
  134. size: 12,
  135. permissions: OC.PERMISSION_ALL,
  136. etag: 'abc',
  137. shareTypes: [OC.Share.SHARE_TYPE_LINK]
  138. }]);
  139. $tr = fileList.$el.find('tbody tr:first');
  140. $action = $tr.find('.action-share');
  141. expect($action.find('>span').text().trim()).toEqual('Shared');
  142. expect($action.find('.icon').hasClass('icon-shared')).toEqual(false);
  143. expect($action.find('.icon').hasClass('icon-public')).toEqual(true);
  144. expect(OC.basename(getImageUrl($tr.find('.filename .thumbnail')))).toEqual('folder-public.svg');
  145. });
  146. it('shows owner name when owner is available but no icons', function() {
  147. var $action, $tr;
  148. fileList.setFiles([{
  149. id: 1,
  150. type: 'dir',
  151. name: 'One.txt',
  152. path: '/subdir',
  153. mimetype: 'text/plain',
  154. size: 12,
  155. permissions: OC.PERMISSION_ALL,
  156. shareOwner: 'User One',
  157. shareOwnerId: 'User One',
  158. etag: 'abc',
  159. shareTypes: []
  160. }]);
  161. $tr = fileList.$el.find('tbody tr:first');
  162. $action = $tr.find('.action-share');
  163. expect($action.find('>span').text().trim()).toEqual('Shared by User One');
  164. expect($action.find('.icon').hasClass('icon-shared')).toEqual(false);
  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 recipients when recipients are available', function() {
  169. var $action, $tr;
  170. fileList.setFiles([{
  171. id: 1,
  172. type: 'dir',
  173. name: 'One.txt',
  174. path: '/subdir',
  175. mimetype: 'text/plain',
  176. size: 12,
  177. permissions: OC.PERMISSION_ALL,
  178. recipientsDisplayName: 'User One, User Two',
  179. recipientData: {
  180. 0: {
  181. shareWith: 'User One',
  182. shareWithDisplayName: 'User One'
  183. },
  184. 1: {
  185. shareWith: 'User Two',
  186. shareWithDisplayName: 'User Two'
  187. }
  188. },
  189. etag: 'abc',
  190. shareTypes: [OC.Share.SHARE_TYPE_USER]
  191. }]);
  192. $tr = fileList.$el.find('tbody tr:first');
  193. $action = $tr.find('.action-share');
  194. expect($action.text().trim()).toEqual('Shared with User One Shared with User Two');
  195. expect($action.find('.icon').hasClass('icon-shared')).toEqual(true);
  196. expect($action.find('.icon').hasClass('icon-public')).toEqual(false);
  197. expect(OC.basename(getImageUrl($tr.find('.filename .thumbnail')))).toEqual('folder-shared.svg');
  198. });
  199. it('shows share action when shared with user who has no share permission', function() {
  200. var $action, $tr;
  201. fileList.setFiles([{
  202. id: 1,
  203. type: 'dir',
  204. name: 'One',
  205. path: '/subdir',
  206. mimetype: 'text/plain',
  207. size: 12,
  208. permissions: OC.PERMISSION_CREATE,
  209. etag: 'abc',
  210. shareOwner: 'User One'
  211. }]);
  212. $tr = fileList.$el.find('tbody tr:first');
  213. expect($tr.find('.action-share').length).toEqual(1);
  214. });
  215. it('do not show share action when share exists but neither permission nor owner is available', function() {
  216. var $action, $tr;
  217. fileList.setFiles([{
  218. id: 1,
  219. type: 'dir',
  220. name: 'One',
  221. path: '/subdir',
  222. mimetype: 'text/plain',
  223. size: 12,
  224. permissions: OC.PERMISSION_CREATE,
  225. etag: 'abc'
  226. }]);
  227. $tr = fileList.$el.find('tbody tr:first');
  228. expect($tr.find('.action-share').length).toEqual(0);
  229. });
  230. });
  231. describe('Share action', function() {
  232. var shareTab;
  233. function makeDummyShareItem(displayName) {
  234. return {
  235. share_with_displayname: displayName
  236. };
  237. }
  238. beforeEach(function() {
  239. // make it look like not the "All files" list
  240. fileList.id = 'test';
  241. shareTab = fileList._detailsView._tabViews[0];
  242. });
  243. afterEach(function() {
  244. shareTab = null;
  245. });
  246. it('clicking share action opens sidebar and share tab', function() {
  247. var showDetailsViewStub = sinon.stub(fileList, 'showDetailsView');
  248. fileList.setFiles([{
  249. id: 1,
  250. type: 'file',
  251. name: 'One.txt',
  252. path: '/subdir',
  253. mimetype: 'text/plain',
  254. size: 12,
  255. permissions: OC.PERMISSION_ALL,
  256. etag: 'abc'
  257. }]);
  258. var $tr = fileList.$el.find('tr:first');
  259. $tr.find('.action-share').click();
  260. expect(showDetailsViewStub.calledOnce).toEqual(true);
  261. expect(showDetailsViewStub.getCall(0).args[0]).toEqual('One.txt');
  262. expect(showDetailsViewStub.getCall(0).args[1]).toEqual('shareTabView');
  263. showDetailsViewStub.restore();
  264. });
  265. it('adds share icon after sharing a non-shared file', function() {
  266. var $action, $tr;
  267. OC.Share.statuses = {};
  268. fileList.setFiles([{
  269. id: 1,
  270. type: 'file',
  271. name: 'One.txt',
  272. path: '/subdir',
  273. mimetype: 'text/plain',
  274. size: 12,
  275. permissions: OC.PERMISSION_ALL,
  276. etag: 'abc'
  277. }]);
  278. $action = fileList.$el.find('tbody tr:first .action-share');
  279. $tr = fileList.$el.find('tr:first');
  280. $tr.find('.action-share').click();
  281. // simulate updating shares
  282. shareTab._dialog.model.set({
  283. shares: [
  284. {share_with_displayname: 'User One', share_with: 'User One'},
  285. {share_with_displayname: 'User Two', share_with: 'User Two'},
  286. {share_with_displayname: 'Group One', share_with: 'Group One'},
  287. {share_with_displayname: 'Group Two', share_with: 'Group Two'}
  288. ]
  289. });
  290. expect($action.text().trim()).toEqual('Shared with Group One Shared with Group Two Shared with User One Shared with User Two');
  291. expect($action.find('.icon').hasClass('icon-shared')).toEqual(true);
  292. expect($action.find('.icon').hasClass('icon-public')).toEqual(false);
  293. });
  294. it('updates share icon after updating shares of a file', function() {
  295. var $action, $tr;
  296. OC.Share.statuses = {1: {link: false, path: '/subdir'}};
  297. fileList.setFiles([{
  298. id: 1,
  299. type: 'file',
  300. name: 'One.txt',
  301. path: '/subdir',
  302. mimetype: 'text/plain',
  303. size: 12,
  304. permissions: OC.PERMISSION_ALL,
  305. etag: 'abc'
  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. {share_with_displayname: 'User One', share_with: 'User One'},
  314. {share_with_displayname: 'User Two', share_with: 'User Two'},
  315. {share_with_displayname: 'User Three', share_with: 'User Three'}
  316. ]
  317. });
  318. expect($action.text().trim()).toEqual('Shared with User One Shared with User Three Shared with User Two');
  319. expect($action.find('.icon').hasClass('icon-shared')).toEqual(true);
  320. expect($action.find('.icon').hasClass('icon-public')).toEqual(false);
  321. });
  322. it('removes share icon after removing all shares from a file', function() {
  323. var $action, $tr;
  324. OC.Share.statuses = {1: {link: false, path: '/subdir'}};
  325. fileList.setFiles([{
  326. id: 1,
  327. type: 'file',
  328. name: 'One.txt',
  329. path: '/subdir',
  330. mimetype: 'text/plain',
  331. size: 12,
  332. permissions: OC.PERMISSION_ALL,
  333. etag: 'abc',
  334. recipients: 'User One, User Two'
  335. }]);
  336. $action = fileList.$el.find('tbody tr:first .action-share');
  337. $tr = fileList.$el.find('tr:first');
  338. $tr.find('.action-share').click();
  339. // simulate updating shares
  340. shareTab._dialog.model.set({
  341. shares: []
  342. });
  343. expect($tr.attr('data-share-recipient-data')).not.toBeDefined();
  344. });
  345. it('keep share text after updating reshare', function() {
  346. var $action, $tr;
  347. OC.Share.statuses = {1: {link: false, path: '/subdir'}};
  348. fileList.setFiles([{
  349. id: 1,
  350. type: 'file',
  351. name: 'One.txt',
  352. path: '/subdir',
  353. mimetype: 'text/plain',
  354. size: 12,
  355. permissions: OC.PERMISSION_ALL,
  356. etag: 'abc',
  357. shareOwner: 'User One',
  358. shareOwnerId: 'User One'
  359. }]);
  360. $action = fileList.$el.find('tbody tr:first .action-share');
  361. $tr = fileList.$el.find('tr:first');
  362. $tr.find('.action-share').click();
  363. // simulate updating shares
  364. shareTab._dialog.model.set({
  365. shares: [{share_with_displayname: 'User Two'}]
  366. });
  367. expect($action.find('>span').text().trim()).toEqual('Shared by User One');
  368. expect($action.find('.icon').hasClass('icon-shared')).toEqual(false);
  369. expect($action.find('.icon').hasClass('icon-public')).toEqual(false);
  370. });
  371. it('keep share text after unsharing reshare', function() {
  372. var $action, $tr;
  373. OC.Share.statuses = {1: {link: false, path: '/subdir'}};
  374. fileList.setFiles([{
  375. id: 1,
  376. type: 'file',
  377. name: 'One.txt',
  378. path: '/subdir',
  379. mimetype: 'text/plain',
  380. size: 12,
  381. permissions: OC.PERMISSION_ALL,
  382. etag: 'abc',
  383. shareOwner: 'User One',
  384. shareOwnerId: 'User One',
  385. recipients: 'User Two',
  386. recipientData: {'User Two': 'User Two'}
  387. }]);
  388. $action = fileList.$el.find('tbody tr:first .action-share');
  389. $tr = fileList.$el.find('tr:first');
  390. $tr.find('.action-share').click();
  391. // simulate updating shares
  392. shareTab._dialog.model.set({
  393. shares: []
  394. });
  395. expect($tr.attr('data-share-recipient-data')).not.toBeDefined();
  396. expect($action.find('>span').text().trim()).toEqual('Shared by User One');
  397. expect($action.find('.icon').hasClass('icon-shared')).toEqual(false);
  398. expect($action.find('.icon').hasClass('icon-public')).toEqual(false);
  399. });
  400. });
  401. describe('Excluded lists', function() {
  402. function createListThenAttach(listId) {
  403. var fileActions = new OCA.Files.FileActions();
  404. fileList.destroy();
  405. fileList = new OCA.Files.FileList(
  406. $('#listContainer'), {
  407. id: listId,
  408. fileActions: fileActions
  409. }
  410. );
  411. OCA.Sharing.Util.attach(fileList);
  412. fileList.setFiles(testFiles);
  413. return fileList;
  414. }
  415. it('does not attach to trashbin or public file lists', function() {
  416. createListThenAttach('trashbin');
  417. expect($('.action-share').length).toEqual(0);
  418. expect($('[data-share-recipient]').length).toEqual(0);
  419. createListThenAttach('files.public');
  420. expect($('.action-share').length).toEqual(0);
  421. expect($('[data-share-recipient]').length).toEqual(0);
  422. });
  423. });
  424. describe('ShareTabView interaction', function() {
  425. var shareTabSpy;
  426. var fileInfoModel;
  427. var configModel;
  428. var shareModel;
  429. beforeEach(function() {
  430. shareTabSpy = sinon.spy(OCA.Sharing, 'ShareTabView');
  431. var attributes = {
  432. itemType: 'file',
  433. itemSource: 123,
  434. possiblePermissions: 31,
  435. permissions: 31
  436. };
  437. fileInfoModel = new OCA.Files.FileInfoModel(testFiles[0]);
  438. configModel = new OC.Share.ShareConfigModel({
  439. enforcePasswordForPublicLink: false,
  440. isResharingAllowed: true,
  441. isDefaultExpireDateEnabled: false,
  442. isDefaultExpireDateEnforced: false,
  443. defaultExpireDate: 7
  444. });
  445. shareModel = new OC.Share.ShareItemModel(attributes, {
  446. configModel: configModel,
  447. fileInfoModel: fileInfoModel
  448. });
  449. /* jshint camelcase: false */
  450. shareModel.set({
  451. reshare: {},
  452. shares: [{
  453. id: 100,
  454. item_source: 1,
  455. permissions: 31,
  456. share_type: OC.Share.SHARE_TYPE_USER,
  457. share_with: 'user1',
  458. share_with_displayname: 'User One'
  459. }, {
  460. id: 102,
  461. item_source: 1,
  462. permissions: 31,
  463. share_type: OC.Share.SHARE_TYPE_REMOTE,
  464. share_with: 'foo@bar.com/baz',
  465. share_with_displayname: 'foo@bar.com/baz'
  466. }]
  467. }, {parse: true});
  468. fileList.destroy();
  469. fileList = new OCA.Files.FileList(
  470. $('#listContainer'), {
  471. id: 'files',
  472. fileActions: new OCA.Files.FileActions()
  473. }
  474. );
  475. OCA.Sharing.Util.attach(fileList);
  476. fileList.setFiles(testFiles);
  477. });
  478. afterEach(function() {
  479. shareTabSpy.restore();
  480. });
  481. it('updates fileInfoModel when shares changed', function() {
  482. var changeHandler = sinon.stub();
  483. fileInfoModel.on('change', changeHandler);
  484. shareTabSpy.getCall(0).returnValue.trigger('sharesChanged', shareModel);
  485. expect(changeHandler.calledOnce).toEqual(true);
  486. expect(changeHandler.getCall(0).args[0].changed).toEqual({
  487. shareTypes: [
  488. OC.Share.SHARE_TYPE_USER,
  489. OC.Share.SHARE_TYPE_REMOTE
  490. ]
  491. });
  492. });
  493. });
  494. });