versionmodelSpec.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /**
  2. * Copyright (c) 2015
  3. *
  4. * @author Daniel Calviño Sánchez <danxuliu@gmail.com>
  5. * @author Robin Appelman <robin@icewind.nl>
  6. * @author Vincent Petry <vincent@nextcloud.com>
  7. *
  8. * @license AGPL-3.0-or-later
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License as
  12. * published by the Free Software Foundation, either version 3 of the
  13. * License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. *
  23. */
  24. describe('OCA.Versions.VersionModel', function() {
  25. var VersionModel = OCA.Versions.VersionModel;
  26. var model;
  27. var uid = OC.currentUser = 'user';
  28. beforeEach(function() {
  29. model = new VersionModel({
  30. id: 10000000,
  31. fileId: 10,
  32. timestamp: 10000000,
  33. fullPath: '/subdir/some file.txt',
  34. name: 'some file.txt',
  35. size: 150,
  36. user: 'user',
  37. client: new OC.Files.Client({
  38. host: 'localhost',
  39. port: 80,
  40. root: '/remote.php/dav/versions/user',
  41. useHTTPS: OC.getProtocol() === 'https'
  42. })
  43. });
  44. });
  45. it('returns the full path', function() {
  46. expect(model.getFullPath()).toEqual('/subdir/some file.txt');
  47. });
  48. it('returns the preview url', function() {
  49. expect(model.getPreviewUrl())
  50. .toEqual(OC.generateUrl('/apps/files_versions/preview') +
  51. '?file=%2Fsubdir%2Fsome%20file.txt&version=10000000'
  52. );
  53. });
  54. it('returns the download url', function() {
  55. expect(model.getDownloadUrl())
  56. .toEqual(OC.linkToRemoteBase('dav') + '/versions/' + uid +
  57. '/versions/10/10000000'
  58. );
  59. });
  60. describe('reverting', function() {
  61. var revertEventStub;
  62. var successStub;
  63. var errorStub;
  64. beforeEach(function() {
  65. revertEventStub = sinon.stub();
  66. errorStub = sinon.stub();
  67. successStub = sinon.stub();
  68. model.on('revert', revertEventStub);
  69. model.on('error', errorStub);
  70. });
  71. it('tells the server to revert when calling the revert method', function(done) {
  72. var promise = model.revert({
  73. success: successStub
  74. });
  75. expect(fakeServer.requests.length).toEqual(1);
  76. var request = fakeServer.requests[0];
  77. expect(request.url)
  78. .toEqual(
  79. OC.linkToRemoteBase('dav') + '/versions/user/versions/10/10000000'
  80. );
  81. expect(request.requestHeaders.Destination).toEqual(OC.getRootPath() + '/remote.php/dav/versions/user/restore/target');
  82. request.respond(201);
  83. promise.then(function() {
  84. expect(revertEventStub.calledOnce).toEqual(true);
  85. expect(successStub.calledOnce).toEqual(true);
  86. expect(errorStub.notCalled).toEqual(true);
  87. done();
  88. });
  89. });
  90. it('triggers error event when server returns a failure', function(done) {
  91. var promise = model.revert({
  92. success: successStub
  93. });
  94. expect(fakeServer.requests.length).toEqual(1);
  95. var responseErrorHeaders = {
  96. "Content-Type": "application/xml"
  97. };
  98. var responseErrorBody =
  99. '<d:error xmlns:d="DAV:" xmlns:s="http://sabredav.org/ns">' +
  100. ' <s:exception>Sabre\\DAV\\Exception\\SomeException</s:exception>' +
  101. ' <s:message>Some error message</s:message>' +
  102. '</d:error>';
  103. fakeServer.requests[0].respond(404, responseErrorHeaders, responseErrorBody);
  104. promise.fail(function() {
  105. expect(revertEventStub.notCalled).toEqual(true);
  106. expect(successStub.notCalled).toEqual(true);
  107. expect(errorStub.calledOnce).toEqual(true);
  108. done();
  109. });
  110. });
  111. });
  112. });