organizationMembersList.spec.jsx 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565
  1. import {browserHistory} from 'react-router';
  2. import selectEvent from 'react-select-event';
  3. import {
  4. render,
  5. renderGlobalModal,
  6. screen,
  7. userEvent,
  8. waitFor,
  9. within,
  10. } from 'sentry-test/reactTestingLibrary';
  11. import {addErrorMessage, addSuccessMessage} from 'sentry/actionCreators/indicator';
  12. import {Client} from 'sentry/api';
  13. import ConfigStore from 'sentry/stores/configStore';
  14. import OrganizationsStore from 'sentry/stores/organizationsStore';
  15. import {trackAnalytics} from 'sentry/utils/analytics';
  16. import OrganizationMembersList from 'sentry/views/settings/organizationMembers/organizationMembersList';
  17. jest.mock('sentry/utils/analytics');
  18. jest.mock('sentry/api');
  19. jest.mock('sentry/actionCreators/indicator');
  20. const roles = [
  21. {
  22. id: 'admin',
  23. name: 'Admin',
  24. desc: 'This is the admin role',
  25. allowed: true,
  26. },
  27. {
  28. id: 'member',
  29. name: 'Member',
  30. desc: 'This is the member role',
  31. allowed: true,
  32. },
  33. {
  34. id: 'owner',
  35. name: 'Owner',
  36. desc: 'This is the owner role',
  37. allowed: true,
  38. },
  39. ];
  40. describe('OrganizationMembersList', function () {
  41. const members = TestStubs.Members();
  42. const ownerTeam = TestStubs.Team({slug: 'owner-team', orgRole: 'owner'});
  43. const member = TestStubs.Member({
  44. id: '5',
  45. email: 'member@sentry.io',
  46. teams: [ownerTeam.slug],
  47. teamRoles: [
  48. {
  49. teamSlug: ownerTeam.slug,
  50. role: null,
  51. },
  52. ],
  53. flags: {
  54. 'sso:linked': true,
  55. },
  56. groupOrgRoles: [
  57. {
  58. teamSlug: ownerTeam.slug,
  59. role: {id: 'owner'},
  60. },
  61. ],
  62. });
  63. const currentUser = members[1];
  64. const organization = TestStubs.Organization({
  65. access: ['member:admin', 'org:admin', 'member:write'],
  66. status: {
  67. id: 'active',
  68. },
  69. });
  70. const defaultProps = {
  71. organization,
  72. router: {routes: []},
  73. location: {query: {}},
  74. };
  75. jest.spyOn(ConfigStore, 'get').mockImplementation(() => currentUser);
  76. afterAll(function () {
  77. ConfigStore.get.mockRestore();
  78. });
  79. beforeEach(function () {
  80. Client.clearMockResponses();
  81. Client.addMockResponse({
  82. url: '/organizations/org-slug/members/me/',
  83. method: 'GET',
  84. body: {roles},
  85. });
  86. Client.addMockResponse({
  87. url: '/organizations/org-slug/members/',
  88. method: 'GET',
  89. body: [...TestStubs.Members(), member],
  90. });
  91. MockApiClient.addMockResponse({
  92. url: `/organizations/org-slug/members/${member.id}/`,
  93. body: member,
  94. });
  95. Client.addMockResponse({
  96. url: '/organizations/org-slug/access-requests/',
  97. method: 'GET',
  98. body: [
  99. {
  100. id: 'pending-id',
  101. member: {
  102. id: 'pending-member-id',
  103. email: '',
  104. name: '',
  105. role: '',
  106. roleName: '',
  107. user: {
  108. id: '',
  109. name: 'sentry@test.com',
  110. },
  111. },
  112. team: TestStubs.Team(),
  113. },
  114. ],
  115. });
  116. Client.addMockResponse({
  117. url: '/organizations/org-slug/auth-provider/',
  118. method: 'GET',
  119. body: {
  120. ...TestStubs.AuthProvider(),
  121. require_link: true,
  122. },
  123. });
  124. Client.addMockResponse({
  125. url: '/organizations/org-slug/teams/',
  126. method: 'GET',
  127. body: [TestStubs.Team(), ownerTeam],
  128. });
  129. Client.addMockResponse({
  130. url: '/organizations/org-slug/invite-requests/',
  131. method: 'GET',
  132. body: [],
  133. });
  134. browserHistory.push.mockReset();
  135. OrganizationsStore.load([organization]);
  136. });
  137. it('can remove a member', async function () {
  138. const deleteMock = MockApiClient.addMockResponse({
  139. url: `/organizations/org-slug/members/${members[0].id}/`,
  140. method: 'DELETE',
  141. });
  142. render(<OrganizationMembersList {...defaultProps} />, {
  143. context: TestStubs.routerContext([{organization}]),
  144. });
  145. await userEvent.click(screen.getAllByRole('button', {name: 'Remove'})[0]);
  146. renderGlobalModal();
  147. await userEvent.click(screen.getByTestId('confirm-button'));
  148. await waitFor(() => expect(addSuccessMessage).toHaveBeenCalled());
  149. expect(deleteMock).toHaveBeenCalled();
  150. expect(browserHistory.push).not.toHaveBeenCalled();
  151. expect(OrganizationsStore.getAll()).toEqual([organization]);
  152. });
  153. it('displays error message when failing to remove member', async function () {
  154. const deleteMock = MockApiClient.addMockResponse({
  155. url: `/organizations/org-slug/members/${members[0].id}/`,
  156. method: 'DELETE',
  157. statusCode: 500,
  158. });
  159. render(<OrganizationMembersList {...defaultProps} />, {
  160. context: TestStubs.routerContext([{organization}]),
  161. });
  162. await userEvent.click(screen.getAllByRole('button', {name: 'Remove'})[0]);
  163. renderGlobalModal();
  164. await userEvent.click(screen.getByTestId('confirm-button'));
  165. await waitFor(() => expect(addErrorMessage).toHaveBeenCalled());
  166. expect(deleteMock).toHaveBeenCalled();
  167. expect(browserHistory.push).not.toHaveBeenCalled();
  168. expect(OrganizationsStore.getAll()).toEqual([organization]);
  169. });
  170. it('can leave org', async function () {
  171. const deleteMock = Client.addMockResponse({
  172. url: `/organizations/org-slug/members/${members[1].id}/`,
  173. method: 'DELETE',
  174. });
  175. render(<OrganizationMembersList {...defaultProps} />, {
  176. context: TestStubs.routerContext([{organization}]),
  177. });
  178. await userEvent.click(screen.getAllByRole('button', {name: 'Leave'})[0]);
  179. renderGlobalModal();
  180. await userEvent.click(screen.getByTestId('confirm-button'));
  181. await waitFor(() => expect(addSuccessMessage).toHaveBeenCalled());
  182. expect(deleteMock).toHaveBeenCalled();
  183. expect(browserHistory.push).toHaveBeenCalledTimes(1);
  184. expect(browserHistory.push).toHaveBeenCalledWith('/organizations/new/');
  185. });
  186. it('can redirect to remaining org after leaving', async function () {
  187. const deleteMock = Client.addMockResponse({
  188. url: `/organizations/org-slug/members/${members[1].id}/`,
  189. method: 'DELETE',
  190. });
  191. const secondOrg = TestStubs.Organization({
  192. slug: 'org-two',
  193. status: {
  194. id: 'active',
  195. },
  196. });
  197. OrganizationsStore.addOrReplace(secondOrg);
  198. render(<OrganizationMembersList {...defaultProps} />, {
  199. context: TestStubs.routerContext([{organization}]),
  200. });
  201. await userEvent.click(screen.getAllByRole('button', {name: 'Leave'})[0]);
  202. renderGlobalModal();
  203. await userEvent.click(screen.getByTestId('confirm-button'));
  204. await waitFor(() => expect(addSuccessMessage).toHaveBeenCalled());
  205. expect(deleteMock).toHaveBeenCalled();
  206. expect(browserHistory.push).toHaveBeenCalledTimes(1);
  207. expect(browserHistory.push).toHaveBeenCalledWith(
  208. `/organizations/${secondOrg.slug}/issues/`
  209. );
  210. expect(OrganizationsStore.getAll()).toEqual([secondOrg]);
  211. });
  212. it('displays error message when failing to leave org', async function () {
  213. const deleteMock = Client.addMockResponse({
  214. url: `/organizations/org-slug/members/${members[1].id}/`,
  215. method: 'DELETE',
  216. statusCode: 500,
  217. });
  218. render(<OrganizationMembersList {...defaultProps} />, {
  219. context: TestStubs.routerContext([{organization}]),
  220. });
  221. await userEvent.click(screen.getAllByRole('button', {name: 'Leave'})[0]);
  222. renderGlobalModal();
  223. await userEvent.click(screen.getByTestId('confirm-button'));
  224. await waitFor(() => expect(addErrorMessage).toHaveBeenCalled());
  225. expect(deleteMock).toHaveBeenCalled();
  226. expect(browserHistory.push).not.toHaveBeenCalled();
  227. expect(OrganizationsStore.getAll()).toEqual([organization]);
  228. });
  229. it('can re-send SSO link to member', async function () {
  230. const inviteMock = MockApiClient.addMockResponse({
  231. url: `/organizations/org-slug/members/${members[0].id}/`,
  232. method: 'PUT',
  233. body: {
  234. id: '1234',
  235. },
  236. });
  237. render(<OrganizationMembersList {...defaultProps} />, {
  238. context: TestStubs.routerContext([{organization}]),
  239. });
  240. expect(inviteMock).not.toHaveBeenCalled();
  241. await userEvent.click(screen.getByRole('button', {name: 'Resend SSO link'}));
  242. expect(inviteMock).toHaveBeenCalled();
  243. });
  244. it('can re-send invite to member', async function () {
  245. const inviteMock = MockApiClient.addMockResponse({
  246. url: `/organizations/org-slug/members/${members[1].id}/`,
  247. method: 'PUT',
  248. body: {
  249. id: '1234',
  250. },
  251. });
  252. render(<OrganizationMembersList {...defaultProps} />, {
  253. context: TestStubs.routerContext([{organization}]),
  254. });
  255. expect(inviteMock).not.toHaveBeenCalled();
  256. await userEvent.click(screen.getByRole('button', {name: 'Resend invite'}));
  257. expect(inviteMock).toHaveBeenCalled();
  258. });
  259. it('can search organization members', async function () {
  260. const searchMock = MockApiClient.addMockResponse({
  261. url: '/organizations/org-slug/members/',
  262. body: [],
  263. });
  264. const routerContext = TestStubs.routerContext();
  265. render(<OrganizationMembersList {...defaultProps} />, {
  266. context: routerContext,
  267. });
  268. await userEvent.type(screen.getByPlaceholderText('Search Members'), 'member');
  269. expect(searchMock).toHaveBeenLastCalledWith(
  270. '/organizations/org-slug/members/',
  271. expect.objectContaining({
  272. method: 'GET',
  273. query: {
  274. query: 'member',
  275. },
  276. })
  277. );
  278. await userEvent.keyboard('{enter}');
  279. expect(routerContext.context.router.push).toHaveBeenCalledTimes(1);
  280. });
  281. it('can filter members', async function () {
  282. const searchMock = MockApiClient.addMockResponse({
  283. url: '/organizations/org-slug/members/',
  284. body: [],
  285. });
  286. const routerContext = TestStubs.routerContext();
  287. render(<OrganizationMembersList {...defaultProps} />, {
  288. context: routerContext,
  289. });
  290. await userEvent.click(screen.getByRole('button', {name: 'Filter'}));
  291. await userEvent.click(screen.getByRole('option', {name: 'Member'}));
  292. expect(searchMock).toHaveBeenLastCalledWith(
  293. '/organizations/org-slug/members/',
  294. expect.objectContaining({
  295. method: 'GET',
  296. query: {query: 'role:member'},
  297. })
  298. );
  299. await userEvent.click(screen.getByRole('option', {name: 'Member'}));
  300. for (const [filter, label] of [
  301. ['isInvited', 'Invited'],
  302. ['has2fa', '2FA'],
  303. ['ssoLinked', 'SSO Linked'],
  304. ]) {
  305. const filterSection = screen.getByRole('listbox', {name: label});
  306. await userEvent.click(
  307. within(filterSection).getByRole('option', {
  308. name: 'True',
  309. })
  310. );
  311. expect(searchMock).toHaveBeenLastCalledWith(
  312. '/organizations/org-slug/members/',
  313. expect.objectContaining({
  314. method: 'GET',
  315. query: {query: `${filter}:true`},
  316. })
  317. );
  318. await userEvent.click(
  319. within(filterSection).getByRole('option', {
  320. name: 'False',
  321. })
  322. );
  323. expect(searchMock).toHaveBeenLastCalledWith(
  324. '/organizations/org-slug/members/',
  325. expect.objectContaining({
  326. method: 'GET',
  327. query: {query: `${filter}:false`},
  328. })
  329. );
  330. await userEvent.click(
  331. within(filterSection).getByRole('option', {
  332. name: 'All',
  333. })
  334. );
  335. }
  336. });
  337. it('can filter members with org roles from team membership', async function () {
  338. const routerContext = TestStubs.routerContext();
  339. render(<OrganizationMembersList {...defaultProps} />, {
  340. context: routerContext,
  341. });
  342. await userEvent.click(screen.getByRole('button', {name: 'Filter'}));
  343. await userEvent.click(screen.getByRole('option', {name: 'Owner'}));
  344. await userEvent.click(screen.getByRole('button', {name: 'Filter'}));
  345. const owners = screen.queryAllByText('Owner');
  346. expect(owners).toHaveLength(3);
  347. });
  348. describe('OrganizationInviteRequests', function () {
  349. const inviteRequest = TestStubs.Member({
  350. id: '123',
  351. user: null,
  352. inviteStatus: 'requested_to_be_invited',
  353. inviter: TestStubs.User(),
  354. role: 'member',
  355. teams: [],
  356. });
  357. const joinRequest = TestStubs.Member({
  358. id: '456',
  359. user: null,
  360. email: 'test@gmail.com',
  361. inviteStatus: 'requested_to_join',
  362. role: 'member',
  363. teams: [],
  364. });
  365. it('disable buttons for no access', function () {
  366. const org = TestStubs.Organization({
  367. status: {
  368. id: 'active',
  369. },
  370. });
  371. MockApiClient.addMockResponse({
  372. url: '/organizations/org-slug/invite-requests/',
  373. method: 'GET',
  374. body: [inviteRequest],
  375. });
  376. MockApiClient.addMockResponse({
  377. url: `/organizations/org-slug/invite-requests/${inviteRequest.id}/`,
  378. method: 'PUT',
  379. });
  380. render(<OrganizationMembersList {...defaultProps} organization={org} />, {
  381. context: TestStubs.routerContext([{organization: org}]),
  382. });
  383. expect(screen.getByText('Pending Members')).toBeInTheDocument();
  384. expect(screen.getByRole('button', {name: 'Approve'})).toBeDisabled();
  385. });
  386. it('can approve invite request and update', async function () {
  387. const org = TestStubs.Organization({
  388. access: ['member:admin', 'org:admin', 'member:write'],
  389. status: {
  390. id: 'active',
  391. },
  392. });
  393. MockApiClient.addMockResponse({
  394. url: '/organizations/org-slug/invite-requests/',
  395. method: 'GET',
  396. body: [inviteRequest],
  397. });
  398. MockApiClient.addMockResponse({
  399. url: `/organizations/org-slug/invite-requests/${inviteRequest.id}/`,
  400. method: 'PUT',
  401. });
  402. render(<OrganizationMembersList {...defaultProps} />, {
  403. context: TestStubs.routerContext([{organization: org}]),
  404. });
  405. expect(screen.getByText('Pending Members')).toBeInTheDocument();
  406. await userEvent.click(screen.getByRole('button', {name: 'Approve'}));
  407. renderGlobalModal();
  408. await userEvent.click(screen.getByTestId('confirm-button'));
  409. expect(screen.queryByText('Pending Members')).not.toBeInTheDocument();
  410. expect(trackAnalytics).toHaveBeenCalledWith('invite_request.approved', {
  411. invite_status: inviteRequest.inviteStatus,
  412. member_id: parseInt(inviteRequest.id, 10),
  413. organization: org,
  414. });
  415. });
  416. it('can deny invite request and remove', async function () {
  417. const org = TestStubs.Organization({
  418. access: ['member:admin', 'org:admin', 'member:write'],
  419. status: {
  420. id: 'active',
  421. },
  422. });
  423. MockApiClient.addMockResponse({
  424. url: '/organizations/org-slug/invite-requests/',
  425. method: 'GET',
  426. body: [joinRequest],
  427. });
  428. MockApiClient.addMockResponse({
  429. url: `/organizations/org-slug/invite-requests/${joinRequest.id}/`,
  430. method: 'DELETE',
  431. });
  432. render(<OrganizationMembersList {...defaultProps} />, {
  433. context: TestStubs.routerContext([{organization: org}]),
  434. });
  435. expect(screen.getByText('Pending Members')).toBeInTheDocument();
  436. await userEvent.click(screen.getByRole('button', {name: 'Deny'}));
  437. expect(screen.queryByText('Pending Members')).not.toBeInTheDocument();
  438. expect(trackAnalytics).toHaveBeenCalledWith('invite_request.denied', {
  439. invite_status: joinRequest.inviteStatus,
  440. member_id: parseInt(joinRequest.id, 10),
  441. organization: org,
  442. });
  443. });
  444. it('can update invite requests', async function () {
  445. const org = TestStubs.Organization({
  446. access: ['member:admin', 'org:admin', 'member:write'],
  447. status: {
  448. id: 'active',
  449. },
  450. });
  451. MockApiClient.addMockResponse({
  452. url: '/organizations/org-slug/invite-requests/',
  453. method: 'GET',
  454. body: [inviteRequest],
  455. });
  456. const updateWithApprove = MockApiClient.addMockResponse({
  457. url: `/organizations/org-slug/invite-requests/${inviteRequest.id}/`,
  458. method: 'PUT',
  459. });
  460. render(<OrganizationMembersList {...defaultProps} />, {
  461. context: TestStubs.routerContext([{organization: org}]),
  462. });
  463. await selectEvent.select(screen.getAllByRole('textbox')[1], ['Admin']);
  464. await userEvent.click(screen.getByRole('button', {name: 'Approve'}));
  465. renderGlobalModal();
  466. await userEvent.click(screen.getByTestId('confirm-button'));
  467. expect(updateWithApprove).toHaveBeenCalledWith(
  468. `/organizations/org-slug/invite-requests/${inviteRequest.id}/`,
  469. expect.objectContaining({data: expect.objectContaining({role: 'admin'})})
  470. );
  471. });
  472. });
  473. });