emails.ts 3.5 KB

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