Skip to content

Commit

Permalink
Unit tests for Cognito integration
Browse files Browse the repository at this point in the history
  • Loading branch information
macenturalxl1 committed Nov 16, 2020
1 parent fcca905 commit 329949c
Show file tree
Hide file tree
Showing 2 changed files with 165 additions and 17 deletions.
21 changes: 9 additions & 12 deletions ui/src/rest/cognito-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,18 +74,15 @@ export class CognitoClient {
};
this.cognitoUser = new CognitoUser(userData);
}

public static signOutCognitoUser(onSuccess: Function, onError: Function) {
const currentUser = this.cognitoUser;
if (currentUser !== undefined) {
currentUser.globalSignOut({
onSuccess: function (msg) {
onSuccess();
},
onFailure: function (err) {
onError(err);
},
});
}
onError('No user is logged in');
this.cognitoUser.globalSignOut({
onSuccess: function () {
onSuccess();
},
onFailure: function (err) {
onError(err.message);
},
});
}
}
161 changes: 156 additions & 5 deletions ui/test/rest/cognito-client.test.ts
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;
}

0 comments on commit 329949c

Please sign in to comment.