versionmodelSpec.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /*
  2. * Copyright (c) 2015
  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.Versions.VersionModel', function() {
  11. var VersionModel = OCA.Versions.VersionModel;
  12. var model;
  13. var uid = OC.currentUser = 'user';
  14. beforeEach(function() {
  15. model = new VersionModel({
  16. id: 10000000,
  17. fileId: 10,
  18. timestamp: 10000000,
  19. fullPath: '/subdir/some file.txt',
  20. name: 'some file.txt',
  21. size: 150,
  22. user: 'user',
  23. client: new OC.Files.Client({
  24. host: 'localhost',
  25. port: 80,
  26. root: '/remote.php/dav/versions/user',
  27. useHTTPS: OC.getProtocol() === 'https'
  28. })
  29. });
  30. });
  31. it('returns the full path', function() {
  32. expect(model.getFullPath()).toEqual('/subdir/some file.txt');
  33. });
  34. it('returns the preview url', function() {
  35. expect(model.getPreviewUrl())
  36. .toEqual(OC.generateUrl('/apps/files_versions/preview') +
  37. '?file=%2Fsubdir%2Fsome%20file.txt&version=10000000'
  38. );
  39. });
  40. it('returns the download url', function() {
  41. expect(model.getDownloadUrl())
  42. .toEqual(OC.linkToRemoteBase('dav') + '/versions/' + uid +
  43. '/versions/10/10000000'
  44. );
  45. });
  46. describe('reverting', function() {
  47. var revertEventStub;
  48. var successStub;
  49. var errorStub;
  50. beforeEach(function() {
  51. revertEventStub = sinon.stub();
  52. errorStub = sinon.stub();
  53. successStub = sinon.stub();
  54. model.on('revert', revertEventStub);
  55. model.on('error', errorStub);
  56. });
  57. it('tells the server to revert when calling the revert method', function(done) {
  58. var promise = model.revert({
  59. success: successStub
  60. });
  61. expect(fakeServer.requests.length).toEqual(1);
  62. var request = fakeServer.requests[0];
  63. expect(request.url)
  64. .toEqual(
  65. OC.linkToRemoteBase('dav') + '/versions/user/versions/10/10000000'
  66. );
  67. expect(request.requestHeaders.Destination).toEqual(OC.getRootPath() + '/remote.php/dav/versions/user/restore/target');
  68. request.respond(201);
  69. promise.then(function() {
  70. expect(revertEventStub.calledOnce).toEqual(true);
  71. expect(successStub.calledOnce).toEqual(true);
  72. expect(errorStub.notCalled).toEqual(true);
  73. done();
  74. });
  75. });
  76. it('triggers error event when server returns a failure', function(done) {
  77. var promise = model.revert({
  78. success: successStub
  79. });
  80. expect(fakeServer.requests.length).toEqual(1);
  81. var responseErrorHeaders = {
  82. "Content-Type": "application/xml"
  83. };
  84. var responseErrorBody =
  85. '<d:error xmlns:d="DAV:" xmlns:s="http://sabredav.org/ns">' +
  86. ' <s:exception>Sabre\\DAV\\Exception\\SomeException</s:exception>' +
  87. ' <s:message>Some error message</s:message>' +
  88. '</d:error>';
  89. fakeServer.requests[0].respond(404, responseErrorHeaders, responseErrorBody);
  90. promise.fail(function() {
  91. expect(revertEventStub.notCalled).toEqual(true);
  92. expect(successStub.notCalled).toEqual(true);
  93. expect(errorStub.calledOnce).toEqual(true);
  94. done();
  95. });
  96. });
  97. });
  98. });