|
@@ -28,8 +28,8 @@ describe('Test: LoginContainer', () => {
|
|
|
// Arrange
|
|
|
render(<LoginContainer />);
|
|
|
const loginButton = screen.getByRole('button', { name: 'Login' });
|
|
|
- const emailInput = screen.getByLabelText('Email address');
|
|
|
- const passwordInput = screen.getByLabelText('Password');
|
|
|
+ const emailInput = screen.getByRole('textbox', { name: 'email' });
|
|
|
+ const passwordInput = screen.getByRole('textbox', { name: 'password' });
|
|
|
|
|
|
// Act
|
|
|
fireEvent.change(emailInput, { target: { value: faker.internet.email() } });
|
|
@@ -49,8 +49,8 @@ describe('Test: LoginContainer', () => {
|
|
|
|
|
|
// Act
|
|
|
const loginButton = screen.getByRole('button', { name: 'Login' });
|
|
|
- const emailInput = screen.getByLabelText('Email address');
|
|
|
- const passwordInput = screen.getByLabelText('Password');
|
|
|
+ const emailInput = screen.getByRole('textbox', { name: 'email' });
|
|
|
+ const passwordInput = screen.getByRole('textbox', { name: 'password' });
|
|
|
|
|
|
fireEvent.change(emailInput, { target: { value: email } });
|
|
|
fireEvent.change(passwordInput, { target: { value: password } });
|
|
@@ -70,8 +70,8 @@ describe('Test: LoginContainer', () => {
|
|
|
|
|
|
// Act
|
|
|
const loginButton = screen.getByRole('button', { name: 'Login' });
|
|
|
- const emailInput = screen.getByLabelText('Email address');
|
|
|
- const passwordInput = screen.getByLabelText('Password');
|
|
|
+ const emailInput = screen.getByRole('textbox', { name: 'email' });
|
|
|
+ const passwordInput = screen.getByRole('textbox', { name: 'password' });
|
|
|
|
|
|
fireEvent.change(emailInput, { target: { value: 'test@test.com' } });
|
|
|
fireEvent.change(passwordInput, { target: { value: 'test' } });
|
|
@@ -86,4 +86,111 @@ describe('Test: LoginContainer', () => {
|
|
|
const token = localStorage.getItem('token');
|
|
|
expect(token).toBeNull();
|
|
|
});
|
|
|
+
|
|
|
+ it('should show totp form if totpSessionId is returned', async () => {
|
|
|
+ // arrange
|
|
|
+ const email = faker.internet.email();
|
|
|
+ const password = faker.internet.password();
|
|
|
+ const totpSessionId = faker.datatype.uuid();
|
|
|
+ server.use(
|
|
|
+ getTRPCMock({
|
|
|
+ path: ['auth', 'login'],
|
|
|
+ type: 'mutation',
|
|
|
+ response: { totpSessionId },
|
|
|
+ }),
|
|
|
+ );
|
|
|
+ render(<LoginContainer />);
|
|
|
+
|
|
|
+ // act
|
|
|
+ const loginButton = screen.getByRole('button', { name: 'Login' });
|
|
|
+ const emailInput = screen.getByRole('textbox', { name: 'email' });
|
|
|
+ const passwordInput = screen.getByRole('textbox', { name: 'password' });
|
|
|
+
|
|
|
+ fireEvent.change(emailInput, { target: { value: email } });
|
|
|
+ fireEvent.change(passwordInput, { target: { value: password } });
|
|
|
+ fireEvent.click(loginButton);
|
|
|
+
|
|
|
+ // assert
|
|
|
+ await waitFor(() => {
|
|
|
+ expect(screen.getByText('Two-factor authentication')).toBeInTheDocument();
|
|
|
+ });
|
|
|
+ });
|
|
|
+
|
|
|
+ it('should show error message if totp code is invalid', async () => {
|
|
|
+ // arrange
|
|
|
+ const { result } = renderHook(() => useToastStore());
|
|
|
+ const email = faker.internet.email();
|
|
|
+ const password = faker.internet.password();
|
|
|
+ const totpSessionId = faker.datatype.uuid();
|
|
|
+ server.use(getTRPCMock({ path: ['auth', 'login'], type: 'mutation', response: { totpSessionId } }));
|
|
|
+ server.use(getTRPCMockError({ path: ['auth', 'verifyTotp'], type: 'mutation', status: 500, message: 'Invalid totp code' }));
|
|
|
+ render(<LoginContainer />);
|
|
|
+
|
|
|
+ // act
|
|
|
+ const loginButton = screen.getByRole('button', { name: 'Login' });
|
|
|
+ const emailInput = screen.getByRole('textbox', { name: 'email' });
|
|
|
+ const passwordInput = screen.getByRole('textbox', { name: 'password' });
|
|
|
+
|
|
|
+ fireEvent.change(emailInput, { target: { value: email } });
|
|
|
+ fireEvent.change(passwordInput, { target: { value: password } });
|
|
|
+ fireEvent.click(loginButton);
|
|
|
+
|
|
|
+ await waitFor(() => {
|
|
|
+ expect(screen.getByText('Two-factor authentication')).toBeInTheDocument();
|
|
|
+ });
|
|
|
+
|
|
|
+ const totpInputs = screen.getAllByRole('textbox', { name: /digit/ });
|
|
|
+
|
|
|
+ totpInputs.forEach((input, index) => {
|
|
|
+ fireEvent.change(input, { target: { value: index } });
|
|
|
+ });
|
|
|
+
|
|
|
+ const totpSubmitButton = screen.getByRole('button', { name: 'Confirm' });
|
|
|
+ fireEvent.click(totpSubmitButton);
|
|
|
+
|
|
|
+ // assert
|
|
|
+ await waitFor(() => {
|
|
|
+ expect(result.current.toasts).toHaveLength(1);
|
|
|
+ expect(result.current.toasts[0].description).toEqual('Invalid totp code');
|
|
|
+ expect(result.current.toasts[0].status).toEqual('error');
|
|
|
+ });
|
|
|
+ });
|
|
|
+
|
|
|
+ it('should add token in localStorage if totp code is valid', async () => {
|
|
|
+ // arrange
|
|
|
+ const email = faker.internet.email();
|
|
|
+ const password = faker.internet.password();
|
|
|
+ const totpSessionId = faker.datatype.uuid();
|
|
|
+ const token = faker.datatype.uuid();
|
|
|
+ server.use(getTRPCMock({ path: ['auth', 'login'], type: 'mutation', response: { totpSessionId } }));
|
|
|
+ server.use(getTRPCMock({ path: ['auth', 'verifyTotp'], type: 'mutation', response: { token } }));
|
|
|
+ render(<LoginContainer />);
|
|
|
+
|
|
|
+ // act
|
|
|
+ const loginButton = screen.getByRole('button', { name: 'Login' });
|
|
|
+ const emailInput = screen.getByRole('textbox', { name: 'email' });
|
|
|
+ const passwordInput = screen.getByRole('textbox', { name: 'password' });
|
|
|
+
|
|
|
+ fireEvent.change(emailInput, { target: { value: email } });
|
|
|
+ fireEvent.change(passwordInput, { target: { value: password } });
|
|
|
+ fireEvent.click(loginButton);
|
|
|
+
|
|
|
+ await waitFor(() => {
|
|
|
+ expect(screen.getByText('Two-factor authentication')).toBeInTheDocument();
|
|
|
+ });
|
|
|
+
|
|
|
+ const totpInputs = screen.getAllByRole('textbox', { name: /digit/ });
|
|
|
+
|
|
|
+ totpInputs.forEach((input, index) => {
|
|
|
+ fireEvent.change(input, { target: { value: index } });
|
|
|
+ });
|
|
|
+
|
|
|
+ const totpSubmitButton = screen.getByRole('button', { name: 'Confirm' });
|
|
|
+ fireEvent.click(totpSubmitButton);
|
|
|
+
|
|
|
+ // assert
|
|
|
+ await waitFor(() => {
|
|
|
+ expect(localStorage.getItem('token')).toEqual(token);
|
|
|
+ });
|
|
|
+ });
|
|
|
});
|