organizationMembersList.spec.tsx 19 KB

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