organizationMembersList.spec.tsx 16 KB

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