deleteHandlerSpec.js 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. /**
  2. * ownCloud
  3. *
  4. * @author Vincent Petry
  5. * @copyright 2014 Vincent Petry <pvince81@owncloud.com>
  6. *
  7. * This library is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
  9. * License as published by the Free Software Foundation; either
  10. * version 3 of the License, or any later version.
  11. *
  12. * This library is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
  16. *
  17. * You should have received a copy of the GNU Affero General Public
  18. * License along with this library. If not, see <http://www.gnu.org/licenses/>.
  19. *
  20. */
  21. describe('DeleteHandler tests', function() {
  22. var showNotificationSpy;
  23. var hideNotificationSpy;
  24. var clock;
  25. var removeCallback;
  26. var markCallback;
  27. var undoCallback;
  28. function init(markCallback, removeCallback, undoCallback) {
  29. var handler = new DeleteHandler('dummyendpoint.php', 'paramid', markCallback, removeCallback);
  30. handler.setNotification(OC.Notification, 'dataid', 'removed %oid entry', undoCallback);
  31. return handler;
  32. }
  33. beforeEach(function() {
  34. showNotificationSpy = sinon.spy(OC.Notification, 'showHtml');
  35. hideNotificationSpy = sinon.spy(OC.Notification, 'hide');
  36. clock = sinon.useFakeTimers();
  37. removeCallback = sinon.stub();
  38. markCallback = sinon.stub();
  39. undoCallback = sinon.stub();
  40. $('#testArea').append('<div id="notification"></div>');
  41. });
  42. afterEach(function() {
  43. showNotificationSpy.restore();
  44. hideNotificationSpy.restore();
  45. clock.restore();
  46. });
  47. it('shows a notification when marking for delete', function() {
  48. var handler = init(markCallback, removeCallback, undoCallback);
  49. handler.mark('some_uid');
  50. expect(showNotificationSpy.calledOnce).toEqual(true);
  51. expect(showNotificationSpy.getCall(0).args[0]).toEqual('removed some_uid entry');
  52. expect(markCallback.calledOnce).toEqual(true);
  53. expect(markCallback.getCall(0).args[0]).toEqual('some_uid');
  54. expect(removeCallback.notCalled).toEqual(true);
  55. expect(undoCallback.notCalled).toEqual(true);
  56. expect(fakeServer.requests.length).toEqual(0);
  57. });
  58. it('deletes first entry and reshows notification on second delete', function() {
  59. fakeServer.respondWith(/\/index\.php\/dummyendpoint.php\/some_uid/, [
  60. 204,
  61. { 'Content-Type': 'application/json' },
  62. JSON.stringify({status: 'success'})
  63. ]);
  64. fakeServer.respondWith(/\/index\.php\/dummyendpoint.php\/some_other_uid/, [
  65. 204,
  66. { 'Content-Type': 'application/json' },
  67. JSON.stringify({status: 'success'})
  68. ]);
  69. var handler = init(markCallback, removeCallback, undoCallback);
  70. handler.mark('some_uid');
  71. expect(showNotificationSpy.calledOnce).toEqual(true);
  72. expect(showNotificationSpy.getCall(0).args[0]).toEqual('removed some_uid entry');
  73. showNotificationSpy.reset();
  74. handler.mark('some_other_uid');
  75. expect(hideNotificationSpy.calledOnce).toEqual(true);
  76. expect(showNotificationSpy.calledOnce).toEqual(true);
  77. expect(showNotificationSpy.getCall(0).args[0]).toEqual('removed some_other_uid entry');
  78. expect(markCallback.calledTwice).toEqual(true);
  79. expect(markCallback.getCall(0).args[0]).toEqual('some_uid');
  80. expect(markCallback.getCall(1).args[0]).toEqual('some_other_uid');
  81. // called only once, because it is called once the second user is deleted
  82. expect(removeCallback.calledOnce).toEqual(true);
  83. expect(undoCallback.notCalled).toEqual(true);
  84. // previous one was delete
  85. expect(fakeServer.requests.length).toEqual(1);
  86. var request = fakeServer.requests[0];
  87. expect(request.url).toEqual(OC.webroot + '/index.php/dummyendpoint.php/some_uid');
  88. });
  89. it('automatically deletes after timeout', function() {
  90. fakeServer.respondWith(/\/index\.php\/dummyendpoint.php\/some_uid/, [
  91. 204,
  92. { 'Content-Type': 'application/json' },
  93. JSON.stringify({status: 'success'})
  94. ]);
  95. var handler = init(markCallback, removeCallback, undoCallback);
  96. handler.mark('some_uid');
  97. clock.tick(5000);
  98. // nothing happens yet
  99. expect(fakeServer.requests.length).toEqual(0);
  100. clock.tick(3000);
  101. expect(fakeServer.requests.length).toEqual(1);
  102. var request = fakeServer.requests[0];
  103. expect(request.url).toEqual(OC.webroot + '/index.php/dummyendpoint.php/some_uid');
  104. });
  105. it('deletes when deleteEntry is called', function() {
  106. fakeServer.respondWith(/\/index\.php\/dummyendpoint.php\/some_uid/, [
  107. 200,
  108. { 'Content-Type': 'application/json' },
  109. JSON.stringify({status: 'success'})
  110. ]);
  111. var handler = init(markCallback, removeCallback, undoCallback);
  112. handler.mark('some_uid');
  113. handler.deleteEntry();
  114. expect(fakeServer.requests.length).toEqual(1);
  115. var request = fakeServer.requests[0];
  116. expect(request.url).toEqual(OC.webroot + '/index.php/dummyendpoint.php/some_uid');
  117. });
  118. it('deletes when deleteEntry is called and escapes', function() {
  119. fakeServer.respondWith(/\/index\.php\/dummyendpoint.php\/some_uid/, [
  120. 200,
  121. { 'Content-Type': 'application/json' },
  122. JSON.stringify({status: 'success'})
  123. ]);
  124. var handler = init(markCallback, removeCallback, undoCallback);
  125. handler.mark('some_uid<>/"..\\');
  126. handler.deleteEntry();
  127. expect(fakeServer.requests.length).toEqual(1);
  128. var request = fakeServer.requests[0];
  129. expect(request.url).toEqual(OC.webroot + '/index.php/dummyendpoint.php/some_uid%3C%3E%2F%22..%5C');
  130. });
  131. it('cancels deletion when undo is clicked', function() {
  132. var handler = init(markCallback, removeCallback, undoCallback);
  133. handler.setNotification(OC.Notification, 'dataid', 'removed %oid entry <span class="undo">Undo</span>', undoCallback);
  134. handler.mark('some_uid');
  135. $('#notification .undo').click();
  136. expect(undoCallback.calledOnce).toEqual(true);
  137. // timer was cancelled
  138. clock.tick(10000);
  139. expect(fakeServer.requests.length).toEqual(0);
  140. });
  141. it('cancels deletion when cancel method is called', function() {
  142. var handler = init(markCallback, removeCallback, undoCallback);
  143. handler.setNotification(OC.Notification, 'dataid', 'removed %oid entry <span class="undo">Undo</span>', undoCallback);
  144. handler.mark('some_uid');
  145. handler.cancel();
  146. // not sure why, seems to be by design
  147. expect(undoCallback.notCalled).toEqual(true);
  148. // timer was cancelled
  149. clock.tick(10000);
  150. expect(fakeServer.requests.length).toEqual(0);
  151. });
  152. it('calls removeCallback after successful server side deletion', function() {
  153. fakeServer.respondWith(/\/index\.php\/dummyendpoint.php\/some_uid/, [
  154. 200,
  155. { 'Content-Type': 'application/json' },
  156. JSON.stringify({status: 'success'})
  157. ]);
  158. var handler = init(markCallback, removeCallback, undoCallback);
  159. handler.mark('some_uid');
  160. handler.deleteEntry();
  161. expect(fakeServer.requests.length).toEqual(1);
  162. var request = fakeServer.requests[0];
  163. var query = OC.parseQueryString(request.requestBody);
  164. expect(removeCallback.calledOnce).toEqual(true);
  165. expect(undoCallback.notCalled).toEqual(true);
  166. expect(removeCallback.getCall(0).args[0]).toEqual('some_uid');
  167. });
  168. it('calls undoCallback and shows alert after failed server side deletion', function() {
  169. // stub t to avoid extra calls
  170. var tStub = sinon.stub(window, 't').returns('text');
  171. fakeServer.respondWith(/\/index\.php\/dummyendpoint.php\/some_uid/, [
  172. 403,
  173. { 'Content-Type': 'application/json' },
  174. JSON.stringify({status: 'error', data: {message: 'test error'}})
  175. ]);
  176. var alertDialogStub = sinon.stub(OC.dialogs, 'alert');
  177. var handler = init(markCallback, removeCallback, undoCallback);
  178. handler.mark('some_uid');
  179. handler.deleteEntry();
  180. expect(fakeServer.requests.length).toEqual(1);
  181. var request = fakeServer.requests[0];
  182. var query = OC.parseQueryString(request.requestBody);
  183. expect(removeCallback.notCalled).toEqual(true);
  184. expect(undoCallback.calledOnce).toEqual(true);
  185. expect(undoCallback.getCall(0).args[0]).toEqual('some_uid');
  186. expect(alertDialogStub.calledOnce);
  187. alertDialogStub.restore();
  188. tStub.restore();
  189. });
  190. });