Skip to content

Commit

Permalink
chore(auth): manual fix linter errors
Browse files Browse the repository at this point in the history
  • Loading branch information
HuiSF committed May 24, 2024
1 parent 60b561e commit 237e5c5
Show file tree
Hide file tree
Showing 26 changed files with 127 additions and 136 deletions.
2 changes: 2 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,11 @@ module.exports = {
'logout_uri',
'id_token',
'access_token',
'refresh_token',
'token_type',
'expires_in',
'error_description',
'error_message',
// exceptions for the notifications package
'campaign_id',
'delivery_type',
Expand Down
3 changes: 0 additions & 3 deletions packages/auth/__tests__/mockData.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
// keeping this so that jest does not complain
test('should do nothing', () => {});

// device tracking mock device data
export const mockDeviceArray = [
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ describe('asserts service errors', () => {
test('it should throw an unknown error when error is null', () => {
try {
const error = null;
expect(assertServiceError(error)).toThrow();
expect(() => {
assertServiceError(error);
}).toThrow();
} catch (error: any) {
expect(error).toBeInstanceOf(AuthError);
expect(error.name).toBe(AmplifyErrorCode.Unknown);
Expand All @@ -17,7 +19,9 @@ describe('asserts service errors', () => {
test('it should throw an unknown error when error is a TypeError', () => {
try {
const error = new TypeError('TypeError');
expect(assertServiceError(error)).toThrow();
expect(() => {
assertServiceError(error);
}).toThrow();
} catch (error: any) {
expect(error).toBeInstanceOf(AuthError);
expect(error.name).toBe(AmplifyErrorCode.Unknown);
Expand All @@ -26,7 +30,9 @@ describe('asserts service errors', () => {
test('it should throw an unknown error when error does not have a name', () => {
try {
const error = new Error('Error');
expect(assertServiceError(error)).toThrow();
expect(() => {
assertServiceError(error);
}).toThrow();
} catch (error: any) {
expect(error).toBeInstanceOf(AuthError);
expect(error.name).toBe(AmplifyErrorCode.Unknown);
Expand All @@ -35,6 +41,8 @@ describe('asserts service errors', () => {
test('it should not throw if the error is coming from the service', () => {
const error = new Error('Service Error');
error.name = InitiateAuthException.InternalErrorException;
expect(assertServiceError(error)).toBeUndefined();
expect(() => {
assertServiceError(error);
}).not.toThrow();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ describe('confirmResetPassword', () => {
});

it('should call the confirmForgotPassword and return void', async () => {
expect(
await confirmResetPassword(authAPITestParams.confirmResetPasswordRequest),
).toBeUndefined();
await expect(
confirmResetPassword(authAPITestParams.confirmResetPasswordRequest),
).resolves.toBeUndefined();
expect(mockConfirmForgotPassword).toHaveBeenCalled();
});

Expand Down Expand Up @@ -137,7 +137,7 @@ describe('confirmResetPassword', () => {
});

it('should add UserContextData', async () => {
window.AmazonCognitoAdvancedSecurityData = {
(window as any).AmazonCognitoAdvancedSecurityData = {
getData() {
return 'abcd';
},
Expand All @@ -164,6 +164,6 @@ describe('confirmResetPassword', () => {
},
}),
);
window.AmazonCognitoAdvancedSecurityData = undefined;
(window as any).AmazonCognitoAdvancedSecurityData = undefined;
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ const authConfig = {
const mockedGetCurrentUser = getCurrentUser as jest.Mock;

describe('confirmSignIn API happy path cases', () => {
let handleChallengeNameSpy;
let handleChallengeNameSpy: jest.SpyInstance;
const { username } = authAPITestParams.user1;
const { password } = authAPITestParams.user1;

Expand Down Expand Up @@ -276,8 +276,8 @@ describe('confirmSignIn API happy path cases', () => {
});

describe('Cognito ASF', () => {
let respondToAuthChallengeSpy;
let handleUserSRPAuthFlowSpy;
let respondToAuthChallengeSpy: jest.SpyInstance;
let handleUserSRPAuthFlowSpy: jest.SpyInstance;

const { username } = authAPITestParams.user1;
const { password } = authAPITestParams.user1;
Expand All @@ -287,7 +287,7 @@ describe('Cognito ASF', () => {
});

// load Cognito ASF polyfill
window.AmazonCognitoAdvancedSecurityData = {
(window as any).AmazonCognitoAdvancedSecurityData = {
getData() {
return 'abcd';
},
Expand Down Expand Up @@ -318,7 +318,7 @@ describe('Cognito ASF', () => {
afterEach(() => {
respondToAuthChallengeSpy.mockClear();
handleUserSRPAuthFlowSpy.mockClear();
window.AmazonCognitoAdvancedSecurityData = undefined;
(window as any).AmazonCognitoAdvancedSecurityData = undefined;
});

afterAll(() => {
Expand Down Expand Up @@ -415,16 +415,14 @@ describe('Cognito ASF', () => {
Amplify.configure({
Auth: authConfig,
});
const handleUserSRPAuthflowSpy = jest
.spyOn(signInHelpers, 'handleUserSRPAuthFlow')
.mockImplementationOnce(
async (): Promise<RespondToAuthChallengeCommandOutput> => ({
ChallengeName: 'SOFTWARE_TOKEN_MFA',
Session: '1234234232',
$metadata: {},
ChallengeParameters: {},
}),
);
jest.spyOn(signInHelpers, 'handleUserSRPAuthFlow').mockImplementationOnce(
async (): Promise<RespondToAuthChallengeCommandOutput> => ({
ChallengeName: 'SOFTWARE_TOKEN_MFA',
Session: '1234234232',
$metadata: {},
ChallengeParameters: {},
}),
);

const result = await signIn({ username, password });

Expand Down Expand Up @@ -460,16 +458,14 @@ describe('Cognito ASF', () => {
Amplify.configure({
Auth: authConfig,
});
const handleUserSRPAuthflowSpy = jest
.spyOn(signInHelpers, 'handleUserSRPAuthFlow')
.mockImplementationOnce(
async (): Promise<RespondToAuthChallengeCommandOutput> => ({
ChallengeName: 'NEW_PASSWORD_REQUIRED',
Session: '1234234232',
$metadata: {},
ChallengeParameters: {},
}),
);
jest.spyOn(signInHelpers, 'handleUserSRPAuthFlow').mockImplementationOnce(
async (): Promise<RespondToAuthChallengeCommandOutput> => ({
ChallengeName: 'NEW_PASSWORD_REQUIRED',
Session: '1234234232',
$metadata: {},
ChallengeParameters: {},
}),
);

const result = await signIn({ username, password });

Expand Down Expand Up @@ -506,16 +502,14 @@ describe('Cognito ASF', () => {
Amplify.configure({
Auth: authConfig,
});
const handleUserSRPAuthflowSpy = jest
.spyOn(signInHelpers, 'handleUserSRPAuthFlow')
.mockImplementationOnce(
async (): Promise<RespondToAuthChallengeCommandOutput> => ({
ChallengeName: 'CUSTOM_CHALLENGE',
Session: '1234234232',
$metadata: {},
ChallengeParameters: {},
}),
);
jest.spyOn(signInHelpers, 'handleUserSRPAuthFlow').mockImplementationOnce(
async (): Promise<RespondToAuthChallengeCommandOutput> => ({
ChallengeName: 'CUSTOM_CHALLENGE',
Session: '1234234232',
$metadata: {},
ChallengeParameters: {},
}),
);

const result = await signIn({ username, password });

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ describe('confirmSignUp', () => {
});

it('should send UserContextData', async () => {
window.AmazonCognitoAdvancedSecurityData = {
(window as any).AmazonCognitoAdvancedSecurityData = {
getData() {
return 'abcd';
},
Expand All @@ -171,6 +171,6 @@ describe('confirmSignUp', () => {
},
);
expect(mockConfirmSignUp).toHaveBeenCalledTimes(1);
window.AmazonCognitoAdvancedSecurityData = undefined;
(window as any).AmazonCognitoAdvancedSecurityData = undefined;
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// SPDX-License-Identifier: Apache-2.0

import {
GetCredentialsForIdentityInput,
GetCredentialsForIdentityOutput,
ResourcesConfig,
getCredentialsForIdentity,
Expand Down Expand Up @@ -75,11 +74,9 @@ describe('Guest Credentials', () => {
new CognitoAWSCredentialsAndIdentityIdProvider(
new DefaultIdentityIdStore(sharedInMemoryStorage),
);
credentialsForIdentityIdSpy.mockImplementationOnce(
async ({}, params: GetCredentialsForIdentityInput) => {
return authAPITestParams.CredentialsForIdentityIdResult as GetCredentialsForIdentityOutput;
},
);
credentialsForIdentityIdSpy.mockImplementationOnce(async () => {
return authAPITestParams.CredentialsForIdentityIdResult as GetCredentialsForIdentityOutput;
});
});
afterEach(() => {
cognitoCredentialsProvider.clearCredentials();
Expand All @@ -101,9 +98,10 @@ describe('Guest Credentials', () => {
{ IdentityId: 'identity-id-test' },
);
expect(
cognitoCredentialsProvider._nextCredentialsRefresh,
(cognitoCredentialsProvider as any)._nextCredentialsRefresh,
).toBeGreaterThan(0);
});

test('in-memory guest creds are returned if not expired and not past TTL', async () => {
await cognitoCredentialsProvider.getCredentialsAndIdentityId({
authenticated: false,
Expand All @@ -122,19 +120,18 @@ describe('Guest Credentials', () => {
expect(credentialsForIdentityIdSpy).toHaveBeenCalledTimes(1);
});
});

describe('Error Path Cases:', () => {
let cognitoCredentialsProvider;
beforeEach(() => {
cognitoCredentialsProvider =
new CognitoAWSCredentialsAndIdentityIdProvider(
new DefaultIdentityIdStore(sharedInMemoryStorage),
);
credentialsForIdentityIdSpy.mockImplementationOnce(
async ({}, params: GetCredentialsForIdentityInput) => {
return authAPITestParams.NoAccessKeyCredentialsForIdentityIdResult as GetCredentialsForIdentityOutput;
},
);
credentialsForIdentityIdSpy.mockImplementationOnce(async () => {
return authAPITestParams.NoAccessKeyCredentialsForIdentityIdResult as GetCredentialsForIdentityOutput;
});
});

afterEach(() => {
cognitoCredentialsProvider.clearCredentials();
});
Expand All @@ -161,18 +158,16 @@ describe('Guest Credentials', () => {
});

describe('Primary Credentials', () => {
let cognitoCredentialsProvider;
let cognitoCredentialsProvider: CognitoAWSCredentialsAndIdentityIdProvider;
describe('Happy Path Cases:', () => {
beforeEach(() => {
cognitoCredentialsProvider =
new CognitoAWSCredentialsAndIdentityIdProvider(
new DefaultIdentityIdStore(sharedInMemoryStorage),
);
credentialsForIdentityIdSpy.mockImplementation(
async ({}, params: GetCredentialsForIdentityInput) => {
return authAPITestParams.CredentialsForIdentityIdResult as GetCredentialsForIdentityOutput;
},
);
credentialsForIdentityIdSpy.mockImplementation(async () => {
return authAPITestParams.CredentialsForIdentityIdResult as GetCredentialsForIdentityOutput;
});
});
afterEach(() => {
cognitoCredentialsProvider.clearCredentials();
Expand All @@ -184,7 +179,7 @@ describe('Primary Credentials', () => {
authConfig: validAuthConfig.Auth!,
tokens: authAPITestParams.ValidAuthTokens,
});
expect(res.credentials.accessKeyId).toEqual(
expect(res?.credentials.accessKeyId).toEqual(
authAPITestParams.CredentialsForIdentityIdResult.Credentials
.AccessKeyId,
);
Expand All @@ -210,7 +205,7 @@ describe('Primary Credentials', () => {
authConfig: validAuthConfig.Auth!,
tokens: authAPITestParams.ValidAuthTokens,
});
expect(res.credentials.accessKeyId).toEqual(
expect(res?.credentials.accessKeyId).toEqual(
authAPITestParams.CredentialsForIdentityIdResult.Credentials
.AccessKeyId,
);
Expand All @@ -231,7 +226,7 @@ describe('Primary Credentials', () => {
);
expect(credentialsForIdentityIdSpy).toHaveBeenCalledTimes(1);

const res = await cognitoCredentialsProvider.getCredentialsAndIdentityId({
await cognitoCredentialsProvider.getCredentialsAndIdentityId({
authenticated: true,
authConfig: validAuthConfig.Auth!,
tokens: authAPITestParams.NewValidAuthTokens,
Expand Down Expand Up @@ -259,11 +254,9 @@ describe('Primary Credentials', () => {
credentialsForIdentityIdSpy?.mockReset();
});
test('Should throw AuthError if either Credentials, accessKeyId or secretKey is absent in the response', async () => {
credentialsForIdentityIdSpy.mockImplementationOnce(
async ({}, params: GetCredentialsForIdentityInput) => {
return authAPITestParams.NoAccessKeyCredentialsForIdentityIdResult as GetCredentialsForIdentityOutput;
},
);
credentialsForIdentityIdSpy.mockImplementationOnce(async () => {
return authAPITestParams.NoAccessKeyCredentialsForIdentityIdResult as GetCredentialsForIdentityOutput;
});
expect(
cognitoCredentialsProvider.getCredentialsAndIdentityId({
authenticated: true,
Expand All @@ -272,11 +265,9 @@ describe('Primary Credentials', () => {
}),
).rejects.toThrow(AuthError);
credentialsForIdentityIdSpy.mockClear();
credentialsForIdentityIdSpy.mockImplementationOnce(
async ({}, params: GetCredentialsForIdentityInput) => {
return authAPITestParams.NoCredentialsForIdentityIdResult as GetCredentialsForIdentityOutput;
},
);
credentialsForIdentityIdSpy.mockImplementationOnce(async () => {
return authAPITestParams.NoCredentialsForIdentityIdResult as GetCredentialsForIdentityOutput;
});
expect(
cognitoCredentialsProvider.getCredentialsAndIdentityId({
authenticated: true,
Expand All @@ -285,11 +276,9 @@ describe('Primary Credentials', () => {
}),
).rejects.toThrow(AuthError);
credentialsForIdentityIdSpy.mockClear();
credentialsForIdentityIdSpy.mockImplementationOnce(
async ({}, params: GetCredentialsForIdentityInput) => {
return authAPITestParams.NoSecretKeyInCredentialsForIdentityIdResult as GetCredentialsForIdentityOutput;
},
);
credentialsForIdentityIdSpy.mockImplementationOnce(async () => {
return authAPITestParams.NoSecretKeyInCredentialsForIdentityIdResult as GetCredentialsForIdentityOutput;
});
expect(
cognitoCredentialsProvider.getCredentialsAndIdentityId({
authenticated: true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
} from '../../../src/providers/cognito';

describe('fetchAuthSession behavior for IdentityPools only', () => {
let credentialsProviderSpy;
let credentialsProviderSpy: jest.SpyInstance;
afterEach(() => {
jest.resetAllMocks();
jest.clearAllMocks();
Expand Down Expand Up @@ -75,9 +75,8 @@ describe('fetchAuthSession behavior for IdentityPools only', () => {
});

describe('fetchAuthSession behavior for UserPools only', () => {
let tokenProviderSpy;
beforeEach(() => {
tokenProviderSpy = jest
jest
.spyOn(cognitoUserPoolsTokenProvider, 'getTokens')
.mockImplementation(async () => {
return {
Expand Down
Loading

0 comments on commit 237e5c5

Please sign in to comment.