diff --git a/.github/workflows/test-changed-auth.yml b/.github/workflows/test-changed-auth.yml index b72c7cd9e2d..84b0ef4adda 100644 --- a/.github/workflows/test-changed-auth.yml +++ b/.github/workflows/test-changed-auth.yml @@ -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" @@ -119,4 +119,4 @@ jobs: - name: Run tests on changed packages run: yarn test:changed auth env: - BROWSERS: 'WebkitHeadless' \ No newline at end of file + BROWSERS: 'WebkitHeadless' diff --git a/packages/auth/src/core/providers/oauth.test.ts b/packages/auth/src/core/providers/oauth.test.ts index 09acbc5b095..2c8554aec1d 100644 --- a/packages/auth/src/core/providers/oauth.test.ts +++ b/packages/auth/src/core/providers/oauth.test.ts @@ -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', @@ -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; + }); });