UserEmailsControllerTests.js 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. /* eslint-disable
  2. max-len,
  3. no-return-assign,
  4. no-unused-vars,
  5. */
  6. // TODO: This file was created by bulk-decaffeinate.
  7. // Fix any style issues and re-enable lint.
  8. /*
  9. * decaffeinate suggestions:
  10. * DS102: Remove unnecessary code created because of implicit returns
  11. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  12. */
  13. const sinon = require('sinon')
  14. const assertCalledWith = sinon.assert.calledWith
  15. const assertNotCalled = sinon.assert.notCalled
  16. const chai = require('chai')
  17. const should = chai.should()
  18. const { assert } = chai
  19. const modulePath = '../../../../app/src/Features/User/UserEmailsController.js'
  20. const SandboxedModule = require('sandboxed-module')
  21. const MockRequest = require('../helpers/MockRequest')
  22. const MockResponse = require('../helpers/MockResponse')
  23. const Errors = require('../../../../app/src/Features/Errors/Errors')
  24. describe('UserEmailsController', function() {
  25. beforeEach(function() {
  26. this.req = new MockRequest()
  27. this.user = { _id: 'mock-user-id' }
  28. this.UserGetter = { getUserFullEmails: sinon.stub() }
  29. this.AuthenticationController = {
  30. getLoggedInUserId: sinon.stub().returns(this.user._id),
  31. setInSessionUser: sinon.stub()
  32. }
  33. this.UserUpdater = {
  34. addEmailAddress: sinon.stub(),
  35. removeEmailAddress: sinon.stub(),
  36. setDefaultEmailAddress: sinon.stub(),
  37. updateV1AndSetDefaultEmailAddress: sinon.stub()
  38. }
  39. this.EmailHelper = { parseEmail: sinon.stub() }
  40. this.endorseAffiliation = sinon.stub().yields()
  41. return (this.UserEmailsController = SandboxedModule.require(modulePath, {
  42. globals: {
  43. console: console
  44. },
  45. requires: {
  46. '../Authentication/AuthenticationController': this
  47. .AuthenticationController,
  48. './UserGetter': this.UserGetter,
  49. './UserUpdater': this.UserUpdater,
  50. '../Helpers/EmailHelper': this.EmailHelper,
  51. './UserEmailsConfirmationHandler': (this.UserEmailsConfirmationHandler = {}),
  52. '../Institutions/InstitutionsAPI': {
  53. endorseAffiliation: this.endorseAffiliation
  54. },
  55. '../Errors/Errors': Errors,
  56. 'logger-sharelatex': {
  57. log() {
  58. return console.log(arguments)
  59. },
  60. err() {}
  61. }
  62. }
  63. }))
  64. })
  65. describe('List', function() {
  66. beforeEach(function() {})
  67. it('lists emails', function(done) {
  68. const fullEmails = [{ some: 'data' }]
  69. this.UserGetter.getUserFullEmails.callsArgWith(1, null, fullEmails)
  70. return this.UserEmailsController.list(this.req, {
  71. json: response => {
  72. assert.deepEqual(response, fullEmails)
  73. assertCalledWith(this.UserGetter.getUserFullEmails, this.user._id)
  74. return done()
  75. }
  76. })
  77. })
  78. })
  79. describe('Add', function() {
  80. beforeEach(function() {
  81. this.newEmail = 'new_email@baz.com'
  82. this.req.body = {
  83. email: this.newEmail,
  84. university: { name: 'University Name' },
  85. department: 'Department',
  86. role: 'Role'
  87. }
  88. this.EmailHelper.parseEmail.returns(this.newEmail)
  89. this.UserEmailsConfirmationHandler.sendConfirmationEmail = sinon
  90. .stub()
  91. .yields()
  92. return this.UserUpdater.addEmailAddress.callsArgWith(3, null)
  93. })
  94. it('adds new email', function(done) {
  95. return this.UserEmailsController.add(this.req, {
  96. sendStatus: code => {
  97. code.should.equal(204)
  98. assertCalledWith(this.EmailHelper.parseEmail, this.newEmail)
  99. assertCalledWith(
  100. this.UserUpdater.addEmailAddress,
  101. this.user._id,
  102. this.newEmail
  103. )
  104. const affiliationOptions = this.UserUpdater.addEmailAddress.lastCall
  105. .args[2]
  106. Object.keys(affiliationOptions).length.should.equal(3)
  107. affiliationOptions.university.should.equal(this.req.body.university)
  108. affiliationOptions.department.should.equal(this.req.body.department)
  109. affiliationOptions.role.should.equal(this.req.body.role)
  110. return done()
  111. }
  112. })
  113. })
  114. it('sends an email confirmation', function(done) {
  115. return this.UserEmailsController.add(this.req, {
  116. sendStatus: code => {
  117. code.should.equal(204)
  118. assertCalledWith(
  119. this.UserEmailsConfirmationHandler.sendConfirmationEmail,
  120. this.user._id,
  121. this.newEmail
  122. )
  123. return done()
  124. }
  125. })
  126. })
  127. it('handles email parse error', function(done) {
  128. this.EmailHelper.parseEmail.returns(null)
  129. return this.UserEmailsController.add(this.req, {
  130. sendStatus: code => {
  131. code.should.equal(422)
  132. assertNotCalled(this.UserUpdater.addEmailAddress)
  133. return done()
  134. }
  135. })
  136. })
  137. })
  138. describe('remove', function() {
  139. beforeEach(function() {
  140. this.email = 'email_to_remove@bar.com'
  141. this.req.body.email = this.email
  142. return this.EmailHelper.parseEmail.returns(this.email)
  143. })
  144. it('removes email', function(done) {
  145. this.UserUpdater.removeEmailAddress.callsArgWith(2, null)
  146. return this.UserEmailsController.remove(this.req, {
  147. sendStatus: code => {
  148. code.should.equal(200)
  149. assertCalledWith(this.EmailHelper.parseEmail, this.email)
  150. assertCalledWith(
  151. this.UserUpdater.removeEmailAddress,
  152. this.user._id,
  153. this.email
  154. )
  155. return done()
  156. }
  157. })
  158. })
  159. it('handles email parse error', function(done) {
  160. this.EmailHelper.parseEmail.returns(null)
  161. return this.UserEmailsController.remove(this.req, {
  162. sendStatus: code => {
  163. code.should.equal(422)
  164. assertNotCalled(this.UserUpdater.removeEmailAddress)
  165. return done()
  166. }
  167. })
  168. })
  169. })
  170. describe('setDefault', function() {
  171. beforeEach(function() {
  172. this.email = 'email_to_set_default@bar.com'
  173. this.req.body.email = this.email
  174. this.EmailHelper.parseEmail.returns(this.email)
  175. this.AuthenticationController.setInSessionUser.returns(null)
  176. })
  177. it('sets default email', function(done) {
  178. this.UserUpdater.setDefaultEmailAddress.yields()
  179. this.UserEmailsController.setDefault(this.req, {
  180. sendStatus: code => {
  181. code.should.equal(200)
  182. assertCalledWith(this.EmailHelper.parseEmail, this.email)
  183. assertCalledWith(
  184. this.AuthenticationController.setInSessionUser,
  185. this.req,
  186. { email: this.email }
  187. )
  188. assertCalledWith(
  189. this.UserUpdater.setDefaultEmailAddress,
  190. this.user._id,
  191. this.email
  192. )
  193. done()
  194. }
  195. })
  196. })
  197. it('handles email parse error', function(done) {
  198. this.EmailHelper.parseEmail.returns(null)
  199. this.UserEmailsController.setDefault(this.req, {
  200. sendStatus: code => {
  201. code.should.equal(422)
  202. assertNotCalled(this.UserUpdater.setDefaultEmailAddress)
  203. return done()
  204. }
  205. })
  206. })
  207. })
  208. describe('endorse', function() {
  209. beforeEach(function() {
  210. this.email = 'email_to_endorse@bar.com'
  211. this.req.body.email = this.email
  212. return this.EmailHelper.parseEmail.returns(this.email)
  213. })
  214. it('endorses affiliation', function(done) {
  215. this.req.body.role = 'Role'
  216. this.req.body.department = 'Department'
  217. return this.UserEmailsController.endorse(this.req, {
  218. sendStatus: code => {
  219. code.should.equal(204)
  220. assertCalledWith(
  221. this.endorseAffiliation,
  222. this.user._id,
  223. this.email,
  224. 'Role',
  225. 'Department'
  226. )
  227. return done()
  228. }
  229. })
  230. })
  231. })
  232. describe('confirm', function() {
  233. beforeEach(function() {
  234. this.UserEmailsConfirmationHandler.confirmEmailFromToken = sinon
  235. .stub()
  236. .yields()
  237. this.res = {
  238. sendStatus: sinon.stub(),
  239. json: sinon.stub()
  240. }
  241. this.res.status = sinon.stub().returns(this.res)
  242. this.next = sinon.stub()
  243. this.token = 'mock-token'
  244. return (this.req.body = { token: this.token })
  245. })
  246. describe('successfully', function() {
  247. beforeEach(function() {
  248. return this.UserEmailsController.confirm(this.req, this.res, this.next)
  249. })
  250. it('should confirm the email from the token', function() {
  251. return this.UserEmailsConfirmationHandler.confirmEmailFromToken
  252. .calledWith(this.token)
  253. .should.equal(true)
  254. })
  255. it('should return a 200 status', function() {
  256. return this.res.sendStatus.calledWith(200).should.equal(true)
  257. })
  258. })
  259. describe('without a token', function() {
  260. beforeEach(function() {
  261. this.req.body.token = null
  262. return this.UserEmailsController.confirm(this.req, this.res, this.next)
  263. })
  264. it('should return a 422 status', function() {
  265. return this.res.sendStatus.calledWith(422).should.equal(true)
  266. })
  267. })
  268. describe('when confirming fails', function() {
  269. beforeEach(function() {
  270. this.UserEmailsConfirmationHandler.confirmEmailFromToken = sinon
  271. .stub()
  272. .yields(new Errors.NotFoundError('not found'))
  273. return this.UserEmailsController.confirm(this.req, this.res, this.next)
  274. })
  275. it('should return a 404 error code with a message', function() {
  276. this.res.status.calledWith(404).should.equal(true)
  277. return this.res.json
  278. .calledWith({
  279. message:
  280. 'Sorry, your confirmation token is invalid or has expired. Please request a new email confirmation link.'
  281. })
  282. .should.equal(true)
  283. })
  284. })
  285. })
  286. })