123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251 |
- import {browserHistory} from 'react-router';
- import {mountWithTheme} from 'sentry-test/enzyme';
- import {logout} from 'sentry/actionCreators/account';
- import AcceptOrganizationInvite from 'sentry/views/acceptOrganizationInvite';
- jest.mock('sentry/actionCreators/account');
- const addMock = body =>
- MockApiClient.addMockResponse({
- url: '/accept-invite/1/abc/',
- method: 'GET',
- body,
- });
- describe('AcceptOrganizationInvite', function () {
- it('can accept invitation', async function () {
- addMock({
- orgSlug: 'test-org',
- needsAuthentication: false,
- needs2fa: false,
- hasAuthProvider: false,
- requireSso: false,
- existingMember: false,
- });
- const wrapper = mountWithTheme(
- <AcceptOrganizationInvite params={{memberId: '1', token: 'abc'}} />
- );
- const joinButton = wrapper.find('Button[aria-label="join-organization"]');
- expect(joinButton.exists()).toBe(true);
- expect(joinButton.text()).toBe('Join the test-org organization');
- const acceptMock = MockApiClient.addMockResponse({
- url: '/accept-invite/1/abc/',
- method: 'POST',
- });
- joinButton.simulate('click');
- expect(acceptMock).toHaveBeenCalled();
- expect(wrapper.find('Button[aria-label="join-organization"]').props().disabled).toBe(
- true
- );
- await tick();
- expect(browserHistory.replace).toHaveBeenCalledWith('/test-org/');
- });
- it('requires authentication to join', function () {
- addMock({
- orgSlug: 'test-org',
- needsAuthentication: true,
- needs2fa: false,
- hasAuthProvider: false,
- requireSso: false,
- existingMember: false,
- });
- const wrapper = mountWithTheme(
- <AcceptOrganizationInvite params={{memberId: '1', token: 'abc'}} />
- );
- const joinButton = wrapper.find('Button[aria-label="join-organization"]');
- expect(joinButton.exists()).toBe(false);
- expect(wrapper.find('[data-test-id="action-info-general"]').exists()).toBe(true);
- expect(wrapper.find('[data-test-id="action-info-sso"]').exists()).toBe(false);
- expect(wrapper.find('Button[aria-label="sso-login"]').exists()).toBe(false);
- expect(wrapper.find('Button[aria-label="create-account"]').exists()).toBe(true);
- expect(wrapper.find('[data-test-id="link-with-existing"]').exists()).toBe(true);
- });
- it('suggests sso authentication to login', function () {
- addMock({
- orgSlug: 'test-org',
- needsAuthentication: true,
- needs2fa: false,
- hasAuthProvider: true,
- requireSso: false,
- existingMember: false,
- });
- const wrapper = mountWithTheme(
- <AcceptOrganizationInvite params={{memberId: '1', token: 'abc'}} />
- );
- const joinButton = wrapper.find('Button[aria-label="join-organization"]');
- expect(joinButton.exists()).toBe(false);
- expect(wrapper.find('[data-test-id="action-info-general"]').exists()).toBe(true);
- expect(wrapper.find('[data-test-id="action-info-sso"]').exists()).toBe(true);
- expect(wrapper.find('Button[aria-label="sso-login"]').exists()).toBe(true);
- expect(wrapper.find('Button[aria-label="create-account"]').exists()).toBe(true);
- expect(wrapper.find('[data-test-id="link-with-existing"]').exists()).toBe(true);
- });
- it('enforce required sso authentication', function () {
- addMock({
- orgSlug: 'test-org',
- needsAuthentication: true,
- needs2fa: false,
- hasAuthProvider: true,
- requireSso: true,
- existingMember: false,
- });
- const wrapper = mountWithTheme(
- <AcceptOrganizationInvite params={{memberId: '1', token: 'abc'}} />
- );
- const joinButton = wrapper.find('Button[aria-label="join-organization"]');
- expect(joinButton.exists()).toBe(false);
- expect(wrapper.find('[data-test-id="action-info-general"]').exists()).toBe(false);
- expect(wrapper.find('[data-test-id="action-info-sso"]').exists()).toBe(true);
- expect(wrapper.find('Button[aria-label="sso-login"]').exists()).toBe(true);
- expect(wrapper.find('Button[aria-label="create-account"]').exists()).toBe(false);
- expect(wrapper.find('[data-test-id="link-with-existing"]').exists()).toBe(false);
- });
- it('enforce required sso authentication for logged in users', function () {
- addMock({
- orgSlug: 'test-org',
- needsAuthentication: false,
- needs2fa: false,
- hasAuthProvider: true,
- requireSso: true,
- existingMember: false,
- });
- const wrapper = mountWithTheme(
- <AcceptOrganizationInvite params={{memberId: '1', token: 'abc'}} />
- );
- const joinButton = wrapper.find('Button[aria-label="join-organization"]');
- expect(joinButton.exists()).toBe(false);
- expect(wrapper.find('[data-test-id="action-info-general"]').exists()).toBe(false);
- expect(wrapper.find('[data-test-id="action-info-sso"]').exists()).toBe(true);
- expect(wrapper.find('Button[aria-label="sso-login"]').exists()).toBe(true);
- expect(wrapper.find('Button[aria-label="create-account"]').exists()).toBe(false);
- expect(wrapper.find('[data-test-id="link-with-existing"]').exists()).toBe(false);
- });
- it('show logout button for logged in users w/ sso and membership', async function () {
- addMock({
- orgSlug: 'test-org',
- needsAuthentication: false,
- needs2fa: false,
- hasAuthProvider: true,
- requireSso: true,
- existingMember: true,
- });
- const wrapper = mountWithTheme(
- <AcceptOrganizationInvite params={{memberId: '1', token: 'abc'}} />
- );
- const existingMember = wrapper.find('[data-test-id="existing-member"]');
- expect(existingMember.exists()).toBe(true);
- const {replace} = window.location;
- window.location.replace = jest.fn();
- existingMember
- .find('[data-test-id="existing-member-link"]')
- .hostNodes()
- .simulate('click');
- expect(logout).toHaveBeenCalled();
- await tick();
- expect(window.location.replace).toHaveBeenCalled();
- window.location.replace = replace;
- });
- it('shows right options for logged in user and optional SSO', function () {
- addMock({
- orgSlug: 'test-org',
- needsAuthentication: false,
- needs2fa: false,
- hasAuthProvider: true,
- requireSso: false,
- existingMember: false,
- });
- const wrapper = mountWithTheme(
- <AcceptOrganizationInvite params={{memberId: '1', token: 'abc'}} />
- );
- const ssoLink = wrapper.find('[data-test-id="action-info-sso"]');
- expect(ssoLink.exists()).toBe(true);
- const joinButton = wrapper.find('Button[aria-label="join-organization"]');
- expect(joinButton.exists()).toBe(true);
- });
- it('shows a logout button for existing members', async function () {
- addMock({
- orgSlug: 'test-org',
- needsAuthentication: false,
- needs2fa: false,
- hasAuthProvider: false,
- requireSso: false,
- existingMember: true,
- });
- const wrapper = mountWithTheme(
- <AcceptOrganizationInvite params={{memberId: '1', token: 'abc'}} />
- );
- const existingMember = wrapper.find('[data-test-id="existing-member"]');
- expect(existingMember.exists()).toBe(true);
- const {replace} = window.location;
- window.location.replace = jest.fn();
- existingMember
- .find('[data-test-id="existing-member-link"]')
- .hostNodes()
- .simulate('click');
- expect(logout).toHaveBeenCalled();
- await tick();
- expect(window.location.replace).toHaveBeenCalled();
- window.location.replace = replace;
- });
- it('shows 2fa warning', function () {
- addMock({
- orgSlug: 'test-org',
- needsAuthentication: false,
- needs2fa: true,
- hasAuthProvider: false,
- requireSso: false,
- existingMember: false,
- });
- const wrapper = mountWithTheme(
- <AcceptOrganizationInvite params={{memberId: '1', token: 'abc'}} />
- );
- expect(wrapper.find('[data-test-id="2fa-warning"]').exists()).toBe(true);
- });
- });
|