Skip to content

Commit

Permalink
Merge pull request #570 from LD4P/test_cognito_utils_561
Browse files Browse the repository at this point in the history
Add unit tests for CognitoUtils class
  • Loading branch information
jmartin-sul authored May 28, 2019
2 parents c07c9a9 + b827122 commit 4960518
Show file tree
Hide file tree
Showing 2 changed files with 150 additions and 2 deletions.
148 changes: 148 additions & 0 deletions __tests__/CognitoUtils.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
// Copyright 2018 Stanford University see Apache2.txt for license

import { AuthenticationDetails, CognitoUser, CognitoUserPool } from 'amazon-cognito-identity-js'
import CognitoUtils from '../src/CognitoUtils'
import Config from '../src/Config'

describe('CognitoUtils', () => {
const username = 'testuser'
const password = 'testpass'

describe('authenticationDetails()', () => {
it('returns an instance of AuthenticationDetails with passed credentials', () => {
const details = CognitoUtils.authenticationDetails(username, password)
expect(details).toBeInstanceOf(AuthenticationDetails)
expect(details.username).toEqual(username)
expect(details.password).toEqual(password)
})
})

describe('cognitoUserPool()', () => {
it('returns an instance of CognitoUserPool with configured user pool and client IDs', () => {
const pool = CognitoUtils.cognitoUserPool()
expect(pool).toBeInstanceOf(CognitoUserPool)
expect(pool.userPoolId).toEqual(Config.awsCognitoUserPoolId)
expect(pool.clientId).toEqual(Config.awsClientID)
})
})

describe('cognitoUser()', () => {
it('returns an instance of CognitoUser with username and user pool', () => {
const user = CognitoUtils.cognitoUser(username)
expect(user).toBeInstanceOf(CognitoUser)
expect(user.username).toEqual(username)
expect(user.pool).toBeInstanceOf(CognitoUserPool)
})
})

describe('authenticateUser()', () => {
const user = CognitoUtils.cognitoUser(username)

afterEach(() => {
jest.restoreAllMocks()
})

it('returns a promise', () => {
const authResult = CognitoUtils.authenticateUser(user, password)
expect(authResult).toBeInstanceOf(Promise)
})

it('resolves on successful authentication', () => {
const mockSession = {}
const authUserSpy = jest.spyOn(user, 'authenticateUser').mockImplementation((_details, callback) => {
return callback.onSuccess(mockSession)
})
return CognitoUtils.authenticateUser(user, password)
.then(authResult => {
expect(authUserSpy).toHaveBeenCalledTimes(1)
expect(authResult).toEqual(mockSession)
})
.catch(error => { throw error })
})

it('rejects on failed authentication', () => {
const mockError = {}
const authUserSpy = jest.spyOn(user, 'authenticateUser').mockImplementation((_details, callback) => {
return callback.onFailure(mockError)
})
return CognitoUtils.authenticateUser(user, password)
.then(result => { throw result })
.catch(error => {
expect(authUserSpy).toHaveBeenCalledTimes(1)
expect(error).toEqual(mockError)
})
})
})

describe('getSession()', () => {
const user = CognitoUtils.cognitoUser(username)

afterEach(() => {
jest.restoreAllMocks()
})

it('returns a promise', () => {
const result = CognitoUtils.getSession(user)
expect(result).toBeInstanceOf(Promise)
})

it('resolves with session data when successful', () => {
const mockError = null
const mockSession = {}
const getSessionSpy = jest.spyOn(user, 'getSession').mockImplementation(callback => {
return callback(mockError, mockSession)
})
return CognitoUtils.getSession(user)
.then(result => {
expect(getSessionSpy).toHaveBeenCalledTimes(1)
expect(result).toEqual(mockSession)
})
.catch(error => { throw error })
})

it('rejects with error info when failing', () => {
const mockError = 'uh oh!'
const getSessionSpy = jest.spyOn(user, 'getSession').mockImplementation(callback => {
return callback(mockError)
})
return CognitoUtils.getSession(user)
.then(result => { throw result })
.catch(error => {
expect(getSessionSpy).toHaveBeenCalledTimes(1)
expect(error).toEqual(mockError)
})
})
})

describe('getIdTokenString()', () => {
const user = CognitoUtils.cognitoUser(username)

afterEach(() => {
jest.restoreAllMocks()
})

it('returns a promise', () => {
const result = CognitoUtils.getIdTokenString(user)
expect(result).toBeInstanceOf(Promise)
})

it('resolves to a JSON web token string', () => {
const token = 'eyjqew5u4yijrg09jh40ghi909tj9jgh...'
const mockError = null
const mockSession = {
idToken: {
jwtToken: token
}
}
const getSessionSpy = jest.spyOn(user, 'getSession').mockImplementation(callback => {
return callback(mockError, mockSession)
})
return CognitoUtils.getIdTokenString(user)
.then(result => {
expect(getSessionSpy).toHaveBeenCalledTimes(1)
expect(result).toEqual(token)
})
.catch(error => { throw error })
})
})
})
4 changes: 2 additions & 2 deletions src/CognitoUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class CognitoUtils {
// and failure callbacks) than .getSession (a single callback with error and success parameters). the Promise wrappers
// give callers a more uniform API for interacting with asynchronous cognito session operations.
cognitoUser.getSession((errInfo, sessionData) => {
if(errInfo) {
if (errInfo) {
reject(errInfo)
} else {
resolve(sessionData)
Expand All @@ -65,7 +65,7 @@ class CognitoUtils {
// a thin wrapper around CognitoUtils.getSession. returns a Promise which resolves to the value from
// sessionData that's most often useful to other parts of the editor (the JWT ID token string).
static getIdTokenString(cognitoUser) {
return CognitoUtils.getSession(cognitoUser).then((sessionData) => sessionData.idToken.jwtToken)
return CognitoUtils.getSession(cognitoUser).then(sessionData => sessionData.idToken.jwtToken)
}
}

Expand Down

0 comments on commit 4960518

Please sign in to comment.