organizationMembersList.spec.tsx 19 KB

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