ProjectCollabratecDetailsTest.js 15 KB

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