organizationTeams.spec.tsx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. import {AccessRequest} from 'sentry-fixture/accessRequest';
  2. import {Organization} from 'sentry-fixture/organization';
  3. import {Team} from 'sentry-fixture/team';
  4. import {User} 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. Team({
  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 = Team({
  67. hasAccess: true,
  68. isMember: false,
  69. });
  70. const getOrgMock = MockApiClient.addMockResponse({
  71. url: '/organizations/org-slug/',
  72. body: Organization(),
  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 = [Team({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 = [Team({flags: {'idp:provisioned': true}, isMember: false})];
  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. 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. 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 = [Team({flags: {'idp:provisioned': true}, isMember: false})];
  151. act(() => void TeamStore.loadInitialData(mockTeams, false, null));
  152. createWrapper({
  153. access: new Set([]),
  154. });
  155. expect(screen.getByRole('button', {name: 'Request Access'})).toBeDisabled();
  156. });
  157. it('cannot leave idp-provisioned team', function () {
  158. const mockTeams = [Team({flags: {'idp:provisioned': true}, isMember: true})];
  159. act(() => void TeamStore.loadInitialData(mockTeams, false, null));
  160. createWrapper({
  161. access: new Set([]),
  162. });
  163. expect(screen.getByRole('button', {name: 'Leave Team'})).toBeDisabled();
  164. });
  165. });
  166. describe('Team Requests', function () {
  167. const {organization, project, routerProps} = initializeOrg({
  168. organization: {
  169. openMembership: false,
  170. },
  171. });
  172. const orgId = organization.slug;
  173. const accessRequest = AccessRequest({
  174. requester: {},
  175. });
  176. const requester = User({
  177. id: '9',
  178. username: 'requester@example.com',
  179. email: 'requester@example.com',
  180. name: 'Requester',
  181. });
  182. const requestList = [accessRequest, AccessRequest({id: '4', requester})];
  183. const createWrapper = (
  184. props?: Partial<React.ComponentProps<typeof OrganizationTeams>>
  185. ) =>
  186. render(
  187. <OrganizationTeams
  188. {...routerProps}
  189. onRemoveAccessRequest={() => {}}
  190. params={{projectId: project.slug}}
  191. features={new Set([])}
  192. access={new Set([])}
  193. organization={organization}
  194. requestList={requestList}
  195. {...props}
  196. />
  197. );
  198. it('renders team request panel', function () {
  199. createWrapper();
  200. expect(screen.getByText('Pending Team Requests')).toBeInTheDocument();
  201. expect(screen.queryAllByTestId('request-message')).toHaveLength(2);
  202. expect(screen.queryAllByTestId('request-message')[0]).toHaveTextContent(
  203. `${accessRequest.member.user?.name} requests access to the #${accessRequest.team.slug} team`
  204. );
  205. });
  206. it('can approve', async function () {
  207. const onUpdateRequestListMock = jest.fn();
  208. const approveMock = MockApiClient.addMockResponse({
  209. url: `/organizations/${orgId}/access-requests/${accessRequest.id}/`,
  210. method: 'PUT',
  211. });
  212. createWrapper({
  213. onRemoveAccessRequest: onUpdateRequestListMock,
  214. });
  215. await userEvent.click(screen.getAllByLabelText('Approve')[0]);
  216. await tick();
  217. expect(approveMock).toHaveBeenCalledWith(
  218. expect.anything(),
  219. expect.objectContaining({
  220. data: {
  221. isApproved: true,
  222. },
  223. })
  224. );
  225. expect(onUpdateRequestListMock).toHaveBeenCalledWith(accessRequest.id, true);
  226. });
  227. it('can deny', async function () {
  228. const onUpdateRequestListMock = jest.fn();
  229. const denyMock = MockApiClient.addMockResponse({
  230. url: `/organizations/${orgId}/access-requests/${accessRequest.id}/`,
  231. method: 'PUT',
  232. });
  233. createWrapper({
  234. onRemoveAccessRequest: onUpdateRequestListMock,
  235. });
  236. await userEvent.click(screen.getAllByLabelText('Deny')[0]);
  237. await tick();
  238. expect(denyMock).toHaveBeenCalledWith(
  239. expect.anything(),
  240. expect.objectContaining({
  241. data: {
  242. isApproved: false,
  243. },
  244. })
  245. );
  246. expect(onUpdateRequestListMock).toHaveBeenCalledWith(accessRequest.id, false);
  247. });
  248. });
  249. describe('Team Roles', function () {
  250. const features = new Set(['team-roles']);
  251. const access = new Set<string>();
  252. it('does not render alert without feature flag', function () {
  253. const {organization, project, routerProps} = initializeOrg({
  254. organization: {orgRole: 'admin'},
  255. });
  256. render(
  257. <OrganizationTeams
  258. {...routerProps}
  259. requestList={[]}
  260. onRemoveAccessRequest={() => {}}
  261. params={{projectId: project.slug}}
  262. features={new Set()}
  263. access={access}
  264. organization={organization}
  265. />
  266. );
  267. expect(screen.queryByText('a minimum team-level role of')).not.toBeInTheDocument();
  268. });
  269. it('renders alert with elevated org role', function () {
  270. const {organization, project, routerProps} = initializeOrg({
  271. organization: {orgRole: 'admin'},
  272. });
  273. render(
  274. <OrganizationTeams
  275. {...routerProps}
  276. requestList={[]}
  277. onRemoveAccessRequest={() => {}}
  278. params={{projectId: project.slug}}
  279. features={features}
  280. access={access}
  281. organization={organization}
  282. />
  283. );
  284. expect(
  285. // Text broken up by styles
  286. screen.getByText(
  287. 'Your organization role as an has granted you a minimum team-level role of'
  288. )
  289. ).toBeInTheDocument();
  290. });
  291. it('does not render alert with lowest org role', function () {
  292. const {organization, project, routerProps} = initializeOrg({
  293. organization: {orgRole: 'member'},
  294. });
  295. render(
  296. <OrganizationTeams
  297. {...routerProps}
  298. requestList={[]}
  299. onRemoveAccessRequest={() => {}}
  300. params={{projectId: project.slug}}
  301. features={features}
  302. access={access}
  303. organization={organization}
  304. />
  305. );
  306. expect(screen.queryByText('a minimum team-level role of')).not.toBeInTheDocument();
  307. });
  308. });
  309. });