organizationMemberDetail.spec.jsx 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  1. import {mountWithTheme} from 'sentry-test/enzyme';
  2. import {mountGlobalModal} from 'sentry-test/modal';
  3. import {updateMember} from 'sentry/actionCreators/members';
  4. import TeamStore from 'sentry/stores/teamStore';
  5. import OrganizationMemberDetail from 'sentry/views/settings/organizationMembers/organizationMemberDetail';
  6. jest.mock('sentry/actionCreators/members', () => ({
  7. updateMember: jest.fn().mockReturnValue(new Promise(() => {})),
  8. }));
  9. describe('OrganizationMemberDetail', function () {
  10. let organization;
  11. let wrapper;
  12. let routerContext;
  13. const team = TestStubs.Team();
  14. const teams = [
  15. team,
  16. TestStubs.Team({
  17. id: '2',
  18. slug: 'new-team',
  19. name: 'New Team',
  20. isMember: false,
  21. }),
  22. ];
  23. const member = TestStubs.Member({
  24. roles: TestStubs.OrgRoleList(),
  25. dateCreated: new Date(),
  26. teams: [team.slug],
  27. });
  28. const pendingMember = TestStubs.Member({
  29. id: 2,
  30. roles: TestStubs.OrgRoleList(),
  31. dateCreated: new Date(),
  32. teams: [team.slug],
  33. invite_link: 'http://example.com/i/abc123',
  34. pending: true,
  35. });
  36. const expiredMember = TestStubs.Member({
  37. id: 3,
  38. roles: TestStubs.OrgRoleList(),
  39. dateCreated: new Date(),
  40. teams: [team.slug],
  41. invite_link: 'http://example.com/i/abc123',
  42. pending: true,
  43. expired: true,
  44. });
  45. describe('Can Edit', function () {
  46. beforeAll(function () {
  47. organization = TestStubs.Organization({teams});
  48. routerContext = TestStubs.routerContext([{organization}]);
  49. });
  50. TeamStore.loadInitialData(teams);
  51. beforeEach(function () {
  52. MockApiClient.clearMockResponses();
  53. MockApiClient.addMockResponse({
  54. url: `/organizations/${organization.slug}/members/${member.id}/`,
  55. body: member,
  56. });
  57. MockApiClient.addMockResponse({
  58. url: `/organizations/${organization.slug}/members/${pendingMember.id}/`,
  59. body: pendingMember,
  60. });
  61. MockApiClient.addMockResponse({
  62. url: `/organizations/${organization.slug}/members/${expiredMember.id}/`,
  63. body: expiredMember,
  64. });
  65. MockApiClient.addMockResponse({
  66. url: `/organizations/${organization.slug}/teams/`,
  67. body: teams,
  68. });
  69. });
  70. it('changes role to owner', function () {
  71. wrapper = mountWithTheme(
  72. <OrganizationMemberDetail params={{memberId: member.id}} />,
  73. routerContext
  74. );
  75. // Should have 4 roles
  76. expect(wrapper.find('OrganizationRoleSelect Radio')).toHaveLength(4);
  77. wrapper.find('OrganizationRoleSelect Radio').last().simulate('click');
  78. expect(wrapper.find('OrganizationRoleSelect Radio').last().prop('checked')).toBe(
  79. true
  80. );
  81. // Save Member
  82. wrapper.find('Button[priority="primary"]').simulate('click');
  83. expect(updateMember).toHaveBeenCalledWith(
  84. expect.anything(),
  85. expect.objectContaining({
  86. data: expect.objectContaining({
  87. role: 'owner',
  88. }),
  89. })
  90. );
  91. });
  92. it('leaves a team', async function () {
  93. wrapper = mountWithTheme(
  94. <OrganizationMemberDetail params={{memberId: member.id}} />,
  95. routerContext
  96. );
  97. // Wait for team list to load
  98. await tick();
  99. // Remove our one team
  100. const button = wrapper.find('TeamSelect TeamRow Button');
  101. expect(button).toHaveLength(1);
  102. button.simulate('click');
  103. // Save Member
  104. wrapper.find('Button[priority="primary"]').simulate('click');
  105. expect(updateMember).toHaveBeenCalledWith(
  106. expect.anything(),
  107. expect.objectContaining({
  108. data: expect.objectContaining({
  109. teams: [],
  110. }),
  111. })
  112. );
  113. });
  114. it('joins a team', function () {
  115. wrapper = mountWithTheme(
  116. <OrganizationMemberDetail params={{memberId: member.id}} />,
  117. routerContext
  118. );
  119. // Wait for team list to fetch.
  120. wrapper.update();
  121. // Should have one team enabled
  122. expect(wrapper.find('TeamPanelItem')).toHaveLength(1);
  123. // Select new team to join
  124. // Open the dropdown
  125. wrapper.find('TeamSelect DropdownButton').simulate('click');
  126. // Click the first item
  127. wrapper.find('TeamSelect [title="new team"]').at(0).simulate('click');
  128. // Save Member
  129. wrapper.find('Button[priority="primary"]').simulate('click');
  130. expect(updateMember).toHaveBeenCalledWith(
  131. expect.anything(),
  132. expect.objectContaining({
  133. data: expect.objectContaining({
  134. teams: ['team-slug', 'new-team'],
  135. }),
  136. })
  137. );
  138. });
  139. });
  140. describe('Cannot Edit', function () {
  141. beforeAll(function () {
  142. organization = TestStubs.Organization({teams, access: ['org:read']});
  143. routerContext = TestStubs.routerContext([{organization}]);
  144. });
  145. it('can not change roles, teams, or save', function () {
  146. wrapper = mountWithTheme(
  147. <OrganizationMemberDetail params={{memberId: member.id}} />,
  148. routerContext
  149. );
  150. wrapper.update();
  151. // Should have 4 roles
  152. expect(wrapper.find('OrganizationRoleSelect').prop('disabled')).toBe(true);
  153. expect(wrapper.find('TeamSelect').prop('disabled')).toBe(true);
  154. expect(wrapper.find('TeamRow Button').first().prop('disabled')).toBe(true);
  155. // Save Member
  156. expect(wrapper.find('Button[priority="primary"]').prop('disabled')).toBe(true);
  157. });
  158. });
  159. describe('Display status', function () {
  160. beforeAll(function () {
  161. organization = TestStubs.Organization({teams, access: ['org:read']});
  162. routerContext = TestStubs.routerContext([{organization}]);
  163. });
  164. it('display pending status', function () {
  165. wrapper = mountWithTheme(
  166. <OrganizationMemberDetail params={{memberId: pendingMember.id}} />,
  167. routerContext
  168. );
  169. expect(wrapper.find('[data-test-id="member-status"]').text()).toEqual(
  170. 'Invitation Pending'
  171. );
  172. });
  173. it('display expired status', function () {
  174. wrapper = mountWithTheme(
  175. <OrganizationMemberDetail params={{memberId: expiredMember.id}} />,
  176. routerContext
  177. );
  178. expect(wrapper.find('[data-test-id="member-status"]').text()).toEqual(
  179. 'Invitation Expired'
  180. );
  181. });
  182. });
  183. describe('Show resend button', function () {
  184. beforeAll(function () {
  185. organization = TestStubs.Organization({teams, access: ['org:read']});
  186. routerContext = TestStubs.routerContext([{organization}]);
  187. });
  188. it('shows for pending', function () {
  189. wrapper = mountWithTheme(
  190. <OrganizationMemberDetail params={{memberId: pendingMember.id}} />,
  191. routerContext
  192. );
  193. const button = wrapper.find('Button[data-test-id="resend-invite"]');
  194. expect(button.text()).toEqual('Resend Invite');
  195. });
  196. it('does not show for expired', function () {
  197. wrapper = mountWithTheme(
  198. <OrganizationMemberDetail params={{memberId: expiredMember.id}} />,
  199. routerContext
  200. );
  201. expect(wrapper.find('Button[data-test-id="resend-invite"]')).toHaveLength(0);
  202. });
  203. });
  204. describe('Reset member 2FA', function () {
  205. const fields = {
  206. roles: TestStubs.OrgRoleList(),
  207. dateCreated: new Date(),
  208. teams: [team.slug],
  209. };
  210. const noAccess = TestStubs.Member({
  211. ...fields,
  212. id: '4',
  213. user: TestStubs.User({has2fa: false}),
  214. });
  215. const no2fa = TestStubs.Member({
  216. ...fields,
  217. id: '5',
  218. user: TestStubs.User({has2fa: false, authenticators: [], canReset2fa: true}),
  219. });
  220. const has2fa = TestStubs.Member({
  221. ...fields,
  222. id: '6',
  223. user: TestStubs.User({
  224. has2fa: true,
  225. authenticators: [
  226. TestStubs.Authenticators().Totp(),
  227. TestStubs.Authenticators().Sms(),
  228. TestStubs.Authenticators().U2f(),
  229. ],
  230. canReset2fa: true,
  231. }),
  232. });
  233. const multipleOrgs = TestStubs.Member({
  234. ...fields,
  235. id: '7',
  236. user: TestStubs.User({
  237. has2fa: true,
  238. authenticators: [TestStubs.Authenticators().Totp()],
  239. canReset2fa: false,
  240. }),
  241. });
  242. beforeAll(function () {
  243. organization = TestStubs.Organization({teams});
  244. routerContext = TestStubs.routerContext([{organization}]);
  245. MockApiClient.clearMockResponses();
  246. MockApiClient.addMockResponse({
  247. url: `/organizations/${organization.slug}/members/${pendingMember.id}/`,
  248. body: pendingMember,
  249. });
  250. MockApiClient.addMockResponse({
  251. url: `/organizations/${organization.slug}/members/${noAccess.id}/`,
  252. body: noAccess,
  253. });
  254. MockApiClient.addMockResponse({
  255. url: `/organizations/${organization.slug}/members/${no2fa.id}/`,
  256. body: no2fa,
  257. });
  258. MockApiClient.addMockResponse({
  259. url: `/organizations/${organization.slug}/members/${has2fa.id}/`,
  260. body: has2fa,
  261. });
  262. MockApiClient.addMockResponse({
  263. url: `/organizations/${organization.slug}/members/${multipleOrgs.id}/`,
  264. body: multipleOrgs,
  265. });
  266. MockApiClient.addMockResponse({
  267. url: `/organizations/${organization.slug}/teams/`,
  268. body: teams,
  269. });
  270. });
  271. const button = 'Button[data-test-id="reset-2fa"]';
  272. const tooltip = 'Tooltip[data-test-id="reset-2fa-tooltip"]';
  273. const expectButtonEnabled = () => {
  274. expect(wrapper.find(button).text()).toEqual('Reset two-factor authentication');
  275. expect(wrapper.find(button).prop('disabled')).toBe(false);
  276. expect(wrapper.find(tooltip).prop('title')).toEqual('');
  277. expect(wrapper.find(tooltip).prop('disabled')).toBe(true);
  278. };
  279. const expectButtonDisabled = title => {
  280. expect(wrapper.find(button).text()).toEqual('Reset two-factor authentication');
  281. expect(wrapper.find(button).prop('disabled')).toBe(true);
  282. expect(wrapper.find(tooltip).prop('title')).toEqual(title);
  283. expect(wrapper.find(tooltip).prop('disabled')).toBe(false);
  284. };
  285. it('does not show for pending member', function () {
  286. wrapper = mountWithTheme(
  287. <OrganizationMemberDetail params={{memberId: pendingMember.id}} />,
  288. routerContext
  289. );
  290. expect(wrapper.find(button)).toHaveLength(0);
  291. });
  292. it('shows tooltip for joined member without permission to edit', function () {
  293. wrapper = mountWithTheme(
  294. <OrganizationMemberDetail params={{memberId: noAccess.id}} />,
  295. routerContext
  296. );
  297. expectButtonDisabled('You do not have permission to perform this action');
  298. });
  299. it('shows tooltip for member without 2fa', function () {
  300. wrapper = mountWithTheme(
  301. <OrganizationMemberDetail params={{memberId: no2fa.id}} />,
  302. routerContext
  303. );
  304. expectButtonDisabled('Not enrolled in two-factor authentication');
  305. });
  306. it('can reset member 2FA', async function () {
  307. const deleteMocks = has2fa.user.authenticators.map(auth =>
  308. MockApiClient.addMockResponse({
  309. url: `/users/${has2fa.user.id}/authenticators/${auth.id}/`,
  310. method: 'DELETE',
  311. })
  312. );
  313. wrapper = mountWithTheme(
  314. <OrganizationMemberDetail params={{memberId: has2fa.id}} />,
  315. routerContext
  316. );
  317. expectButtonEnabled();
  318. wrapper.find(button).simulate('click');
  319. const modal = await mountGlobalModal();
  320. modal.find('Button[data-test-id="confirm-button"]').simulate('click');
  321. deleteMocks.forEach(deleteMock => {
  322. expect(deleteMock).toHaveBeenCalled();
  323. });
  324. });
  325. it('shows tooltip for member in multiple orgs', function () {
  326. wrapper = mountWithTheme(
  327. <OrganizationMemberDetail params={{memberId: multipleOrgs.id}} />,
  328. routerContext
  329. );
  330. expectButtonDisabled('Cannot be reset since user is in more than one organization');
  331. });
  332. it('shows tooltip for member in 2FA required org', function () {
  333. organization.require2FA = true;
  334. MockApiClient.addMockResponse({
  335. url: `/organizations/${organization.slug}/members/${has2fa.id}/`,
  336. body: has2fa,
  337. });
  338. wrapper = mountWithTheme(
  339. <OrganizationMemberDetail params={{memberId: has2fa.id}} />,
  340. routerContext
  341. );
  342. expectButtonDisabled(
  343. 'Cannot be reset since two-factor is required for this organization'
  344. );
  345. });
  346. });
  347. });