organizationContext.spec.tsx 7.8 KB

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