personal-setting-linked-accounts.spec.ts 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. // Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. import { visitView } from '#tests/support/components/visitView.ts'
  3. import { mockApplicationConfig } from '#tests/support/mock-applicationConfig.ts'
  4. import { mockUserCurrent } from '#tests/support/mock-userCurrent.ts'
  5. import { waitForNextTick } from '#tests/support/utils.ts'
  6. import { EnumAuthenticationProvider } from '#shared/graphql/types.ts'
  7. import { convertToGraphQLId } from '#shared/graphql/utils.ts'
  8. import { waitForUserCurrentRemoveLinkedAccountMutationCalls } from '#desktop/pages/personal-setting/graphql/mutations/userCurrentLinkedAccount.mocks.ts'
  9. describe('linked accounts page', () => {
  10. it('is not accessible if no providers are enabled', async () => {
  11. const view = await visitView('/personal-setting/linked-accounts')
  12. await vi.waitFor(() => {
  13. expect(view, 'correctly redirects to error page').toHaveCurrentUrl(
  14. '/error-tab',
  15. )
  16. })
  17. })
  18. describe('with enabled providers', () => {
  19. beforeEach(() => {
  20. mockApplicationConfig({
  21. auth_facebook: true,
  22. auth_github: true,
  23. })
  24. mockUserCurrent({
  25. lastname: 'Doe',
  26. firstname: 'John',
  27. fullname: 'John Doe',
  28. id: convertToGraphQLId('User', 4),
  29. authorizations: [
  30. {
  31. __typename: 'Authorization',
  32. id: convertToGraphQLId('Authorization', 1),
  33. provider: EnumAuthenticationProvider.Github,
  34. uid: '85683661',
  35. username: 'foobar',
  36. },
  37. ],
  38. })
  39. })
  40. it('renders a list of authorization providers', async () => {
  41. const view = await visitView('/personal-setting/linked-accounts')
  42. expect(view.getByText('GitHub')).toBeInTheDocument() // application
  43. expect(view.getByText('foobar')).toBeInTheDocument() // username for GitHub
  44. expect(view.getByText('Facebook')).toBeInTheDocument() // application
  45. expect(view.getAllByIconName('plus-square-fill')).toHaveLength(1)
  46. expect(view.getAllByIconName('trash3')).toHaveLength(1)
  47. })
  48. it('links a new authorization provider', async (context) => {
  49. context.skipConsole = true
  50. const view = await visitView('/personal-setting/linked-accounts')
  51. expect(
  52. view.queryByLabelText('Link account on GitHub'),
  53. ).not.toBeInTheDocument()
  54. expect(view.getByLabelText('Remove account link on GitHub'))
  55. expect(
  56. view.queryByLabelText('Link account on Facebook'),
  57. ).toBeInTheDocument()
  58. await view.events.click(view.getByLabelText('Link account on Facebook'))
  59. })
  60. it('removes an authorization provider', async () => {
  61. const view = await visitView('/personal-setting/linked-accounts')
  62. await view.events.click(
  63. view.getByLabelText('Remove account link on GitHub'),
  64. )
  65. expect(
  66. await view.findByRole('dialog', { name: 'Delete Object' }),
  67. ).toBeInTheDocument()
  68. expect(
  69. view.getByText('Are you sure you want to delete this object?'),
  70. ).toBeInTheDocument()
  71. await view.events.click(
  72. view.getByRole('button', { name: 'Delete Object' }),
  73. )
  74. const mockCalls =
  75. await waitForUserCurrentRemoveLinkedAccountMutationCalls()
  76. expect(mockCalls[0].variables).toEqual({
  77. provider: EnumAuthenticationProvider.Github,
  78. uid: '85683661',
  79. })
  80. mockUserCurrent({
  81. lastname: 'Doe',
  82. firstname: 'John',
  83. fullname: 'John Doe',
  84. id: convertToGraphQLId('User', 4),
  85. authorizations: [],
  86. })
  87. await waitForNextTick()
  88. expect(view.queryByIconName('trash3')).not.toBeInTheDocument()
  89. expect(view.getAllByIconName('plus-square-fill')).toHaveLength(2)
  90. })
  91. })
  92. })