HistoryManagerTests.js 10 KB

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