ThirdPartyIdentityManagerTests.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. const sinon = require('sinon')
  2. const { expect } = require('chai')
  3. const SandboxedModule = require('sandboxed-module')
  4. const OError = require('@overleaf/o-error')
  5. const {
  6. ThirdPartyUserNotFoundError,
  7. } = require('../../../../app/src/Features/Errors/Errors')
  8. const modulePath =
  9. '../../../../app/src/Features/User/ThirdPartyIdentityManager.js'
  10. describe('ThirdPartyIdentityManager', function () {
  11. beforeEach(function () {
  12. this.userId = 'a1b2c3'
  13. this.user = {
  14. _id: this.userId,
  15. email: 'example@overleaf.com',
  16. }
  17. this.externalUserId = 'id789'
  18. this.externalData = {}
  19. this.auditLog = { initiatorId: this.userId, ipAddress: '0:0:0:0' }
  20. this.ThirdPartyIdentityManager = SandboxedModule.require(modulePath, {
  21. requires: {
  22. '../../../../app/src/Features/User/UserAuditLogHandler':
  23. (this.UserAuditLogHandler = {
  24. promises: {
  25. addEntry: sinon.stub().resolves(),
  26. },
  27. }),
  28. '../../../../app/src/Features/Email/EmailHandler': (this.EmailHandler =
  29. {
  30. promises: {
  31. sendEmail: sinon.stub().resolves(),
  32. },
  33. }),
  34. '../../../../app/src/models/User': {
  35. User: (this.User = {
  36. findOneAndUpdate: sinon
  37. .stub()
  38. .returns({ exec: sinon.stub().resolves(this.user) }),
  39. findOne: sinon.stub().returns({
  40. exec: sinon.stub().resolves(undefined),
  41. }),
  42. }),
  43. },
  44. '@overleaf/settings': {
  45. oauthProviders: {
  46. google: {
  47. name: 'Google',
  48. },
  49. orcid: {
  50. name: 'ORCID',
  51. },
  52. },
  53. },
  54. },
  55. })
  56. })
  57. describe('getUser', function () {
  58. it('should throw an error when missing providerId or externalUserId', async function () {
  59. await expect(
  60. this.ThirdPartyIdentityManager.promises.getUser(undefined, undefined)
  61. ).to.be.rejectedWith(OError, `invalid SSO arguments`)
  62. })
  63. describe('when user linked', function () {
  64. beforeEach(function () {
  65. this.User.findOne.returns({
  66. exec: sinon.stub().resolves(this.user),
  67. })
  68. })
  69. it('should return the user', async function () {
  70. this.User.findOne.returns({
  71. exec: sinon.stub().resolves(this.user),
  72. })
  73. const user = await this.ThirdPartyIdentityManager.promises.getUser(
  74. 'google',
  75. 'an-id-linked'
  76. )
  77. expect(user).to.deep.equal(this.user)
  78. })
  79. })
  80. it('should return ThirdPartyUserNotFoundError when no user linked', async function () {
  81. let error
  82. try {
  83. await this.ThirdPartyIdentityManager.promises.getUser(
  84. 'google',
  85. 'an-id-not-linked'
  86. )
  87. } catch (err) {
  88. error = err
  89. }
  90. expect(error).to.be.instanceOf(ThirdPartyUserNotFoundError)
  91. })
  92. })
  93. describe('link', function () {
  94. it('should send email alert', async function () {
  95. await this.ThirdPartyIdentityManager.promises.link(
  96. this.userId,
  97. 'google',
  98. this.externalUserId,
  99. this.externalData,
  100. this.auditLog
  101. )
  102. const emailCall = this.EmailHandler.promises.sendEmail.getCall(0)
  103. expect(emailCall.args[0]).to.equal('securityAlert')
  104. expect(emailCall.args[1].actionDescribed).to.contain(
  105. 'a Google account was linked'
  106. )
  107. })
  108. it('should update user audit log', async function () {
  109. await this.ThirdPartyIdentityManager.promises.link(
  110. this.userId,
  111. 'google',
  112. this.externalUserId,
  113. this.externalData,
  114. this.auditLog
  115. )
  116. expect(
  117. this.UserAuditLogHandler.promises.addEntry
  118. ).to.have.been.calledOnceWith(
  119. this.userId,
  120. 'link-sso',
  121. this.auditLog.initiatorId,
  122. this.auditLog.ipAddress,
  123. {
  124. providerId: 'google',
  125. }
  126. )
  127. })
  128. describe('errors', function () {
  129. const anError = new Error('oops')
  130. it('should not unlink if the UserAuditLogHandler throws an error', async function () {
  131. this.UserAuditLogHandler.promises.addEntry.throws(anError)
  132. await expect(
  133. this.ThirdPartyIdentityManager.promises.link(
  134. this.userId,
  135. 'google',
  136. this.externalUserId,
  137. this.externalData,
  138. this.auditLog
  139. )
  140. ).to.be.rejectedWith(anError)
  141. expect(this.User.findOneAndUpdate).to.not.have.been.called
  142. })
  143. describe('EmailHandler', function () {
  144. beforeEach(function () {
  145. this.EmailHandler.promises.sendEmail.rejects(anError)
  146. })
  147. it('should log but not return the error', async function () {
  148. await expect(
  149. this.ThirdPartyIdentityManager.promises.link(
  150. this.userId,
  151. 'google',
  152. this.externalUserId,
  153. this.externalData,
  154. this.auditLog
  155. )
  156. ).to.be.fulfilled
  157. expect(this.logger.error.lastCall).to.be.calledWithExactly(
  158. {
  159. err: anError,
  160. userId: this.userId,
  161. },
  162. 'could not send security alert email when new account linked'
  163. )
  164. })
  165. })
  166. })
  167. })
  168. describe('unlink', function () {
  169. it('should send email alert', async function () {
  170. await this.ThirdPartyIdentityManager.promises.unlink(
  171. this.userId,
  172. 'orcid',
  173. this.auditLog
  174. )
  175. const emailCall = this.EmailHandler.promises.sendEmail.getCall(0)
  176. expect(emailCall.args[0]).to.equal('securityAlert')
  177. expect(emailCall.args[1].actionDescribed).to.contain(
  178. 'an ORCID account was unlinked from'
  179. )
  180. })
  181. it('should update user audit log', async function () {
  182. await this.ThirdPartyIdentityManager.promises.unlink(
  183. this.userId,
  184. 'orcid',
  185. this.auditLog
  186. )
  187. expect(
  188. this.UserAuditLogHandler.promises.addEntry
  189. ).to.have.been.calledOnceWith(
  190. this.userId,
  191. 'unlink-sso',
  192. this.auditLog.initiatorId,
  193. this.auditLog.ipAddress,
  194. {
  195. providerId: 'orcid',
  196. }
  197. )
  198. })
  199. describe('errors', function () {
  200. const anError = new Error('oops')
  201. it('should not unlink if the UserAuditLogHandler throws an error', async function () {
  202. this.UserAuditLogHandler.promises.addEntry.throws(anError)
  203. await expect(
  204. this.ThirdPartyIdentityManager.promises.unlink(
  205. this.userId,
  206. 'orcid',
  207. this.auditLog
  208. )
  209. ).to.be.rejectedWith(anError)
  210. expect(this.User.findOneAndUpdate).to.not.have.been.called
  211. })
  212. describe('EmailHandler', function () {
  213. beforeEach(function () {
  214. this.EmailHandler.promises.sendEmail.rejects(anError)
  215. })
  216. it('should log but not return the error', async function () {
  217. await expect(
  218. this.ThirdPartyIdentityManager.promises.unlink(
  219. this.userId,
  220. 'google',
  221. this.auditLog
  222. )
  223. ).to.be.fulfilled
  224. expect(this.logger.error.lastCall).to.be.calledWithExactly(
  225. {
  226. err: anError,
  227. userId: this.userId,
  228. },
  229. 'could not send security alert email when account no longer linked'
  230. )
  231. })
  232. })
  233. })
  234. })
  235. })