organizationTeams.spec.tsx 11 KB

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