Skip to content

Commit

Permalink
not throw when errors for signOut
Browse files Browse the repository at this point in the history
  • Loading branch information
elorzafe committed Aug 25, 2023
1 parent 3f4a76c commit 197d855
Show file tree
Hide file tree
Showing 2 changed files with 115 additions and 11 deletions.
112 changes: 111 additions & 1 deletion packages/auth/__tests__/providers/cognito/signOut.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@ const mockedAccessToken =
'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyLCJvcmlnaW5fanRpIjoiYXNjIn0.4X9nPnldRthcZwi9b0y3rvNn1jvzHnkgJjeEmzmq5VQ';
const mockRefreshToken = 'abcdefghijk';

describe('signOut tests no oauth', () => {
describe('signOut tests no oauth happy path', () => {
let tokenStoreSpy;
let tokenOrchestratorSpy;
let globalSignOutSpy;
let revokeTokenSpy;

beforeEach(() => {
Amplify.configure(
{
Expand Down Expand Up @@ -90,6 +91,115 @@ describe('signOut tests no oauth', () => {
expect(tokenOrchestratorSpy).toBeCalled();
expect(tokenStoreSpy).toBeCalled();
});

beforeAll(() => {
jest.resetAllMocks();
});

afterEach(() => {
jest.resetAllMocks();
});
});

describe('signOut tests no oauth request fail', () => {
let tokenStoreSpy;
let tokenOrchestratorSpy;
let globalSignOutSpy;
let revokeTokenSpy;

beforeAll(() => {
jest.resetAllMocks();
});

afterEach(() => {
jest.resetAllMocks();
});

beforeEach(() => {
Amplify.configure(
{
Auth: {
userPoolWebClientId: '111111-aaaaa-42d8-891d-ee81a1549398',
userPoolId: 'us-west-2_zzzzz',
identityPoolId: 'us-west-2:xxxxxx',
},
},
{
Auth: {
tokenProvider: TokenProvider.CognitoUserPoolsTokenProvider,
},
}
);

revokeTokenSpy = jest
.spyOn(clients, 'revokeToken')
.mockImplementation(async () => {
throw new Error('fail!!!');
});

tokenStoreSpy = jest
.spyOn(TokenProvider.DefaultTokenStore.prototype, 'loadTokens')
.mockImplementation(async () => {
return {
accessToken: decodeJWT(mockedAccessToken),
refreshToken: mockRefreshToken,
clockDrift: 0,
};
});

tokenOrchestratorSpy = jest
.spyOn(TokenProvider.tokenOrchestrator, 'clearTokens')
.mockImplementation(async () => {});

globalSignOutSpy = jest
.spyOn(clients, 'globalSignOut')
.mockImplementation(async () => {
console.log('calling spy');
throw new Error('fail!!!');
});
});

test('test client signOut no oauth', async () => {
try {
await signOut({ global: false });
} catch (err) {
fail('this shouldnt happen');
}

expect(revokeTokenSpy).toBeCalledWith(
{
region: 'us-west-2',
},
{
ClientId: '111111-aaaaa-42d8-891d-ee81a1549398',
Token: 'abcdefghijk',
}
);

expect(globalSignOutSpy).not.toHaveBeenCalled();
expect(tokenOrchestratorSpy).toBeCalled();
expect(tokenStoreSpy).toBeCalled();
});

test('global sign out no oauth', async () => {
try {
await signOut({ global: true });
} catch (err) {
fail('this shouldnt happen');
}

expect(globalSignOutSpy).toBeCalledWith(
{
region: 'us-west-2',
},
{
AccessToken: mockedAccessToken,
}
);

expect(tokenOrchestratorSpy).toBeCalled();
expect(tokenStoreSpy).toBeCalled();
});
});

describe('signOut tests with oauth', () => {
Expand Down
14 changes: 4 additions & 10 deletions packages/auth/src/providers/cognito/apis/signOut.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ async function clientSignOut(authConfig: AuthConfig) {

await handleOAuthSignOut(authConfig);
} catch (err) {
throwSignOutError(err);
// this shouldn't throw
// TODO(v6): add logger message
} finally {
tokenOrchestrator.clearTokens();
}
Expand All @@ -79,20 +80,13 @@ async function globalSignOut(authConfig: AuthConfig) {

await handleOAuthSignOut(authConfig);
} catch (err) {
throwSignOutError(err);
// it should not throw
// TODO(v6): add logger
} finally {
tokenOrchestrator.clearTokens();
}
}

function throwSignOutError(underlyingError: string) {
throw new AuthError({
message: 'SignOut error',
name: 'SignOutError',
underlyingError,
});
}

function handleOAuthSignOut(authConfig: AuthConfig) {
try {
assertOAuthConfig(authConfig);
Expand Down

0 comments on commit 197d855

Please sign in to comment.