externalSpec.js 7.2 KB

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