GroupSSOHandlerTests.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. const SandboxedModule = require('sandboxed-module')
  2. const sinon = require('sinon')
  3. const chai = require('chai')
  4. const { expect } = chai
  5. const { ObjectId } = require('mongodb')
  6. const Errors = require('../../../../app/src/Features/Errors/Errors')
  7. const MODULE_PATH = '../../../../app/src/Features/Subscription/GroupSSOHandler'
  8. describe('GroupSSOHandler', function () {
  9. beforeEach(function () {
  10. this.user = { _id: new ObjectId(), enrollment: { sso: [] } }
  11. this.subscription = {
  12. _id: new ObjectId().toString(),
  13. admin_id: new ObjectId(),
  14. member_ids: [this.user._id],
  15. }
  16. this.samlIdentifier = {
  17. externalUserId: 'user@external.com',
  18. userIdAttribute: 'email',
  19. providerId: `ol-group-subscription-id:${this.subscription._id.toString()}`,
  20. }
  21. this.auditLog = {
  22. initiatorId: 'test-initiator-id',
  23. ipAddress: '127.0.0.1',
  24. }
  25. this.ssoConfigData = {
  26. entryPoint: 'https://example.com/saml',
  27. certificates: ['abc'],
  28. userIdAttribute: 'nameId',
  29. enabled: true,
  30. }
  31. this.SSOConfig = {
  32. findById: sinon.stub().returns({
  33. exec: sinon.stub().resolves(this.ssoConfigData),
  34. }),
  35. }
  36. this.UserAuditLogHandler = {
  37. promises: {
  38. addEntry: sinon.stub().resolves(),
  39. },
  40. }
  41. this.UserUpdater = {
  42. promises: {
  43. updateUser: sinon.stub().resolves(),
  44. },
  45. }
  46. this.SAMLIdentityManager = {
  47. getUser: sinon.stub().resolves(),
  48. }
  49. this.User = {
  50. findOne: sinon.stub().returns({
  51. exec: sinon.stub().resolves(this.user),
  52. }),
  53. }
  54. this.GroupSSOHandler = SandboxedModule.require(MODULE_PATH, {
  55. requires: {
  56. '../../models/SSOConfig': { SSOConfig: this.SSOConfig },
  57. '../User/UserAuditLogHandler': this.UserAuditLogHandler,
  58. '../User/UserUpdater': this.UserUpdater,
  59. '../User/SAMLIdentityManager': this.SAMLIdentityManager,
  60. '../../models/User': {
  61. User: this.User,
  62. },
  63. },
  64. })
  65. })
  66. describe('checkUserCanEnrollInSubscription', function () {
  67. it('should throw an error if the subscription is not found', async function () {
  68. this.SSOConfig.findById.returns({
  69. exec: sinon.stub().resolves(undefined),
  70. })
  71. await expect(
  72. this.GroupSSOHandler.promises.checkUserCanEnrollInSubscription(
  73. this.user._id,
  74. this.subscription
  75. )
  76. ).to.be.rejectedWith(Errors.SAMLGroupSSODisabledError)
  77. })
  78. it('should throw an error if SSO is not enabled for the group', async function () {
  79. const disabledSSOConfig = { ...this.ssoConfig, enabled: false }
  80. this.SSOConfig.findById.returns({
  81. exec: sinon.stub().resolves(disabledSSOConfig),
  82. })
  83. await expect(
  84. this.GroupSSOHandler.promises.checkUserCanEnrollInSubscription(
  85. this.user._id,
  86. this.subscription
  87. )
  88. ).to.be.rejectedWith(Errors.SAMLGroupSSODisabledError)
  89. })
  90. it('should throw an error if the user is not a member of the group', async function () {
  91. const testSubscription = {
  92. ...this.subscription,
  93. member_ids: [],
  94. }
  95. await expect(
  96. this.GroupSSOHandler.promises.checkUserCanEnrollInSubscription(
  97. this.user._id,
  98. testSubscription
  99. )
  100. ).to.be.rejectedWith(Errors.SAMLGroupSSOLoginIdentityNotFoundError)
  101. })
  102. it('should throw an error if the user is already enrolled to the group', async function () {
  103. const testUser = {
  104. ...this.user,
  105. enrollment: {
  106. sso: [{ groupId: this.subscription._id }],
  107. },
  108. }
  109. this.User.findOne.returns({
  110. exec: sinon.stub().resolves(testUser),
  111. })
  112. await expect(
  113. this.GroupSSOHandler.promises.checkUserCanEnrollInSubscription(
  114. this.user._id,
  115. this.subscription
  116. )
  117. ).to.be.rejectedWith(Errors.SAMLIdentityExistsError)
  118. })
  119. it('should resolve if the user can be enrolled to the group', async function () {
  120. await expect(
  121. this.GroupSSOHandler.promises.checkUserCanEnrollInSubscription(
  122. this.user._id,
  123. this.subscription
  124. )
  125. ).to.be.fulfilled
  126. })
  127. })
  128. describe('enrollInSubscription', function () {
  129. it('should throw an error if the user cannot be enrolled', async function () {
  130. const disabledSSOConfig = { ...this.ssoConfig, enabled: false }
  131. this.SSOConfig.findById.returns({
  132. exec: sinon.stub().resolves(disabledSSOConfig),
  133. })
  134. await expect(
  135. this.GroupSSOHandler.promises.enrollInSubscription(
  136. this.user._id,
  137. this.subscription,
  138. this.samlIdentifier.externalUserId,
  139. this.samlIdentifier.userIdAttribute,
  140. this.auditLog
  141. )
  142. ).to.be.rejectedWith(Errors.SAMLGroupSSODisabledError)
  143. })
  144. it('should throw an error if an identical SAML identity for the subscription/user already exists', async function () {
  145. this.SAMLIdentityManager.getUser.resolves(this.user)
  146. await expect(
  147. this.GroupSSOHandler.promises.enrollInSubscription(
  148. this.user._id,
  149. this.subscription,
  150. this.samlIdentifier.externalUserId,
  151. this.samlIdentifier.userIdAttribute,
  152. this.auditLog
  153. )
  154. ).to.be.rejectedWith(Errors.SAMLIdentityExistsError)
  155. })
  156. it("should add an entry the user's SSO enrollment and samlIdentifiers lists", async function () {
  157. await this.GroupSSOHandler.promises.enrollInSubscription(
  158. this.user._id,
  159. this.subscription,
  160. this.samlIdentifier.externalUserId,
  161. this.samlIdentifier.userIdAttribute,
  162. this.auditLog
  163. )
  164. expect(this.UserUpdater.promises.updateUser).to.have.been.calledWith(
  165. this.user._id,
  166. {
  167. $push: {
  168. samlIdentifiers: this.samlIdentifier,
  169. 'enrollment.sso': {
  170. groupId: this.subscription._id,
  171. linkedAt: sinon.match.instanceOf(Date),
  172. primary: true,
  173. },
  174. },
  175. }
  176. )
  177. })
  178. it('should add an entry to the user audit log', async function () {
  179. await this.GroupSSOHandler.promises.enrollInSubscription(
  180. this.user._id,
  181. this.subscription,
  182. this.samlIdentifier.externalUserId,
  183. this.samlIdentifier.userIdAttribute,
  184. this.auditLog
  185. )
  186. expect(
  187. this.UserAuditLogHandler.promises.addEntry
  188. ).to.have.been.calledWith(
  189. this.user._id,
  190. 'group-sso-link',
  191. this.auditLog.initiatorId,
  192. this.auditLog.ipAddress,
  193. this.samlIdentifier
  194. )
  195. })
  196. })
  197. })