123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406 |
- import {mountWithTheme} from 'sentry-test/enzyme';
- import {mountGlobalModal} from 'sentry-test/modal';
- import {updateMember} from 'sentry/actionCreators/members';
- import TeamStore from 'sentry/stores/teamStore';
- import OrganizationMemberDetail from 'sentry/views/settings/organizationMembers/organizationMemberDetail';
- jest.mock('sentry/actionCreators/members', () => ({
- updateMember: jest.fn().mockReturnValue(new Promise(() => {})),
- }));
- describe('OrganizationMemberDetail', function () {
- let organization;
- let wrapper;
- let routerContext;
- const team = TestStubs.Team();
- const teams = [
- team,
- TestStubs.Team({
- id: '2',
- slug: 'new-team',
- name: 'New Team',
- isMember: false,
- }),
- ];
- const member = TestStubs.Member({
- roles: TestStubs.OrgRoleList(),
- dateCreated: new Date(),
- teams: [team.slug],
- });
- const pendingMember = TestStubs.Member({
- id: 2,
- roles: TestStubs.OrgRoleList(),
- dateCreated: new Date(),
- teams: [team.slug],
- invite_link: 'http://example.com/i/abc123',
- pending: true,
- });
- const expiredMember = TestStubs.Member({
- id: 3,
- roles: TestStubs.OrgRoleList(),
- dateCreated: new Date(),
- teams: [team.slug],
- invite_link: 'http://example.com/i/abc123',
- pending: true,
- expired: true,
- });
- describe('Can Edit', function () {
- beforeAll(function () {
- organization = TestStubs.Organization({teams});
- routerContext = TestStubs.routerContext([{organization}]);
- });
- TeamStore.loadInitialData(teams);
- beforeEach(function () {
- MockApiClient.clearMockResponses();
- MockApiClient.addMockResponse({
- url: `/organizations/${organization.slug}/members/${member.id}/`,
- body: member,
- });
- MockApiClient.addMockResponse({
- url: `/organizations/${organization.slug}/members/${pendingMember.id}/`,
- body: pendingMember,
- });
- MockApiClient.addMockResponse({
- url: `/organizations/${organization.slug}/members/${expiredMember.id}/`,
- body: expiredMember,
- });
- MockApiClient.addMockResponse({
- url: `/organizations/${organization.slug}/teams/`,
- body: teams,
- });
- });
- it('changes role to owner', function () {
- wrapper = mountWithTheme(
- <OrganizationMemberDetail params={{memberId: member.id}} />,
- routerContext
- );
- // Should have 4 roles
- expect(wrapper.find('OrganizationRoleSelect Radio')).toHaveLength(4);
- wrapper.find('OrganizationRoleSelect Radio').last().simulate('click');
- expect(wrapper.find('OrganizationRoleSelect Radio').last().prop('checked')).toBe(
- true
- );
- // Save Member
- wrapper.find('Button[priority="primary"]').simulate('click');
- expect(updateMember).toHaveBeenCalledWith(
- expect.anything(),
- expect.objectContaining({
- data: expect.objectContaining({
- role: 'owner',
- }),
- })
- );
- });
- it('leaves a team', async function () {
- wrapper = mountWithTheme(
- <OrganizationMemberDetail params={{memberId: member.id}} />,
- routerContext
- );
- // Wait for team list to load
- await tick();
- // Remove our one team
- const button = wrapper.find('TeamSelect TeamRow Button');
- expect(button).toHaveLength(1);
- button.simulate('click');
- // Save Member
- wrapper.find('Button[priority="primary"]').simulate('click');
- expect(updateMember).toHaveBeenCalledWith(
- expect.anything(),
- expect.objectContaining({
- data: expect.objectContaining({
- teams: [],
- }),
- })
- );
- });
- it('joins a team', function () {
- wrapper = mountWithTheme(
- <OrganizationMemberDetail params={{memberId: member.id}} />,
- routerContext
- );
- // Wait for team list to fetch.
- wrapper.update();
- // Should have one team enabled
- expect(wrapper.find('TeamPanelItem')).toHaveLength(1);
- // Select new team to join
- // Open the dropdown
- wrapper.find('TeamSelect DropdownButton').simulate('click');
- // Click the first item
- wrapper.find('TeamSelect [title="new team"]').at(0).simulate('click');
- // Save Member
- wrapper.find('Button[priority="primary"]').simulate('click');
- expect(updateMember).toHaveBeenCalledWith(
- expect.anything(),
- expect.objectContaining({
- data: expect.objectContaining({
- teams: ['team-slug', 'new-team'],
- }),
- })
- );
- });
- });
- describe('Cannot Edit', function () {
- beforeAll(function () {
- organization = TestStubs.Organization({teams, access: ['org:read']});
- routerContext = TestStubs.routerContext([{organization}]);
- });
- it('can not change roles, teams, or save', function () {
- wrapper = mountWithTheme(
- <OrganizationMemberDetail params={{memberId: member.id}} />,
- routerContext
- );
- wrapper.update();
- // Should have 4 roles
- expect(wrapper.find('OrganizationRoleSelect').prop('disabled')).toBe(true);
- expect(wrapper.find('TeamSelect').prop('disabled')).toBe(true);
- expect(wrapper.find('TeamRow Button').first().prop('disabled')).toBe(true);
- // Save Member
- expect(wrapper.find('Button[priority="primary"]').prop('disabled')).toBe(true);
- });
- });
- describe('Display status', function () {
- beforeAll(function () {
- organization = TestStubs.Organization({teams, access: ['org:read']});
- routerContext = TestStubs.routerContext([{organization}]);
- });
- it('display pending status', function () {
- wrapper = mountWithTheme(
- <OrganizationMemberDetail params={{memberId: pendingMember.id}} />,
- routerContext
- );
- expect(wrapper.find('[data-test-id="member-status"]').text()).toEqual(
- 'Invitation Pending'
- );
- });
- it('display expired status', function () {
- wrapper = mountWithTheme(
- <OrganizationMemberDetail params={{memberId: expiredMember.id}} />,
- routerContext
- );
- expect(wrapper.find('[data-test-id="member-status"]').text()).toEqual(
- 'Invitation Expired'
- );
- });
- });
- describe('Show resend button', function () {
- beforeAll(function () {
- organization = TestStubs.Organization({teams, access: ['org:read']});
- routerContext = TestStubs.routerContext([{organization}]);
- });
- it('shows for pending', function () {
- wrapper = mountWithTheme(
- <OrganizationMemberDetail params={{memberId: pendingMember.id}} />,
- routerContext
- );
- const button = wrapper.find('Button[data-test-id="resend-invite"]');
- expect(button.text()).toEqual('Resend Invite');
- });
- it('does not show for expired', function () {
- wrapper = mountWithTheme(
- <OrganizationMemberDetail params={{memberId: expiredMember.id}} />,
- routerContext
- );
- expect(wrapper.find('Button[data-test-id="resend-invite"]')).toHaveLength(0);
- });
- });
- describe('Reset member 2FA', function () {
- const fields = {
- roles: TestStubs.OrgRoleList(),
- dateCreated: new Date(),
- teams: [team.slug],
- };
- const noAccess = TestStubs.Member({
- ...fields,
- id: '4',
- user: TestStubs.User({has2fa: false}),
- });
- const no2fa = TestStubs.Member({
- ...fields,
- id: '5',
- user: TestStubs.User({has2fa: false, authenticators: [], canReset2fa: true}),
- });
- const has2fa = TestStubs.Member({
- ...fields,
- id: '6',
- user: TestStubs.User({
- has2fa: true,
- authenticators: [
- TestStubs.Authenticators().Totp(),
- TestStubs.Authenticators().Sms(),
- TestStubs.Authenticators().U2f(),
- ],
- canReset2fa: true,
- }),
- });
- const multipleOrgs = TestStubs.Member({
- ...fields,
- id: '7',
- user: TestStubs.User({
- has2fa: true,
- authenticators: [TestStubs.Authenticators().Totp()],
- canReset2fa: false,
- }),
- });
- beforeAll(function () {
- organization = TestStubs.Organization({teams});
- routerContext = TestStubs.routerContext([{organization}]);
- MockApiClient.clearMockResponses();
- MockApiClient.addMockResponse({
- url: `/organizations/${organization.slug}/members/${pendingMember.id}/`,
- body: pendingMember,
- });
- MockApiClient.addMockResponse({
- url: `/organizations/${organization.slug}/members/${noAccess.id}/`,
- body: noAccess,
- });
- MockApiClient.addMockResponse({
- url: `/organizations/${organization.slug}/members/${no2fa.id}/`,
- body: no2fa,
- });
- MockApiClient.addMockResponse({
- url: `/organizations/${organization.slug}/members/${has2fa.id}/`,
- body: has2fa,
- });
- MockApiClient.addMockResponse({
- url: `/organizations/${organization.slug}/members/${multipleOrgs.id}/`,
- body: multipleOrgs,
- });
- MockApiClient.addMockResponse({
- url: `/organizations/${organization.slug}/teams/`,
- body: teams,
- });
- });
- const button = 'Button[data-test-id="reset-2fa"]';
- const tooltip = 'Tooltip[data-test-id="reset-2fa-tooltip"]';
- const expectButtonEnabled = () => {
- expect(wrapper.find(button).text()).toEqual('Reset two-factor authentication');
- expect(wrapper.find(button).prop('disabled')).toBe(false);
- expect(wrapper.find(tooltip).prop('title')).toEqual('');
- expect(wrapper.find(tooltip).prop('disabled')).toBe(true);
- };
- const expectButtonDisabled = title => {
- expect(wrapper.find(button).text()).toEqual('Reset two-factor authentication');
- expect(wrapper.find(button).prop('disabled')).toBe(true);
- expect(wrapper.find(tooltip).prop('title')).toEqual(title);
- expect(wrapper.find(tooltip).prop('disabled')).toBe(false);
- };
- it('does not show for pending member', function () {
- wrapper = mountWithTheme(
- <OrganizationMemberDetail params={{memberId: pendingMember.id}} />,
- routerContext
- );
- expect(wrapper.find(button)).toHaveLength(0);
- });
- it('shows tooltip for joined member without permission to edit', function () {
- wrapper = mountWithTheme(
- <OrganizationMemberDetail params={{memberId: noAccess.id}} />,
- routerContext
- );
- expectButtonDisabled('You do not have permission to perform this action');
- });
- it('shows tooltip for member without 2fa', function () {
- wrapper = mountWithTheme(
- <OrganizationMemberDetail params={{memberId: no2fa.id}} />,
- routerContext
- );
- expectButtonDisabled('Not enrolled in two-factor authentication');
- });
- it('can reset member 2FA', async function () {
- const deleteMocks = has2fa.user.authenticators.map(auth =>
- MockApiClient.addMockResponse({
- url: `/users/${has2fa.user.id}/authenticators/${auth.id}/`,
- method: 'DELETE',
- })
- );
- wrapper = mountWithTheme(
- <OrganizationMemberDetail params={{memberId: has2fa.id}} />,
- routerContext
- );
- expectButtonEnabled();
- wrapper.find(button).simulate('click');
- const modal = await mountGlobalModal();
- modal.find('Button[data-test-id="confirm-button"]').simulate('click');
- deleteMocks.forEach(deleteMock => {
- expect(deleteMock).toHaveBeenCalled();
- });
- });
- it('shows tooltip for member in multiple orgs', function () {
- wrapper = mountWithTheme(
- <OrganizationMemberDetail params={{memberId: multipleOrgs.id}} />,
- routerContext
- );
- expectButtonDisabled('Cannot be reset since user is in more than one organization');
- });
- it('shows tooltip for member in 2FA required org', function () {
- organization.require2FA = true;
- MockApiClient.addMockResponse({
- url: `/organizations/${organization.slug}/members/${has2fa.id}/`,
- body: has2fa,
- });
- wrapper = mountWithTheme(
- <OrganizationMemberDetail params={{memberId: has2fa.id}} />,
- routerContext
- );
- expectButtonDisabled(
- 'Cannot be reset since two-factor is required for this organization'
- );
- });
- });
- });
|