organizationContext.spec.tsx 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. import type {RouteContextInterface} from 'react-router';
  2. import {LocationFixture} from 'sentry-fixture/locationFixture';
  3. import {OrganizationFixture} from 'sentry-fixture/organization';
  4. import {ProjectFixture} from 'sentry-fixture/project';
  5. import {RouterFixture} from 'sentry-fixture/routerFixture';
  6. import {TeamFixture} from 'sentry-fixture/team';
  7. import {UserFixture} from 'sentry-fixture/user';
  8. import {render, screen, waitFor} from 'sentry-test/reactTestingLibrary';
  9. import * as orgsActionCreators from 'sentry/actionCreators/organizations';
  10. import {openSudo} from 'sentry/actionCreators/sudoModal';
  11. import ConfigStore from 'sentry/stores/configStore';
  12. import OrganizationStore from 'sentry/stores/organizationStore';
  13. import ProjectsStore from 'sentry/stores/projectsStore';
  14. import TeamStore from 'sentry/stores/teamStore';
  15. import type {Organization} from 'sentry/types/organization';
  16. import useOrganization from 'sentry/utils/useOrganization';
  17. import {OrganizationContextProvider, useEnsureOrganization} from './organizationContext';
  18. import {RouteContext} from './routeContext';
  19. jest.mock('sentry/actionCreators/sudoModal');
  20. describe('OrganizationContext', function () {
  21. let getOrgMock: jest.Mock;
  22. let getProjectsMock: jest.Mock;
  23. let getTeamsMock: jest.Mock;
  24. const organization = OrganizationFixture();
  25. const project = ProjectFixture();
  26. const team = TeamFixture();
  27. const router: RouteContextInterface = {
  28. router: RouterFixture(),
  29. location: LocationFixture(),
  30. routes: [],
  31. params: {orgId: organization.slug},
  32. };
  33. function setupOrgMocks(org: Organization) {
  34. const orgMock = MockApiClient.addMockResponse({
  35. url: `/organizations/${org.slug}/`,
  36. body: org,
  37. });
  38. const projectMock = MockApiClient.addMockResponse({
  39. url: `/organizations/${org.slug}/projects/`,
  40. body: [project],
  41. });
  42. const teamMock = MockApiClient.addMockResponse({
  43. url: `/organizations/${org.slug}/teams/`,
  44. body: [team],
  45. });
  46. return {orgMock, projectMock, teamMock};
  47. }
  48. beforeEach(function () {
  49. MockApiClient.clearMockResponses();
  50. const {orgMock, projectMock, teamMock} = setupOrgMocks(organization);
  51. getOrgMock = orgMock;
  52. getProjectsMock = projectMock;
  53. getTeamsMock = teamMock;
  54. jest.spyOn(TeamStore, 'loadInitialData');
  55. jest.spyOn(ProjectsStore, 'loadInitialData');
  56. ConfigStore.init();
  57. OrganizationStore.reset();
  58. jest.spyOn(console, 'error').mockImplementation(jest.fn());
  59. });
  60. afterEach(function () {
  61. // eslint-disable-next-line no-console
  62. jest.mocked(console.error).mockRestore();
  63. });
  64. function OrganizationLoaderStub() {
  65. useEnsureOrganization();
  66. return null;
  67. }
  68. /**
  69. * Used to test that the organization context is propegated
  70. */
  71. function OrganizationName() {
  72. const org = useOrganization({allowNull: true});
  73. return <div>{org?.slug ?? 'no-org'}</div>;
  74. }
  75. it('fetches org, projects, teams, and provides organization context', async function () {
  76. render(
  77. <OrganizationContextProvider>
  78. <OrganizationLoaderStub />
  79. <OrganizationName />
  80. </OrganizationContextProvider>
  81. );
  82. expect(await screen.findByText(organization.slug)).toBeInTheDocument();
  83. expect(getOrgMock).toHaveBeenCalled();
  84. expect(getProjectsMock).toHaveBeenCalled();
  85. expect(getTeamsMock).toHaveBeenCalled();
  86. });
  87. it('does not fetch if organization is already set', async function () {
  88. OrganizationStore.onUpdate(organization);
  89. render(
  90. <OrganizationContextProvider>
  91. <OrganizationLoaderStub />
  92. <OrganizationName />
  93. </OrganizationContextProvider>
  94. );
  95. expect(await screen.findByText(organization.slug)).toBeInTheDocument();
  96. expect(getOrgMock).not.toHaveBeenCalled();
  97. });
  98. it('fetches new org when router params change', async function () {
  99. // First render with org-slug
  100. const {rerender} = render(
  101. <RouteContext.Provider value={router}>
  102. <OrganizationContextProvider>
  103. <OrganizationLoaderStub />
  104. <OrganizationName />
  105. </OrganizationContextProvider>
  106. </RouteContext.Provider>
  107. );
  108. expect(await screen.findByText(organization.slug)).toBeInTheDocument();
  109. const anotherOrg = OrganizationFixture({slug: 'another-org'});
  110. const {orgMock, projectMock, teamMock} = setupOrgMocks(anotherOrg);
  111. const switchOrganization = jest.spyOn(orgsActionCreators, 'switchOrganization');
  112. // re-render with another-org
  113. rerender(
  114. <RouteContext.Provider value={{...router, params: {orgId: 'another-org'}}}>
  115. <OrganizationContextProvider>
  116. <OrganizationLoaderStub />
  117. <OrganizationName />
  118. </OrganizationContextProvider>
  119. </RouteContext.Provider>
  120. );
  121. expect(await screen.findByText(anotherOrg.slug)).toBeInTheDocument();
  122. expect(orgMock).toHaveBeenCalled();
  123. expect(projectMock).toHaveBeenCalled();
  124. expect(teamMock).toHaveBeenCalled();
  125. expect(switchOrganization).toHaveBeenCalled();
  126. });
  127. it('opens sudo modal for superusers for nonmember org with active staff', async function () {
  128. ConfigStore.set('user', UserFixture({isSuperuser: true, isStaff: true}));
  129. organization.access = [];
  130. getOrgMock = MockApiClient.addMockResponse({
  131. url: `/organizations/${organization.slug}/`,
  132. body: organization,
  133. });
  134. render(
  135. <OrganizationContextProvider>
  136. <OrganizationLoaderStub />
  137. <OrganizationName />
  138. </OrganizationContextProvider>
  139. );
  140. await waitFor(() => !OrganizationStore.getState().loading);
  141. await waitFor(() => expect(openSudo).toHaveBeenCalled());
  142. });
  143. it('opens sudo modal for superusers on 403s', async function () {
  144. ConfigStore.set('user', UserFixture({isSuperuser: true}));
  145. getOrgMock = MockApiClient.addMockResponse({
  146. url: '/organizations/org-slug/',
  147. statusCode: 403,
  148. });
  149. render(
  150. <OrganizationContextProvider>
  151. <OrganizationLoaderStub />
  152. <OrganizationName />
  153. </OrganizationContextProvider>
  154. );
  155. await waitFor(() => !OrganizationStore.getState().loading);
  156. // eslint-disable-next-line no-console
  157. await waitFor(() => expect(console.error).toHaveBeenCalled());
  158. expect(openSudo).toHaveBeenCalled();
  159. });
  160. /**
  161. * This test will rarely happen since most configurations are now using customer domains
  162. */
  163. it('uses last organization slug from ConfigStore', async function () {
  164. const configStoreOrg = OrganizationFixture({slug: 'config-store-org'});
  165. ConfigStore.set('lastOrganization', configStoreOrg.slug);
  166. const {orgMock, projectMock, teamMock} = setupOrgMocks(configStoreOrg);
  167. // orgId is not present in the router.
  168. render(
  169. <RouteContext.Provider value={{...router, params: {}}}>
  170. <OrganizationContextProvider>
  171. <OrganizationLoaderStub />
  172. <OrganizationName />
  173. </OrganizationContextProvider>
  174. </RouteContext.Provider>
  175. );
  176. expect(await screen.findByText(configStoreOrg.slug)).toBeInTheDocument();
  177. expect(orgMock).toHaveBeenCalled();
  178. expect(projectMock).toHaveBeenCalled();
  179. expect(teamMock).toHaveBeenCalled();
  180. });
  181. });