externalSpec.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. /**
  2. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  3. * SPDX-FileCopyrightText: 2015 ownCloud, Inc.
  4. * SPDX-License-Identifier: AGPL-3.0-or-later
  5. */
  6. describe('OCA.Sharing external tests', function() {
  7. var plugin;
  8. var urlQueryStub;
  9. var promptDialogStub;
  10. var confirmDialogStub;
  11. function dummyShowDialog() {
  12. var deferred = $.Deferred();
  13. deferred.resolve();
  14. return deferred.promise();
  15. }
  16. beforeEach(function() {
  17. plugin = OCA.Sharing.ExternalShareDialogPlugin;
  18. urlQueryStub = sinon.stub(OC.Util.History, 'parseUrlQuery');
  19. confirmDialogStub = sinon.stub(OC.dialogs, 'confirm').callsFake(dummyShowDialog);
  20. promptDialogStub = sinon.stub(OC.dialogs, 'prompt').callsFake(dummyShowDialog);
  21. plugin.filesApp = {
  22. fileList: {
  23. reload: sinon.stub()
  24. }
  25. };
  26. });
  27. afterEach(function() {
  28. urlQueryStub.restore();
  29. confirmDialogStub.restore();
  30. promptDialogStub.restore();
  31. plugin = null;
  32. });
  33. describe('confirmation dialog from URL', function() {
  34. var testShare;
  35. /**
  36. * Checks that the server call's query matches what is
  37. * expected.
  38. *
  39. * @param {Object} expectedQuery expected query params
  40. */
  41. function checkRequest(expectedQuery) {
  42. var request = fakeServer.requests[0];
  43. var query = OC.parseQueryString(request.requestBody);
  44. expect(request.method).toEqual('POST');
  45. expect(query).toEqual(expectedQuery);
  46. request.respond(
  47. 200,
  48. {'Content-Type': 'application/json'},
  49. JSON.stringify({status: 'success'})
  50. );
  51. expect(plugin.filesApp.fileList.reload.calledOnce).toEqual(true);
  52. }
  53. beforeEach(function() {
  54. testShare = {
  55. remote: 'http://example.com/owncloud',
  56. token: 'abcdefg',
  57. owner: 'theowner',
  58. ownerDisplayName: 'The Generous Owner',
  59. name: 'the share name'
  60. };
  61. });
  62. it('does nothing when no share was passed in URL', function() {
  63. urlQueryStub.returns({});
  64. plugin.processIncomingShareFromUrl();
  65. expect(promptDialogStub.notCalled).toEqual(true);
  66. expect(confirmDialogStub.notCalled).toEqual(true);
  67. expect(fakeServer.requests.length).toEqual(0);
  68. });
  69. it('sends share info to server on confirm', function() {
  70. urlQueryStub.returns(testShare);
  71. plugin.processIncomingShareFromUrl();
  72. expect(promptDialogStub.notCalled).toEqual(true);
  73. expect(confirmDialogStub.calledOnce).toEqual(true);
  74. confirmDialogStub.getCall(0).args[2](true);
  75. expect(fakeServer.requests.length).toEqual(1);
  76. checkRequest({
  77. remote: 'http://example.com/owncloud',
  78. token: 'abcdefg',
  79. owner: 'theowner',
  80. ownerDisplayName: 'The Generous Owner',
  81. name: 'the share name',
  82. password: ''
  83. });
  84. });
  85. it('sends share info with password to server on confirm', function() {
  86. testShare = _.extend(testShare, {protected: 1});
  87. urlQueryStub.returns(testShare);
  88. plugin.processIncomingShareFromUrl();
  89. expect(promptDialogStub.calledOnce).toEqual(true);
  90. expect(confirmDialogStub.notCalled).toEqual(true);
  91. promptDialogStub.getCall(0).args[2](true, 'thepassword');
  92. expect(fakeServer.requests.length).toEqual(1);
  93. checkRequest({
  94. remote: 'http://example.com/owncloud',
  95. token: 'abcdefg',
  96. owner: 'theowner',
  97. ownerDisplayName: 'The Generous Owner',
  98. name: 'the share name',
  99. password: 'thepassword'
  100. });
  101. });
  102. it('does not send share info on cancel', function() {
  103. urlQueryStub.returns(testShare);
  104. plugin.processIncomingShareFromUrl();
  105. expect(promptDialogStub.notCalled).toEqual(true);
  106. expect(confirmDialogStub.calledOnce).toEqual(true);
  107. confirmDialogStub.getCall(0).args[2](false);
  108. expect(fakeServer.requests.length).toEqual(0);
  109. });
  110. });
  111. describe('show dialog for each share to confirm', function() {
  112. var testShare;
  113. /**
  114. * Call processSharesToConfirm() and make the fake server
  115. * return the passed response.
  116. *
  117. * @param {Array} response list of shares to process
  118. */
  119. function processShares(response) {
  120. plugin.processSharesToConfirm();
  121. expect(fakeServer.requests.length).toEqual(1);
  122. var req = fakeServer.requests[0];
  123. expect(req.method).toEqual('GET');
  124. expect(req.url).toEqual(OC.getRootPath() + '/index.php/apps/files_sharing/api/externalShares');
  125. req.respond(
  126. 200,
  127. {'Content-Type': 'application/json'},
  128. JSON.stringify(response)
  129. );
  130. }
  131. beforeEach(function() {
  132. testShare = {
  133. id: 123,
  134. remote: 'http://example.com/owncloud',
  135. token: 'abcdefg',
  136. owner: 'theowner',
  137. ownerDisplayName: 'The Generous Owner',
  138. name: 'the share name'
  139. };
  140. });
  141. it('does not show any dialog if no shares to confirm', function() {
  142. processShares([]);
  143. expect(confirmDialogStub.notCalled).toEqual(true);
  144. expect(promptDialogStub.notCalled).toEqual(true);
  145. });
  146. it('sends accept info to server on confirm', function() {
  147. processShares([testShare]);
  148. expect(promptDialogStub.notCalled).toEqual(true);
  149. expect(confirmDialogStub.calledOnce).toEqual(true);
  150. confirmDialogStub.getCall(0).args[2](true);
  151. expect(fakeServer.requests.length).toEqual(2);
  152. var request = fakeServer.requests[1];
  153. var query = OC.parseQueryString(request.requestBody);
  154. expect(request.method).toEqual('POST');
  155. expect(query).toEqual({id: '123'});
  156. expect(request.url).toEqual(
  157. OC.getRootPath() + '/index.php/apps/files_sharing/api/externalShares'
  158. );
  159. expect(plugin.filesApp.fileList.reload.notCalled).toEqual(true);
  160. request.respond(
  161. 200,
  162. {'Content-Type': 'application/json'},
  163. JSON.stringify({status: 'success'})
  164. );
  165. expect(plugin.filesApp.fileList.reload.calledOnce).toEqual(true);
  166. });
  167. it('sends delete info to server on cancel', function() {
  168. processShares([testShare]);
  169. expect(promptDialogStub.notCalled).toEqual(true);
  170. expect(confirmDialogStub.calledOnce).toEqual(true);
  171. confirmDialogStub.getCall(0).args[2](false);
  172. expect(fakeServer.requests.length).toEqual(2);
  173. var request = fakeServer.requests[1];
  174. expect(request.method).toEqual('DELETE');
  175. expect(request.url).toEqual(
  176. OC.getRootPath() + '/index.php/apps/files_sharing/api/externalShares/123'
  177. );
  178. expect(plugin.filesApp.fileList.reload.notCalled).toEqual(true);
  179. request.respond(
  180. 200,
  181. {'Content-Type': 'application/json'},
  182. JSON.stringify({status: 'success'})
  183. );
  184. expect(plugin.filesApp.fileList.reload.notCalled).toEqual(true);
  185. });
  186. xit('shows another dialog when multiple shares need to be accepted', function() {
  187. // TODO: enable this test when fixing multiple dialogs issue / confirm loop
  188. var testShare2 = _.extend({}, testShare);
  189. testShare2.id = 256;
  190. processShares([testShare, testShare2]);
  191. // confirm first one
  192. expect(confirmDialogStub.calledOnce).toEqual(true);
  193. confirmDialogStub.getCall(0).args[2](true);
  194. // next dialog not shown yet
  195. expect(confirmDialogStub.calledOnce);
  196. // respond to the first accept request
  197. fakeServer.requests[1].respond(
  198. 200,
  199. {'Content-Type': 'application/json'},
  200. JSON.stringify({status: 'success'})
  201. );
  202. // don't reload yet, there are other shares to confirm
  203. expect(plugin.filesApp.fileList.reload.notCalled).toEqual(true);
  204. // cancel second share
  205. expect(confirmDialogStub.calledTwice).toEqual(true);
  206. confirmDialogStub.getCall(1).args[2](true);
  207. // reload only called at the very end
  208. expect(plugin.filesApp.fileList.reload.calledOnce).toEqual(true);
  209. });
  210. });
  211. });