newAuthToken.spec.tsx 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. import {initializeOrg} from 'sentry-test/initializeOrg';
  2. import {render, screen, userEvent} from 'sentry-test/reactTestingLibrary';
  3. import * as indicators from 'sentry/actionCreators/indicator';
  4. import OrganizationsStore from 'sentry/stores/organizationsStore';
  5. import type {OrgAuthToken} from 'sentry/types';
  6. import {OrganizationAuthTokensNewAuthToken} from 'sentry/views/settings/organizationAuthTokens/newAuthToken';
  7. describe('OrganizationAuthTokensNewAuthToken', function () {
  8. const ENDPOINT = '/organizations/org-slug/org-auth-tokens/';
  9. const {organization, router} = initializeOrg();
  10. const defaultProps = {
  11. organization,
  12. router,
  13. location: router.location,
  14. params: {orgId: organization.slug},
  15. routes: router.routes,
  16. route: {},
  17. routeParams: router.params,
  18. };
  19. beforeEach(function () {
  20. OrganizationsStore.addOrReplace(organization);
  21. });
  22. afterEach(function () {
  23. MockApiClient.clearMockResponses();
  24. });
  25. it('can create token', async function () {
  26. render(<OrganizationAuthTokensNewAuthToken {...defaultProps} />);
  27. const generatedToken: OrgAuthToken & {token: string} = {
  28. id: '1',
  29. name: 'My Token',
  30. token: 'sntrys_XXXXXXX',
  31. tokenLastCharacters: 'XXXX',
  32. dateCreated: new Date('2023-01-01T00:00:00.000Z'),
  33. scopes: ['org:read'],
  34. };
  35. const mock = MockApiClient.addMockResponse({
  36. url: ENDPOINT,
  37. method: 'POST',
  38. body: generatedToken,
  39. });
  40. expect(screen.queryByLabelText('Generated token')).not.toBeInTheDocument();
  41. await userEvent.type(screen.getByLabelText('Name'), 'My Token');
  42. await userEvent.click(screen.getByRole('button', {name: 'Create Auth Token'}));
  43. expect(screen.getByLabelText('Generated token')).toHaveValue('sntrys_XXXXXXX');
  44. expect(screen.queryByLabelText('Name')).not.toBeInTheDocument();
  45. expect(mock).toHaveBeenCalledWith(
  46. ENDPOINT,
  47. expect.objectContaining({
  48. data: {name: 'My Token'},
  49. })
  50. );
  51. });
  52. it('handles API errors when creating token', async function () {
  53. jest.spyOn(indicators, 'addErrorMessage');
  54. render(<OrganizationAuthTokensNewAuthToken {...defaultProps} />);
  55. const mock = MockApiClient.addMockResponse({
  56. url: ENDPOINT,
  57. method: 'POST',
  58. body: {
  59. detail: 'Test API error occurred.',
  60. },
  61. statusCode: 400,
  62. });
  63. expect(screen.queryByLabelText('Generated token')).not.toBeInTheDocument();
  64. await userEvent.type(screen.getByLabelText('Name'), 'My Token');
  65. await userEvent.click(screen.getByRole('button', {name: 'Create Auth Token'}));
  66. expect(screen.queryByLabelText('Generated token')).not.toBeInTheDocument();
  67. expect(indicators.addErrorMessage).toHaveBeenCalledWith(
  68. 'Failed to create a new auth token.'
  69. );
  70. expect(mock).toHaveBeenCalledWith(
  71. ENDPOINT,
  72. expect.objectContaining({
  73. data: {name: 'My Token'},
  74. })
  75. );
  76. });
  77. it('handles missing_system_url_prefix API error when creating token', async function () {
  78. jest.spyOn(indicators, 'addErrorMessage');
  79. render(<OrganizationAuthTokensNewAuthToken {...defaultProps} />);
  80. const mock = MockApiClient.addMockResponse({
  81. url: ENDPOINT,
  82. method: 'POST',
  83. body: {
  84. detail: {message: 'test message', code: 'missing_system_url_prefix'},
  85. },
  86. statusCode: 400,
  87. });
  88. expect(screen.queryByLabelText('Generated token')).not.toBeInTheDocument();
  89. await userEvent.type(screen.getByLabelText('Name'), 'My Token');
  90. await userEvent.click(screen.getByRole('button', {name: 'Create Auth Token'}));
  91. expect(screen.queryByLabelText('Generated token')).not.toBeInTheDocument();
  92. expect(indicators.addErrorMessage).toHaveBeenCalledWith(
  93. 'You have to configure `system.url-prefix` in your Sentry instance in order to generate tokens.'
  94. );
  95. expect(mock).toHaveBeenCalledWith(
  96. ENDPOINT,
  97. expect.objectContaining({
  98. data: {name: 'My Token'},
  99. })
  100. );
  101. });
  102. });