organizationMembersList.spec.tsx 18 KB

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