index.spec.tsx 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. import {GitHubIntegrationFixture} from 'sentry-fixture/githubIntegration';
  2. import {OrganizationFixture} from 'sentry-fixture/organization';
  3. import {ProjectFixture} from 'sentry-fixture/project';
  4. import {initializeOrg} from 'sentry-test/initializeOrg';
  5. import {
  6. act,
  7. render,
  8. renderGlobalModal,
  9. screen,
  10. userEvent,
  11. waitFor,
  12. within,
  13. } from 'sentry-test/reactTestingLibrary';
  14. import ConfigStore from 'sentry/stores/configStore';
  15. import OrganizationsStore from 'sentry/stores/organizationsStore';
  16. import ProjectsStore from 'sentry/stores/projectsStore';
  17. import type {Config} from 'sentry/types/system';
  18. import {trackAnalytics} from 'sentry/utils/analytics';
  19. import OrganizationGeneralSettings from 'sentry/views/settings/organizationGeneralSettings';
  20. jest.mock('sentry/utils/analytics');
  21. describe('OrganizationGeneralSettings', function () {
  22. const ENDPOINT = '/organizations/org-slug/';
  23. const {organization, router} = initializeOrg();
  24. let configState: Config;
  25. const defaultProps = {
  26. organization,
  27. router,
  28. location: router.location,
  29. params: {orgId: organization.slug},
  30. routes: router.routes,
  31. route: {},
  32. routeParams: router.params,
  33. };
  34. beforeEach(function () {
  35. configState = ConfigStore.getState();
  36. OrganizationsStore.addOrReplace(organization);
  37. MockApiClient.addMockResponse({
  38. url: `/organizations/${organization.slug}/auth-provider/`,
  39. method: 'GET',
  40. });
  41. MockApiClient.addMockResponse({
  42. url: `/organizations/${organization.slug}/integrations/?provider_key=github`,
  43. method: 'GET',
  44. body: [GitHubIntegrationFixture()],
  45. });
  46. });
  47. afterEach(function () {
  48. act(function () {
  49. ConfigStore.loadInitialData(configState);
  50. });
  51. });
  52. it('can enable "early adopter"', async function () {
  53. render(<OrganizationGeneralSettings {...defaultProps} />);
  54. const mock = MockApiClient.addMockResponse({
  55. url: ENDPOINT,
  56. method: 'PUT',
  57. });
  58. await userEvent.click(screen.getByRole('checkbox', {name: /early adopter/i}));
  59. await waitFor(() => {
  60. expect(mock).toHaveBeenCalledWith(
  61. ENDPOINT,
  62. expect.objectContaining({
  63. data: {isEarlyAdopter: true},
  64. })
  65. );
  66. });
  67. });
  68. it('can enable "codecov access"', async function () {
  69. const organizationWithCodecovFeature = OrganizationFixture({
  70. features: ['codecov-integration'],
  71. codecovAccess: false,
  72. });
  73. render(<OrganizationGeneralSettings {...defaultProps} />, {
  74. organization: organizationWithCodecovFeature,
  75. });
  76. const mock = MockApiClient.addMockResponse({
  77. url: ENDPOINT,
  78. method: 'PUT',
  79. });
  80. await userEvent.click(
  81. screen.getByRole('checkbox', {name: /Enable Code Coverage Insights/i})
  82. );
  83. await waitFor(() => {
  84. expect(mock).toHaveBeenCalledWith(
  85. ENDPOINT,
  86. expect.objectContaining({
  87. data: {codecovAccess: true},
  88. })
  89. );
  90. });
  91. expect(trackAnalytics).toHaveBeenCalled();
  92. });
  93. it('changes org slug and redirects to new slug', async function () {
  94. render(<OrganizationGeneralSettings {...defaultProps} />, {router});
  95. const mock = MockApiClient.addMockResponse({
  96. url: ENDPOINT,
  97. method: 'PUT',
  98. body: {...organization, slug: 'new-slug'},
  99. });
  100. await userEvent.clear(screen.getByRole('textbox', {name: /slug/i}));
  101. await userEvent.type(screen.getByRole('textbox', {name: /slug/i}), 'new-slug');
  102. await userEvent.click(screen.getByLabelText('Save'));
  103. await waitFor(() => {
  104. expect(mock).toHaveBeenCalledWith(
  105. ENDPOINT,
  106. expect.objectContaining({
  107. data: {slug: 'new-slug'},
  108. })
  109. );
  110. });
  111. await waitFor(() => {
  112. expect(router.replace).toHaveBeenCalledWith(
  113. expect.objectContaining({pathname: '/settings/new-slug/'})
  114. );
  115. });
  116. });
  117. it('changes org slug and redirects to new customer-domain', async function () {
  118. ConfigStore.set('features', new Set(['system:multi-region']));
  119. const org = OrganizationFixture();
  120. const updateMock = MockApiClient.addMockResponse({
  121. url: `/organizations/${organization.slug}/`,
  122. method: 'PUT',
  123. body: {...org, slug: 'acme', links: {organizationUrl: 'https://acme.sentry.io'}},
  124. });
  125. render(<OrganizationGeneralSettings {...defaultProps} />, {organization: org});
  126. const input = screen.getByRole('textbox', {name: /slug/i});
  127. await userEvent.clear(input);
  128. await userEvent.type(input, 'acme');
  129. await userEvent.click(screen.getByLabelText('Save'));
  130. await waitFor(() => {
  131. expect(updateMock).toHaveBeenCalledWith(
  132. '/organizations/org-slug/',
  133. expect.objectContaining({
  134. data: {
  135. slug: 'acme',
  136. },
  137. })
  138. );
  139. expect(window.location.replace).toHaveBeenCalledWith(
  140. 'https://acme.sentry.io/settings/organization/'
  141. );
  142. });
  143. });
  144. it('disables the entire form if user does not have write access', function () {
  145. const readOnlyOrg = OrganizationFixture({access: ['org:read']});
  146. render(<OrganizationGeneralSettings {...defaultProps} />, {
  147. organization: readOnlyOrg,
  148. });
  149. const formElements = [
  150. ...screen.getAllByRole('textbox'),
  151. ...screen.getAllByRole('button'),
  152. ...screen.getAllByRole('checkbox'),
  153. ];
  154. for (const formElement of formElements) {
  155. expect(formElement).toBeDisabled();
  156. }
  157. expect(
  158. screen.getByText(
  159. 'These settings can only be edited by users with the organization owner or manager role.'
  160. )
  161. ).toBeInTheDocument();
  162. });
  163. it('does not have remove organization button without org:admin permission', function () {
  164. render(<OrganizationGeneralSettings {...defaultProps} />, {
  165. organization: OrganizationFixture({
  166. access: ['org:write'],
  167. }),
  168. });
  169. expect(
  170. screen.queryByRole('button', {name: /remove organization/i})
  171. ).not.toBeInTheDocument();
  172. });
  173. it('can remove organization when org admin', async function () {
  174. act(() => ProjectsStore.loadInitialData([ProjectFixture({slug: 'project'})]));
  175. render(<OrganizationGeneralSettings {...defaultProps} />, {
  176. organization: OrganizationFixture({access: ['org:admin']}),
  177. });
  178. renderGlobalModal();
  179. const mock = MockApiClient.addMockResponse({
  180. url: ENDPOINT,
  181. method: 'DELETE',
  182. });
  183. await userEvent.click(screen.getByRole('button', {name: /remove organization/i}));
  184. const modal = screen.getByRole('dialog');
  185. expect(
  186. within(modal).getByText('This will also remove the following associated projects:')
  187. ).toBeInTheDocument();
  188. expect(within(modal).getByText('project')).toBeInTheDocument();
  189. await userEvent.click(
  190. within(modal).getByRole('button', {name: /remove organization/i})
  191. );
  192. await waitFor(() => {
  193. expect(mock).toHaveBeenCalledWith(
  194. ENDPOINT,
  195. expect.objectContaining({
  196. method: 'DELETE',
  197. })
  198. );
  199. });
  200. });
  201. });