HistoryManagerTests.js 8.7 KB

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