From 737326d5bf4be325c3159dce180fe9b3478da5aa Mon Sep 17 00:00:00 2001 From: Jack Meyer Date: Fri, 23 Oct 2020 20:16:55 -0500 Subject: [PATCH] chore: remove login --- src/App.tsx | 2 - src/HospitalRun.tsx | 7 +- src/__tests__/login/Login.test.tsx | 314 ----------------------------- src/login/Login.tsx | 79 -------- src/shared/config/pouchdb.ts | 5 - src/user/user-slice.ts | 6 + 6 files changed, 7 insertions(+), 406 deletions(-) delete mode 100644 src/__tests__/login/Login.test.tsx delete mode 100644 src/login/Login.tsx diff --git a/src/App.tsx b/src/App.tsx index 0760e629c0..1fa00b0480 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -7,7 +7,6 @@ import { useDispatch } from 'react-redux' import { BrowserRouter, Route, Switch } from 'react-router-dom' import HospitalRun from './HospitalRun' -import Login from './login/Login' import { TitleProvider } from './page-header/title/TitleContext' import { remoteDb } from './shared/config/pouchdb' import { getCurrentSession } from './user/user-slice' @@ -41,7 +40,6 @@ const App: React.FC = () => { }> - diff --git a/src/HospitalRun.tsx b/src/HospitalRun.tsx index 31d8fe336e..4bd60bdd01 100644 --- a/src/HospitalRun.tsx +++ b/src/HospitalRun.tsx @@ -1,7 +1,7 @@ import { Toaster } from '@hospitalrun/components' import React from 'react' import { useSelector } from 'react-redux' -import { Redirect, Route, Switch } from 'react-router-dom' +import { Route, Switch } from 'react-router-dom' import Dashboard from './dashboard/Dashboard' import Imagings from './imagings/Imagings' @@ -23,11 +23,6 @@ import { RootState } from './shared/store' const HospitalRun = () => { const { title } = useTitle() const { sidebarCollapsed } = useSelector((state: RootState) => state.components) - const { user } = useSelector((root: RootState) => root.user) - - if (user === undefined) { - return - } return (
diff --git a/src/__tests__/login/Login.test.tsx b/src/__tests__/login/Login.test.tsx deleted file mode 100644 index df6e098b53..0000000000 --- a/src/__tests__/login/Login.test.tsx +++ /dev/null @@ -1,314 +0,0 @@ -import { Alert } from '@hospitalrun/components' -import { mount, ReactWrapper } from 'enzyme' -import { createMemoryHistory } from 'history' -import React from 'react' -import Button from 'react-bootstrap/Button' -import { act } from 'react-dom/test-utils' -import { Provider } from 'react-redux' -import { Router } from 'react-router' -import createMockStore, { MockStore } from 'redux-mock-store' -import thunk from 'redux-thunk' - -import Login from '../../login/Login' -import TextInputWithLabelFormGroup from '../../shared/components/input/TextInputWithLabelFormGroup' -import { remoteDb } from '../../shared/config/pouchdb' -import User from '../../shared/model/User' -import { RootState } from '../../shared/store' - -const mockStore = createMockStore([thunk]) - -describe('Login', () => { - const history = createMemoryHistory() - let store: MockStore - - const setup = (storeValue: any = { loginError: {}, user: {} as User }) => { - history.push('/login') - store = mockStore(storeValue) - - const wrapper = mount( - - - - - , - ) - - wrapper.update() - return wrapper - } - - describe('Layout initial validations', () => { - it('should render a login form', async () => { - let wrapper: any - - await act(async () => { - wrapper = await setup() - }) - - const form = wrapper.find('form') - expect(form).toHaveLength(1) - }) - - it('should render a username and password input', async () => { - let wrapper: any - - await act(async () => { - wrapper = setup() - }) - - const user = wrapper.find(TextInputWithLabelFormGroup) - expect(user).toHaveLength(2) - }) - - it('should render a submit button', async () => { - let wrapper: any - await act(async () => { - wrapper = setup() - }) - - const button = wrapper.find(Button) - expect(button).toHaveLength(1) - }) - }) - - describe('Unable to login', () => { - it('should get field required error message if no username is provided', async () => { - let wrapper: any - - await act(async () => { - wrapper = await setup() - }) - - jest.spyOn(remoteDb, 'logIn') - - const password = wrapper.find('#passwordTextInput').at(0) - await act(async () => { - const onChange = password.prop('onChange') as any - await onChange({ currentTarget: { value: 'password' } }) - }) - - wrapper.update() - - const saveButton = wrapper.find({ type: 'submit' }).at(0) - - await act(async () => { - const onClick = saveButton.prop('onClick') as any - await onClick({ preventDefault: jest.fn() }) - }) - - wrapper.update() - - expect(remoteDb.logIn).toHaveBeenCalledWith('', 'password') - expect(history.location.pathname).toEqual('/login') - expect(store.getActions()).toContainEqual({ - type: 'user/loginError', - payload: { - message: 'user.login.error.message.required', - username: 'user.login.error.username.required', - password: 'user.login.error.password.required', - }, - }) - }) - - it('should show required username error message', async () => { - let wrapper: any - - await act(async () => { - wrapper = await setup({ - user: { - loginError: { - message: 'user.login.error.message.required', - username: 'user.login.error.username.required', - password: 'user.login.error.password.required', - }, - }, - } as any) - }) - - let password: ReactWrapper = wrapper.find('#passwordTextInput').at(0) - await act(async () => { - const onChange = password.prop('onChange') as any - await onChange({ currentTarget: { value: 'password' } }) - }) - - wrapper.update() - - const alert = wrapper.find(Alert) - const username = wrapper.find('#usernameTextInput') - password = wrapper.find('#passwordTextInput') - - const usernameFeedback = username.find('Feedback') - const passwordFeedback = password.find('Feedback') - - expect(alert.prop('message')).toEqual('user.login.error.message.required') - expect(usernameFeedback.hasClass('undefined')).not.toBeTruthy() - expect(passwordFeedback.hasClass('undefined')).toBeTruthy() - }) - - it('should get field required error message if no password is provided', async () => { - let wrapper: any - - await act(async () => { - wrapper = await setup() - }) - - jest.spyOn(remoteDb, 'logIn') - - const username = wrapper.find('#usernameTextInput').at(0) - await act(async () => { - const onChange = username.prop('onChange') as any - await onChange({ currentTarget: { value: 'username' } }) - }) - - wrapper.update() - - const saveButton = wrapper.find({ type: 'submit' }).at(0) - - await act(async () => { - const onClick = saveButton.prop('onClick') as any - await onClick({ preventDefault: jest.fn() }) - }) - - wrapper.update() - - expect(remoteDb.logIn).toHaveBeenCalledWith('username', '') - expect(history.location.pathname).toEqual('/login') - expect(store.getActions()).toContainEqual({ - type: 'user/loginError', - payload: { - message: 'user.login.error.message.required', - username: 'user.login.error.username.required', - password: 'user.login.error.password.required', - }, - }) - }) - - it('should show required password error message', async () => { - let wrapper: any - - await act(async () => { - wrapper = await setup({ - user: { - loginError: { - message: 'user.login.error.message.required', - username: 'user.login.error.username.required', - password: 'user.login.error.password.required', - }, - }, - } as any) - }) - - let username: ReactWrapper = wrapper.find('#usernameTextInput').at(0) - await act(async () => { - const onChange = username.prop('onChange') as any - await onChange({ currentTarget: { value: 'username' } }) - }) - - wrapper.update() - - const alert = wrapper.find(Alert) - const password = wrapper.find('#passwordTextInput').at(0) - username = wrapper.find('#usernameTextInput').at(0) - - const passwordFeedback = password.find('Feedback') - const usernameFeedback = username.find('Feedback') - - expect(alert.prop('message')).toEqual('user.login.error.message.required') - expect(passwordFeedback.hasClass('undefined')).not.toBeTruthy() - expect(usernameFeedback.hasClass('undefined')).toBeTruthy() - }) - - it('should get incorrect username or password error when incorrect username or password is provided', async () => { - let wrapper: any - - await act(async () => { - wrapper = await setup() - }) - - jest.spyOn(remoteDb, 'logIn').mockRejectedValue({ status: 401 }) - - const username = wrapper.find('#usernameTextInput').at(0) - await act(async () => { - const onChange = username.prop('onChange') as any - await onChange({ currentTarget: { value: 'username' } }) - }) - - const password = wrapper.find('#passwordTextInput').at(0) - await act(async () => { - const onChange = password.prop('onChange') as any - await onChange({ currentTarget: { value: 'password' } }) - }) - - wrapper.update() - - const saveButton = wrapper.find({ type: 'submit' }).at(0) - await act(async () => { - const onClick = saveButton.prop('onClick') as any - await onClick({ preventDefault: jest.fn() }) - }) - - wrapper.update() - - expect(remoteDb.logIn).toHaveBeenCalledWith('username', 'password') - expect(history.location.pathname).toEqual('/login') - expect(store.getActions()).toContainEqual({ - type: 'user/loginError', - payload: { - message: 'user.login.error.message.incorrect', - }, - }) - }) - }) - - describe('Sucessfully login', () => { - it('should log in if username and password is correct', async () => { - let wrapper: any - - await act(async () => { - wrapper = await setup() - }) - - jest.spyOn(remoteDb, 'logIn').mockResolvedValue({ - name: 'username', - ok: true, - roles: [], - }) - - jest.spyOn(remoteDb, 'getUser').mockResolvedValue({ - _id: 'userId', - metadata: { - givenName: 'test', - familyName: 'user', - }, - } as any) - - const username = wrapper.find('#usernameTextInput').at(0) - await act(async () => { - const onChange = username.prop('onChange') as any - await onChange({ currentTarget: { value: 'username' } }) - }) - - const password = wrapper.find('#passwordTextInput').at(0) - await act(async () => { - const onChange = password.prop('onChange') as any - await onChange({ currentTarget: { value: 'password' } }) - }) - - const saveButton = wrapper.find({ type: 'submit' }).at(0) - - await act(async () => { - const onClick = saveButton.prop('onClick') as any - await onClick({ preventDefault: jest.fn() }) - }) - - wrapper.update() - - expect(store.getActions()[0].payload.user).toEqual({ - id: 'userId', - givenName: 'test', - familyName: 'user', - }) - expect(store.getActions()[0].type).toEqual('user/loginSuccess') - }) - }) -}) diff --git a/src/login/Login.tsx b/src/login/Login.tsx deleted file mode 100644 index 6ca3aefd3e..0000000000 --- a/src/login/Login.tsx +++ /dev/null @@ -1,79 +0,0 @@ -import { Alert, Container, Panel } from '@hospitalrun/components' -import React, { useState } from 'react' -import Button from 'react-bootstrap/Button' -import { useDispatch, useSelector } from 'react-redux' -import { Redirect } from 'react-router-dom' - -import TextInputWithLabelFormGroup from '../shared/components/input/TextInputWithLabelFormGroup' -import useTranslator from '../shared/hooks/useTranslator' -import logo from '../shared/static/images/logo-on-transparent.png' -import { RootState } from '../shared/store' -import { login } from '../user/user-slice' - -const Login = () => { - const dispatch = useDispatch() - const { t } = useTranslator() - const [username, setUsername] = useState('') - const [password, setPassword] = useState('') - const { loginError, user } = useSelector((root: RootState) => root.user) - - const onUsernameChange = (event: React.ChangeEvent) => { - const { value } = event.currentTarget - setUsername(value) - } - - const onPasswordChange = (event: React.ChangeEvent) => { - const { value } = event.currentTarget - setPassword(value) - } - - const onSignInClick = async (event: React.MouseEvent) => { - event.preventDefault() - await dispatch(login(username, password)) - } - - if (user) { - return - } - - return ( - <> - - HospitalRun -
- - {loginError?.message && ( - - )} - - - - -
-
- - ) -} - -export default Login diff --git a/src/shared/config/pouchdb.ts b/src/shared/config/pouchdb.ts index c3fc9264e4..e7eb77c722 100644 --- a/src/shared/config/pouchdb.ts +++ b/src/shared/config/pouchdb.ts @@ -21,12 +21,7 @@ if (process.env.NODE_ENV === 'test') { serverDb = new PouchDB('hospitalrun', { skip_setup: true, adapter: 'memory' }) localDb = new PouchDB('local_hospitalrun', { skip_setup: true, adapter: 'memory' }) } else { - serverDb = new PouchDB(`${process.env.REACT_APP_HOSPITALRUN_API}/hospitalrun`, { - skip_setup: true, - }) - localDb = new PouchDB('local_hospitalrun', { skip_setup: true }) - localDb.sync(serverDb, { live: true, retry: true }) } export const schema = [ diff --git a/src/user/user-slice.ts b/src/user/user-slice.ts index 7774b1b666..6d72c8f806 100644 --- a/src/user/user-slice.ts +++ b/src/user/user-slice.ts @@ -19,6 +19,12 @@ interface UserState { } const initialState: UserState = { + user: { + givenName: 'HospitalRun', + familyName: 'Test', + fullName: 'HospitalRun Test', + id: 'test-hospitalrun', + }, permissions: [ Permissions.ReadPatients, Permissions.WritePatients,