emails.ts 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. }
  60. export function setInstitutionMeta(institutionData: Partial<Institution>) {
  61. setDefaultMeta()
  62. window.metaAttributesCache.set('ol-notificationsInstitution', [
  63. merge(cloneDeep(fakeInstitutionData), institutionData),
  64. ])
  65. }
  66. export function institutionSetupMocks(fetchMock: FetchMock) {
  67. defaultSetupMocks(fetchMock)
  68. fetchMock.delete(/\/notifications\/*/, 200, { delay: MOCK_DELAY })
  69. }
  70. export function setCommonMeta(notificationData: DeepPartial<Notification>) {
  71. setDefaultMeta()
  72. window.metaAttributesCache.set('ol-notifications', [notificationData])
  73. }
  74. export function commonSetupMocks(fetchMock: FetchMock) {
  75. defaultSetupMocks(fetchMock)
  76. fetchMock.post(
  77. /\/project\/[A-Za-z0-9]+\/invite\/token\/[A-Za-z0-9]+\/accept/,
  78. 200,
  79. { delay: MOCK_DELAY }
  80. )
  81. }
  82. export function setReconfirmationMeta() {
  83. setDefaultMeta()
  84. window.metaAttributesCache.set('ol-userEmails', [fakeReconfirmationUsersData])
  85. }
  86. export function reconfirmationSetupMocks(fetchMock: FetchMock) {
  87. defaultSetupMocks(fetchMock)
  88. }
  89. export function setReconfirmAffiliationMeta() {
  90. setDefaultMeta()
  91. window.metaAttributesCache.set(
  92. 'ol-reconfirmedViaSAML',
  93. fakeReconfirmationUsersData.samlProviderId
  94. )
  95. }
  96. export function reconfirmAffiliationSetupMocks(fetchMock: FetchMock) {
  97. defaultSetupMocks(fetchMock)
  98. fetchMock.post(/\/api\/project/, {
  99. status: 200,
  100. body: {
  101. projects: [{}],
  102. totalSize: 0,
  103. },
  104. })
  105. }