import {render, screen, userEvent, waitFor} from 'sentry-test/reactTestingLibrary';
import ConfigStore from 'sentry/stores/configStore';
import {browserHistory} from 'sentry/utils/browserHistory';
import LoginForm from 'sentry/views/auth/loginForm';
async function doLogin() {
await userEvent.type(screen.getByRole('textbox', {name: 'Account'}), 'test@test.com');
await userEvent.type(screen.getByRole('textbox', {name: 'Password'}), '12345pass');
await userEvent.click(screen.getByRole('button', {name: 'Continue'}));
}
describe('LoginForm', function () {
const emptyAuthConfig = {
canRegister: false,
githubLoginLink: '',
googleLoginLink: '',
hasNewsletter: false,
serverHostname: '',
vstsLoginLink: '',
};
it('handles errors', async function () {
MockApiClient.addMockResponse({
url: '/auth/login/',
method: 'POST',
statusCode: 400,
body: {
detail: 'Login attempt failed',
errors: {__all__: 'Bad username password'},
},
});
render();
await doLogin();
expect(await screen.findByText('Bad username password')).toBeInTheDocument();
});
it('handles success', async function () {
const userObject = {
id: 1,
name: 'Joe',
};
const mockRequest = MockApiClient.addMockResponse({
url: '/auth/login/',
method: 'POST',
statusCode: 200,
body: {
user: userObject,
nextUri: '/next/',
},
});
render();
await doLogin();
expect(mockRequest).toHaveBeenCalledWith(
'/auth/login/',
expect.objectContaining({
data: {username: 'test@test.com', password: '12345pass'},
})
);
await waitFor(() => expect(ConfigStore.get('user')).toEqual(userObject));
expect(browserHistory.push).toHaveBeenCalledWith({pathname: '/next/'});
});
it('renders login provider buttons', function () {
const authConfig = {
...emptyAuthConfig,
vstsLoginLink: '/vstsLogin',
githubLoginLink: '/githubLogin',
};
render();
expect(screen.getByText('Sign in with GitHub')).toBeInTheDocument();
expect(screen.getByText('Sign in with Azure DevOps')).toBeInTheDocument();
});
});