ProjectCollabratecDetailsTest.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515
  1. /* eslint-disable
  2. max-len,
  3. mocha/no-identical-title,
  4. no-return-assign,
  5. no-unused-vars,
  6. */
  7. // TODO: This file was created by bulk-decaffeinate.
  8. // Fix any style issues and re-enable lint.
  9. /*
  10. * decaffeinate suggestions:
  11. * DS102: Remove unnecessary code created because of implicit returns
  12. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  13. */
  14. const { ObjectId } = require('mongodb')
  15. const Path = require('path')
  16. const SandboxedModule = require('sandboxed-module')
  17. const assert = require('assert')
  18. const chai = require('chai')
  19. const sinon = require('sinon')
  20. const { expect } = chai
  21. const modulePath = Path.join(
  22. __dirname,
  23. '../../../../app/src/Features/Project/ProjectCollabratecDetailsHandler'
  24. )
  25. describe('ProjectCollabratecDetailsHandler', function() {
  26. beforeEach(function() {
  27. this.projectId = ObjectId('5bea8747c7bba6012fcaceb3')
  28. this.userId = ObjectId('5be316a9c7f6aa03802ea8fb')
  29. this.userId2 = ObjectId('5c1794b3f0e89b1d1c577eca')
  30. this.ProjectModel = {}
  31. this.ProjectCollabratecDetailsHandler = SandboxedModule.require(
  32. modulePath,
  33. {
  34. globals: {
  35. console: console
  36. },
  37. requires: {
  38. mongodb: { ObjectId },
  39. '../../models/Project': { Project: this.ProjectModel }
  40. }
  41. }
  42. )
  43. return (this.callback = sinon.stub())
  44. })
  45. describe('initializeCollabratecProject', function() {
  46. describe('when update succeeds', function() {
  47. beforeEach(function() {
  48. this.ProjectModel.update = sinon.stub().yields()
  49. return this.ProjectCollabratecDetailsHandler.initializeCollabratecProject(
  50. this.projectId,
  51. this.userId,
  52. 'collabratec-document-id',
  53. 'collabratec-private-group-id',
  54. this.callback
  55. )
  56. })
  57. it('should update project model', function() {
  58. const update = {
  59. $set: {
  60. collabratecUsers: [
  61. {
  62. user_id: this.userId,
  63. collabratec_document_id: 'collabratec-document-id',
  64. collabratec_privategroup_id: 'collabratec-private-group-id'
  65. }
  66. ]
  67. }
  68. }
  69. return expect(this.ProjectModel.update).to.have.been.calledWith(
  70. { _id: this.projectId },
  71. update,
  72. this.callback
  73. )
  74. })
  75. })
  76. describe('when update has error', function() {
  77. beforeEach(function() {
  78. this.ProjectModel.update = sinon.stub().yields('error')
  79. return this.ProjectCollabratecDetailsHandler.initializeCollabratecProject(
  80. this.projectId,
  81. this.userId,
  82. 'collabratec-document-id',
  83. 'collabratec-private-group-id',
  84. this.callback
  85. )
  86. })
  87. it('should callback with error', function() {
  88. return expect(this.callback).to.have.been.calledWith('error')
  89. })
  90. })
  91. describe('with invalid args', function() {
  92. beforeEach(function() {
  93. this.ProjectModel.update = sinon.stub()
  94. return this.ProjectCollabratecDetailsHandler.initializeCollabratecProject(
  95. 'bad-project-id',
  96. 'bad-user-id',
  97. 'collabratec-document-id',
  98. 'collabratec-private-group-id',
  99. this.callback
  100. )
  101. })
  102. it('should not update', function() {
  103. return expect(this.ProjectModel.update).not.to.have.been.called
  104. })
  105. it('should callback with error', function() {
  106. return expect(this.callback.firstCall.args[0]).to.be.instanceOf(Error)
  107. })
  108. })
  109. })
  110. describe('isLinkedCollabratecUserProject', function() {
  111. beforeEach(function() {
  112. return (this.ProjectModel.findOne = sinon.stub().yields())
  113. })
  114. describe('when find succeeds', function() {
  115. describe('when user project found', function() {
  116. beforeEach(function() {
  117. this.ProjectModel.findOne = sinon.stub().yields(null, 'project')
  118. return this.ProjectCollabratecDetailsHandler.isLinkedCollabratecUserProject(
  119. this.projectId,
  120. this.userId,
  121. this.callback
  122. )
  123. })
  124. it('should call find with project and user id', function() {
  125. return expect(this.ProjectModel.findOne).to.have.been.calledWithMatch(
  126. {
  127. _id: ObjectId(this.projectId),
  128. collabratecUsers: {
  129. $elemMatch: {
  130. user_id: ObjectId(this.userId)
  131. }
  132. }
  133. }
  134. )
  135. })
  136. it('should callback with true', function() {
  137. return expect(this.callback).to.have.been.calledWith(null, true)
  138. })
  139. })
  140. describe('when user project found', function() {
  141. beforeEach(function() {
  142. this.ProjectModel.findOne = sinon.stub().yields(null, null)
  143. return this.ProjectCollabratecDetailsHandler.isLinkedCollabratecUserProject(
  144. this.projectId,
  145. this.userId,
  146. this.callback
  147. )
  148. })
  149. it('should callback with false', function() {
  150. return expect(this.callback).to.have.been.calledWith(null, false)
  151. })
  152. })
  153. })
  154. describe('when find has error', function() {
  155. beforeEach(function() {
  156. this.ProjectModel.findOne = sinon.stub().yields('error')
  157. return this.ProjectCollabratecDetailsHandler.isLinkedCollabratecUserProject(
  158. this.projectId,
  159. this.userId,
  160. this.callback
  161. )
  162. })
  163. it('should callback with error', function() {
  164. return expect(this.callback).to.have.been.calledWith('error')
  165. })
  166. })
  167. describe('with invalid args', function() {
  168. beforeEach(function() {
  169. this.ProjectModel.findOne = sinon.stub()
  170. return this.ProjectCollabratecDetailsHandler.isLinkedCollabratecUserProject(
  171. 'bad-project-id',
  172. 'bad-user-id',
  173. this.callback
  174. )
  175. })
  176. it('should not update', function() {
  177. return expect(this.ProjectModel.findOne).not.to.have.been.called
  178. })
  179. it('should callback with error', function() {
  180. return expect(this.callback.firstCall.args[0]).to.be.instanceOf(Error)
  181. })
  182. })
  183. })
  184. describe('linkCollabratecUserProject', function() {
  185. describe('when update succeeds', function() {
  186. beforeEach(function() {
  187. this.ProjectModel.update = sinon.stub().yields()
  188. return this.ProjectCollabratecDetailsHandler.linkCollabratecUserProject(
  189. this.projectId,
  190. this.userId,
  191. 'collabratec-document-id',
  192. this.callback
  193. )
  194. })
  195. it('should update project model', function() {
  196. const query = {
  197. _id: this.projectId,
  198. collabratecUsers: {
  199. $not: {
  200. $elemMatch: {
  201. collabratec_document_id: 'collabratec-document-id',
  202. user_id: this.userId
  203. }
  204. }
  205. }
  206. }
  207. const update = {
  208. $push: {
  209. collabratecUsers: {
  210. collabratec_document_id: 'collabratec-document-id',
  211. user_id: this.userId
  212. }
  213. }
  214. }
  215. return expect(this.ProjectModel.update).to.have.been.calledWith(
  216. query,
  217. update,
  218. this.callback
  219. )
  220. })
  221. })
  222. describe('when update has error', function() {
  223. beforeEach(function() {
  224. this.ProjectModel.update = sinon.stub().yields('error')
  225. return this.ProjectCollabratecDetailsHandler.linkCollabratecUserProject(
  226. this.projectId,
  227. this.userId,
  228. 'collabratec-document-id',
  229. this.callback
  230. )
  231. })
  232. it('should callback with error', function() {
  233. return expect(this.callback).to.have.been.calledWith('error')
  234. })
  235. })
  236. describe('with invalid args', function() {
  237. beforeEach(function() {
  238. this.ProjectModel.update = sinon.stub()
  239. return this.ProjectCollabratecDetailsHandler.linkCollabratecUserProject(
  240. 'bad-project-id',
  241. 'bad-user-id',
  242. 'collabratec-document-id',
  243. this.callback
  244. )
  245. })
  246. it('should not update', function() {
  247. return expect(this.ProjectModel.update).not.to.have.been.called
  248. })
  249. it('should callback with error', function() {
  250. return expect(this.callback.firstCall.args[0]).to.be.instanceOf(Error)
  251. })
  252. })
  253. })
  254. describe('setCollabratecUsers', function() {
  255. beforeEach(function() {
  256. return (this.collabratecUsers = [
  257. {
  258. user_id: this.userId,
  259. collabratec_document_id: 'collabratec-document-id-1',
  260. collabratec_privategroup_id: 'collabratec-private-group-id-1'
  261. },
  262. {
  263. user_id: this.userId2,
  264. collabratec_document_id: 'collabratec-document-id-2',
  265. collabratec_privategroup_id: 'collabratec-private-group-id-2'
  266. }
  267. ])
  268. })
  269. describe('when update succeeds', function() {
  270. beforeEach(function() {
  271. this.ProjectModel.update = sinon.stub().yields()
  272. return this.ProjectCollabratecDetailsHandler.setCollabratecUsers(
  273. this.projectId,
  274. this.collabratecUsers,
  275. this.callback
  276. )
  277. })
  278. it('should update project model', function() {
  279. const update = {
  280. $set: {
  281. collabratecUsers: this.collabratecUsers
  282. }
  283. }
  284. return expect(this.ProjectModel.update).to.have.been.calledWith(
  285. { _id: this.projectId },
  286. update,
  287. this.callback
  288. )
  289. })
  290. })
  291. describe('when update has error', function() {
  292. beforeEach(function() {
  293. this.ProjectModel.update = sinon.stub().yields('error')
  294. return this.ProjectCollabratecDetailsHandler.setCollabratecUsers(
  295. this.projectId,
  296. this.collabratecUsers,
  297. this.callback
  298. )
  299. })
  300. it('should callback with error', function() {
  301. return expect(this.callback).to.have.been.calledWith('error')
  302. })
  303. })
  304. describe('with invalid project_id', function() {
  305. beforeEach(function() {
  306. this.ProjectModel.update = sinon.stub()
  307. return this.ProjectCollabratecDetailsHandler.setCollabratecUsers(
  308. 'bad-project-id',
  309. this.collabratecUsers,
  310. this.callback
  311. )
  312. })
  313. it('should not update', function() {
  314. return expect(this.ProjectModel.update).not.to.have.been.called
  315. })
  316. it('should callback with error', function() {
  317. return expect(this.callback.firstCall.args[0]).to.be.instanceOf(Error)
  318. })
  319. })
  320. describe('with invalid user_id', function() {
  321. beforeEach(function() {
  322. this.collabratecUsers[1].user_id = 'bad-user-id'
  323. this.ProjectModel.update = sinon.stub()
  324. return this.ProjectCollabratecDetailsHandler.setCollabratecUsers(
  325. this.projectId,
  326. this.collabratecUsers,
  327. this.callback
  328. )
  329. })
  330. it('should not update', function() {
  331. return expect(this.ProjectModel.update).not.to.have.been.called
  332. })
  333. it('should callback with error', function() {
  334. return expect(this.callback.firstCall.args[0]).to.be.instanceOf(Error)
  335. })
  336. })
  337. })
  338. describe('unlinkCollabratecUserProject', function() {
  339. describe('when update succeeds', function() {
  340. beforeEach(function() {
  341. this.ProjectModel.update = sinon.stub().yields()
  342. return this.ProjectCollabratecDetailsHandler.unlinkCollabratecUserProject(
  343. this.projectId,
  344. this.userId,
  345. this.callback
  346. )
  347. })
  348. it('should update project model', function() {
  349. const query = { _id: this.projectId }
  350. const update = {
  351. $pull: {
  352. collabratecUsers: {
  353. user_id: this.userId
  354. }
  355. }
  356. }
  357. return expect(this.ProjectModel.update).to.have.been.calledWith(
  358. query,
  359. update,
  360. this.callback
  361. )
  362. })
  363. })
  364. describe('when update has error', function() {
  365. beforeEach(function() {
  366. this.ProjectModel.update = sinon.stub().yields('error')
  367. return this.ProjectCollabratecDetailsHandler.unlinkCollabratecUserProject(
  368. this.projectId,
  369. this.userId,
  370. this.callback
  371. )
  372. })
  373. it('should callback with error', function() {
  374. return expect(this.callback).to.have.been.calledWith('error')
  375. })
  376. })
  377. describe('with invalid args', function() {
  378. beforeEach(function() {
  379. this.ProjectModel.update = sinon.stub()
  380. return this.ProjectCollabratecDetailsHandler.unlinkCollabratecUserProject(
  381. 'bad-project-id',
  382. 'bad-user-id',
  383. this.callback
  384. )
  385. })
  386. it('should not update', function() {
  387. return expect(this.ProjectModel.update).not.to.have.been.called
  388. })
  389. it('should callback with error', function() {
  390. return expect(this.callback.firstCall.args[0]).to.be.instanceOf(Error)
  391. })
  392. })
  393. })
  394. describe('updateCollabratecUserIds', function() {
  395. describe('when update succeeds', function() {
  396. beforeEach(function() {
  397. this.ProjectModel.update = sinon.stub().yields()
  398. return this.ProjectCollabratecDetailsHandler.updateCollabratecUserIds(
  399. this.userId,
  400. this.userId2,
  401. this.callback
  402. )
  403. })
  404. it('should update project model', function() {
  405. return expect(this.ProjectModel.update).to.have.been.calledWith(
  406. { 'collabratecUsers.user_id': this.userId },
  407. { $set: { 'collabratecUsers.$.user_id': this.userId2 } },
  408. { multi: true },
  409. this.callback
  410. )
  411. })
  412. })
  413. describe('when update has error', function() {
  414. beforeEach(function() {
  415. this.ProjectModel.update = sinon.stub().yields('error')
  416. return this.ProjectCollabratecDetailsHandler.updateCollabratecUserIds(
  417. this.userId,
  418. this.userId2,
  419. this.callback
  420. )
  421. })
  422. it('should callback with error', function() {
  423. return expect(this.callback).to.have.been.calledWith('error')
  424. })
  425. })
  426. describe('with invalid old_user_id', function() {
  427. beforeEach(function() {
  428. this.ProjectModel.update = sinon.stub()
  429. return this.ProjectCollabratecDetailsHandler.updateCollabratecUserIds(
  430. 'bad-user-id',
  431. this.userId2,
  432. this.callback
  433. )
  434. })
  435. it('should not update', function() {
  436. return expect(this.ProjectModel.update).not.to.have.been.called
  437. })
  438. it('should callback with error', function() {
  439. return expect(this.callback.firstCall.args[0]).to.be.instanceOf(Error)
  440. })
  441. })
  442. describe('with invalid new_user_id', function() {
  443. beforeEach(function() {
  444. this.ProjectModel.update = sinon.stub()
  445. return this.ProjectCollabratecDetailsHandler.updateCollabratecUserIds(
  446. this.userId,
  447. 'bad-user-id',
  448. this.callback
  449. )
  450. })
  451. it('should not update', function() {
  452. return expect(this.ProjectModel.update).not.to.have.been.called
  453. })
  454. it('should callback with error', function() {
  455. return expect(this.callback.firstCall.args[0]).to.be.instanceOf(Error)
  456. })
  457. })
  458. })
  459. })