organizationTeams.spec.tsx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. import {AccessRequestFixture} from 'sentry-fixture/accessRequest';
  2. import {OrganizationFixture} from 'sentry-fixture/organization';
  3. import {TeamFixture} from 'sentry-fixture/team';
  4. import {UserFixture} from 'sentry-fixture/user';
  5. import {initializeOrg} from 'sentry-test/initializeOrg';
  6. import {act, render, screen, userEvent, waitFor} from 'sentry-test/reactTestingLibrary';
  7. import {openCreateTeamModal} from 'sentry/actionCreators/modal';
  8. import TeamStore from 'sentry/stores/teamStore';
  9. import recreateRoute from 'sentry/utils/recreateRoute';
  10. import OrganizationTeams from 'sentry/views/settings/organizationTeams/organizationTeams';
  11. jest.mocked(recreateRoute).mockReturnValue('');
  12. jest.mock('sentry/actionCreators/modal', () => ({
  13. openCreateTeamModal: jest.fn(),
  14. }));
  15. describe('OrganizationTeams', function () {
  16. describe('Open Membership', function () {
  17. const {organization, project, routerProps} = initializeOrg({
  18. organization: {
  19. openMembership: true,
  20. },
  21. });
  22. const createWrapper = (
  23. props?: Partial<React.ComponentProps<typeof OrganizationTeams>>
  24. ) =>
  25. render(
  26. <OrganizationTeams
  27. {...routerProps}
  28. onRemoveAccessRequest={() => {}}
  29. requestList={[]}
  30. params={{projectId: project.slug}}
  31. features={new Set(['open-membership'])}
  32. access={new Set(['project:admin'])}
  33. organization={organization}
  34. {...props}
  35. />
  36. );
  37. it('opens "create team modal" when creating a new team from header', async function () {
  38. createWrapper();
  39. // Click "Create Team" in Panel Header
  40. await userEvent.click(screen.getByLabelText('Create Team'));
  41. // action creator to open "create team modal" is called
  42. expect(openCreateTeamModal).toHaveBeenCalledWith(
  43. expect.objectContaining({
  44. organization: expect.objectContaining({
  45. slug: organization.slug,
  46. }),
  47. })
  48. );
  49. });
  50. it('can join team and have link to details', function () {
  51. const mockTeams = [
  52. TeamFixture({
  53. hasAccess: true,
  54. isMember: false,
  55. }),
  56. ];
  57. act(() => void TeamStore.loadInitialData(mockTeams, false, null));
  58. createWrapper({
  59. access: new Set([]),
  60. });
  61. expect(screen.getByLabelText('Join Team')).toBeInTheDocument();
  62. // Should also link to details
  63. expect(screen.getByTestId('team-link')).toBeInTheDocument();
  64. });
  65. it('reloads projects after joining a team', async function () {
  66. const team = TeamFixture({
  67. hasAccess: true,
  68. isMember: false,
  69. });
  70. const getOrgMock = MockApiClient.addMockResponse({
  71. url: '/organizations/org-slug/',
  72. body: OrganizationFixture(),
  73. });
  74. MockApiClient.addMockResponse({
  75. url: `/organizations/org-slug/members/me/teams/${team.slug}/`,
  76. method: 'POST',
  77. body: {...team, isMember: true},
  78. });
  79. const mockTeams = [team];
  80. act(() => void TeamStore.loadInitialData(mockTeams, false, null));
  81. createWrapper({access: new Set([])});
  82. await userEvent.click(screen.getByLabelText('Join Team'));
  83. await waitFor(() => {
  84. expect(getOrgMock).toHaveBeenCalledTimes(1);
  85. });
  86. });
  87. it('cannot leave idp-provisioned team', function () {
  88. const mockTeams = [TeamFixture({flags: {'idp:provisioned': true}, isMember: true})];
  89. act(() => void TeamStore.loadInitialData(mockTeams, false, null));
  90. createWrapper();
  91. expect(screen.getByRole('button', {name: 'Leave Team'})).toBeDisabled();
  92. });
  93. it('cannot join idp-provisioned team', function () {
  94. const mockTeams = [
  95. TeamFixture({flags: {'idp:provisioned': true}, isMember: false}),
  96. ];
  97. act(() => void TeamStore.loadInitialData(mockTeams, false, null));
  98. createWrapper({
  99. access: new Set([]),
  100. });
  101. expect(screen.getByRole('button', {name: 'Join Team'})).toBeDisabled();
  102. });
  103. });
  104. describe('Closed Membership', function () {
  105. const {organization, project, routerProps} = initializeOrg({
  106. organization: {
  107. openMembership: false,
  108. },
  109. });
  110. const createWrapper = (
  111. props?: Partial<React.ComponentProps<typeof OrganizationTeams>>
  112. ) =>
  113. render(
  114. <OrganizationTeams
  115. {...routerProps}
  116. onRemoveAccessRequest={() => {}}
  117. requestList={[]}
  118. params={{projectId: project.slug}}
  119. features={new Set([])}
  120. access={new Set([])}
  121. organization={organization}
  122. {...props}
  123. />
  124. );
  125. it('can request access to team and does not have link to details', function () {
  126. const mockTeams = [
  127. TeamFixture({
  128. hasAccess: false,
  129. isMember: false,
  130. }),
  131. ];
  132. act(() => void TeamStore.loadInitialData(mockTeams, false, null));
  133. createWrapper({access: new Set([])});
  134. expect(screen.getByLabelText('Request Access')).toBeInTheDocument();
  135. // Should also not link to details because of lack of access
  136. expect(screen.queryByTestId('team-link')).not.toBeInTheDocument();
  137. });
  138. it('can leave team when you are a member', function () {
  139. const mockTeams = [
  140. TeamFixture({
  141. hasAccess: true,
  142. isMember: true,
  143. }),
  144. ];
  145. act(() => void TeamStore.loadInitialData(mockTeams, false, null));
  146. createWrapper({
  147. access: new Set([]),
  148. });
  149. expect(screen.getByLabelText('Leave Team')).toBeInTheDocument();
  150. });
  151. it('cannot request to join idp-provisioned team', function () {
  152. const mockTeams = [
  153. TeamFixture({flags: {'idp:provisioned': true}, isMember: false}),
  154. ];
  155. act(() => void TeamStore.loadInitialData(mockTeams, false, null));
  156. createWrapper({
  157. access: new Set([]),
  158. });
  159. expect(screen.getByRole('button', {name: 'Request Access'})).toBeDisabled();
  160. });
  161. it('cannot leave idp-provisioned team', function () {
  162. const mockTeams = [TeamFixture({flags: {'idp:provisioned': true}, isMember: true})];
  163. act(() => void TeamStore.loadInitialData(mockTeams, false, null));
  164. createWrapper({
  165. access: new Set([]),
  166. });
  167. expect(screen.getByRole('button', {name: 'Leave Team'})).toBeDisabled();
  168. });
  169. });
  170. describe('Team Requests', function () {
  171. const {organization, project, routerProps} = initializeOrg({
  172. organization: {
  173. openMembership: false,
  174. },
  175. });
  176. const orgId = organization.slug;
  177. const accessRequest = AccessRequestFixture({
  178. requester: {},
  179. });
  180. const requester = UserFixture({
  181. id: '9',
  182. username: 'requester@example.com',
  183. email: 'requester@example.com',
  184. name: 'Requester',
  185. });
  186. const requestList = [accessRequest, AccessRequestFixture({id: '4', requester})];
  187. const createWrapper = (
  188. props?: Partial<React.ComponentProps<typeof OrganizationTeams>>
  189. ) =>
  190. render(
  191. <OrganizationTeams
  192. {...routerProps}
  193. onRemoveAccessRequest={() => {}}
  194. params={{projectId: project.slug}}
  195. features={new Set([])}
  196. access={new Set([])}
  197. organization={organization}
  198. requestList={requestList}
  199. {...props}
  200. />
  201. );
  202. it('renders team request panel', function () {
  203. createWrapper();
  204. expect(screen.getByText('Pending Team Requests')).toBeInTheDocument();
  205. expect(screen.queryAllByTestId('request-message')).toHaveLength(2);
  206. expect(screen.queryAllByTestId('request-message')[0]).toHaveTextContent(
  207. `${accessRequest.member.user?.name} requests access to the #${accessRequest.team.slug} team`
  208. );
  209. });
  210. it('can approve', async function () {
  211. const onUpdateRequestListMock = jest.fn();
  212. const approveMock = MockApiClient.addMockResponse({
  213. url: `/organizations/${orgId}/access-requests/${accessRequest.id}/`,
  214. method: 'PUT',
  215. });
  216. createWrapper({
  217. onRemoveAccessRequest: onUpdateRequestListMock,
  218. });
  219. await userEvent.click(screen.getAllByLabelText('Approve')[0]);
  220. await tick();
  221. expect(approveMock).toHaveBeenCalledWith(
  222. expect.anything(),
  223. expect.objectContaining({
  224. data: {
  225. isApproved: true,
  226. },
  227. })
  228. );
  229. expect(onUpdateRequestListMock).toHaveBeenCalledWith(accessRequest.id, true);
  230. });
  231. it('can deny', async function () {
  232. const onUpdateRequestListMock = jest.fn();
  233. const denyMock = MockApiClient.addMockResponse({
  234. url: `/organizations/${orgId}/access-requests/${accessRequest.id}/`,
  235. method: 'PUT',
  236. });
  237. createWrapper({
  238. onRemoveAccessRequest: onUpdateRequestListMock,
  239. });
  240. await userEvent.click(screen.getAllByLabelText('Deny')[0]);
  241. await tick();
  242. expect(denyMock).toHaveBeenCalledWith(
  243. expect.anything(),
  244. expect.objectContaining({
  245. data: {
  246. isApproved: false,
  247. },
  248. })
  249. );
  250. expect(onUpdateRequestListMock).toHaveBeenCalledWith(accessRequest.id, false);
  251. });
  252. });
  253. describe('Team Roles', function () {
  254. const features = new Set(['team-roles']);
  255. const access = new Set<string>();
  256. it('does not render alert without feature flag', function () {
  257. const {organization, project, routerProps} = initializeOrg({
  258. organization: {orgRole: 'admin'},
  259. });
  260. render(
  261. <OrganizationTeams
  262. {...routerProps}
  263. requestList={[]}
  264. onRemoveAccessRequest={() => {}}
  265. params={{projectId: project.slug}}
  266. features={new Set()}
  267. access={access}
  268. organization={organization}
  269. />
  270. );
  271. expect(screen.queryByText('a minimum team-level role of')).not.toBeInTheDocument();
  272. });
  273. it('renders alert with elevated org role', function () {
  274. const {organization, project, routerProps} = initializeOrg({
  275. organization: {orgRole: 'admin'},
  276. });
  277. render(
  278. <OrganizationTeams
  279. {...routerProps}
  280. requestList={[]}
  281. onRemoveAccessRequest={() => {}}
  282. params={{projectId: project.slug}}
  283. features={features}
  284. access={access}
  285. organization={organization}
  286. />
  287. );
  288. expect(
  289. // Text broken up by styles
  290. screen.getByText(
  291. 'Your organization role as an has granted you a minimum team-level role of'
  292. )
  293. ).toBeInTheDocument();
  294. });
  295. it('does not render alert with lowest org role', function () {
  296. const {organization, project, routerProps} = initializeOrg({
  297. organization: {orgRole: 'member'},
  298. });
  299. render(
  300. <OrganizationTeams
  301. {...routerProps}
  302. requestList={[]}
  303. onRemoveAccessRequest={() => {}}
  304. params={{projectId: project.slug}}
  305. features={features}
  306. access={access}
  307. organization={organization}
  308. />
  309. );
  310. expect(screen.queryByText('a minimum team-level role of')).not.toBeInTheDocument();
  311. });
  312. });
  313. });