session.spec.ts 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. // Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. import { createPinia, setActivePinia } from 'pinia'
  3. import {
  4. mockGraphQLApi,
  5. mockGraphQLSubscription,
  6. } from '#tests/support/mock-graphql-api.ts'
  7. import { CurrentUserDocument } from '#shared/graphql/queries/currentUser.api.ts'
  8. import { CurrentUserUpdatesDocument } from '#shared/graphql/subscriptions/currentUserUpdates.api.ts'
  9. import { useSessionStore } from '../session.ts'
  10. const userData = {
  11. __typename: 'User',
  12. id: '123456789',
  13. internalId: 1,
  14. firstname: 'John',
  15. lastname: 'Doe',
  16. fullname: 'John Doe',
  17. email: 'zammad@example.com',
  18. image: 'c2715a3e92c7e375b8c212d25d431e2a',
  19. preferences: {
  20. locale: 'de-de',
  21. },
  22. personalSettings: {
  23. __typename: 'UserPersonalSettings',
  24. notificationConfig: {
  25. __typename: 'UserPersonalSettingsNotificationConfig',
  26. groupIds: [1],
  27. matrix: {
  28. __typename: 'UserPersonalSettingsNotificationMatrix',
  29. create: {
  30. __typename: 'UserPersonalSettingsNotificationMatrixRow',
  31. channel: {
  32. __typename: 'UserPersonalSettingsNotificationMatrixChannel',
  33. email: true,
  34. online: true,
  35. },
  36. criteria: {
  37. __typename: 'UserPersonalSettingsNotificationMatrixCriteria',
  38. no: false,
  39. ownedByMe: true,
  40. ownedByNobody: true,
  41. subscribed: true,
  42. },
  43. },
  44. escalation: {
  45. __typename: 'UserPersonalSettingsNotificationMatrixRow',
  46. channel: {
  47. __typename: 'UserPersonalSettingsNotificationMatrixChannel',
  48. email: true,
  49. online: true,
  50. },
  51. criteria: {
  52. __typename: 'UserPersonalSettingsNotificationMatrixCriteria',
  53. no: false,
  54. ownedByMe: true,
  55. ownedByNobody: true,
  56. subscribed: true,
  57. },
  58. },
  59. reminderReached: {
  60. __typename: 'UserPersonalSettingsNotificationMatrixRow',
  61. channel: {
  62. __typename: 'UserPersonalSettingsNotificationMatrixChannel',
  63. email: true,
  64. online: true,
  65. },
  66. criteria: {
  67. __typename: 'UserPersonalSettingsNotificationMatrixCriteria',
  68. no: false,
  69. ownedByMe: true,
  70. ownedByNobody: true,
  71. subscribed: true,
  72. },
  73. },
  74. update: {
  75. __typename: 'UserPersonalSettingsNotificationMatrixRow',
  76. channel: {
  77. __typename: 'UserPersonalSettingsNotificationMatrixChannel',
  78. email: true,
  79. online: true,
  80. },
  81. criteria: {
  82. __typename: 'UserPersonalSettingsNotificationMatrixCriteria',
  83. no: false,
  84. ownedByMe: true,
  85. ownedByNobody: true,
  86. subscribed: true,
  87. },
  88. },
  89. },
  90. },
  91. notificationSound: {
  92. __typename: 'UserPersonalSettingsNotificationSound',
  93. enabled: true,
  94. file: 'Ring',
  95. },
  96. },
  97. objectAttributeValues: [],
  98. authorizations: [],
  99. organization: {
  100. __typename: 'Organization',
  101. id: '234241',
  102. internalId: 1,
  103. name: 'Zammad Foundation',
  104. objectAttributeValues: [],
  105. active: true,
  106. },
  107. permissions: {
  108. __typename: 'Permission',
  109. names: ['admin'],
  110. },
  111. hasSecondaryOrganizations: false,
  112. outOfOffice: false,
  113. outOfOfficeStartAt: null,
  114. outOfOfficeEndAt: null,
  115. outOfOfficeReplacement: null,
  116. }
  117. describe('Session Store', () => {
  118. beforeEach(() => {
  119. setActivePinia(createPinia())
  120. })
  121. it('is empty by default', () => {
  122. const session = useSessionStore()
  123. expect(session.id).toBe(null)
  124. expect(session.user).toBe(null)
  125. })
  126. it('get current user and check on user update subscription', async () => {
  127. const session = useSessionStore()
  128. mockGraphQLApi(CurrentUserDocument).willResolve({
  129. currentUser: userData,
  130. })
  131. const userUpdateSubscription = mockGraphQLSubscription(
  132. CurrentUserUpdatesDocument,
  133. )
  134. await session.getCurrentUser()
  135. expect(session.user).toEqual(userData)
  136. const updatedUserData = {
  137. ...userData,
  138. firstname: 'Jane',
  139. lastname: 'Doe',
  140. fullname: 'Jane Doe',
  141. }
  142. await userUpdateSubscription.next({
  143. data: {
  144. userUpdates: {
  145. __typename: 'UserUpdatesPayload',
  146. user: updatedUserData,
  147. },
  148. },
  149. })
  150. expect(session.user).toEqual(updatedUserData)
  151. })
  152. })