ChangePasswordPage.test.tsx 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. import { fireEvent, render, screen, waitFor } from '@testing-library/react';
  2. import userEvent from '@testing-library/user-event';
  3. import React from 'react';
  4. import config from 'app/core/config';
  5. import { getNavModel } from '../../core/selectors/navModel';
  6. import { backendSrv } from '../../core/services/backend_srv';
  7. import { Props, ChangePasswordPage } from './ChangePasswordPage';
  8. import { initialUserState } from './state/reducers';
  9. const defaultProps: Props = {
  10. ...initialUserState,
  11. user: {
  12. id: 1,
  13. name: 'Test User',
  14. email: 'test@test.com',
  15. login: 'test',
  16. isDisabled: false,
  17. isGrafanaAdmin: false,
  18. orgId: 0,
  19. authLabels: ['github'],
  20. },
  21. navModel: getNavModel(
  22. {
  23. 'profile-settings': {
  24. icon: 'sliders-v-alt',
  25. id: 'profile-settings',
  26. parentItem: {
  27. id: 'profile',
  28. text: 'Test User',
  29. img: '/avatar/46d229b033af06a191ff2267bca9ae56',
  30. url: '/profile',
  31. },
  32. text: 'Preferences',
  33. url: '/profile',
  34. },
  35. },
  36. 'profile-settings'
  37. ),
  38. loadUser: jest.fn(),
  39. changePassword: jest.fn(),
  40. };
  41. async function getTestContext(overrides: Partial<Props> = {}) {
  42. jest.clearAllMocks();
  43. jest.spyOn(backendSrv, 'get').mockResolvedValue({
  44. id: 1,
  45. name: 'Test User',
  46. email: 'test@test.com',
  47. login: 'test',
  48. isDisabled: false,
  49. isGrafanaAdmin: false,
  50. orgId: 0,
  51. });
  52. const props = { ...defaultProps, ...overrides };
  53. const { rerender } = render(<ChangePasswordPage {...props} />);
  54. await waitFor(() => expect(props.loadUser).toHaveBeenCalledTimes(1));
  55. return { rerender, props };
  56. }
  57. describe('ChangePasswordPage', () => {
  58. it('should show loading placeholder', async () => {
  59. await getTestContext({ user: null });
  60. expect(screen.getByText(/loading \.\.\./i)).toBeInTheDocument();
  61. });
  62. it('should show change password form when user has loaded', async () => {
  63. await getTestContext();
  64. expect(screen.getByText('Change Your Password')).toBeInTheDocument();
  65. expect(screen.getByLabelText('Old password')).toBeInTheDocument();
  66. expect(screen.getByLabelText('New password')).toBeInTheDocument();
  67. expect(screen.getByLabelText('Confirm password')).toBeInTheDocument();
  68. expect(screen.getByRole('button', { name: 'Change Password' })).toBeInTheDocument();
  69. expect(screen.getByRole('link', { name: 'Cancel' })).toBeInTheDocument();
  70. expect(screen.getByRole('link', { name: 'Cancel' })).toHaveAttribute('href', '/profile');
  71. });
  72. it('should call changePassword if change password is valid', async () => {
  73. const { props } = await getTestContext();
  74. await userEvent.type(screen.getByLabelText('Old password'), 'test');
  75. await userEvent.type(screen.getByLabelText('New password'), 'admin');
  76. await userEvent.type(screen.getByLabelText('Confirm password'), 'admin');
  77. fireEvent.click(screen.getByRole('button', { name: 'Change Password' }));
  78. await waitFor(() => {
  79. expect(props.changePassword).toHaveBeenCalledTimes(1);
  80. expect(props.changePassword).toHaveBeenCalledWith(
  81. {
  82. confirmNew: 'admin',
  83. newPassword: 'admin',
  84. oldPassword: 'test',
  85. },
  86. expect.anything()
  87. );
  88. });
  89. });
  90. it('should cannot change password form if ldap or authProxy enabled', async () => {
  91. config.ldapEnabled = true;
  92. const { rerender } = await getTestContext();
  93. expect(
  94. screen.getByText('You cannot change password when LDAP or auth proxy authentication is enabled.')
  95. ).toBeInTheDocument();
  96. config.ldapEnabled = false;
  97. config.authProxyEnabled = true;
  98. rerender(<ChangePasswordPage {...defaultProps} />);
  99. expect(
  100. screen.getByText('You cannot change password when LDAP or auth proxy authentication is enabled.')
  101. ).toBeInTheDocument();
  102. config.authProxyEnabled = false;
  103. });
  104. it('should show cannot change password if disableLoginForm is true and auth', async () => {
  105. config.disableLoginForm = true;
  106. await getTestContext();
  107. expect(screen.getByText('Password cannot be changed here.')).toBeInTheDocument();
  108. config.disableLoginForm = false;
  109. });
  110. });