organizationTeams.spec.tsx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. import {initializeOrg} from 'sentry-test/initializeOrg';
  2. import {act, render, screen, userEvent, waitFor} from 'sentry-test/reactTestingLibrary';
  3. import {openCreateTeamModal} from 'sentry/actionCreators/modal';
  4. import TeamStore from 'sentry/stores/teamStore';
  5. import recreateRoute from 'sentry/utils/recreateRoute';
  6. import OrganizationTeams from 'sentry/views/settings/organizationTeams/organizationTeams';
  7. jest.mocked(recreateRoute).mockReturnValue('');
  8. jest.mock('sentry/actionCreators/modal', () => ({
  9. openCreateTeamModal: jest.fn(),
  10. }));
  11. describe('OrganizationTeams', function () {
  12. describe('Open Membership', function () {
  13. const {organization, project, routerProps} = initializeOrg({
  14. organization: {
  15. openMembership: true,
  16. },
  17. });
  18. const createWrapper = (
  19. props?: Partial<React.ComponentProps<typeof OrganizationTeams>>
  20. ) =>
  21. render(
  22. <OrganizationTeams
  23. {...routerProps}
  24. onRemoveAccessRequest={() => {}}
  25. requestList={[]}
  26. params={{projectId: project.slug}}
  27. features={new Set(['open-membership'])}
  28. access={new Set(['project:admin'])}
  29. organization={organization}
  30. {...props}
  31. />
  32. );
  33. it('opens "create team modal" when creating a new team from header', async function () {
  34. createWrapper();
  35. // Click "Create Team" in Panel Header
  36. await userEvent.click(screen.getByLabelText('Create Team'));
  37. // action creator to open "create team modal" is called
  38. expect(openCreateTeamModal).toHaveBeenCalledWith(
  39. expect.objectContaining({
  40. organization: expect.objectContaining({
  41. slug: organization.slug,
  42. }),
  43. })
  44. );
  45. });
  46. it('can join team and have link to details', function () {
  47. const mockTeams = [
  48. TestStubs.Team({
  49. hasAccess: true,
  50. isMember: false,
  51. }),
  52. ];
  53. act(() => void TeamStore.loadInitialData(mockTeams, false, null));
  54. createWrapper({
  55. access: new Set([]),
  56. });
  57. expect(screen.getByLabelText('Join Team')).toBeInTheDocument();
  58. // Should also link to details
  59. expect(screen.getByTestId('team-link')).toBeInTheDocument();
  60. });
  61. it('reloads projects after joining a team', async function () {
  62. const team = TestStubs.Team({
  63. hasAccess: true,
  64. isMember: false,
  65. });
  66. const getOrgMock = MockApiClient.addMockResponse({
  67. url: '/organizations/org-slug/',
  68. body: TestStubs.Organization(),
  69. });
  70. MockApiClient.addMockResponse({
  71. url: `/organizations/org-slug/members/me/teams/${team.slug}/`,
  72. method: 'POST',
  73. body: {...team, isMember: true},
  74. });
  75. const mockTeams = [team];
  76. act(() => void TeamStore.loadInitialData(mockTeams, false, null));
  77. createWrapper({access: new Set([])});
  78. await userEvent.click(screen.getByLabelText('Join Team'));
  79. await waitFor(() => {
  80. expect(getOrgMock).toHaveBeenCalledTimes(1);
  81. });
  82. });
  83. it('cannot leave idp-provisioned team', function () {
  84. const mockTeams = [
  85. TestStubs.Team({flags: {'idp:provisioned': true}, isMember: true}),
  86. ];
  87. act(() => void TeamStore.loadInitialData(mockTeams, false, null));
  88. createWrapper();
  89. expect(screen.getByRole('button', {name: 'Leave Team'})).toBeDisabled();
  90. });
  91. it('cannot join idp-provisioned team', function () {
  92. const mockTeams = [
  93. TestStubs.Team({flags: {'idp:provisioned': true}, isMember: false}),
  94. ];
  95. act(() => void TeamStore.loadInitialData(mockTeams, false, null));
  96. createWrapper({
  97. access: new Set([]),
  98. });
  99. expect(screen.getByRole('button', {name: 'Join Team'})).toBeDisabled();
  100. });
  101. });
  102. describe('Closed Membership', function () {
  103. const {organization, project, routerProps} = initializeOrg({
  104. organization: {
  105. openMembership: false,
  106. },
  107. });
  108. const createWrapper = (
  109. props?: Partial<React.ComponentProps<typeof OrganizationTeams>>
  110. ) =>
  111. render(
  112. <OrganizationTeams
  113. {...routerProps}
  114. onRemoveAccessRequest={() => {}}
  115. requestList={[]}
  116. params={{projectId: project.slug}}
  117. features={new Set([])}
  118. access={new Set([])}
  119. organization={organization}
  120. {...props}
  121. />
  122. );
  123. it('can request access to team and does not have link to details', function () {
  124. const mockTeams = [
  125. TestStubs.Team({
  126. hasAccess: false,
  127. isMember: false,
  128. }),
  129. ];
  130. act(() => void TeamStore.loadInitialData(mockTeams, false, null));
  131. createWrapper({access: new Set([])});
  132. expect(screen.getByLabelText('Request Access')).toBeInTheDocument();
  133. // Should also not link to details because of lack of access
  134. expect(screen.queryByTestId('team-link')).not.toBeInTheDocument();
  135. });
  136. it('can leave team when you are a member', function () {
  137. const mockTeams = [
  138. TestStubs.Team({
  139. hasAccess: true,
  140. isMember: true,
  141. }),
  142. ];
  143. act(() => void TeamStore.loadInitialData(mockTeams, false, null));
  144. createWrapper({
  145. access: new Set([]),
  146. });
  147. expect(screen.getByLabelText('Leave Team')).toBeInTheDocument();
  148. });
  149. it('cannot request to join idp-provisioned team', function () {
  150. const mockTeams = [
  151. TestStubs.Team({flags: {'idp:provisioned': true}, isMember: false}),
  152. ];
  153. act(() => void TeamStore.loadInitialData(mockTeams, false, null));
  154. createWrapper({
  155. access: new Set([]),
  156. });
  157. expect(screen.getByRole('button', {name: 'Request Access'})).toBeDisabled();
  158. });
  159. it('cannot leave idp-provisioned team', function () {
  160. const mockTeams = [
  161. TestStubs.Team({flags: {'idp:provisioned': true}, isMember: true}),
  162. ];
  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 = TestStubs.AccessRequest({
  178. requester: {},
  179. });
  180. const requester = TestStubs.User({
  181. id: '9',
  182. username: 'requester@example.com',
  183. email: 'requester@example.com',
  184. name: 'Requester',
  185. });
  186. const requestList = [accessRequest, TestStubs.AccessRequest({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. });