organizationMembersList.spec.tsx 16 KB

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