123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313 |
- import {OrganizationFixture} from 'sentry-fixture/organization';
- import {initializeOrg} from 'sentry-test/initializeOrg';
- import {
- render,
- renderGlobalModal,
- screen,
- userEvent,
- waitForElementToBeRemoved,
- } from 'sentry-test/reactTestingLibrary';
- import {textWithMarkupMatcher} from 'sentry-test/utils';
- import * as indicators from 'sentry/actionCreators/indicator';
- import OrganizationsStore from 'sentry/stores/organizationsStore';
- import type {OrgAuthToken} from 'sentry/types';
- import {OrganizationAuthTokensIndex} from 'sentry/views/settings/organizationAuthTokens';
- describe('OrganizationAuthTokensIndex', function () {
- const ENDPOINT = '/organizations/org-slug/org-auth-tokens/';
- const PROJECTS_ENDPOINT = '/organizations/org-slug/projects/';
- const {organization, project, router} = initializeOrg();
- const defaultProps = {
- organization,
- router,
- location: router.location,
- params: {orgId: organization.slug},
- routes: router.routes,
- route: {},
- routeParams: router.params,
- };
- let projectsMock: jest.Mock<any>;
- beforeEach(function () {
- OrganizationsStore.addOrReplace(organization);
- projectsMock = MockApiClient.addMockResponse({
- url: PROJECTS_ENDPOINT,
- method: 'GET',
- body: [project],
- });
- });
- afterEach(function () {
- MockApiClient.clearMockResponses();
- });
- it('shows tokens', async function () {
- const tokens: OrgAuthToken[] = [
- {
- id: '1',
- name: 'My Token 1',
- tokenLastCharacters: '1234',
- dateCreated: new Date('2023-01-01T00:00:00.000Z'),
- scopes: ['org:read'],
- },
- {
- id: '2',
- name: 'My Token 2',
- tokenLastCharacters: 'ABCD',
- dateCreated: new Date('2023-01-01T00:00:00.000Z'),
- scopes: ['org:read'],
- dateLastUsed: new Date(),
- projectLastUsedId: project.id,
- },
- ];
- const mock = MockApiClient.addMockResponse({
- url: ENDPOINT,
- method: 'GET',
- body: tokens,
- });
- render(<OrganizationAuthTokensIndex {...defaultProps} />);
- await waitForElementToBeRemoved(() => screen.queryByTestId('loading-indicator'));
- // Then list
- expect(
- await screen.findByText(
- textWithMarkupMatcher('a few seconds ago in project Project Name')
- )
- ).toBeInTheDocument();
- expect(screen.getByText('My Token 1')).toBeInTheDocument();
- expect(screen.getByText('My Token 2')).toBeInTheDocument();
- expect(screen.getByText('never used')).toBeInTheDocument();
- expect(screen.queryByTestId('loading-error')).not.toBeInTheDocument();
- expect(screen.queryByTestId('loading-indicator')).not.toBeInTheDocument();
- expect(screen.queryByTestId('empty-state')).not.toBeInTheDocument();
- expect(mock).toHaveBeenCalledTimes(1);
- expect(mock).toHaveBeenCalledWith(ENDPOINT, expect.objectContaining({method: 'GET'}));
- expect(projectsMock).toHaveBeenCalledTimes(1);
- expect(projectsMock).toHaveBeenCalledWith(
- PROJECTS_ENDPOINT,
- expect.objectContaining({method: 'GET', query: {query: `id:${project.id}`}})
- );
- });
- it('shows unused tokens', async function () {
- const tokens: OrgAuthToken[] = [
- {
- id: '1',
- name: 'My Token 1',
- tokenLastCharacters: '1234',
- dateCreated: new Date('2023-01-01T00:00:00.000Z'),
- scopes: ['org:read'],
- },
- {
- id: '2',
- name: 'My Token 2',
- tokenLastCharacters: 'ABCD',
- dateCreated: new Date('2023-01-01T00:00:00.000Z'),
- scopes: ['org:read'],
- },
- ];
- MockApiClient.addMockResponse({
- url: ENDPOINT,
- method: 'GET',
- body: tokens,
- });
- render(<OrganizationAuthTokensIndex {...defaultProps} />);
- await waitForElementToBeRemoved(() => screen.queryByTestId('loading-indicator'));
- // Then list
- expect(screen.getByText('My Token 1')).toBeInTheDocument();
- expect(screen.getByText('My Token 2')).toBeInTheDocument();
- expect(screen.getAllByText('never used')).toHaveLength(2);
- });
- it('handle error when loading tokens', async function () {
- const mock = MockApiClient.addMockResponse({
- url: ENDPOINT,
- method: 'GET',
- statusCode: 400,
- });
- render(<OrganizationAuthTokensIndex {...defaultProps} />);
- expect(await screen.findByTestId('loading-error')).toHaveTextContent(
- 'Failed to load auth tokens for the organization.'
- );
- expect(screen.queryByTestId('loading-indicator')).not.toBeInTheDocument();
- expect(screen.queryByTestId('empty-state')).not.toBeInTheDocument();
- expect(mock).toHaveBeenCalledTimes(1);
- });
- it('shows empty state', async function () {
- const tokens: OrgAuthToken[] = [];
- MockApiClient.addMockResponse({
- url: ENDPOINT,
- method: 'GET',
- body: tokens,
- });
- render(<OrganizationAuthTokensIndex {...defaultProps} />);
- await waitForElementToBeRemoved(() => screen.queryByTestId('loading-indicator'));
- expect(screen.getByTestId('empty-state')).toBeInTheDocument();
- expect(screen.queryByTestId('loading-error')).not.toBeInTheDocument();
- expect(screen.queryByTestId('loading-indicator')).not.toBeInTheDocument();
- });
- describe('revoking', function () {
- it('allows to revoke tokens', async function () {
- jest.spyOn(indicators, 'addSuccessMessage');
- const tokens: OrgAuthToken[] = [
- {
- id: '1',
- name: 'My Token 1',
- tokenLastCharacters: '1234',
- dateCreated: new Date('2023-01-01T00:00:00.000Z'),
- scopes: ['org:read'],
- },
- {
- id: '2',
- name: 'My Token 2',
- tokenLastCharacters: 'ABCD',
- dateCreated: new Date('2023-01-01T00:00:00.000Z'),
- scopes: ['org:read'],
- },
- {
- id: '3',
- name: 'My Token 3',
- tokenLastCharacters: 'ABCD',
- dateCreated: new Date('2023-01-01T00:00:00.000Z'),
- scopes: ['org:read'],
- },
- ];
- MockApiClient.addMockResponse({
- url: ENDPOINT,
- method: 'GET',
- body: tokens,
- });
- const deleteMock = MockApiClient.addMockResponse({
- url: `${ENDPOINT}2/`,
- method: 'DELETE',
- });
- render(<OrganizationAuthTokensIndex {...defaultProps} />);
- renderGlobalModal();
- expect(await screen.findByText('My Token 1')).toBeInTheDocument();
- expect(screen.getByText('My Token 2')).toBeInTheDocument();
- expect(screen.getByText('My Token 3')).toBeInTheDocument();
- expect(screen.getByLabelText('Revoke My Token 2')).toBeEnabled();
- await userEvent.click(screen.getByLabelText('Revoke My Token 2'));
- // Confirm modal
- await userEvent.click(screen.getByRole('button', {name: 'Confirm'}));
- expect(screen.getByText('My Token 1')).toBeInTheDocument();
- expect(screen.queryByText('My Token 2')).not.toBeInTheDocument();
- expect(screen.getByText('My Token 3')).toBeInTheDocument();
- expect(indicators.addSuccessMessage).toHaveBeenCalledWith(
- 'Revoked auth token for the organization.'
- );
- expect(deleteMock).toHaveBeenCalledTimes(1);
- });
- it('handles API error when revoking token', async function () {
- jest.spyOn(indicators, 'addErrorMessage');
- const tokens: OrgAuthToken[] = [
- {
- id: '1',
- name: 'My Token 1',
- tokenLastCharacters: '1234',
- dateCreated: new Date('2023-01-01T00:00:00.000Z'),
- scopes: ['org:read'],
- },
- ];
- MockApiClient.addMockResponse({
- url: ENDPOINT,
- method: 'GET',
- body: tokens,
- });
- const deleteMock = MockApiClient.addMockResponse({
- url: `${ENDPOINT}1/`,
- method: 'DELETE',
- statusCode: 400,
- });
- render(<OrganizationAuthTokensIndex {...defaultProps} />);
- renderGlobalModal();
- expect(await screen.findByText('My Token 1')).toBeInTheDocument();
- expect(screen.getByLabelText('Revoke My Token 1')).toBeEnabled();
- await userEvent.click(screen.getByLabelText('Revoke My Token 1'));
- // Confirm modal
- await userEvent.click(screen.getByRole('button', {name: 'Confirm'}));
- expect(screen.getByText('My Token 1')).toBeInTheDocument();
- expect(indicators.addErrorMessage).toHaveBeenCalledWith(
- 'Failed to revoke the auth token for the organization.'
- );
- expect(deleteMock).toHaveBeenCalledTimes(1);
- });
- it('does not allow to revoke without permission', async function () {
- const org = OrganizationFixture({
- access: ['org:read'],
- });
- const tokens: OrgAuthToken[] = [
- {
- id: '1',
- name: 'My Token 1',
- tokenLastCharacters: '1234',
- dateCreated: new Date('2023-01-01T00:00:00.000Z'),
- scopes: ['org:read'],
- },
- ];
- const props = {
- ...defaultProps,
- organization: org,
- };
- MockApiClient.addMockResponse({
- url: ENDPOINT,
- method: 'GET',
- body: tokens,
- });
- render(<OrganizationAuthTokensIndex {...props} />, {organization: org});
- expect(await screen.findByText('My Token 1')).toBeInTheDocument();
- expect(screen.getByLabelText('Revoke My Token 1')).toBeDisabled();
- });
- });
- });
|