1
0

shareSpec.js 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. /**
  2. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  3. * SPDX-FileCopyrightText: 2014-2016 ownCloud, Inc.
  4. * SPDX-License-Identifier: AGPL-3.0-or-later
  5. */
  6. describe('OCA.Sharing.Util tests', function() {
  7. var fileList;
  8. var testFiles;
  9. function getImageUrl($el) {
  10. // might be slightly different cross-browser
  11. var url = $el.css('background-image');
  12. var r = url.match(/url\(['"]?([^'")]*)['"]?\)/);
  13. if (!r) {
  14. return url;
  15. }
  16. return r[1];
  17. }
  18. beforeEach(function() {
  19. var $content = $('<div id="app-content"></div>');
  20. $('#testArea').append($content);
  21. // dummy file list
  22. var $div = $(
  23. '<div id="listContainer">' +
  24. '<table class="files-filestable list-container view-grid">' +
  25. '<thead></thead>' +
  26. '<tbody class="files-fileList"></tbody>' +
  27. '</table>' +
  28. '</div>');
  29. $('#app-content').append($div);
  30. var fileActions = new OCA.Files.FileActions();
  31. fileList = new OCA.Files.FileList(
  32. $div, {
  33. fileActions : fileActions
  34. }
  35. );
  36. OCA.Sharing.Util.attach(fileList);
  37. testFiles = [{
  38. id: 1,
  39. type: 'file',
  40. name: 'One.txt',
  41. path: '/subdir',
  42. mimetype: 'text/plain',
  43. size: 12,
  44. permissions: OC.PERMISSION_ALL,
  45. etag: 'abc',
  46. shareOwner: 'User One',
  47. isShareMountPoint: false,
  48. shareTypes: [OC.Share.SHARE_TYPE_USER]
  49. }];
  50. });
  51. afterEach(function() {
  52. delete OCA.Sharing.sharesLoaded;
  53. delete OC.Share.droppedDown;
  54. fileList.destroy();
  55. fileList = null;
  56. });
  57. describe('Sharing data in table row', function() {
  58. // TODO: test data-permissions, data-share-owner, etc
  59. });
  60. describe('Share action icon', function() {
  61. it('do not shows share text when not shared', function() {
  62. var $action, $tr;
  63. OC.Share.statuses = {};
  64. fileList.setFiles([{
  65. id: 1,
  66. type: 'dir',
  67. name: 'One',
  68. path: '/subdir',
  69. mimetype: 'httpd/unix-directory',
  70. size: 12,
  71. permissions: OC.PERMISSION_ALL,
  72. etag: 'abc',
  73. shareTypes: []
  74. }]);
  75. $tr = fileList.$el.find('tbody tr:first');
  76. $action = $tr.find('.action-share');
  77. expect($action.find('.icon').hasClass('icon-shared')).toEqual(true);
  78. expect($action.find('.icon').hasClass('icon-public')).toEqual(false);
  79. expect(OC.basename(getImageUrl($tr.find('.filename .thumbnail')))).toEqual('folder.svg');
  80. });
  81. it('shows simple share text with share icon', function() {
  82. var $action, $tr;
  83. fileList.setFiles([{
  84. id: 1,
  85. type: 'dir',
  86. name: 'One',
  87. path: '/subdir',
  88. mimetype: 'text/plain',
  89. size: 12,
  90. permissions: OC.PERMISSION_ALL,
  91. etag: 'abc',
  92. shareTypes: [OC.Share.SHARE_TYPE_USER]
  93. }]);
  94. $tr = fileList.$el.find('tbody tr:first');
  95. $action = $tr.find('.action-share');
  96. expect($action.find('>span').text().trim()).toEqual('Shared');
  97. expect($action.find('.icon').hasClass('icon-shared')).toEqual(true);
  98. expect($action.find('.icon').hasClass('icon-public')).toEqual(false);
  99. expect(OC.basename(getImageUrl($tr.find('.filename .thumbnail')))).toEqual('folder-shared.svg');
  100. });
  101. it('shows simple share text with share icon when shared to a room', function() {
  102. var $action, $tr;
  103. fileList.setFiles([{
  104. id: 1,
  105. type: 'dir',
  106. name: 'One',
  107. path: '/subdir',
  108. mimetype: 'text/plain',
  109. size: 12,
  110. permissions: OC.PERMISSION_ALL,
  111. etag: 'abc',
  112. shareTypes: [OC.Share.SHARE_TYPE_ROOM]
  113. }]);
  114. $tr = fileList.$el.find('tbody tr:first');
  115. $action = $tr.find('.action-share');
  116. expect($action.find('>span').text().trim()).toEqual('Shared');
  117. expect($action.find('.icon').hasClass('icon-shared')).toEqual(true);
  118. expect($action.find('.icon').hasClass('icon-public')).toEqual(false);
  119. expect(OC.basename(getImageUrl($tr.find('.filename .thumbnail')))).toEqual('folder-shared.svg');
  120. });
  121. it('shows simple share text with public icon when shared with link', function() {
  122. var $action, $tr;
  123. OC.Share.statuses = {1: {link: true, path: '/subdir'}};
  124. fileList.setFiles([{
  125. id: 1,
  126. type: 'dir',
  127. name: 'One',
  128. path: '/subdir',
  129. mimetype: 'text/plain',
  130. size: 12,
  131. permissions: OC.PERMISSION_ALL,
  132. etag: 'abc',
  133. shareTypes: [OC.Share.SHARE_TYPE_LINK]
  134. }]);
  135. $tr = fileList.$el.find('tbody tr:first');
  136. $action = $tr.find('.action-share');
  137. expect($action.find('>span').text().trim()).toEqual('Shared');
  138. expect($action.find('.icon').hasClass('icon-shared')).toEqual(false);
  139. expect($action.find('.icon').hasClass('icon-public')).toEqual(true);
  140. expect(OC.basename(getImageUrl($tr.find('.filename .thumbnail')))).toEqual('folder-public.svg');
  141. });
  142. it('shows owner name when owner is available but no icons', function() {
  143. var $action, $tr;
  144. fileList.setFiles([{
  145. id: 1,
  146. type: 'dir',
  147. name: 'One.txt',
  148. path: '/subdir',
  149. mimetype: 'text/plain',
  150. size: 12,
  151. permissions: OC.PERMISSION_ALL,
  152. shareOwner: 'User One',
  153. shareOwnerId: 'User One',
  154. etag: 'abc',
  155. shareTypes: []
  156. }]);
  157. $tr = fileList.$el.find('tbody tr:first');
  158. $action = $tr.find('.action-share');
  159. expect($action.find('>span').text().trim()).toEqual('Shared by User One');
  160. expect($action.find('.icon').hasClass('icon-shared')).toEqual(false);
  161. expect($action.find('.icon').hasClass('icon-public')).toEqual(false);
  162. expect(OC.basename(getImageUrl($tr.find('.filename .thumbnail')))).toEqual('folder-shared.svg');
  163. });
  164. it('shows recipients when recipients are available', function() {
  165. var $action, $tr;
  166. fileList.setFiles([{
  167. id: 1,
  168. type: 'dir',
  169. name: 'One.txt',
  170. path: '/subdir',
  171. mimetype: 'text/plain',
  172. size: 12,
  173. permissions: OC.PERMISSION_ALL,
  174. recipientsDisplayName: 'User One, User Two',
  175. recipientData: {
  176. 0: {
  177. shareWith: 'User One',
  178. shareWithDisplayName: 'User One'
  179. },
  180. 1: {
  181. shareWith: 'User Two',
  182. shareWithDisplayName: 'User Two'
  183. }
  184. },
  185. etag: 'abc',
  186. shareTypes: [OC.Share.SHARE_TYPE_USER]
  187. }]);
  188. $tr = fileList.$el.find('tbody tr:first');
  189. $action = $tr.find('.action-share');
  190. expect($action.text().trim()).toEqual('Shared with User One Shared with User Two');
  191. expect($action.find('.icon').hasClass('icon-shared')).toEqual(true);
  192. expect($action.find('.icon').hasClass('icon-public')).toEqual(false);
  193. expect(OC.basename(getImageUrl($tr.find('.filename .thumbnail')))).toEqual('folder-shared.svg');
  194. });
  195. it('shows share action when shared with user who has no share permission', function() {
  196. var $action, $tr;
  197. fileList.setFiles([{
  198. id: 1,
  199. type: 'dir',
  200. name: 'One',
  201. path: '/subdir',
  202. mimetype: 'text/plain',
  203. size: 12,
  204. permissions: OC.PERMISSION_CREATE,
  205. etag: 'abc',
  206. shareOwner: 'User One'
  207. }]);
  208. $tr = fileList.$el.find('tbody tr:first');
  209. expect($tr.find('.action-share').length).toEqual(1);
  210. });
  211. it('do not show share action when share exists but neither permission nor owner is available', function() {
  212. var $action, $tr;
  213. fileList.setFiles([{
  214. id: 1,
  215. type: 'dir',
  216. name: 'One',
  217. path: '/subdir',
  218. mimetype: 'text/plain',
  219. size: 12,
  220. permissions: OC.PERMISSION_CREATE,
  221. etag: 'abc'
  222. }]);
  223. $tr = fileList.$el.find('tbody tr:first');
  224. expect($tr.find('.action-share').length).toEqual(0);
  225. });
  226. });
  227. describe('Excluded lists', function() {
  228. function createListThenAttach(listId) {
  229. var fileActions = new OCA.Files.FileActions();
  230. fileList.destroy();
  231. fileList = new OCA.Files.FileList(
  232. $('#listContainer'), {
  233. id: listId,
  234. fileActions: fileActions
  235. }
  236. );
  237. OCA.Sharing.Util.attach(fileList);
  238. fileList.setFiles(testFiles);
  239. return fileList;
  240. }
  241. it('does not attach to trashbin or public file lists', function() {
  242. createListThenAttach('trashbin');
  243. expect($('.action-share').length).toEqual(0);
  244. expect($('[data-share-recipient]').length).toEqual(0);
  245. createListThenAttach('files.public');
  246. expect($('.action-share').length).toEqual(0);
  247. expect($('[data-share-recipient]').length).toEqual(0);
  248. });
  249. });
  250. describe('ShareTabView interaction', function() {
  251. var shareTabSpy;
  252. var fileInfoModel;
  253. var configModel;
  254. var shareModel;
  255. beforeEach(function() {
  256. shareTabSpy = sinon.spy(OCA.Sharing, 'ShareTabView');
  257. var attributes = {
  258. itemType: 'file',
  259. itemSource: 123,
  260. possiblePermissions: 31,
  261. permissions: 31
  262. };
  263. fileInfoModel = new OCA.Files.FileInfoModel(testFiles[0]);
  264. configModel = new OC.Share.ShareConfigModel({
  265. enforcePasswordForPublicLink: false,
  266. isResharingAllowed: true,
  267. isDefaultExpireDateEnabled: false,
  268. isDefaultExpireDateEnforced: false,
  269. defaultExpireDate: 7
  270. });
  271. shareModel = new OC.Share.ShareItemModel(attributes, {
  272. configModel: configModel,
  273. fileInfoModel: fileInfoModel
  274. });
  275. /* jshint camelcase: false */
  276. shareModel.set({
  277. reshare: {},
  278. shares: [{
  279. id: 100,
  280. item_source: 1,
  281. permissions: 31,
  282. share_type: OC.Share.SHARE_TYPE_USER,
  283. share_with: 'user1',
  284. share_with_displayname: 'User One'
  285. }, {
  286. id: 102,
  287. item_source: 1,
  288. permissions: 31,
  289. share_type: OC.Share.SHARE_TYPE_REMOTE,
  290. share_with: 'foo@bar.com/baz',
  291. share_with_displayname: 'foo@bar.com/baz'
  292. }]
  293. }, {parse: true});
  294. fileList.destroy();
  295. fileList = new OCA.Files.FileList(
  296. $('#listContainer'), {
  297. id: 'files',
  298. fileActions: new OCA.Files.FileActions()
  299. }
  300. );
  301. OCA.Sharing.Util.attach(fileList);
  302. fileList.setFiles(testFiles);
  303. });
  304. afterEach(function() {
  305. shareTabSpy.restore();
  306. });
  307. });
  308. });