You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

externalSpec.js 7.2KB

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