forked from gchq/Kai
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
fcca905
commit 329949c
Showing
2 changed files
with
165 additions
and
17 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
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,14 +1,165 @@ | ||
import { AuthenticationDetails, CognitoUser } from 'amazon-cognito-identity-js'; | ||
import { CognitoClient } from '../../src/rest/cognito-client'; | ||
import { RestClient } from '../../src/rest/rest-client'; | ||
|
||
jest.mock('amazon-cognito-identity-js'); | ||
jest.mock('../../src/rest/rest-client'); | ||
|
||
describe('Existing User Sign In', () => { | ||
it('should call onSuccess when login is successful', () => { | ||
mockSuccessfulAuthenticateUser('My-co6n1t0-t0k3n'); | ||
|
||
describe('ResetTempPasswrod Service', () => { | ||
it('should reset the password successfully with no errors', () => { | ||
const username = 'John Smith'; | ||
const password = 'Password1'; | ||
const onSuccess = jest.fn(); | ||
const onFailure = jest.fn(); | ||
const onError = jest.fn(); | ||
|
||
CognitoClient.login(username, password, onSuccess, onError); | ||
|
||
expect(onSuccess).toHaveBeenCalledTimes(1); | ||
expect(onError).toHaveBeenCalledTimes(0); | ||
expect(RestClient.setJwtToken).toHaveBeenLastCalledWith('My-co6n1t0-t0k3n'); | ||
}); | ||
it('should call onError with error message when login fails', () => { | ||
mockFailAuthenticateUser('Failed because of blahhh'); | ||
|
||
const username = 'John Smith'; | ||
const password = 'Password1'; | ||
const onSuccess = jest.fn(); | ||
const onError = jest.fn(); | ||
|
||
CognitoClient.login(username, password, onSuccess, onError); | ||
|
||
expect(onSuccess).toHaveBeenCalledTimes(0); | ||
expect(onError).toHaveBeenCalledTimes(1); | ||
expect(onError).toHaveBeenLastCalledWith('Failed because of blahhh'); | ||
}); | ||
}); | ||
|
||
describe('New User Sign In', () => { | ||
it('should call onSuccess when login is successful', () => { | ||
mockSuccessfulAuthenticateNewUser('My-co6n1t0-t0k3n'); | ||
|
||
const onSuccess = jest.fn(); | ||
const onError = jest.fn(); | ||
|
||
CognitoClient.loginAndSetNewPassword('John Smith', 'P@$$word', 'N3wPassw0rd', onSuccess, onError); | ||
|
||
expect(onSuccess).toHaveBeenCalledTimes(1); | ||
expect(onError).toHaveBeenCalledTimes(0); | ||
expect(RestClient.setJwtToken).toHaveBeenLastCalledWith('My-co6n1t0-t0k3n'); | ||
}); | ||
it('should call onError with error message when login fails', () => { | ||
mockFailAuthenticateNewUser('Unable to set new password'); | ||
|
||
const onSuccess = jest.fn(); | ||
const onError = jest.fn(); | ||
|
||
CognitoClient.login(username, password, onSuccess, onFailure); | ||
CognitoClient.loginAndSetNewPassword('John Smith', 'P@$$word', 'N3wPassw0rd', onSuccess, onError); | ||
|
||
// expect(onFailure).toHaveBeenCalledTimes(1); | ||
expect(onSuccess).toHaveBeenCalledTimes(0); | ||
expect(onError).toHaveBeenCalledTimes(1); | ||
expect(onError).toHaveBeenLastCalledWith('Unable to set new password'); | ||
}); | ||
}); | ||
|
||
describe('Sign Out', () => { | ||
it('should call onSuccess when login is successful', () => { | ||
mockSuccessfulGlobalSignOut(); | ||
|
||
const onSuccess = jest.fn(); | ||
const onError = jest.fn(); | ||
|
||
CognitoClient.signOutCognitoUser(onSuccess, onError); | ||
|
||
expect(onSuccess).toHaveBeenCalledTimes(1); | ||
expect(onError).toHaveBeenCalledTimes(0); | ||
}); | ||
it('should call onSuccess when login is successful', () => { | ||
mockFailedGlobalSignOut('Failed to sign out'); | ||
|
||
const onSuccess = jest.fn(); | ||
const onError = jest.fn(); | ||
|
||
CognitoClient.signOutCognitoUser(onSuccess, onError); | ||
|
||
expect(onSuccess).toHaveBeenCalledTimes(0); | ||
expect(onError).toHaveBeenCalledTimes(1); | ||
expect(onError).toHaveBeenLastCalledWith('Failed to sign out'); | ||
}); | ||
}); | ||
|
||
function mockSuccessfulAuthenticateUser(jwtToken: string) { | ||
const result = { | ||
getIdToken: () => { | ||
return { | ||
getJwtToken: () => { | ||
return jwtToken; | ||
}, | ||
}; | ||
}, | ||
}; | ||
// @ts-ignore | ||
CognitoUser.prototype.authenticateUser.mockImplementationOnce( | ||
(authDetails: AuthenticationDetails, callBacks: CognitoCallBacks) => { | ||
callBacks.onSuccess(result); | ||
} | ||
); | ||
} | ||
|
||
function mockFailAuthenticateUser(errorMessage: string) { | ||
// @ts-ignore | ||
CognitoUser.prototype.authenticateUser.mockImplementationOnce( | ||
(authDetails: AuthenticationDetails, callBacks: CognitoCallBacks) => { | ||
callBacks.onFailure({ message: errorMessage }); | ||
} | ||
); | ||
} | ||
|
||
function mockSuccessfulAuthenticateNewUser(jwtToken: string) { | ||
const result = { | ||
getIdToken: () => { | ||
return { | ||
getJwtToken: () => { | ||
return jwtToken; | ||
}, | ||
}; | ||
}, | ||
}; | ||
// @ts-ignore | ||
CognitoUser.prototype.authenticateUser.mockImplementationOnce( | ||
(authDetails: AuthenticationDetails, callBacks: CognitoCallBacks) => { | ||
callBacks.onSuccess(result); | ||
callBacks.newPasswordRequired(jest.fn()); | ||
} | ||
); | ||
} | ||
|
||
function mockFailAuthenticateNewUser(errorMessage: string) { | ||
// @ts-ignore | ||
CognitoUser.prototype.authenticateUser.mockImplementationOnce( | ||
(authDetails: AuthenticationDetails, callBacks: CognitoCallBacks) => { | ||
callBacks.onFailure({ message: errorMessage }); | ||
} | ||
); | ||
} | ||
|
||
function mockSuccessfulGlobalSignOut() { | ||
// @ts-ignore | ||
CognitoUser.prototype.globalSignOut.mockImplementationOnce((callBacks: CognitoCallBacks) => { | ||
callBacks.onSuccess(); | ||
}); | ||
} | ||
|
||
function mockFailedGlobalSignOut(errorMessage: string) { | ||
// @ts-ignore | ||
CognitoUser.prototype.globalSignOut.mockImplementationOnce((callBacks: CognitoCallBacks) => { | ||
callBacks.onFailure({ message: errorMessage }); | ||
}); | ||
} | ||
|
||
interface CognitoCallBacks { | ||
onSuccess: (result?: any) => void; | ||
onFailure: (error: { message: string }) => void; | ||
newPasswordRequired: (userAttributes: any) => void; | ||
} |