123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240 |
- import {browserHistory} from 'react-router';
- import {render, screen, userEvent, waitFor} from 'sentry-test/reactTestingLibrary';
- 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,
- });
- const getJoinButton = () =>
- screen.queryByRole('button', {name: 'Join the test-org organization'});
- describe('AcceptOrganizationInvite', function () {
- it('can accept invitation', async function () {
- addMock({
- orgSlug: 'test-org',
- needsAuthentication: false,
- needs2fa: false,
- hasAuthProvider: false,
- requireSso: false,
- existingMember: false,
- });
- render(<AcceptOrganizationInvite params={{memberId: '1', token: 'abc'}} />);
- const acceptMock = MockApiClient.addMockResponse({
- url: '/accept-invite/1/abc/',
- method: 'POST',
- });
- const joinButton = getJoinButton();
- userEvent.click(joinButton);
- expect(acceptMock).toHaveBeenCalled();
- expect(joinButton).toBeDisabled();
- await waitFor(() =>
- 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,
- });
- render(<AcceptOrganizationInvite params={{memberId: '1', token: 'abc'}} />);
- expect(getJoinButton()).not.toBeInTheDocument();
- expect(screen.getByTestId('action-info-general')).toBeInTheDocument();
- expect(screen.queryByTestId('action-info-sso')).not.toBeInTheDocument();
- expect(
- screen.getByRole('button', {name: 'Create a new account'})
- ).toBeInTheDocument();
- expect(
- screen.getByRole('link', {name: 'Login using an existing account'})
- ).toBeInTheDocument();
- });
- it('suggests sso authentication to login', function () {
- addMock({
- orgSlug: 'test-org',
- needsAuthentication: true,
- needs2fa: false,
- hasAuthProvider: true,
- requireSso: false,
- existingMember: false,
- ssoProvider: 'SSO',
- });
- render(<AcceptOrganizationInvite params={{memberId: '1', token: 'abc'}} />);
- expect(getJoinButton()).not.toBeInTheDocument();
- expect(screen.getByTestId('action-info-general')).toBeInTheDocument();
- expect(screen.getByTestId('action-info-sso')).toBeInTheDocument();
- expect(screen.getByRole('button', {name: 'Join with SSO'})).toBeInTheDocument();
- expect(
- screen.getByRole('button', {name: 'Create a new account'})
- ).toBeInTheDocument();
- expect(
- screen.getByRole('link', {name: 'Login using an existing account'})
- ).toBeInTheDocument();
- });
- it('enforce required sso authentication', function () {
- addMock({
- orgSlug: 'test-org',
- needsAuthentication: true,
- needs2fa: false,
- hasAuthProvider: true,
- requireSso: true,
- existingMember: false,
- ssoProvider: 'SSO',
- });
- render(<AcceptOrganizationInvite params={{memberId: '1', token: 'abc'}} />);
- expect(getJoinButton()).not.toBeInTheDocument();
- expect(screen.queryByTestId('action-info-general')).not.toBeInTheDocument();
- expect(screen.getByTestId('action-info-sso')).toBeInTheDocument();
- expect(screen.getByRole('button', {name: 'Join with SSO'})).toBeInTheDocument();
- expect(
- screen.queryByRole('button', {name: 'Create a new account'})
- ).not.toBeInTheDocument();
- expect(
- screen.queryByRole('link', {name: 'Login using an existing account'})
- ).not.toBeInTheDocument();
- });
- it('enforce required sso authentication for logged in users', function () {
- addMock({
- orgSlug: 'test-org',
- needsAuthentication: false,
- needs2fa: false,
- hasAuthProvider: true,
- requireSso: true,
- existingMember: false,
- ssoProvider: 'SSO',
- });
- render(<AcceptOrganizationInvite params={{memberId: '1', token: 'abc'}} />);
- expect(getJoinButton()).not.toBeInTheDocument();
- expect(screen.queryByTestId('action-info-general')).not.toBeInTheDocument();
- expect(screen.getByTestId('action-info-sso')).toBeInTheDocument();
- expect(screen.getByRole('button', {name: 'Join with SSO'})).toBeInTheDocument();
- expect(
- screen.queryByRole('button', {name: 'Create a new account'})
- ).not.toBeInTheDocument();
- expect(
- screen.queryByRole('link', {name: 'Login using an existing account'})
- ).not.toBeInTheDocument();
- });
- 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,
- ssoProvider: 'SSO',
- });
- render(<AcceptOrganizationInvite params={{memberId: '1', token: 'abc'}} />);
- expect(screen.getByTestId('existing-member')).toBeInTheDocument();
- const {replace} = window.location;
- window.location.replace = jest.fn();
- userEvent.click(screen.getByTestId('existing-member-link'));
- expect(logout).toHaveBeenCalled();
- await waitFor(() => 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,
- ssoProvider: 'SSO',
- });
- render(<AcceptOrganizationInvite params={{memberId: '1', token: 'abc'}} />);
- expect(screen.getByTestId('action-info-sso')).toBeInTheDocument();
- expect(getJoinButton()).toBeInTheDocument();
- });
- it('shows a logout button for existing members', async function () {
- addMock({
- orgSlug: 'test-org',
- needsAuthentication: false,
- needs2fa: false,
- hasAuthProvider: false,
- requireSso: false,
- existingMember: true,
- });
- render(<AcceptOrganizationInvite params={{memberId: '1', token: 'abc'}} />);
- expect(screen.getByTestId('existing-member')).toBeInTheDocument();
- const {replace} = window.location;
- window.location.replace = jest.fn();
- userEvent.click(screen.getByTestId('existing-member-link'));
- expect(logout).toHaveBeenCalled();
- await waitFor(() => 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,
- });
- render(<AcceptOrganizationInvite params={{memberId: '1', token: 'abc'}} />);
- expect(
- screen.getByRole('button', {name: 'Configure Two-Factor Auth'})
- ).toBeInTheDocument();
- });
- });
|