apiNewToken.spec.tsx 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. import {render, screen, userEvent, waitFor} from 'sentry-test/reactTestingLibrary';
  2. import selectEvent from 'sentry-test/selectEvent';
  3. import ApiNewToken from 'sentry/views/settings/account/apiNewToken';
  4. describe('ApiNewToken', function () {
  5. it('renders', function () {
  6. render(<ApiNewToken />);
  7. });
  8. it('renders with disabled "Create Token" button', async function () {
  9. render(<ApiNewToken />);
  10. expect(await screen.getByRole('button', {name: 'Create Token'})).toBeDisabled();
  11. });
  12. it('submits with correct hierarchical scopes', async function () {
  13. MockApiClient.clearMockResponses();
  14. const assignMock = MockApiClient.addMockResponse({
  15. method: 'POST',
  16. url: `/api-tokens/`,
  17. });
  18. render(<ApiNewToken />);
  19. const createButton = await screen.getByRole('button', {name: 'Create Token'});
  20. const selectByValue = (name, value) =>
  21. selectEvent.select(screen.getByRole('textbox', {name}), value);
  22. // Assigning Admin here will also grant read + write access to the resource
  23. await selectByValue('Project', 'Admin');
  24. await selectByValue('Release', 'Admin');
  25. await selectByValue('Team', 'Admin');
  26. await selectByValue('Issue & Event', 'Admin');
  27. await selectByValue('Organization', 'Admin');
  28. await selectByValue('Member', 'Admin');
  29. await userEvent.click(createButton);
  30. await waitFor(() =>
  31. expect(assignMock).toHaveBeenCalledWith(
  32. '/api-tokens/',
  33. expect.objectContaining({
  34. data: expect.objectContaining({
  35. scopes: expect.arrayContaining([
  36. 'project:read',
  37. 'project:write',
  38. 'project:admin',
  39. 'project:releases',
  40. 'team:read',
  41. 'team:write',
  42. 'team:admin',
  43. 'event:read',
  44. 'event:write',
  45. 'event:admin',
  46. 'org:read',
  47. 'org:write',
  48. 'org:admin',
  49. 'org:integrations',
  50. 'member:read',
  51. 'member:write',
  52. 'member:admin',
  53. ]),
  54. }),
  55. })
  56. )
  57. );
  58. });
  59. it('creates token with optional name', async function () {
  60. MockApiClient.clearMockResponses();
  61. const assignMock = MockApiClient.addMockResponse({
  62. method: 'POST',
  63. url: `/api-tokens/`,
  64. });
  65. render(<ApiNewToken />);
  66. const createButton = screen.getByRole('button', {name: 'Create Token'});
  67. const selectByValue = (name, value) =>
  68. selectEvent.select(screen.getByRole('textbox', {name}), value);
  69. await selectByValue('Project', 'Admin');
  70. await selectByValue('Release', 'Admin');
  71. await userEvent.type(screen.getByLabelText('Name'), 'My Token');
  72. await userEvent.click(createButton);
  73. await waitFor(() =>
  74. expect(assignMock).toHaveBeenCalledWith(
  75. '/api-tokens/',
  76. expect.objectContaining({
  77. data: expect.objectContaining({
  78. name: 'My Token',
  79. scopes: expect.arrayContaining([
  80. 'project:read',
  81. 'project:write',
  82. 'project:admin',
  83. 'project:releases',
  84. ]),
  85. }),
  86. })
  87. )
  88. );
  89. });
  90. it('creates token without name', async function () {
  91. MockApiClient.clearMockResponses();
  92. const assignMock = MockApiClient.addMockResponse({
  93. method: 'POST',
  94. url: `/api-tokens/`,
  95. });
  96. render(<ApiNewToken />);
  97. const createButton = screen.getByRole('button', {name: 'Create Token'});
  98. const selectByValue = (name, value) =>
  99. selectEvent.select(screen.getByRole('textbox', {name}), value);
  100. await selectByValue('Project', 'Admin');
  101. await selectByValue('Release', 'Admin');
  102. await userEvent.click(createButton);
  103. await waitFor(() =>
  104. expect(assignMock).toHaveBeenCalledWith(
  105. '/api-tokens/',
  106. expect.objectContaining({
  107. data: expect.objectContaining({
  108. name: '', // expect a blank name
  109. scopes: expect.arrayContaining([
  110. 'project:read',
  111. 'project:write',
  112. 'project:admin',
  113. 'project:releases',
  114. ]),
  115. }),
  116. })
  117. )
  118. );
  119. });
  120. });