index.spec.tsx 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. import {OrganizationFixture} from 'sentry-fixture/organization';
  2. import {SecretFixture} from 'sentry-fixture/secret';
  3. import {initializeOrg} from 'sentry-test/initializeOrg';
  4. import {
  5. render,
  6. renderGlobalModal,
  7. screen,
  8. userEvent,
  9. within,
  10. } from 'sentry-test/reactTestingLibrary';
  11. import * as indicators from 'sentry/actionCreators/indicator';
  12. import OrganizationsStore from 'sentry/stores/organizationsStore';
  13. import {
  14. OrganizationFeatureFlagsChangeTracking,
  15. type Secret,
  16. } from 'sentry/views/settings/featureFlags/changeTracking';
  17. describe('OrganizationFeatureFlagsIndex', function () {
  18. const SECRETS_ENDPOINT = '/organizations/org-slug/flags/signing-secrets/';
  19. const LOGS_ENDPOINT = '/organizations/org-slug/flags/logs/';
  20. const {organization} = initializeOrg();
  21. beforeEach(function () {
  22. MockApiClient.addMockResponse({
  23. url: '/organizations/org-slug/users/1234/',
  24. body: {},
  25. });
  26. MockApiClient.addMockResponse({
  27. url: LOGS_ENDPOINT,
  28. method: 'GET',
  29. body: {data: []},
  30. });
  31. OrganizationsStore.addOrReplace(organization);
  32. });
  33. afterEach(function () {
  34. MockApiClient.clearMockResponses();
  35. });
  36. it('shows secrets', async function () {
  37. const secrets: Secret[] = [
  38. SecretFixture(),
  39. SecretFixture({id: 2, provider: 'openfeature', secret: '456def****'}),
  40. ];
  41. const mock = MockApiClient.addMockResponse({
  42. url: SECRETS_ENDPOINT,
  43. method: 'GET',
  44. body: {data: secrets},
  45. });
  46. render(<OrganizationFeatureFlagsChangeTracking />);
  47. const secretsTable = within(screen.getByTestId('secrets-table'));
  48. expect(await secretsTable.findByText('launchdarkly')).toBeInTheDocument();
  49. expect(await secretsTable.findByText('openfeature')).toBeInTheDocument();
  50. expect(secretsTable.queryByTestId('loading-error')).not.toBeInTheDocument();
  51. expect(secretsTable.queryByTestId('loading-indicator')).not.toBeInTheDocument();
  52. expect(secretsTable.queryByTestId('empty-state')).not.toBeInTheDocument();
  53. expect(mock).toHaveBeenCalledTimes(1);
  54. expect(mock).toHaveBeenCalledWith(
  55. SECRETS_ENDPOINT,
  56. expect.objectContaining({method: 'GET'})
  57. );
  58. });
  59. it('handle error when loading secrets', async function () {
  60. const mock = MockApiClient.addMockResponse({
  61. url: SECRETS_ENDPOINT,
  62. method: 'GET',
  63. statusCode: 400,
  64. });
  65. render(<OrganizationFeatureFlagsChangeTracking />);
  66. const secretsTable = within(screen.getByTestId('secrets-table'));
  67. expect(await secretsTable.findByTestId('loading-error')).toHaveTextContent(
  68. 'Failed to load secrets and providers for the organization.'
  69. );
  70. expect(secretsTable.queryByTestId('loading-indicator')).not.toBeInTheDocument();
  71. expect(secretsTable.queryByTestId('empty-state')).not.toBeInTheDocument();
  72. expect(mock).toHaveBeenCalledTimes(1);
  73. });
  74. it('shows empty state', async function () {
  75. const secrets: Secret[] = [];
  76. MockApiClient.addMockResponse({
  77. url: SECRETS_ENDPOINT,
  78. method: 'GET',
  79. body: {data: secrets},
  80. });
  81. render(<OrganizationFeatureFlagsChangeTracking />);
  82. const secretsTable = within(screen.getByTestId('secrets-table'));
  83. expect(await secretsTable.findByTestId('empty-state')).toBeInTheDocument();
  84. expect(secretsTable.queryByTestId('loading-error')).not.toBeInTheDocument();
  85. expect(secretsTable.queryByTestId('loading-indicator')).not.toBeInTheDocument();
  86. });
  87. describe('removing', function () {
  88. it('allows to remove secrets', async function () {
  89. jest.spyOn(indicators, 'addSuccessMessage');
  90. const secrets: Secret[] = [
  91. SecretFixture(),
  92. SecretFixture({id: 2, provider: 'openfeature', secret: '456def****'}),
  93. ];
  94. MockApiClient.addMockResponse({
  95. url: SECRETS_ENDPOINT,
  96. method: 'GET',
  97. body: {data: secrets},
  98. });
  99. const deleteMock = MockApiClient.addMockResponse({
  100. url: `${SECRETS_ENDPOINT}1/`,
  101. method: 'DELETE',
  102. });
  103. render(<OrganizationFeatureFlagsChangeTracking />);
  104. renderGlobalModal();
  105. const secretsTable = within(screen.getByTestId('secrets-table'));
  106. expect(await secretsTable.findByText('openfeature')).toBeInTheDocument();
  107. expect(secretsTable.getByText('launchdarkly')).toBeInTheDocument();
  108. expect(
  109. secretsTable.getByLabelText('Remove secret for launchdarkly provider')
  110. ).toBeEnabled();
  111. await userEvent.click(
  112. secretsTable.getByLabelText('Remove secret for launchdarkly provider')
  113. );
  114. // Confirm modal
  115. await userEvent.click(screen.getByRole('button', {name: 'Confirm'}));
  116. expect(secretsTable.getByText('openfeature')).toBeInTheDocument();
  117. expect(secretsTable.queryByText('launchdarkly')).not.toBeInTheDocument();
  118. expect(indicators.addSuccessMessage).toHaveBeenCalledWith(
  119. 'Removed the provider and signing secret for the organization.'
  120. );
  121. expect(deleteMock).toHaveBeenCalledTimes(1);
  122. });
  123. it('does not allow to remove without permission', async function () {
  124. const org = OrganizationFixture({
  125. access: ['org:integrations'],
  126. });
  127. const secrets: Secret[] = [
  128. SecretFixture(),
  129. SecretFixture({
  130. id: 2,
  131. provider: 'openfeature',
  132. secret: '456def**************************',
  133. }),
  134. ];
  135. MockApiClient.addMockResponse({
  136. url: SECRETS_ENDPOINT,
  137. method: 'GET',
  138. body: {data: secrets},
  139. });
  140. render(<OrganizationFeatureFlagsChangeTracking />, {organization: org});
  141. const secretsTable = within(screen.getByTestId('secrets-table'));
  142. expect(await secretsTable.findByText('launchdarkly')).toBeInTheDocument();
  143. expect(
  144. secretsTable.getByLabelText('Remove secret for launchdarkly provider')
  145. ).toBeDisabled();
  146. });
  147. });
  148. });