ThirdPartyIdentityManagerTests.js 6.2 KB

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