ProjectCollabratecDetailsTest.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  1. const { ObjectId } = require('mongodb')
  2. const Path = require('path')
  3. const SandboxedModule = require('sandboxed-module')
  4. const { expect } = require('chai')
  5. const sinon = require('sinon')
  6. const modulePath = Path.join(
  7. __dirname,
  8. '../../../../app/src/Features/Project/ProjectCollabratecDetailsHandler'
  9. )
  10. describe('ProjectCollabratecDetailsHandler', function () {
  11. beforeEach(function () {
  12. this.projectId = new ObjectId('5bea8747c7bba6012fcaceb3')
  13. this.userId = new ObjectId('5be316a9c7f6aa03802ea8fb')
  14. this.userId2 = new ObjectId('5c1794b3f0e89b1d1c577eca')
  15. this.ProjectModel = {}
  16. this.ProjectCollabratecDetailsHandler = SandboxedModule.require(
  17. modulePath,
  18. {
  19. requires: {
  20. mongodb: { ObjectId },
  21. '../../models/Project': { Project: this.ProjectModel },
  22. },
  23. }
  24. )
  25. })
  26. describe('initializeCollabratecProject', function () {
  27. describe('when update succeeds', function () {
  28. beforeEach(async function () {
  29. this.ProjectModel.updateOne = sinon
  30. .stub()
  31. .returns({ exec: sinon.stub().resolves() })
  32. await this.ProjectCollabratecDetailsHandler.promises.initializeCollabratecProject(
  33. this.projectId,
  34. this.userId,
  35. 'collabratec-document-id',
  36. 'collabratec-private-group-id'
  37. )
  38. })
  39. it('should update project model', function () {
  40. const update = {
  41. $set: {
  42. collabratecUsers: [
  43. {
  44. user_id: this.userId,
  45. collabratec_document_id: 'collabratec-document-id',
  46. collabratec_privategroup_id: 'collabratec-private-group-id',
  47. },
  48. ],
  49. },
  50. }
  51. expect(this.ProjectModel.updateOne).to.have.been.calledWith(
  52. { _id: this.projectId },
  53. update
  54. )
  55. })
  56. })
  57. describe('when update has error', function () {
  58. beforeEach(function () {
  59. this.ProjectModel.updateOne = sinon
  60. .stub()
  61. .returns({ exec: sinon.stub().rejects() })
  62. })
  63. it('should be rejected', async function () {
  64. await expect(
  65. this.ProjectCollabratecDetailsHandler.promises.initializeCollabratecProject(
  66. this.projectId,
  67. this.userId,
  68. 'collabratec-document-id',
  69. 'collabratec-private-group-id'
  70. )
  71. ).to.be.rejected
  72. })
  73. })
  74. describe('with invalid args', function () {
  75. beforeEach(function () {
  76. this.ProjectModel.updateOne = sinon
  77. .stub()
  78. .returns({ exec: sinon.stub().resolves() })
  79. this.resultPromise =
  80. this.ProjectCollabratecDetailsHandler.promises.initializeCollabratecProject(
  81. 'bad-project-id',
  82. 'bad-user-id',
  83. 'collabratec-document-id',
  84. 'collabratec-private-group-id'
  85. )
  86. })
  87. it('should be rejected without updating', async function () {
  88. await expect(this.resultPromise).to.be.rejected
  89. expect(this.ProjectModel.updateOne).not.to.have.been.called
  90. })
  91. })
  92. })
  93. describe('isLinkedCollabratecUserProject', function () {
  94. beforeEach(function () {
  95. this.ProjectModel.findOne = sinon.stub().resolves()
  96. })
  97. describe('when find succeeds', function () {
  98. describe('when user project found', function () {
  99. beforeEach(async function () {
  100. this.ProjectModel.findOne = sinon
  101. .stub()
  102. .returns({ exec: sinon.stub().resolves('project') })
  103. this.result =
  104. await this.ProjectCollabratecDetailsHandler.promises.isLinkedCollabratecUserProject(
  105. this.projectId,
  106. this.userId
  107. )
  108. })
  109. it('should call find with project and user id', function () {
  110. expect(this.ProjectModel.findOne).to.have.been.calledWithMatch({
  111. _id: new ObjectId(this.projectId),
  112. collabratecUsers: {
  113. $elemMatch: {
  114. user_id: new ObjectId(this.userId),
  115. },
  116. },
  117. })
  118. })
  119. it('should return true', function () {
  120. expect(this.result).to.equal(true)
  121. })
  122. })
  123. describe('when user project is not found', function () {
  124. beforeEach(async function () {
  125. this.ProjectModel.findOne = sinon
  126. .stub()
  127. .returns({ exec: sinon.stub().resolves(null) })
  128. this.result =
  129. await this.ProjectCollabratecDetailsHandler.promises.isLinkedCollabratecUserProject(
  130. this.projectId,
  131. this.userId
  132. )
  133. })
  134. it('should return false', function () {
  135. expect(this.result).to.equal(false)
  136. })
  137. })
  138. })
  139. describe('when find has error', function () {
  140. beforeEach(function () {
  141. this.ProjectModel.findOne = sinon
  142. .stub()
  143. .returns({ exec: sinon.stub().rejects() })
  144. })
  145. it('should be rejected', async function () {
  146. await expect(
  147. this.ProjectCollabratecDetailsHandler.promises.isLinkedCollabratecUserProject(
  148. this.projectId,
  149. this.userId
  150. )
  151. ).to.be.rejected
  152. })
  153. })
  154. describe('with invalid args', function () {
  155. beforeEach(function () {
  156. this.ProjectModel.findOne = sinon
  157. .stub()
  158. .returns({ exec: sinon.stub().resolves() })
  159. this.resultPromise =
  160. this.ProjectCollabratecDetailsHandler.promises.isLinkedCollabratecUserProject(
  161. 'bad-project-id',
  162. 'bad-user-id'
  163. )
  164. })
  165. it('should be rejected without updating', async function () {
  166. await expect(this.resultPromise).to.be.rejected
  167. expect(this.ProjectModel.findOne).not.to.have.been.called
  168. })
  169. })
  170. })
  171. describe('linkCollabratecUserProject', function () {
  172. describe('when update succeeds', function () {
  173. beforeEach(async function () {
  174. this.ProjectModel.updateOne = sinon
  175. .stub()
  176. .returns({ exec: sinon.stub().resolves() })
  177. await this.ProjectCollabratecDetailsHandler.promises.linkCollabratecUserProject(
  178. this.projectId,
  179. this.userId,
  180. 'collabratec-document-id'
  181. )
  182. })
  183. it('should update project model', function () {
  184. const query = {
  185. _id: this.projectId,
  186. collabratecUsers: {
  187. $not: {
  188. $elemMatch: {
  189. collabratec_document_id: 'collabratec-document-id',
  190. user_id: this.userId,
  191. },
  192. },
  193. },
  194. }
  195. const update = {
  196. $push: {
  197. collabratecUsers: {
  198. collabratec_document_id: 'collabratec-document-id',
  199. user_id: this.userId,
  200. },
  201. },
  202. }
  203. expect(this.ProjectModel.updateOne).to.have.been.calledWith(
  204. query,
  205. update
  206. )
  207. })
  208. })
  209. describe('when update has error', function () {
  210. beforeEach(function () {
  211. this.ProjectModel.updateOne = sinon
  212. .stub()
  213. .returns({ exec: sinon.stub().rejects() })
  214. })
  215. it('should be rejected', async function () {
  216. await expect(
  217. this.ProjectCollabratecDetailsHandler.promises.linkCollabratecUserProject(
  218. this.projectId,
  219. this.userId,
  220. 'collabratec-document-id'
  221. )
  222. ).to.be.rejected
  223. })
  224. })
  225. describe('with invalid args', function () {
  226. beforeEach(function () {
  227. this.ProjectModel.updateOne = sinon
  228. .stub()
  229. .returns({ exec: sinon.stub().resolves() })
  230. this.resultPromise =
  231. this.ProjectCollabratecDetailsHandler.promises.linkCollabratecUserProject(
  232. 'bad-project-id',
  233. 'bad-user-id',
  234. 'collabratec-document-id'
  235. )
  236. })
  237. it('should be rejected without updating', async function () {
  238. await expect(this.resultPromise).to.be.rejected
  239. expect(this.ProjectModel.updateOne).not.to.have.been.called
  240. })
  241. })
  242. })
  243. describe('setCollabratecUsers', function () {
  244. beforeEach(function () {
  245. this.collabratecUsers = [
  246. {
  247. user_id: this.userId,
  248. collabratec_document_id: 'collabratec-document-id-1',
  249. collabratec_privategroup_id: 'collabratec-private-group-id-1',
  250. },
  251. {
  252. user_id: this.userId2,
  253. collabratec_document_id: 'collabratec-document-id-2',
  254. collabratec_privategroup_id: 'collabratec-private-group-id-2',
  255. },
  256. ]
  257. })
  258. describe('when update succeeds', function () {
  259. beforeEach(async function () {
  260. this.ProjectModel.updateOne = sinon
  261. .stub()
  262. .returns({ exec: sinon.stub().resolves() })
  263. await this.ProjectCollabratecDetailsHandler.promises.setCollabratecUsers(
  264. this.projectId,
  265. this.collabratecUsers
  266. )
  267. })
  268. it('should update project model', function () {
  269. const update = {
  270. $set: {
  271. collabratecUsers: this.collabratecUsers,
  272. },
  273. }
  274. expect(this.ProjectModel.updateOne).to.have.been.calledWith(
  275. { _id: this.projectId },
  276. update
  277. )
  278. })
  279. })
  280. describe('when update has error', function () {
  281. beforeEach(function () {
  282. this.ProjectModel.updateOne = sinon
  283. .stub()
  284. .returns({ exec: sinon.stub().rejects() })
  285. })
  286. it('should be rejected', async function () {
  287. await expect(
  288. this.ProjectCollabratecDetailsHandler.promises.setCollabratecUsers(
  289. this.projectId,
  290. this.collabratecUsers
  291. )
  292. ).to.be.rejected
  293. })
  294. })
  295. describe('with invalid project_id', function () {
  296. beforeEach(function () {
  297. this.ProjectModel.updateOne = sinon
  298. .stub()
  299. .returns({ exec: sinon.stub().resolves() })
  300. this.resultPromise =
  301. this.ProjectCollabratecDetailsHandler.promises.setCollabratecUsers(
  302. 'bad-project-id',
  303. this.collabratecUsers
  304. )
  305. })
  306. it('should be rejected without updating', async function () {
  307. await expect(this.resultPromise).to.be.rejected
  308. expect(this.ProjectModel.updateOne).not.to.have.been.called
  309. })
  310. })
  311. describe('with invalid user_id', function () {
  312. beforeEach(function () {
  313. this.collabratecUsers[1].user_id = 'bad-user-id'
  314. this.ProjectModel.updateOne = sinon
  315. .stub()
  316. .returns({ exec: sinon.stub().resolves() })
  317. this.resultPromise =
  318. this.ProjectCollabratecDetailsHandler.promises.setCollabratecUsers(
  319. this.projectId,
  320. this.collabratecUsers
  321. )
  322. })
  323. it('should be rejected without updating', async function () {
  324. await expect(this.resultPromise).to.be.rejected
  325. expect(this.ProjectModel.updateOne).not.to.have.been.called
  326. })
  327. })
  328. })
  329. describe('unlinkCollabratecUserProject', function () {
  330. describe('when update succeeds', function () {
  331. beforeEach(async function () {
  332. this.ProjectModel.updateOne = sinon
  333. .stub()
  334. .returns({ exec: sinon.stub().resolves() })
  335. await this.ProjectCollabratecDetailsHandler.promises.unlinkCollabratecUserProject(
  336. this.projectId,
  337. this.userId
  338. )
  339. })
  340. it('should update project model', function () {
  341. const query = { _id: this.projectId }
  342. const update = {
  343. $pull: {
  344. collabratecUsers: {
  345. user_id: this.userId,
  346. },
  347. },
  348. }
  349. expect(this.ProjectModel.updateOne).to.have.been.calledWith(
  350. query,
  351. update
  352. )
  353. })
  354. })
  355. describe('when update has error', function () {
  356. beforeEach(function () {
  357. this.ProjectModel.updateOne = sinon
  358. .stub()
  359. .returns({ exec: sinon.stub().rejects() })
  360. })
  361. it('should be rejected', async function () {
  362. await expect(
  363. this.ProjectCollabratecDetailsHandler.promises.unlinkCollabratecUserProject(
  364. this.projectId,
  365. this.userId
  366. )
  367. ).to.be.rejected
  368. })
  369. })
  370. describe('with invalid args', function () {
  371. beforeEach(function () {
  372. this.ProjectModel.updateOne = sinon
  373. .stub()
  374. .returns({ exec: sinon.stub().resolves() })
  375. this.resultPromise =
  376. this.ProjectCollabratecDetailsHandler.promises.unlinkCollabratecUserProject(
  377. 'bad-project-id',
  378. 'bad-user-id'
  379. )
  380. })
  381. it('should be rejected without updating', async function () {
  382. await expect(this.resultPromise).to.be.rejected
  383. expect(this.ProjectModel.updateOne).not.to.have.been.called
  384. })
  385. })
  386. })
  387. })