externalSpec.js 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. /**
  2. * Copyright (c) 2015 Vincent Petry <pvince81@owncloud.com>
  3. *
  4. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  5. * @author Vincent Petry <vincent@nextcloud.com>
  6. *
  7. * @license AGPL-3.0-or-later
  8. *
  9. * This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License as
  11. * published by the Free Software Foundation, either version 3 of the
  12. * License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. *
  22. */
  23. describe('OCA.Sharing external tests', function() {
  24. var plugin;
  25. var urlQueryStub;
  26. var promptDialogStub;
  27. var confirmDialogStub;
  28. function dummyShowDialog() {
  29. var deferred = $.Deferred();
  30. deferred.resolve();
  31. return deferred.promise();
  32. }
  33. beforeEach(function() {
  34. plugin = OCA.Sharing.ExternalShareDialogPlugin;
  35. urlQueryStub = sinon.stub(OC.Util.History, 'parseUrlQuery');
  36. confirmDialogStub = sinon.stub(OC.dialogs, 'confirm').callsFake(dummyShowDialog);
  37. promptDialogStub = sinon.stub(OC.dialogs, 'prompt').callsFake(dummyShowDialog);
  38. plugin.filesApp = {
  39. fileList: {
  40. reload: sinon.stub()
  41. }
  42. };
  43. });
  44. afterEach(function() {
  45. urlQueryStub.restore();
  46. confirmDialogStub.restore();
  47. promptDialogStub.restore();
  48. plugin = null;
  49. });
  50. describe('confirmation dialog from URL', function() {
  51. var testShare;
  52. /**
  53. * Checks that the server call's query matches what is
  54. * expected.
  55. *
  56. * @param {Object} expectedQuery expected query params
  57. */
  58. function checkRequest(expectedQuery) {
  59. var request = fakeServer.requests[0];
  60. var query = OC.parseQueryString(request.requestBody);
  61. expect(request.method).toEqual('POST');
  62. expect(query).toEqual(expectedQuery);
  63. request.respond(
  64. 200,
  65. {'Content-Type': 'application/json'},
  66. JSON.stringify({status: 'success'})
  67. );
  68. expect(plugin.filesApp.fileList.reload.calledOnce).toEqual(true);
  69. }
  70. beforeEach(function() {
  71. testShare = {
  72. remote: 'http://example.com/owncloud',
  73. token: 'abcdefg',
  74. owner: 'theowner',
  75. ownerDisplayName: 'The Generous Owner',
  76. name: 'the share name'
  77. };
  78. });
  79. it('does nothing when no share was passed in URL', function() {
  80. urlQueryStub.returns({});
  81. plugin.processIncomingShareFromUrl();
  82. expect(promptDialogStub.notCalled).toEqual(true);
  83. expect(confirmDialogStub.notCalled).toEqual(true);
  84. expect(fakeServer.requests.length).toEqual(0);
  85. });
  86. it('sends share info to server on confirm', function() {
  87. urlQueryStub.returns(testShare);
  88. plugin.processIncomingShareFromUrl();
  89. expect(promptDialogStub.notCalled).toEqual(true);
  90. expect(confirmDialogStub.calledOnce).toEqual(true);
  91. confirmDialogStub.getCall(0).args[2](true);
  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: ''
  100. });
  101. });
  102. it('sends share info with password to server on confirm', function() {
  103. testShare = _.extend(testShare, {protected: 1});
  104. urlQueryStub.returns(testShare);
  105. plugin.processIncomingShareFromUrl();
  106. expect(promptDialogStub.calledOnce).toEqual(true);
  107. expect(confirmDialogStub.notCalled).toEqual(true);
  108. promptDialogStub.getCall(0).args[2](true, 'thepassword');
  109. expect(fakeServer.requests.length).toEqual(1);
  110. checkRequest({
  111. remote: 'http://example.com/owncloud',
  112. token: 'abcdefg',
  113. owner: 'theowner',
  114. ownerDisplayName: 'The Generous Owner',
  115. name: 'the share name',
  116. password: 'thepassword'
  117. });
  118. });
  119. it('does not send share info on cancel', function() {
  120. urlQueryStub.returns(testShare);
  121. plugin.processIncomingShareFromUrl();
  122. expect(promptDialogStub.notCalled).toEqual(true);
  123. expect(confirmDialogStub.calledOnce).toEqual(true);
  124. confirmDialogStub.getCall(0).args[2](false);
  125. expect(fakeServer.requests.length).toEqual(0);
  126. });
  127. });
  128. describe('show dialog for each share to confirm', function() {
  129. var testShare;
  130. /**
  131. * Call processSharesToConfirm() and make the fake server
  132. * return the passed response.
  133. *
  134. * @param {Array} response list of shares to process
  135. */
  136. function processShares(response) {
  137. plugin.processSharesToConfirm();
  138. expect(fakeServer.requests.length).toEqual(1);
  139. var req = fakeServer.requests[0];
  140. expect(req.method).toEqual('GET');
  141. expect(req.url).toEqual(OC.getRootPath() + '/index.php/apps/files_sharing/api/externalShares');
  142. req.respond(
  143. 200,
  144. {'Content-Type': 'application/json'},
  145. JSON.stringify(response)
  146. );
  147. }
  148. beforeEach(function() {
  149. testShare = {
  150. id: 123,
  151. remote: 'http://example.com/owncloud',
  152. token: 'abcdefg',
  153. owner: 'theowner',
  154. ownerDisplayName: 'The Generous Owner',
  155. name: 'the share name'
  156. };
  157. });
  158. it('does not show any dialog if no shares to confirm', function() {
  159. processShares([]);
  160. expect(confirmDialogStub.notCalled).toEqual(true);
  161. expect(promptDialogStub.notCalled).toEqual(true);
  162. });
  163. it('sends accept info to server on confirm', function() {
  164. processShares([testShare]);
  165. expect(promptDialogStub.notCalled).toEqual(true);
  166. expect(confirmDialogStub.calledOnce).toEqual(true);
  167. confirmDialogStub.getCall(0).args[2](true);
  168. expect(fakeServer.requests.length).toEqual(2);
  169. var request = fakeServer.requests[1];
  170. var query = OC.parseQueryString(request.requestBody);
  171. expect(request.method).toEqual('POST');
  172. expect(query).toEqual({id: '123'});
  173. expect(request.url).toEqual(
  174. OC.getRootPath() + '/index.php/apps/files_sharing/api/externalShares'
  175. );
  176. expect(plugin.filesApp.fileList.reload.notCalled).toEqual(true);
  177. request.respond(
  178. 200,
  179. {'Content-Type': 'application/json'},
  180. JSON.stringify({status: 'success'})
  181. );
  182. expect(plugin.filesApp.fileList.reload.calledOnce).toEqual(true);
  183. });
  184. it('sends delete info to server on cancel', function() {
  185. processShares([testShare]);
  186. expect(promptDialogStub.notCalled).toEqual(true);
  187. expect(confirmDialogStub.calledOnce).toEqual(true);
  188. confirmDialogStub.getCall(0).args[2](false);
  189. expect(fakeServer.requests.length).toEqual(2);
  190. var request = fakeServer.requests[1];
  191. expect(request.method).toEqual('DELETE');
  192. expect(request.url).toEqual(
  193. OC.getRootPath() + '/index.php/apps/files_sharing/api/externalShares/123'
  194. );
  195. expect(plugin.filesApp.fileList.reload.notCalled).toEqual(true);
  196. request.respond(
  197. 200,
  198. {'Content-Type': 'application/json'},
  199. JSON.stringify({status: 'success'})
  200. );
  201. expect(plugin.filesApp.fileList.reload.notCalled).toEqual(true);
  202. });
  203. xit('shows another dialog when multiple shares need to be accepted', function() {
  204. // TODO: enable this test when fixing multiple dialogs issue / confirm loop
  205. var testShare2 = _.extend({}, testShare);
  206. testShare2.id = 256;
  207. processShares([testShare, testShare2]);
  208. // confirm first one
  209. expect(confirmDialogStub.calledOnce).toEqual(true);
  210. confirmDialogStub.getCall(0).args[2](true);
  211. // next dialog not shown yet
  212. expect(confirmDialogStub.calledOnce);
  213. // respond to the first accept request
  214. fakeServer.requests[1].respond(
  215. 200,
  216. {'Content-Type': 'application/json'},
  217. JSON.stringify({status: 'success'})
  218. );
  219. // don't reload yet, there are other shares to confirm
  220. expect(plugin.filesApp.fileList.reload.notCalled).toEqual(true);
  221. // cancel second share
  222. expect(confirmDialogStub.calledTwice).toEqual(true);
  223. confirmDialogStub.getCall(1).args[2](true);
  224. // reload only called at the very end
  225. expect(plugin.filesApp.fileList.reload.calledOnce).toEqual(true);
  226. });
  227. });
  228. });