apiNewToken.spec.tsx 4.4 KB

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