emails.ts 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. import { merge, cloneDeep } from 'lodash'
  2. import { FetchMockStatic } from 'fetch-mock'
  3. import { UserEmailData } from '../../../../types/user-email'
  4. import {
  5. Institution,
  6. Notification,
  7. } from '../../../../types/project/dashboard/notification'
  8. import { DeepPartial, DeepReadonly } from '../../../../types/utils'
  9. import { Project } from '../../../../types/project/dashboard/api'
  10. const MOCK_DELAY = 1000
  11. const fakeInstitutionData = {
  12. email: 'email@example.com',
  13. institutionEmail: 'institution@example.com',
  14. institutionId: 123,
  15. institutionName: 'Abc Institution',
  16. requestedEmail: 'requested@example.com',
  17. } as DeepReadonly<Institution>
  18. export const fakeReconfirmationUsersData = {
  19. affiliation: {
  20. institution: {
  21. ssoEnabled: false,
  22. ssoBeta: false,
  23. name: 'Abc Institution',
  24. },
  25. },
  26. samlProviderId: 'Saml Provider',
  27. email: 'reconfirmation-email@overleaf.com',
  28. default: false,
  29. } as DeepReadonly<UserEmailData>
  30. const fakeNotificationData = {
  31. messageOpts: {
  32. projectId: '123',
  33. projectName: 'Abc Project',
  34. ssoEnabled: false,
  35. institutionId: '456',
  36. userName: 'fakeUser',
  37. university_name: 'Abc University',
  38. token: 'abcdef',
  39. },
  40. } as DeepReadonly<Notification>
  41. export function defaultSetupMocks(fetchMock: FetchMockStatic) {
  42. // at least one project is required to show some notifications
  43. const projects = [{}] as Project[]
  44. fetchMock.post(/\/api\/project/, {
  45. status: 200,
  46. body: {
  47. projects,
  48. totalSize: projects.length,
  49. },
  50. })
  51. }
  52. export function setDefaultMeta() {
  53. window.metaAttributesCache = new Map()
  54. window.metaAttributesCache.set('ol-ExposedSettings', {
  55. ...window.metaAttributesCache.get('ol-ExposedSettings'),
  56. emailConfirmationDisabled: false,
  57. samlInitPath: '/fakeSaml',
  58. appName: 'Overleaf',
  59. })
  60. window.metaAttributesCache.set('ol-notificationsInstitution', [])
  61. window.metaAttributesCache.set('ol-userEmails', [])
  62. }
  63. export function errorsMocks(fetchMock: FetchMockStatic) {
  64. defaultSetupMocks(fetchMock)
  65. fetchMock.post(/\/user\/emails\/*/, 500, { delay: MOCK_DELAY })
  66. fetchMock.post(
  67. /\/project\/[A-Za-z0-9]+\/invite\/token\/[A-Za-z0-9]+\/accept/,
  68. 500,
  69. { delay: MOCK_DELAY }
  70. )
  71. fetchMock.post(/\/user\/emails\/send-reconfirmation/, 500, {
  72. delay: MOCK_DELAY,
  73. })
  74. }
  75. export function setInstitutionMeta(institutionData: Partial<Institution>) {
  76. setDefaultMeta()
  77. window.metaAttributesCache.set('ol-notificationsInstitution', [
  78. merge(cloneDeep(fakeInstitutionData), institutionData),
  79. ])
  80. }
  81. export function institutionSetupMocks(fetchMock: FetchMockStatic) {
  82. defaultSetupMocks(fetchMock)
  83. fetchMock.delete(/\/notifications\/*/, 200, { delay: MOCK_DELAY })
  84. }
  85. export function setCommonMeta(notificationData: DeepPartial<Notification>) {
  86. setDefaultMeta()
  87. window.metaAttributesCache.set('ol-notifications', [
  88. merge(cloneDeep(fakeNotificationData), notificationData),
  89. ])
  90. }
  91. export function commonSetupMocks(fetchMock: FetchMockStatic) {
  92. defaultSetupMocks(fetchMock)
  93. fetchMock.post(
  94. /\/project\/[A-Za-z0-9]+\/invite\/token\/[A-Za-z0-9]+\/accept/,
  95. 200,
  96. { delay: MOCK_DELAY }
  97. )
  98. }
  99. export function setReconfirmationMeta() {
  100. setDefaultMeta()
  101. window.metaAttributesCache.set('ol-userEmails', [fakeReconfirmationUsersData])
  102. }
  103. export function reconfirmationSetupMocks(fetchMock: FetchMockStatic) {
  104. defaultSetupMocks(fetchMock)
  105. fetchMock.post(/\/user\/emails\/resend_confirmation/, 200, {
  106. delay: MOCK_DELAY,
  107. })
  108. }
  109. export function setReconfirmAffiliationMeta() {
  110. setDefaultMeta()
  111. window.metaAttributesCache.set(
  112. 'ol-reconfirmedViaSAML',
  113. fakeReconfirmationUsersData.samlProviderId
  114. )
  115. }
  116. export function reconfirmAffiliationSetupMocks(fetchMock: FetchMockStatic) {
  117. defaultSetupMocks(fetchMock)
  118. fetchMock.post(
  119. /\/api\/project/,
  120. {
  121. status: 200,
  122. body: {
  123. projects: [{}],
  124. totalSize: 0,
  125. },
  126. },
  127. { overwriteRoutes: true }
  128. )
  129. fetchMock.post(/\/user\/emails\/send-reconfirmation/, 200, {
  130. delay: MOCK_DELAY,
  131. })
  132. }