organizationMembersList.spec.tsx 17 KB

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