settingsSpec.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  1. /**
  2. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  3. * SPDX-FileCopyrightText: 2015-2016 ownCloud, Inc.
  4. * SPDX-License-Identifier: AGPL-3.0-or-later
  5. */
  6. describe('OCA.Files_External.Settings tests', function() {
  7. var clock;
  8. var select2Stub;
  9. var select2ApplicableUsers;
  10. beforeEach(function() {
  11. clock = sinon.useFakeTimers();
  12. select2ApplicableUsers = [];
  13. select2Stub = sinon.stub($.fn, 'select2').callsFake(function(args) {
  14. if (args === 'val') {
  15. return select2ApplicableUsers;
  16. }
  17. return {
  18. on: function() { return this; }
  19. };
  20. });
  21. // view still requires an existing DOM table
  22. $('#testArea').append(
  23. '<table id="externalStorage" data-admin="true">' +
  24. '<thead></thead>' +
  25. '<tbody>' +
  26. '<tr id="addMountPoint" data-id="">' +
  27. '<td class="status"></td>' +
  28. '<td class="mountPoint"><input type="text" name="mountPoint"/></td>' +
  29. '<td class="backend">' +
  30. '<select class="selectBackend">' +
  31. '<option disable selected>Add storage</option>' +
  32. '<option value="\\OC\\TestBackend">Test Backend</option>' +
  33. '<option value="\\OC\\AnotherTestBackend">Another Test Backend</option>' +
  34. '<option value="\\OC\\InputsTestBackend">Inputs test backend</option>' +
  35. '</select>' +
  36. '</td>' +
  37. '<td class="authentication"></td>' +
  38. '<td class="configuration"></td>' +
  39. '<td class="applicable">' +
  40. '<input type="checkbox" class="applicableToAllUsers">' +
  41. '<input type="hidden" class="applicableUsers">' +
  42. '</td>' +
  43. '<td class="mountOptionsToggle">'+
  44. '<div class="icon-more" title="Advanced settings" deluminate_imagetype="unknown"></div>'+
  45. '<input type="hidden" class="mountOptions"/>'+
  46. '</td>'+
  47. '<td class="save">'+
  48. '<div class="icon-checkmark" title="Save" deluminate_imagetype="unknown"></div>'+
  49. '</td>'+
  50. '</tr>' +
  51. '</tbody>' +
  52. '</table>'
  53. );
  54. // these are usually appended into the data attribute
  55. // within the DOM by the server template
  56. $('#externalStorage .selectBackend:first').data('configurations', {
  57. '\\OC\\TestBackend': {
  58. 'identifier': '\\OC\\TestBackend',
  59. 'name': 'Test Backend',
  60. 'configuration': {
  61. 'field1': {
  62. 'value': 'Display Name 1'
  63. },
  64. 'field2': {
  65. 'value': 'Display Name 2',
  66. 'flags': 1
  67. }
  68. },
  69. 'authSchemes': {
  70. 'builtin': true,
  71. },
  72. 'priority': 11
  73. },
  74. '\\OC\\AnotherTestBackend': {
  75. 'identifier': '\\OC\\AnotherTestBackend',
  76. 'name': 'Another Test Backend',
  77. 'configuration': {
  78. 'field1': {
  79. 'value': 'Display Name 1'
  80. },
  81. 'field2': {
  82. 'value': 'Display Name 2',
  83. 'flags': 1
  84. }
  85. },
  86. 'authSchemes': {
  87. 'builtin': true,
  88. },
  89. 'priority': 12
  90. },
  91. '\\OC\\InputsTestBackend': {
  92. 'identifier': '\\OC\\InputsTestBackend',
  93. 'name': 'Inputs test backend',
  94. 'configuration': {
  95. 'field_text': {
  96. 'value': 'Text field'
  97. },
  98. 'field_password': {
  99. 'value': ',Password field',
  100. 'type': 2
  101. },
  102. 'field_bool': {
  103. 'value': 'Boolean field',
  104. 'type': 1
  105. },
  106. 'field_hidden': {
  107. 'value': 'Hidden field',
  108. 'type': 3
  109. },
  110. 'field_text_optional': {
  111. 'value': 'Text field optional',
  112. 'flags': 1
  113. },
  114. 'field_password_optional': {
  115. 'value': 'Password field optional',
  116. 'flags': 1,
  117. 'type': 2
  118. }
  119. },
  120. 'authSchemes': {
  121. 'builtin': true,
  122. },
  123. 'priority': 13
  124. }
  125. }
  126. );
  127. $('#externalStorage #addMountPoint .authentication:first').data('mechanisms', {
  128. 'mechanism1': {
  129. 'identifier': 'mechanism1',
  130. 'name': 'Mechanism 1',
  131. 'configuration': {
  132. },
  133. 'scheme': 'builtin',
  134. 'visibility': 3
  135. },
  136. });
  137. });
  138. afterEach(function() {
  139. select2Stub.restore();
  140. clock.restore();
  141. });
  142. describe('storage configuration', function() {
  143. var view;
  144. function selectBackend(backendName) {
  145. view.$el.find('.selectBackend:first').val(backendName).trigger('change');
  146. view.$el.find('.applicableToAllUsers').prop('checked', true).trigger('change');
  147. }
  148. beforeEach(function() {
  149. var $el = $('#externalStorage');
  150. view = new OCA.Files_External.Settings.MountConfigListView($el, {encryptionEnabled: false});
  151. });
  152. afterEach(function() {
  153. view = null;
  154. });
  155. describe('selecting backend', function() {
  156. it('populates the row and creates a new empty one', function() {
  157. selectBackend('\\OC\\TestBackend');
  158. var $firstRow = view.$el.find('tr:first');
  159. expect($firstRow.find('.backend').text()).toEqual('Test Backend');
  160. expect($firstRow.find('.selectBackend').length).toEqual(0);
  161. // TODO: check "remove" button visibility
  162. // the suggested mount point name
  163. expect($firstRow.find('[name=mountPoint]').val()).toEqual('TestBackend');
  164. // TODO: check that the options have been created
  165. // TODO: check select2 call on the ".applicableUsers" element
  166. var $emptyRow = $firstRow.next('tr');
  167. expect($emptyRow.length).toEqual(1);
  168. expect($emptyRow.find('.selectBackend').length).toEqual(1);
  169. expect($emptyRow.find('.applicable select').length).toEqual(0);
  170. // TODO: check "remove" button visibility
  171. });
  172. it('shows row even if selection row is hidden', function() {
  173. selectBackend('\\OC\\TestBackend');
  174. view.$el.find('tr#addMountPoint').hide();
  175. expect(view.$el.find('tr:first').is(':visible')).toBe(true);
  176. expect(view.$el.find('tr#addMountPoint').is(':visible')).toBe(false);
  177. });
  178. // TODO: test with personal mounts (no applicable fields)
  179. // TODO: test suggested mount point logic
  180. });
  181. describe('saving storages', function() {
  182. var $tr;
  183. beforeEach(function() {
  184. selectBackend('\\OC\\TestBackend');
  185. $tr = view.$el.find('tr:first');
  186. });
  187. it('saves storage after clicking the save button', function() {
  188. var $field1 = $tr.find('input[data-parameter=field1]');
  189. expect($field1.length).toEqual(1);
  190. $field1.val('test');
  191. $field1.trigger(new $.Event('keyup', {keyCode: 97}));
  192. var $mountOptionsField = $tr.find('input.mountOptions');
  193. expect($mountOptionsField.length).toEqual(1);
  194. $mountOptionsField.val(JSON.stringify({previews:true}));
  195. var $saveButton = $tr.find('td.save .icon-checkmark');
  196. $saveButton.click();
  197. expect(fakeServer.requests.length).toEqual(1);
  198. var request = fakeServer.requests[0];
  199. expect(request.url).toEqual(OC.getRootPath() + '/index.php/apps/files_external/globalstorages');
  200. expect(JSON.parse(request.requestBody)).toEqual({
  201. backend: '\\OC\\TestBackend',
  202. authMechanism: 'mechanism1',
  203. backendOptions: {
  204. 'field1': 'test',
  205. 'field2': ''
  206. },
  207. mountPoint: 'TestBackend',
  208. priority: 11,
  209. applicableUsers: [],
  210. applicableGroups: [],
  211. mountOptions: {
  212. 'previews': true
  213. },
  214. testOnly: true
  215. });
  216. // TODO: respond and check data-id
  217. });
  218. it('saves storage with applicable users', function() {
  219. var $field1 = $tr.find('input[data-parameter=field1]');
  220. expect($field1.length).toEqual(1);
  221. $field1.val('test');
  222. $field1.trigger(new $.Event('keyup', {keyCode: 97}));
  223. $tr.find('.applicableToAllUsers').prop('checked', false).trigger('change');
  224. select2ApplicableUsers = ['user1', 'user2', 'group1(group)', 'group2(group)'];
  225. var $saveButton = $tr.find('td.save .icon-checkmark');
  226. $saveButton.click();
  227. expect(fakeServer.requests.length).toEqual(1);
  228. var request = fakeServer.requests[0];
  229. expect(request.url).toEqual(OC.getRootPath() + '/index.php/apps/files_external/globalstorages');
  230. expect(JSON.parse(request.requestBody)).toEqual({
  231. backend: '\\OC\\TestBackend',
  232. authMechanism: 'mechanism1',
  233. backendOptions: {
  234. 'field1': 'test',
  235. 'field2': ''
  236. },
  237. mountPoint: 'TestBackend',
  238. priority: 11,
  239. applicableUsers: ['user1', 'user2'],
  240. applicableGroups: ['group1', 'group2'],
  241. mountOptions: {
  242. encrypt: true,
  243. previews: true,
  244. enable_sharing: false,
  245. filesystem_check_changes: 1,
  246. encoding_compatibility: false,
  247. readonly: false,
  248. },
  249. testOnly: true
  250. });
  251. // TODO: respond and check data-id
  252. });
  253. it('does not saves storage without applicable users and unchecked all users checkbox', function() {
  254. var $field1 = $tr.find('input[data-parameter=field1]');
  255. expect($field1.length).toEqual(1);
  256. $field1.val('test');
  257. $field1.trigger(new $.Event('keyup', {keyCode: 97}));
  258. $tr.find('.applicableToAllUsers').prop('checked', false).trigger('change');
  259. var $saveButton = $tr.find('td.save .icon-checkmark');
  260. $saveButton.click();
  261. expect(fakeServer.requests.length).toEqual(0);
  262. });
  263. it('saves storage after closing mount options popovermenu', function() {
  264. $tr.find('.mountOptionsToggle .icon-more').click();
  265. $tr.find('[name=previews]').trigger(new $.Event('keyup', {keyCode: 97}));
  266. $tr.find('input[data-parameter=field1]').val('test');
  267. // does not save inside the popovermenu
  268. expect(fakeServer.requests.length).toEqual(0);
  269. $('body').mouseup();
  270. // but after closing the popovermenu
  271. expect(fakeServer.requests.length).toEqual(1);
  272. });
  273. // TODO: status indicator
  274. });
  275. describe('validate storage configuration', function() {
  276. var $tr;
  277. beforeEach(function() {
  278. selectBackend('\\OC\\InputsTestBackend');
  279. $tr = view.$el.find('tr:first');
  280. });
  281. it('lists missing fields in storage errors', function() {
  282. $tr.find('.applicableToAllUsers').prop('checked', false).trigger('change');
  283. var storage = view.getStorageConfig($tr);
  284. expect(storage.errors).toEqual({
  285. backendOptions: ['field_text', 'field_password'],
  286. requiredApplicable: true,
  287. });
  288. });
  289. it('does not list applicable when all users checkbox is ticked', function() {
  290. var storage = view.getStorageConfig($tr);
  291. expect(storage.errors).toEqual({
  292. backendOptions: ['field_text', 'field_password']
  293. });
  294. });
  295. it('highlights missing non-optional fields', function() {
  296. _.each([
  297. 'field_text',
  298. 'field_password'
  299. ], function(param) {
  300. expect($tr.find('input[data-parameter='+param+']').hasClass('warning-input')).toBe(true);
  301. });
  302. _.each([
  303. 'field_bool',
  304. 'field_hidden',
  305. 'field_text_optional',
  306. 'field_password_optional'
  307. ], function(param) {
  308. expect($tr.find('input[data-parameter='+param+']').hasClass('warning-input')).toBe(false);
  309. });
  310. });
  311. it('validates correct storage', function() {
  312. $tr.find('[name=mountPoint]').val('mountpoint');
  313. $tr.find('input[data-parameter=field_text]').val('foo');
  314. $tr.find('input[data-parameter=field_password]').val('bar');
  315. $tr.find('input[data-parameter=field_text_optional]').val('foobar');
  316. // don't set field_password_optional
  317. $tr.find('input[data-parameter=field_hidden]').val('baz');
  318. var storage = view.getStorageConfig($tr);
  319. expect(storage.validate()).toBe(true);
  320. });
  321. it('checks missing mount point', function() {
  322. $tr.find('[name=mountPoint]').val('');
  323. $tr.find('input[data-parameter=field_text]').val('foo');
  324. $tr.find('input[data-parameter=field_password]').val('bar');
  325. var storage = view.getStorageConfig($tr);
  326. expect(storage.validate()).toBe(false);
  327. });
  328. });
  329. describe('update storage', function() {
  330. // TODO
  331. });
  332. describe('delete storage', function() {
  333. // TODO
  334. });
  335. describe('recheck storages', function() {
  336. // TODO
  337. });
  338. describe('mount options popovermenu', function() {
  339. var $tr;
  340. var $td;
  341. beforeEach(function() {
  342. selectBackend('\\OC\\TestBackend');
  343. $tr = view.$el.find('tr:first');
  344. $td = $tr.find('.mountOptionsToggle');
  345. });
  346. it('shows popovermenu when clicking on toggle button, hides when clicking outside', function() {
  347. $td.find('.icon-more').click();
  348. expect($td.find('.popovermenu.open').length).toEqual(1);
  349. $('body').mouseup();
  350. expect($td.find('.popovermenu.open').length).toEqual(0);
  351. });
  352. it('doesnt show the encryption option when encryption is disabled', function () {
  353. view._encryptionEnabled = false;
  354. $td.find('.icon-more').click();
  355. expect($td.find('.popovermenu [name=encrypt]:visible').length).toEqual(0);
  356. $('body').mouseup();
  357. expect($td.find('.popovermenu.open').length).toEqual(0);
  358. });
  359. it('reads config from mountOptions field', function() {
  360. $tr.find('input.mountOptions').val(JSON.stringify({previews:false}));
  361. $td.find('.icon-more').click();
  362. expect($td.find('.popovermenu [name=previews]').prop('checked')).toEqual(false);
  363. $('body').mouseup();
  364. $tr.find('input.mountOptions').val(JSON.stringify({previews:true}));
  365. $td.find('.icon-more').click();
  366. expect($td.find('.popovermenu [name=previews]').prop('checked')).toEqual(true);
  367. });
  368. it('writes config into mountOptions field', function() {
  369. $td.find('.icon-more').click();
  370. // defaults to true
  371. var $field = $td.find('.popovermenu [name=previews]');
  372. expect($field.prop('checked')).toEqual(true);
  373. $td.find('.popovermenu [name=filesystem_check_changes]').val(0);
  374. $('body').mouseup();
  375. expect(JSON.parse($tr.find('input.mountOptions').val())).toEqual({
  376. encrypt: true,
  377. previews: true,
  378. enable_sharing: false,
  379. filesystem_check_changes: 0,
  380. encoding_compatibility: false,
  381. readonly: false
  382. });
  383. });
  384. });
  385. });
  386. describe('allow user mounts section', function() {
  387. // TODO: test allowUserMounting section
  388. });
  389. });