login-maintenance.spec.ts 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. // Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. import { waitFor } from '@testing-library/vue'
  3. import { useApplicationStore } from '#shared/stores/application.ts'
  4. import { visitView } from '#tests/support/components/visitView.ts'
  5. import { mockApplicationConfig } from '#tests/support/mock-applicationConfig.ts'
  6. import { mockAuthentication } from '#tests/support/mock-authentication.ts'
  7. import {
  8. mockGraphQLApi,
  9. mockGraphQLSubscription,
  10. } from '#tests/support/mock-graphql-api.ts'
  11. import { ConfigUpdatesDocument } from '#shared/graphql/subscriptions/configUpdates.api.ts'
  12. import { LogoutDocument } from '#shared/graphql/mutations/logout.api.ts'
  13. import { ApplicationConfigDocument } from '#shared/graphql/queries/applicationConfig.api.ts'
  14. import { useAuthenticationStore } from '#shared/stores/authentication.ts'
  15. import { mockPermissions } from '#tests/support/mock-permissions.ts'
  16. import {
  17. mockPublicLinks,
  18. mockPublicLinksSubscription,
  19. } from '#shared/entities/public-links/__tests__/mocks/mockPublicLinks.ts'
  20. // TODO: import '#tests/graphql/builders/mocks.ts'
  21. vi.mock('#shared/server/apollo/client.ts', () => {
  22. return {
  23. clearApolloClientStore: () => {
  24. return Promise.resolve()
  25. },
  26. }
  27. })
  28. beforeEach(() => {
  29. mockPublicLinks([])
  30. mockPublicLinksSubscription()
  31. mockApplicationConfig({ product_name: 'Zammad' })
  32. })
  33. describe('testing login maintenance mode', () => {
  34. it('check not visible maintenance mode message, when maintenance mode is not active', async () => {
  35. mockApplicationConfig({
  36. maintenance_mode: false,
  37. })
  38. const view = await visitView('/login')
  39. const maintenanceModeMessage = view.queryByText(
  40. 'Zammad is currently in maintenance mode. Only administrators can log in. Please wait until the maintenance window is over.',
  41. )
  42. expect(maintenanceModeMessage).not.toBeInTheDocument()
  43. })
  44. it('check for maintenance mode message', async () => {
  45. mockApplicationConfig({
  46. maintenance_mode: true,
  47. })
  48. const view = await visitView('/login')
  49. const maintenanceModeMessage = view.queryByText(
  50. 'Zammad is currently in maintenance mode. Only administrators can log in. Please wait until the maintenance window is over.',
  51. )
  52. expect(maintenanceModeMessage).toBeInTheDocument()
  53. })
  54. it('check for maintenance mode login custom message (e.g. to announce maintenance)', async () => {
  55. mockApplicationConfig({
  56. maintenance_login: true,
  57. maintenance_login_message: 'Custom maintenance login message.',
  58. })
  59. const view = await visitView('/login')
  60. const maintenanceModeCustomMessage = view.queryByText(
  61. 'Custom maintenance login message.',
  62. )
  63. expect(maintenanceModeCustomMessage).toBeInTheDocument()
  64. })
  65. it('does not logout for admin user after maintenance mode switch', async () => {
  66. mockApplicationConfig({
  67. maintenance_mode: false,
  68. })
  69. mockAuthentication(true)
  70. mockPermissions(['admin.maintenance'])
  71. const mockSubscription = mockGraphQLSubscription(ConfigUpdatesDocument)
  72. const application = useApplicationStore()
  73. application.initializeConfigUpdateSubscription()
  74. await visitView('/')
  75. // Change maintenance mode to trigger the logout for non admin user.
  76. await mockSubscription.next({
  77. data: {
  78. configUpdates: {
  79. setting: {
  80. key: 'maintenance_mode',
  81. value: true,
  82. },
  83. },
  84. },
  85. })
  86. expect(useAuthenticationStore().authenticated).toBe(true)
  87. })
  88. it('check logout for non admin user after maintenance mode switch', async () => {
  89. mockApplicationConfig({
  90. maintenance_mode: false,
  91. })
  92. mockAuthentication(true)
  93. mockPermissions(['agent'])
  94. mockGraphQLApi(LogoutDocument).willResolve({
  95. logout: {
  96. success: true,
  97. errors: null,
  98. externalLogoutUrl: null,
  99. },
  100. })
  101. const mockSubscription = mockGraphQLSubscription(ConfigUpdatesDocument)
  102. const application = useApplicationStore()
  103. application.initializeConfigUpdateSubscription()
  104. mockGraphQLApi(ApplicationConfigDocument).willResolve({
  105. applicationConfig: [
  106. {
  107. key: 'maintenance_mode',
  108. value: true,
  109. },
  110. {
  111. key: 'product_name',
  112. value: 'Zammad',
  113. },
  114. ],
  115. })
  116. const view = await visitView('/')
  117. // Change maintenance mode to trigger the logout for non admin user.
  118. await mockSubscription.next({
  119. data: {
  120. configUpdates: {
  121. setting: {
  122. key: 'maintenance_mode',
  123. value: true,
  124. },
  125. },
  126. },
  127. })
  128. expect(useAuthenticationStore().authenticated).toBe(false)
  129. await waitFor(() => {
  130. const maintenanceModeMessage = view.queryByText(
  131. 'Zammad is currently in maintenance mode. Only administrators can log in. Please wait until the maintenance window is over.',
  132. )
  133. expect(maintenanceModeMessage).toBeInTheDocument()
  134. })
  135. })
  136. })