UserEmailsControllerTests.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. const sinon = require('sinon')
  2. const assertCalledWith = sinon.assert.calledWith
  3. const assertNotCalled = sinon.assert.notCalled
  4. const chai = require('chai')
  5. const { assert } = chai
  6. const modulePath = '../../../../app/src/Features/User/UserEmailsController.js'
  7. const SandboxedModule = require('sandboxed-module')
  8. const MockRequest = require('../helpers/MockRequest')
  9. const Errors = require('../../../../app/src/Features/Errors/Errors')
  10. describe('UserEmailsController', function() {
  11. beforeEach(function() {
  12. this.req = new MockRequest()
  13. this.user = { _id: 'mock-user-id' }
  14. this.UserGetter = { getUserFullEmails: sinon.stub() }
  15. this.AuthenticationController = {
  16. getLoggedInUserId: sinon.stub().returns(this.user._id),
  17. setInSessionUser: sinon.stub()
  18. }
  19. this.Features = {
  20. hasFeature: sinon.stub()
  21. }
  22. this.UserUpdater = {
  23. addEmailAddress: sinon.stub(),
  24. removeEmailAddress: sinon.stub(),
  25. setDefaultEmailAddress: sinon.stub(),
  26. updateV1AndSetDefaultEmailAddress: sinon.stub()
  27. }
  28. this.EmailHelper = { parseEmail: sinon.stub() }
  29. this.endorseAffiliation = sinon.stub().yields()
  30. this.InstitutionsAPI = {
  31. endorseAffiliation: this.endorseAffiliation,
  32. getInstitutionViaDomain: sinon
  33. .stub()
  34. .withArgs('overleaf.com')
  35. .resolves({ sso_enabled: true })
  36. .withArgs('example.com')
  37. .resolves({ sso_enabled: false })
  38. }
  39. this.UserEmailsController = SandboxedModule.require(modulePath, {
  40. globals: {
  41. console: console
  42. },
  43. requires: {
  44. '../Authentication/AuthenticationController': this
  45. .AuthenticationController,
  46. '../../infrastructure/Features': this.Features,
  47. './UserGetter': this.UserGetter,
  48. './UserUpdater': this.UserUpdater,
  49. '../Helpers/EmailHelper': this.EmailHelper,
  50. './UserEmailsConfirmationHandler': (this.UserEmailsConfirmationHandler = {}),
  51. '../Institutions/InstitutionsAPI': this.InstitutionsAPI,
  52. '../Errors/Errors': Errors,
  53. 'logger-sharelatex': {
  54. log() {
  55. console.log(arguments)
  56. },
  57. err() {}
  58. }
  59. }
  60. })
  61. })
  62. describe('List', function() {
  63. beforeEach(function() {})
  64. it('lists emails', function(done) {
  65. const fullEmails = [{ some: 'data' }]
  66. this.UserGetter.getUserFullEmails.callsArgWith(1, null, fullEmails)
  67. this.UserEmailsController.list(this.req, {
  68. json: response => {
  69. assert.deepEqual(response, fullEmails)
  70. assertCalledWith(this.UserGetter.getUserFullEmails, this.user._id)
  71. done()
  72. }
  73. })
  74. })
  75. })
  76. describe('Add', function() {
  77. beforeEach(function() {
  78. this.newEmail = 'new_email@baz.com'
  79. this.req.body = {
  80. email: this.newEmail,
  81. university: { name: 'University Name' },
  82. department: 'Department',
  83. role: 'Role'
  84. }
  85. this.EmailHelper.parseEmail.returns(this.newEmail)
  86. this.UserEmailsConfirmationHandler.sendConfirmationEmail = sinon
  87. .stub()
  88. .yields()
  89. this.UserUpdater.addEmailAddress.callsArgWith(3, null)
  90. })
  91. it('adds new email', function(done) {
  92. this.UserEmailsController.add(this.req, {
  93. sendStatus: code => {
  94. code.should.equal(204)
  95. assertCalledWith(this.EmailHelper.parseEmail, this.newEmail)
  96. assertCalledWith(
  97. this.UserUpdater.addEmailAddress,
  98. this.user._id,
  99. this.newEmail
  100. )
  101. const affiliationOptions = this.UserUpdater.addEmailAddress.lastCall
  102. .args[2]
  103. Object.keys(affiliationOptions).length.should.equal(3)
  104. affiliationOptions.university.should.equal(this.req.body.university)
  105. affiliationOptions.department.should.equal(this.req.body.department)
  106. affiliationOptions.role.should.equal(this.req.body.role)
  107. done()
  108. }
  109. })
  110. })
  111. it('sends an email confirmation', function(done) {
  112. this.UserEmailsController.add(this.req, {
  113. sendStatus: code => {
  114. code.should.equal(204)
  115. assertCalledWith(
  116. this.UserEmailsConfirmationHandler.sendConfirmationEmail,
  117. this.user._id,
  118. this.newEmail
  119. )
  120. done()
  121. }
  122. })
  123. })
  124. it('handles email parse error', function(done) {
  125. this.EmailHelper.parseEmail.returns(null)
  126. this.UserEmailsController.add(this.req, {
  127. sendStatus: code => {
  128. code.should.equal(422)
  129. assertNotCalled(this.UserUpdater.addEmailAddress)
  130. done()
  131. }
  132. })
  133. })
  134. })
  135. describe('remove', function() {
  136. beforeEach(function() {
  137. this.email = 'email_to_remove@bar.com'
  138. this.req.body.email = this.email
  139. this.EmailHelper.parseEmail.returns(this.email)
  140. })
  141. it('removes email', function(done) {
  142. this.UserUpdater.removeEmailAddress.callsArgWith(2, null)
  143. this.UserEmailsController.remove(this.req, {
  144. sendStatus: code => {
  145. code.should.equal(200)
  146. assertCalledWith(this.EmailHelper.parseEmail, this.email)
  147. assertCalledWith(
  148. this.UserUpdater.removeEmailAddress,
  149. this.user._id,
  150. this.email
  151. )
  152. done()
  153. }
  154. })
  155. })
  156. it('handles email parse error', function(done) {
  157. this.EmailHelper.parseEmail.returns(null)
  158. this.UserEmailsController.remove(this.req, {
  159. sendStatus: code => {
  160. code.should.equal(422)
  161. assertNotCalled(this.UserUpdater.removeEmailAddress)
  162. done()
  163. }
  164. })
  165. })
  166. })
  167. describe('setDefault', function() {
  168. beforeEach(function() {
  169. this.email = 'email_to_set_default@bar.com'
  170. this.req.body.email = this.email
  171. this.EmailHelper.parseEmail.returns(this.email)
  172. this.AuthenticationController.setInSessionUser.returns(null)
  173. })
  174. it('sets default email', function(done) {
  175. this.UserUpdater.setDefaultEmailAddress.yields()
  176. this.UserEmailsController.setDefault(this.req, {
  177. sendStatus: code => {
  178. code.should.equal(200)
  179. assertCalledWith(this.EmailHelper.parseEmail, this.email)
  180. assertCalledWith(
  181. this.AuthenticationController.setInSessionUser,
  182. this.req,
  183. { email: this.email }
  184. )
  185. assertCalledWith(
  186. this.UserUpdater.setDefaultEmailAddress,
  187. this.user._id,
  188. this.email
  189. )
  190. done()
  191. }
  192. })
  193. })
  194. it('handles email parse error', function(done) {
  195. this.EmailHelper.parseEmail.returns(null)
  196. this.UserEmailsController.setDefault(this.req, {
  197. sendStatus: code => {
  198. code.should.equal(422)
  199. assertNotCalled(this.UserUpdater.setDefaultEmailAddress)
  200. done()
  201. }
  202. })
  203. })
  204. })
  205. describe('endorse', function() {
  206. beforeEach(function() {
  207. this.email = 'email_to_endorse@bar.com'
  208. this.req.body.email = this.email
  209. this.EmailHelper.parseEmail.returns(this.email)
  210. })
  211. it('endorses affiliation', function(done) {
  212. this.req.body.role = 'Role'
  213. this.req.body.department = 'Department'
  214. this.UserEmailsController.endorse(this.req, {
  215. sendStatus: code => {
  216. code.should.equal(204)
  217. assertCalledWith(
  218. this.endorseAffiliation,
  219. this.user._id,
  220. this.email,
  221. 'Role',
  222. 'Department'
  223. )
  224. done()
  225. }
  226. })
  227. })
  228. })
  229. describe('confirm', function() {
  230. beforeEach(function() {
  231. this.UserEmailsConfirmationHandler.confirmEmailFromToken = sinon
  232. .stub()
  233. .yields()
  234. this.res = {
  235. sendStatus: sinon.stub(),
  236. json: sinon.stub()
  237. }
  238. this.res.status = sinon.stub().returns(this.res)
  239. this.next = sinon.stub()
  240. this.token = 'mock-token'
  241. this.req.body = { token: this.token }
  242. })
  243. describe('successfully', function() {
  244. beforeEach(function() {
  245. this.UserEmailsController.confirm(this.req, this.res, this.next)
  246. })
  247. it('should confirm the email from the token', function() {
  248. this.UserEmailsConfirmationHandler.confirmEmailFromToken
  249. .calledWith(this.token)
  250. .should.equal(true)
  251. })
  252. it('should return a 200 status', function() {
  253. this.res.sendStatus.calledWith(200).should.equal(true)
  254. })
  255. })
  256. describe('without a token', function() {
  257. beforeEach(function() {
  258. this.req.body.token = null
  259. this.UserEmailsController.confirm(this.req, this.res, this.next)
  260. })
  261. it('should return a 422 status', function() {
  262. this.res.status.calledWith(422).should.equal(true)
  263. })
  264. })
  265. describe('when confirming fails', function() {
  266. beforeEach(function() {
  267. this.UserEmailsConfirmationHandler.confirmEmailFromToken = sinon
  268. .stub()
  269. .yields(new Errors.NotFoundError('not found'))
  270. this.UserEmailsController.confirm(this.req, this.res, this.next)
  271. })
  272. it('should return a 404 error code with a message', function() {
  273. this.res.status.calledWith(404).should.equal(true)
  274. this.res.json
  275. .calledWith({
  276. message: this.req.i18n.translate('confirmation_token_invalid')
  277. })
  278. .should.equal(true)
  279. })
  280. })
  281. })
  282. describe('resendConfirmation', function() {
  283. beforeEach(function() {
  284. this.req = {
  285. body: {}
  286. }
  287. this.res = {
  288. sendStatus: sinon.stub()
  289. }
  290. this.next = sinon.stub()
  291. this.UserEmailsConfirmationHandler.sendConfirmationEmail = sinon
  292. .stub()
  293. .yields()
  294. })
  295. describe('when institution SSO is released', function() {
  296. beforeEach(function() {
  297. this.Features.hasFeature.withArgs('saml').returns(true)
  298. })
  299. describe('for an institution SSO email', function() {
  300. beforeEach(function() {
  301. this.req.body.email = 'with-sso@overleaf.com'
  302. })
  303. it('should not send the email', function() {
  304. this.UserEmailsController.resendConfirmation(
  305. this.req,
  306. this.res,
  307. () => {
  308. this.UserEmailsConfirmationHandler.sendConfirmationEmail.should
  309. .not.have.been.called.once
  310. }
  311. )
  312. })
  313. })
  314. describe('for a non-institution SSO email', function() {
  315. beforeEach(function() {
  316. this.req.body.email = 'without-sso@example.com'
  317. })
  318. it('should send the email', function() {
  319. this.UserEmailsController.resendConfirmation(
  320. this.req,
  321. this.res,
  322. () => {
  323. this.UserEmailsConfirmationHandler.sendConfirmationEmail.should
  324. .have.been.called.once
  325. }
  326. )
  327. })
  328. })
  329. })
  330. describe('when institution SSO is not released', function() {
  331. beforeEach(function() {
  332. this.Features.hasFeature.withArgs('saml').returns(false)
  333. })
  334. describe('for an institution SSO email', function() {
  335. beforeEach(function() {
  336. this.req.body.email = 'with-sso@overleaf.com'
  337. })
  338. it('should send the email', function() {
  339. this.UserEmailsController.resendConfirmation(
  340. this.req,
  341. this.res,
  342. () => {
  343. this.UserEmailsConfirmationHandler.sendConfirmationEmail.should
  344. .have.been.called.once
  345. }
  346. )
  347. })
  348. })
  349. describe('for a non-institution SSO email', function() {
  350. beforeEach(function() {
  351. this.req.body.email = 'without-sso@example.com'
  352. })
  353. it('should send the email', function() {
  354. this.UserEmailsController.resendConfirmation(
  355. this.req,
  356. this.res,
  357. () => {
  358. this.UserEmailsConfirmationHandler.sendConfirmationEmail.should
  359. .have.been.called.once
  360. }
  361. )
  362. })
  363. })
  364. })
  365. })
  366. })