organizationMembersList.spec.tsx 17 KB

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