organizationMembersList.spec.tsx 19 KB

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