-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Switches to AWS Amplify for Cognito.
closes #2336
- Loading branch information
1 parent
1042c2c
commit 8733e60
Showing
37 changed files
with
972 additions
and
742 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,38 +1,83 @@ | ||
// Copyright 2019 Stanford University see LICENSE for license | ||
import { | ||
authenticationFailed, authenticationSucceeded, signedOut, | ||
authenticate, signIn, signOut, | ||
} from 'actionCreators/authenticate' | ||
import configureMockStore from 'redux-mock-store' | ||
import thunk from 'redux-thunk' | ||
import Auth from '@aws-amplify/auth' | ||
|
||
describe('authenticationFailed', () => { | ||
const currentUser = { hello: 'world' } | ||
const errInfoauthenticate = { foo: 'bar' } | ||
jest.mock('@aws-amplify/auth') | ||
|
||
const authResult = { | ||
currentUser, | ||
authenticationError: errInfoauthenticate, | ||
} | ||
const mockStore = configureMockStore([thunk]) | ||
|
||
it('returns the failure action', () => { | ||
expect(authenticationFailed(authResult).type).toEqual('AUTHENTICATION_FAILURE') | ||
describe('authenticate', () => { | ||
describe('user already in state', () => { | ||
it('does not authenticate', async () => { | ||
const store = mockStore({ authenticate: { user: { username: 'havram' } } }) | ||
await store.dispatch(authenticate()) | ||
expect(store.getActions()).toEqual([]) | ||
}) | ||
}) | ||
describe('successful', () => { | ||
it('dispatches actions to add user', async () => { | ||
Auth.currentAuthenticatedUser.mockResolvedValue({ username: 'havram', something: 'else' }) | ||
const store = mockStore({ authenticate: { user: undefined } }) | ||
await store.dispatch(authenticate()) | ||
expect(store.getActions()).toHaveAction('SET_USER', { username: 'havram' }) | ||
}) | ||
}) | ||
describe('failure', () => { | ||
it('dispatches actions to remove user', async () => { | ||
Auth.currentAuthenticatedUser.mockRejectedValue() | ||
const store = mockStore({ authenticate: { user: undefined } }) | ||
await store.dispatch(authenticate()) | ||
expect(store.getActions()).toHaveAction('REMOVE_USER') | ||
}) | ||
}) | ||
}) | ||
|
||
describe('authenticationSucceeded', () => { | ||
const currentUser = { hello: 'world' } | ||
const sessionData = { foo: 'bar' } | ||
describe('signIn', () => { | ||
describe('successful', () => { | ||
it('dispatches actions to add user', async () => { | ||
Auth.signIn.mockResolvedValue({ username: 'havram', something: 'else' }) | ||
const store = mockStore() | ||
await store.dispatch(signIn('havram', 'm&rc', 'testerrorkey')) | ||
|
||
const authResult = { | ||
currentUser, | ||
currentSession: sessionData, | ||
} | ||
expect(Auth.signIn).toHaveBeenCalledWith('havram', 'm&rc') | ||
expect(store.getActions()).toHaveAction('SET_USER', { username: 'havram' }) | ||
}) | ||
}) | ||
describe('failure', () => { | ||
it('dispatches actions to remove user', async () => { | ||
Auth.signIn.mockRejectedValue(new Error('Bad user')) | ||
const store = mockStore() | ||
await store.dispatch(signIn('mdewey', 'amh&rst', 'testerrorkey')) | ||
|
||
it('returns the success action', () => { | ||
expect(authenticationSucceeded(authResult).type).toEqual('AUTHENTICATION_SUCCESS') | ||
expect(store.getActions()).toHaveAction('CLEAR_ERRORS', 'testerrorkey') | ||
expect(store.getActions()).toHaveAction('ADD_ERROR', { errorKey: 'testerrorkey', error: 'Login failed: Bad user' }) | ||
expect(store.getActions()).toHaveAction('REMOVE_USER') | ||
}) | ||
}) | ||
}) | ||
|
||
describe('signedOut', () => { | ||
it('returns the signed out action', () => { | ||
expect(signedOut().type).toEqual('SIGN_OUT_SUCCESS') | ||
describe('signOut', () => { | ||
describe('successful', () => { | ||
it('dispatches actions to remove user', async () => { | ||
Auth.signOut.mockResolvedValue() | ||
const store = mockStore() | ||
await store.dispatch(signOut()) | ||
|
||
expect(Auth.signOut).toHaveBeenCalled() | ||
expect(store.getActions()).toHaveAction('REMOVE_USER') | ||
}) | ||
}) | ||
describe('failure', () => { | ||
it('does nothing', async () => { | ||
Auth.signOut.mockRejectedValue() | ||
const store = mockStore() | ||
await store.dispatch(signOut()) | ||
|
||
expect(store.getActions()).toEqual([]) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,19 @@ | ||
// Copyright 2018 Stanford University see LICENSE for license | ||
|
||
import * as awsCognito from 'amazon-cognito-identity-js' | ||
import { | ||
fireEvent, screen, waitForElementToBeRemoved, | ||
} from '@testing-library/react' | ||
|
||
import { createState } from 'stateUtils' | ||
import { createStore, renderApp } from 'testUtils' | ||
import Auth from '@aws-amplify/auth' | ||
|
||
jest.mock('amazon-cognito-identity-js') | ||
|
||
function MockCognitoUserAuthenticationSuccess(_userData) { | ||
this.username = 'Baz Le Quux' | ||
this.authenticateUser = (_authnDetails, callbacks) => { | ||
callbacks.onSuccess({ currentSession: { idToken: {} } }) | ||
} | ||
} | ||
|
||
function MockCognitoUserAuthenticationFailure(_userData) { | ||
this.username = 'Baz Le Quux' | ||
this.authenticateUser = (_authnDetails, callbacks) => { | ||
callbacks.onFailure({ | ||
code: 'NotAuthorizedException', name: 'NotAuthorizedException', message: 'Incorrect username or password.', | ||
}) | ||
} | ||
} | ||
|
||
jest.mock('@aws-amplify/auth') | ||
|
||
describe('user authentication', () => { | ||
Auth.signOut.mockResolvedValue() | ||
Auth.currentAuthenticatedUser.mockRejectedValue() | ||
it('allows a logged in user to log out and allows a new one to login', async () => { | ||
Auth.signIn.mockResolvedValue({ username: 'Baz Le Quux' }) | ||
renderApp() | ||
screen.getByText(/Foo McBar/) // user Foo McBar should be logged in already when using default test redux store | ||
|
||
|
@@ -46,8 +31,6 @@ describe('user authentication', () => { | |
screen.getByText(/Sinopia Version \d+.\d+.\d+ highlights/) | ||
screen.getByRole('link', { name: 'Sinopia help site' }) | ||
|
||
awsCognito.CognitoUser.mockImplementation(MockCognitoUserAuthenticationSuccess) | ||
|
||
// login as a different user | ||
fireEvent.change(screen.getByLabelText('User name'), { target: { value: '[email protected]' } }) | ||
fireEvent.change(screen.getByLabelText('Password'), { target: { value: 'Password2' } }) | ||
|
@@ -59,6 +42,7 @@ describe('user authentication', () => { | |
}) | ||
|
||
it('presents a helpful error when the user enters the wrong password', async () => { | ||
Auth.signIn.mockRejectedValue(new Error('Incorrect username or password.')) | ||
const state = createState({ notAuthenticated: true }) | ||
const store = createStore(state) | ||
renderApp(store) | ||
|
@@ -69,8 +53,6 @@ describe('user authentication', () => { | |
screen.getByText(/Sinopia Version \d+.\d+.\d+ highlights/) | ||
screen.getByRole('link', { name: 'Sinopia help site' }) | ||
|
||
awsCognito.CognitoUser.mockImplementation(MockCognitoUserAuthenticationFailure) | ||
|
||
// try to login | ||
fireEvent.change(screen.getByLabelText('User name'), { target: { value: '[email protected]' } }) | ||
fireEvent.change(screen.getByLabelText('Password'), { target: { value: 'password1' } }) | ||
|
Oops, something went wrong.