reconfirmation-info.test.tsx 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. import {
  2. fireEvent,
  3. render,
  4. screen,
  5. waitFor,
  6. waitForElementToBeRemoved,
  7. } from '@testing-library/react'
  8. import sinon from 'sinon'
  9. import { expect } from 'chai'
  10. import fetchMock from 'fetch-mock'
  11. import { cloneDeep } from 'lodash'
  12. import ReconfirmationInfo from '../../../../../../frontend/js/features/settings/components/emails/reconfirmation-info'
  13. import { ssoUserData } from '../../fixtures/test-user-email-data'
  14. import { UserEmailData } from '../../../../../../types/user-email'
  15. import { UserEmailsProvider } from '../../../../../../frontend/js/features/settings/context/user-email-context'
  16. function renderReconfirmationInfo(data: UserEmailData) {
  17. return render(
  18. <UserEmailsProvider>
  19. <ReconfirmationInfo userEmailData={data} />
  20. </UserEmailsProvider>
  21. )
  22. }
  23. describe('<ReconfirmationInfo/>', function () {
  24. beforeEach(function () {
  25. window.metaAttributesCache = window.metaAttributesCache || new Map()
  26. fetchMock.get('/user/emails?ensureAffiliation=true', [])
  27. })
  28. afterEach(function () {
  29. fetchMock.reset()
  30. })
  31. describe('reconfirmed via SAML', function () {
  32. beforeEach(function () {
  33. window.metaAttributesCache.set(
  34. 'ol-reconfirmedViaSAML',
  35. 'sso-prof-saml-id'
  36. )
  37. })
  38. afterEach(function () {
  39. window.metaAttributesCache = new Map()
  40. })
  41. it('show reconfirmed confirmation', function () {
  42. renderReconfirmationInfo(ssoUserData)
  43. screen.getByText('SSO University')
  44. screen.getByText(/affiliation is confirmed/)
  45. screen.getByText(/Thank you!/)
  46. })
  47. })
  48. describe('in reconfirm notification period', function () {
  49. let inReconfirmUserData: UserEmailData
  50. const locationStub = sinon.stub()
  51. const originalLocation = window.location
  52. beforeEach(function () {
  53. window.metaAttributesCache.set('ol-ExposedSettings', {
  54. samlInitPath: '/saml',
  55. })
  56. Object.defineProperty(window, 'location', {
  57. value: {
  58. assign: locationStub,
  59. },
  60. })
  61. inReconfirmUserData = cloneDeep(ssoUserData)
  62. if (inReconfirmUserData.affiliation) {
  63. inReconfirmUserData.affiliation.inReconfirmNotificationPeriod = true
  64. }
  65. })
  66. afterEach(function () {
  67. window.metaAttributesCache = new Map()
  68. Object.defineProperty(window, 'location', {
  69. value: originalLocation,
  70. })
  71. })
  72. it('renders prompt', function () {
  73. renderReconfirmationInfo(inReconfirmUserData)
  74. screen.getByText(/Are you still at/)
  75. screen.getByText('SSO University')
  76. screen.getByText(
  77. /Please take a moment to confirm your institutional email address/
  78. )
  79. screen.getByRole('link', { name: 'Learn more' })
  80. expect(screen.queryByText(/add a new primary email address/)).to.not.exist
  81. })
  82. it('renders default emails prompt', function () {
  83. inReconfirmUserData.default = true
  84. renderReconfirmationInfo(inReconfirmUserData)
  85. screen.getByText(/add a new primary email address/)
  86. })
  87. describe('SAML reconfirmations', function () {
  88. beforeEach(function () {
  89. window.metaAttributesCache.set('ol-ExposedSettings', {
  90. hasSamlFeature: true,
  91. samlInitPath: '/saml/init',
  92. })
  93. })
  94. it('redirects to SAML flow', async function () {
  95. renderReconfirmationInfo(inReconfirmUserData)
  96. const confirmButton = screen.getByRole('button', {
  97. name: 'Confirm Affiliation',
  98. }) as HTMLButtonElement
  99. await waitFor(() => {
  100. expect(confirmButton.disabled).to.be.false
  101. })
  102. fireEvent.click(confirmButton)
  103. await waitFor(() => {
  104. expect(confirmButton.disabled).to.be.true
  105. })
  106. sinon.assert.calledOnce(locationStub)
  107. sinon.assert.calledWithMatch(
  108. locationStub,
  109. '/saml/init?university_id=2&reconfirm=/user/settings'
  110. )
  111. })
  112. })
  113. describe('Email reconfirmations', function () {
  114. beforeEach(function () {
  115. window.metaAttributesCache.set('ol-ExposedSettings', {
  116. hasSamlFeature: false,
  117. })
  118. fetchMock.post('/user/emails/send-reconfirmation', 200)
  119. })
  120. it('sends and resends confirmation email', async function () {
  121. renderReconfirmationInfo(inReconfirmUserData)
  122. const confirmButton = screen.getByRole('button', {
  123. name: 'Confirm Affiliation',
  124. }) as HTMLButtonElement
  125. await waitFor(() => {
  126. expect(confirmButton.disabled).to.be.false
  127. })
  128. fireEvent.click(confirmButton)
  129. await waitFor(() => {
  130. expect(confirmButton.disabled).to.be.true
  131. })
  132. expect(fetchMock.called()).to.be.true
  133. // the confirmation text should now be displayed
  134. screen.getByText(/Please check your email inbox to confirm/)
  135. // try the resend button
  136. fetchMock.resetHistory()
  137. const resendButton = screen.getByRole('button', {
  138. name: 'Resend confirmation email',
  139. }) as HTMLButtonElement
  140. fireEvent.click(resendButton)
  141. screen.getByText(/Sending/)
  142. expect(fetchMock.called()).to.be.true
  143. await waitForElementToBeRemoved(() => screen.getByText(/Sending/))
  144. screen.getByRole('button', {
  145. name: 'Resend confirmation email',
  146. })
  147. })
  148. })
  149. })
  150. })