organizationTeams.spec.jsx 10 KB

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