organizationMembersList.spec.tsx 18 KB

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