123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143 |
- import {render, screen, userEvent, waitFor} from 'sentry-test/reactTestingLibrary';
- import selectEvent from 'sentry-test/selectEvent';
- import ApiNewToken from 'sentry/views/settings/account/apiNewToken';
- describe('ApiNewToken', function () {
- it('renders', function () {
- render(<ApiNewToken />);
- });
- it('renders with disabled "Create Token" button', async function () {
- render(<ApiNewToken />);
- expect(await screen.getByRole('button', {name: 'Create Token'})).toBeDisabled();
- });
- it('submits with correct hierarchical scopes', async function () {
- MockApiClient.clearMockResponses();
- const assignMock = MockApiClient.addMockResponse({
- method: 'POST',
- url: `/api-tokens/`,
- });
- render(<ApiNewToken />);
- const createButton = await screen.getByRole('button', {name: 'Create Token'});
- const selectByValue = (name, value) =>
- selectEvent.select(screen.getByRole('textbox', {name}), value);
- // Assigning Admin here will also grant read + write access to the resource
- await selectByValue('Project', 'Admin');
- await selectByValue('Release', 'Admin');
- await selectByValue('Team', 'Admin');
- await selectByValue('Issue & Event', 'Admin');
- await selectByValue('Organization', 'Admin');
- await selectByValue('Member', 'Admin');
- await userEvent.click(createButton);
- await waitFor(() =>
- expect(assignMock).toHaveBeenCalledWith(
- '/api-tokens/',
- expect.objectContaining({
- data: expect.objectContaining({
- scopes: expect.arrayContaining([
- 'project:read',
- 'project:write',
- 'project:admin',
- 'project:releases',
- 'team:read',
- 'team:write',
- 'team:admin',
- 'event:read',
- 'event:write',
- 'event:admin',
- 'org:read',
- 'org:write',
- 'org:admin',
- 'org:integrations',
- 'member:read',
- 'member:write',
- 'member:admin',
- ]),
- }),
- })
- )
- );
- });
- it('creates token with optional name', async function () {
- MockApiClient.clearMockResponses();
- const assignMock = MockApiClient.addMockResponse({
- method: 'POST',
- url: `/api-tokens/`,
- });
- render(<ApiNewToken />);
- const createButton = screen.getByRole('button', {name: 'Create Token'});
- const selectByValue = (name, value) =>
- selectEvent.select(screen.getByRole('textbox', {name}), value);
- await selectByValue('Project', 'Admin');
- await selectByValue('Release', 'Admin');
- await userEvent.type(screen.getByLabelText('Name'), 'My Token');
- await userEvent.click(createButton);
- await waitFor(() =>
- expect(assignMock).toHaveBeenCalledWith(
- '/api-tokens/',
- expect.objectContaining({
- data: expect.objectContaining({
- name: 'My Token',
- scopes: expect.arrayContaining([
- 'project:read',
- 'project:write',
- 'project:admin',
- 'project:releases',
- ]),
- }),
- })
- )
- );
- });
- it('creates token without name', async function () {
- MockApiClient.clearMockResponses();
- const assignMock = MockApiClient.addMockResponse({
- method: 'POST',
- url: `/api-tokens/`,
- });
- render(<ApiNewToken />);
- const createButton = screen.getByRole('button', {name: 'Create Token'});
- const selectByValue = (name, value) =>
- selectEvent.select(screen.getByRole('textbox', {name}), value);
- await selectByValue('Project', 'Admin');
- await selectByValue('Release', 'Admin');
- await userEvent.click(createButton);
- await waitFor(() =>
- expect(assignMock).toHaveBeenCalledWith(
- '/api-tokens/',
- expect.objectContaining({
- data: expect.objectContaining({
- name: '', // expect a blank name
- scopes: expect.arrayContaining([
- 'project:read',
- 'project:write',
- 'project:admin',
- 'project:releases',
- ]),
- }),
- })
- )
- );
- });
- });
|