UrlCacheTests.js 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. /* eslint-disable
  2. no-return-assign,
  3. no-unused-vars,
  4. */
  5. // TODO: This file was created by bulk-decaffeinate.
  6. // Fix any style issues and re-enable lint.
  7. /*
  8. * decaffeinate suggestions:
  9. * DS101: Remove unnecessary use of Array.from
  10. * DS102: Remove unnecessary code created because of implicit returns
  11. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  12. */
  13. const SandboxedModule = require('sandboxed-module');
  14. const sinon = require('sinon');
  15. require('chai').should();
  16. const modulePath = require('path').join(__dirname, '../../../app/js/UrlCache');
  17. const { EventEmitter } = require("events");
  18. describe("UrlCache", function() {
  19. beforeEach(function() {
  20. this.callback = sinon.stub();
  21. this.url = "www.example.com/file";
  22. this.project_id = "project-id-123";
  23. return this.UrlCache = SandboxedModule.require(modulePath, { requires: {
  24. "./db" : {},
  25. "./UrlFetcher" : (this.UrlFetcher = {}),
  26. "logger-sharelatex": (this.logger = {log: sinon.stub()}),
  27. "settings-sharelatex": (this.Settings = { path: {clsiCacheDir: "/cache/dir"} }),
  28. "fs": (this.fs = {})
  29. }
  30. });});
  31. describe("_doesUrlNeedDownloading", function() {
  32. beforeEach(function() {
  33. this.lastModified = new Date();
  34. return this.lastModifiedRoundedToSeconds = new Date(Math.floor(this.lastModified.getTime() / 1000) * 1000);
  35. });
  36. describe("when URL does not exist in cache", function() {
  37. beforeEach(function() {
  38. this.UrlCache._findUrlDetails = sinon.stub().callsArgWith(2, null, null);
  39. return this.UrlCache._doesUrlNeedDownloading(this.project_id, this.url, this.lastModified, this.callback);
  40. });
  41. return it("should return the callback with true", function() {
  42. return this.callback.calledWith(null, true).should.equal(true);
  43. });
  44. });
  45. return describe("when URL does exist in cache", function() {
  46. beforeEach(function() {
  47. this.urlDetails = {};
  48. return this.UrlCache._findUrlDetails = sinon.stub().callsArgWith(2, null, this.urlDetails);
  49. });
  50. describe("when the modified date is more recent than the cached modified date", function() {
  51. beforeEach(function() {
  52. this.urlDetails.lastModified = new Date(this.lastModified.getTime() - 1000);
  53. return this.UrlCache._doesUrlNeedDownloading(this.project_id, this.url, this.lastModified, this.callback);
  54. });
  55. it("should get the url details", function() {
  56. return this.UrlCache._findUrlDetails
  57. .calledWith(this.project_id, this.url)
  58. .should.equal(true);
  59. });
  60. return it("should return the callback with true", function() {
  61. return this.callback.calledWith(null, true).should.equal(true);
  62. });
  63. });
  64. describe("when the cached modified date is more recent than the modified date", function() {
  65. beforeEach(function() {
  66. this.urlDetails.lastModified = new Date(this.lastModified.getTime() + 1000);
  67. return this.UrlCache._doesUrlNeedDownloading(this.project_id, this.url, this.lastModified, this.callback);
  68. });
  69. return it("should return the callback with false", function() {
  70. return this.callback.calledWith(null, false).should.equal(true);
  71. });
  72. });
  73. describe("when the cached modified date is equal to the modified date", function() {
  74. beforeEach(function() {
  75. this.urlDetails.lastModified = this.lastModified;
  76. return this.UrlCache._doesUrlNeedDownloading(this.project_id, this.url, this.lastModified, this.callback);
  77. });
  78. return it("should return the callback with false", function() {
  79. return this.callback.calledWith(null, false).should.equal(true);
  80. });
  81. });
  82. describe("when the provided modified date does not exist", function() {
  83. beforeEach(function() {
  84. this.lastModified = null;
  85. return this.UrlCache._doesUrlNeedDownloading(this.project_id, this.url, this.lastModified, this.callback);
  86. });
  87. return it("should return the callback with true", function() {
  88. return this.callback.calledWith(null, true).should.equal(true);
  89. });
  90. });
  91. return describe("when the URL does not have a modified date", function() {
  92. beforeEach(function() {
  93. this.urlDetails.lastModified = null;
  94. return this.UrlCache._doesUrlNeedDownloading(this.project_id, this.url, this.lastModified, this.callback);
  95. });
  96. return it("should return the callback with true", function() {
  97. return this.callback.calledWith(null, true).should.equal(true);
  98. });
  99. });
  100. });
  101. });
  102. describe("_ensureUrlIsInCache", function() {
  103. beforeEach(function() {
  104. this.UrlFetcher.pipeUrlToFile = sinon.stub().callsArg(2);
  105. return this.UrlCache._updateOrCreateUrlDetails = sinon.stub().callsArg(3);
  106. });
  107. describe("when the URL needs updating", function() {
  108. beforeEach(function() {
  109. this.UrlCache._doesUrlNeedDownloading = sinon.stub().callsArgWith(3, null, true);
  110. return this.UrlCache._ensureUrlIsInCache(this.project_id, this.url, this.lastModified, this.callback);
  111. });
  112. it("should check that the url needs downloading", function() {
  113. return this.UrlCache._doesUrlNeedDownloading
  114. .calledWith(this.project_id, this.url, this.lastModifiedRoundedToSeconds)
  115. .should.equal(true);
  116. });
  117. it("should download the URL to the cache file", function() {
  118. return this.UrlFetcher.pipeUrlToFile
  119. .calledWith(this.url, this.UrlCache._cacheFilePathForUrl(this.project_id, this.url))
  120. .should.equal(true);
  121. });
  122. it("should update the database entry", function() {
  123. return this.UrlCache._updateOrCreateUrlDetails
  124. .calledWith(this.project_id, this.url, this.lastModifiedRoundedToSeconds)
  125. .should.equal(true);
  126. });
  127. return it("should return the callback with the cache file path", function() {
  128. return this.callback
  129. .calledWith(null, this.UrlCache._cacheFilePathForUrl(this.project_id, this.url))
  130. .should.equal(true);
  131. });
  132. });
  133. return describe("when the URL does not need updating", function() {
  134. beforeEach(function() {
  135. this.UrlCache._doesUrlNeedDownloading = sinon.stub().callsArgWith(3, null, false);
  136. return this.UrlCache._ensureUrlIsInCache(this.project_id, this.url, this.lastModified, this.callback);
  137. });
  138. it("should not download the URL to the cache file", function() {
  139. return this.UrlFetcher.pipeUrlToFile
  140. .called.should.equal(false);
  141. });
  142. return it("should return the callback with the cache file path", function() {
  143. return this.callback
  144. .calledWith(null, this.UrlCache._cacheFilePathForUrl(this.project_id, this.url))
  145. .should.equal(true);
  146. });
  147. });
  148. });
  149. describe("downloadUrlToFile", function() {
  150. beforeEach(function() {
  151. this.cachePath = "path/to/cached/url";
  152. this.destPath = "path/to/destination";
  153. this.UrlCache._copyFile = sinon.stub().callsArg(2);
  154. this.UrlCache._ensureUrlIsInCache = sinon.stub().callsArgWith(3, null, this.cachePath);
  155. return this.UrlCache.downloadUrlToFile(this.project_id, this.url, this.destPath, this.lastModified, this.callback);
  156. });
  157. it("should ensure the URL is downloaded and updated in the cache", function() {
  158. return this.UrlCache._ensureUrlIsInCache
  159. .calledWith(this.project_id, this.url, this.lastModified)
  160. .should.equal(true);
  161. });
  162. it("should copy the file to the new location", function() {
  163. return this.UrlCache._copyFile
  164. .calledWith(this.cachePath, this.destPath)
  165. .should.equal(true);
  166. });
  167. return it("should call the callback", function() {
  168. return this.callback.called.should.equal(true);
  169. });
  170. });
  171. describe("_deleteUrlCacheFromDisk", function() {
  172. beforeEach(function() {
  173. this.fs.unlink = sinon.stub().callsArg(1);
  174. return this.UrlCache._deleteUrlCacheFromDisk(this.project_id, this.url, this.callback);
  175. });
  176. it("should delete the cache file", function() {
  177. return this.fs.unlink
  178. .calledWith(this.UrlCache._cacheFilePathForUrl(this.project_id, this.url))
  179. .should.equal(true);
  180. });
  181. return it("should call the callback", function() {
  182. return this.callback.called.should.equal(true);
  183. });
  184. });
  185. describe("_clearUrlFromCache", function() {
  186. beforeEach(function() {
  187. this.UrlCache._deleteUrlCacheFromDisk = sinon.stub().callsArg(2);
  188. this.UrlCache._clearUrlDetails = sinon.stub().callsArg(2);
  189. return this.UrlCache._clearUrlFromCache(this.project_id, this.url, this.callback);
  190. });
  191. it("should delete the file on disk", function() {
  192. return this.UrlCache._deleteUrlCacheFromDisk
  193. .calledWith(this.project_id, this.url)
  194. .should.equal(true);
  195. });
  196. it("should clear the entry in the database", function() {
  197. return this.UrlCache._clearUrlDetails
  198. .calledWith(this.project_id, this.url)
  199. .should.equal(true);
  200. });
  201. return it("should call the callback", function() {
  202. return this.callback.called.should.equal(true);
  203. });
  204. });
  205. return describe("clearProject", function() {
  206. beforeEach(function() {
  207. this.urls = [
  208. "www.example.com/file1",
  209. "www.example.com/file2"
  210. ];
  211. this.UrlCache._findAllUrlsInProject = sinon.stub().callsArgWith(1, null, this.urls);
  212. this.UrlCache._clearUrlFromCache = sinon.stub().callsArg(2);
  213. return this.UrlCache.clearProject(this.project_id, this.callback);
  214. });
  215. it("should clear the cache for each url in the project", function() {
  216. return Array.from(this.urls).map((url) =>
  217. this.UrlCache._clearUrlFromCache
  218. .calledWith(this.project_id, url)
  219. .should.equal(true));
  220. });
  221. return it("should call the callback", function() {
  222. return this.callback.called.should.equal(true);
  223. });
  224. });
  225. });