Skip to content

Increase unit test coverage for oauth.ts to 100% #9140

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/test-changed-auth.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ env:
# the behavior to use the new URLs.
CHROMEDRIVER_CDNURL: https://googlechromelabs.github.io/
CHROMEDRIVER_CDNBINARIESURL: https://storage.googleapis.com/chrome-for-testing-public
CHROME_VALIDATED_VERSION: linux-120.0.6099.71
CHROME_VALIDATED_VERSION: linux-137.0.7151.119
# Bump Node memory limit
NODE_OPTIONS: "--max_old_space_size=4096"

Expand Down Expand Up @@ -119,4 +119,4 @@ jobs:
- name: Run tests on changed packages
run: yarn test:changed auth
env:
BROWSERS: 'WebkitHeadless'
BROWSERS: 'WebkitHeadless'
99 changes: 99 additions & 0 deletions packages/auth/src/core/providers/oauth.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,15 @@ import { AuthErrorCode } from '../errors';
import { UserCredentialImpl } from '../user/user_credential_impl';
import { _createError } from '../util/assert';
import { OAuthProvider } from './oauth';
import { OAuthCredential } from '../credentials/oauth';

describe('core/providers/oauth', () => {
const callMethod = (tokenResponse: any): OAuthCredential => {
return (OAuthProvider as any).oauthCredentialFromTaggedObject({
_tokenResponse: tokenResponse
});
};

it('generates the correct type of oauth credential', () => {
const cred = new OAuthProvider('google.com').credential({
idToken: 'id-token',
Expand Down Expand Up @@ -125,4 +132,96 @@ describe('core/providers/oauth', () => {
expect(cred.signInMethod).to.eq('foo.test');
expect((cred.toJSON() as { nonce: string }).nonce).to.eq('i-am-a-nonce');
});

it('creates OAuthCredential from valid object input', () => {
const input = {
providerId: 'google.com',
signInMethod: 'google.com',
idToken: 'id-token',
accessToken: 'access-token'
};

const credential = OAuthProvider.credentialFromJSON(input);
expect(credential).to.be.instanceOf(OAuthCredential);
expect(credential.providerId).to.equal('google.com');
expect(credential.signInMethod).to.equal('google.com');
expect(credential.idToken).to.equal('id-token');
expect(credential.accessToken).to.equal('access-token');
});

it('creates OAuthCredential from valid JSON string input', () => {
const input = JSON.stringify({
providerId: 'providerid',
signInMethod: 'providerid',
accessToken: 'access-token'
});

const credential = OAuthProvider.credentialFromJSON(input);
expect(credential).to.be.instanceOf(OAuthCredential);
expect(credential.providerId).to.equal('providerid');
expect(credential.signInMethod).to.equal('providerid');
expect(credential.accessToken).to.equal('access-token');
});

it('throws an error if providerId or signInMethod is missing', () => {
const input = {
idToken: 'missing-provider-id'
};

expect(() => {
OAuthProvider.credentialFromJSON(input);
}).to.throw(AuthErrorCode.ARGUMENT_ERROR);
});

it('throws an error if JSON string is invalid', () => {
const invalidJson = '{ not valid json }';

expect(() => {
OAuthProvider.credentialFromJSON(invalidJson);
}).to.throw(SyntaxError);
});

it('returns null if tokenResponse is missing', () => {
const result = callMethod(undefined);
expect(result).to.be.null;
});

it('returns null if all tokens (idToken, accessToken, tokenSecret, pendingToken) are missing', () => {
const result = callMethod({
providerId: 'google.com'
// all token fields missing
});
expect(result).to.be.null;
});

it('returns null if providerId is missing', () => {
const result = callMethod({
oauthIdToken: 'id-token',
oauthAccessToken: 'access-token'
// providerId is missing
});
expect(result).to.be.null;
});

it('returns null if OAuthProvider._credential throws', () => {
const proto = OAuthProvider.prototype as any;
const original = proto._credential;

// Temporarily replace _credential to throw an error
proto._credential = () => {
throw new Error('Simulated error');
};

const result = (OAuthProvider as any).oauthCredentialFromTaggedObject({
_tokenResponse: {
providerId: 'google.com',
oauthIdToken: 'id-token'
}
});

expect(result).to.be.null;

// Restore original method
proto._credential = original;
});
});
Loading