HistoryManagerTests.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. const { expect } = require('chai')
  2. const sinon = require('sinon')
  3. const SandboxedModule = require('sandboxed-module')
  4. const { ObjectId } = require('mongodb-legacy')
  5. const {
  6. cleanupTestDatabase,
  7. db,
  8. waitForDb,
  9. } = require('../../../../app/src/infrastructure/mongodb')
  10. const MODULE_PATH = '../../../../app/src/Features/History/HistoryManager'
  11. const GLOBAL_BLOBS = [
  12. 'e69de29bb2d1d6434b8b29ae775ad8c2e48c5391',
  13. '02426c2b3a484003ca42ed52b374b7907b757d12',
  14. ]
  15. describe('HistoryManager', function () {
  16. before(async function () {
  17. await waitForDb()
  18. })
  19. before(cleanupTestDatabase)
  20. before(async function () {
  21. await db.projectHistoryGlobalBlobs.insertMany(
  22. GLOBAL_BLOBS.map(sha => ({
  23. _id: sha,
  24. byteLength: 0,
  25. stringLength: 0,
  26. }))
  27. )
  28. })
  29. beforeEach(function () {
  30. this.user_id = 'user-id-123'
  31. this.historyId = new ObjectId().toString()
  32. this.AuthenticationController = {
  33. getLoggedInUserId: sinon.stub().returns(this.user_id),
  34. }
  35. this.FetchUtils = {
  36. fetchJson: sinon.stub(),
  37. fetchNothing: sinon.stub().resolves(),
  38. }
  39. this.projectHistoryUrl = 'http://project_history.example.com'
  40. this.v1HistoryUrl = 'http://v1_history.example.com'
  41. this.v1HistoryUser = 'system'
  42. this.v1HistoryPassword = 'verysecret'
  43. this.settings = {
  44. apis: {
  45. filestore: {
  46. url: 'http://filestore.example.com',
  47. },
  48. project_history: {
  49. url: this.projectHistoryUrl,
  50. },
  51. v1_history: {
  52. url: this.v1HistoryUrl,
  53. user: this.v1HistoryUser,
  54. pass: this.v1HistoryPassword,
  55. buckets: {
  56. globalBlobs: 'globalBlobs',
  57. projectBlobs: 'projectBlobs',
  58. },
  59. },
  60. },
  61. }
  62. this.UserGetter = {
  63. promises: {
  64. getUsersByV1Ids: sinon.stub(),
  65. getUsers: sinon.stub(),
  66. },
  67. }
  68. this.project = {
  69. overleaf: {
  70. history: {
  71. id: this.historyId,
  72. },
  73. },
  74. }
  75. this.ProjectGetter = {
  76. promises: {
  77. getProject: sinon.stub().resolves(this.project),
  78. },
  79. }
  80. this.HistoryBackupDeletionHandler = {
  81. deleteProject: sinon.stub().resolves(),
  82. }
  83. this.HistoryManager = SandboxedModule.require(MODULE_PATH, {
  84. requires: {
  85. '../../infrastructure/mongodb': { ObjectId, db, waitForDb },
  86. '@overleaf/fetch-utils': this.FetchUtils,
  87. '@overleaf/settings': this.settings,
  88. '../User/UserGetter': this.UserGetter,
  89. '../Project/ProjectGetter': this.ProjectGetter,
  90. './HistoryBackupDeletionHandler': this.HistoryBackupDeletionHandler,
  91. },
  92. })
  93. })
  94. describe('getFilestoreBlobURL', function () {
  95. beforeEach(async function () {
  96. await this.HistoryManager.loadGlobalBlobsPromise
  97. })
  98. it('should return a global blob location', function () {
  99. for (const sha of GLOBAL_BLOBS) {
  100. expect(this.HistoryManager.getFilestoreBlobURL('42', sha)).to.equal(
  101. `${this.settings.apis.filestore.url}/history/global/hash/${sha}`
  102. )
  103. }
  104. })
  105. it('should return a project blob location for a v1 project', function () {
  106. const historyId = 42
  107. const sha = '6ddfa0578a67fe5ad6623a8665ec9aafce1eb5ca'
  108. expect(this.HistoryManager.getFilestoreBlobURL(historyId, sha)).to.equal(
  109. `${this.settings.apis.filestore.url}/history/project/${historyId}/hash/${sha}`
  110. )
  111. })
  112. it('should return a project blob location for a mongo project', function () {
  113. const historyId = '424242424242424242424242'
  114. const sha = '6ddfa0578a67fe5ad6623a8665ec9aafce1eb5ca'
  115. expect(this.HistoryManager.getFilestoreBlobURL(historyId, sha)).to.equal(
  116. `${this.settings.apis.filestore.url}/history/project/${historyId}/hash/${sha}`
  117. )
  118. })
  119. })
  120. describe('initializeProject', function () {
  121. beforeEach(function () {
  122. this.settings.apis.project_history.initializeHistoryForNewProjects = true
  123. })
  124. describe('project history returns a successful response', function () {
  125. beforeEach(async function () {
  126. this.FetchUtils.fetchJson.resolves({ project: { id: this.historyId } })
  127. this.result = await this.HistoryManager.promises.initializeProject(
  128. this.historyId
  129. )
  130. })
  131. it('should call the project history api', function () {
  132. this.FetchUtils.fetchJson.should.have.been.calledWithMatch(
  133. `${this.settings.apis.project_history.url}/project`,
  134. { method: 'POST' }
  135. )
  136. })
  137. it('should return the overleaf id', function () {
  138. expect(this.result).to.equal(this.historyId)
  139. })
  140. })
  141. describe('project history returns a response without the project id', function () {
  142. it('should throw an error', async function () {
  143. this.FetchUtils.fetchJson.resolves({ project: {} })
  144. await expect(
  145. this.HistoryManager.promises.initializeProject(this.historyId)
  146. ).to.be.rejected
  147. })
  148. })
  149. describe('project history errors', function () {
  150. it('should propagate the error', async function () {
  151. this.FetchUtils.fetchJson.rejects(new Error('problem connecting'))
  152. await expect(
  153. this.HistoryManager.promises.initializeProject(this.historyId)
  154. ).to.be.rejected
  155. })
  156. })
  157. })
  158. describe('injectUserDetails', function () {
  159. beforeEach(function () {
  160. this.user1 = {
  161. _id: (this.user_id1 = '123456'),
  162. first_name: 'Jane',
  163. last_name: 'Doe',
  164. email: 'jane@example.com',
  165. overleaf: { id: 5011 },
  166. }
  167. this.user1_view = {
  168. id: this.user_id1,
  169. first_name: 'Jane',
  170. last_name: 'Doe',
  171. email: 'jane@example.com',
  172. }
  173. this.user2 = {
  174. _id: (this.user_id2 = 'abcdef'),
  175. first_name: 'John',
  176. last_name: 'Doe',
  177. email: 'john@example.com',
  178. }
  179. this.user2_view = {
  180. id: this.user_id2,
  181. first_name: 'John',
  182. last_name: 'Doe',
  183. email: 'john@example.com',
  184. }
  185. this.UserGetter.promises.getUsersByV1Ids.resolves([this.user1])
  186. this.UserGetter.promises.getUsers.resolves([this.user1, this.user2])
  187. })
  188. describe('with a diff', function () {
  189. it('should turn user_ids into user objects', async function () {
  190. const diff = await this.HistoryManager.promises.injectUserDetails({
  191. diff: [
  192. {
  193. i: 'foo',
  194. meta: {
  195. users: [this.user_id1],
  196. },
  197. },
  198. {
  199. i: 'bar',
  200. meta: {
  201. users: [this.user_id2],
  202. },
  203. },
  204. ],
  205. })
  206. expect(diff.diff[0].meta.users).to.deep.equal([this.user1_view])
  207. expect(diff.diff[1].meta.users).to.deep.equal([this.user2_view])
  208. })
  209. it('should handle v1 user ids', async function () {
  210. const diff = await this.HistoryManager.promises.injectUserDetails({
  211. diff: [
  212. {
  213. i: 'foo',
  214. meta: {
  215. users: [5011],
  216. },
  217. },
  218. {
  219. i: 'bar',
  220. meta: {
  221. users: [this.user_id2],
  222. },
  223. },
  224. ],
  225. })
  226. expect(diff.diff[0].meta.users).to.deep.equal([this.user1_view])
  227. expect(diff.diff[1].meta.users).to.deep.equal([this.user2_view])
  228. })
  229. it('should leave user objects', async function () {
  230. const diff = await this.HistoryManager.promises.injectUserDetails({
  231. diff: [
  232. {
  233. i: 'foo',
  234. meta: {
  235. users: [this.user1_view],
  236. },
  237. },
  238. {
  239. i: 'bar',
  240. meta: {
  241. users: [this.user_id2],
  242. },
  243. },
  244. ],
  245. })
  246. expect(diff.diff[0].meta.users).to.deep.equal([this.user1_view])
  247. expect(diff.diff[1].meta.users).to.deep.equal([this.user2_view])
  248. })
  249. it('should handle a binary diff marker', async function () {
  250. const diff = await this.HistoryManager.promises.injectUserDetails({
  251. diff: { binary: true },
  252. })
  253. expect(diff.diff.binary).to.be.true
  254. })
  255. })
  256. describe('with a list of updates', function () {
  257. it('should turn user_ids into user objects', async function () {
  258. const updates = await this.HistoryManager.promises.injectUserDetails({
  259. updates: [
  260. {
  261. fromV: 5,
  262. toV: 8,
  263. meta: {
  264. users: [this.user_id1],
  265. },
  266. },
  267. {
  268. fromV: 4,
  269. toV: 5,
  270. meta: {
  271. users: [this.user_id2],
  272. },
  273. },
  274. ],
  275. })
  276. expect(updates.updates[0].meta.users).to.deep.equal([this.user1_view])
  277. expect(updates.updates[1].meta.users).to.deep.equal([this.user2_view])
  278. })
  279. it('should leave user objects', async function () {
  280. const updates = await this.HistoryManager.promises.injectUserDetails({
  281. updates: [
  282. {
  283. fromV: 5,
  284. toV: 8,
  285. meta: {
  286. users: [this.user1_view],
  287. },
  288. },
  289. {
  290. fromV: 4,
  291. toV: 5,
  292. meta: {
  293. users: [this.user_id2],
  294. },
  295. },
  296. ],
  297. })
  298. expect(updates.updates[0].meta.users).to.deep.equal([this.user1_view])
  299. expect(updates.updates[1].meta.users).to.deep.equal([this.user2_view])
  300. })
  301. })
  302. })
  303. describe('deleteProject', function () {
  304. const projectId = new ObjectId()
  305. const historyId = new ObjectId()
  306. beforeEach(async function () {
  307. await this.HistoryManager.promises.deleteProject(projectId, historyId)
  308. })
  309. it('should call the project-history service', async function () {
  310. expect(this.FetchUtils.fetchNothing).to.have.been.calledWith(
  311. `${this.projectHistoryUrl}/project/${projectId}`,
  312. { method: 'DELETE' }
  313. )
  314. })
  315. it('should call the v1-history service', async function () {
  316. expect(this.FetchUtils.fetchNothing).to.have.been.calledWith(
  317. `${this.v1HistoryUrl}/projects/${historyId}`,
  318. {
  319. method: 'DELETE',
  320. basicAuth: {
  321. user: this.v1HistoryUser,
  322. password: this.v1HistoryPassword,
  323. },
  324. }
  325. )
  326. })
  327. it('should call the history-backup-deletion service', async function () {
  328. expect(
  329. this.HistoryBackupDeletionHandler.deleteProject
  330. ).to.have.been.calledWith(projectId)
  331. })
  332. })
  333. })