newAuthToken.spec.tsx 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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 {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. details: ['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. });