organizationTeams.spec.tsx 11 KB

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