Skip to content

Commit 653955c

Browse files
authored
Merge aa83460 into ab5c2a0
2 parents ab5c2a0 + aa83460 commit 653955c

File tree

2 files changed

+101
-2
lines changed

2 files changed

+101
-2
lines changed

.github/workflows/test-changed-auth.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ env:
2323
# the behavior to use the new URLs.
2424
CHROMEDRIVER_CDNURL: https://googlechromelabs.github.io/
2525
CHROMEDRIVER_CDNBINARIESURL: https://storage.googleapis.com/chrome-for-testing-public
26-
CHROME_VALIDATED_VERSION: linux-120.0.6099.71
26+
CHROME_VALIDATED_VERSION: linux-137.0.7151.119
2727
# Bump Node memory limit
2828
NODE_OPTIONS: "--max_old_space_size=4096"
2929

@@ -119,4 +119,4 @@ jobs:
119119
- name: Run tests on changed packages
120120
run: yarn test:changed auth
121121
env:
122-
BROWSERS: 'WebkitHeadless'
122+
BROWSERS: 'WebkitHeadless'

packages/auth/src/core/providers/oauth.test.ts

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,15 @@ import { AuthErrorCode } from '../errors';
2626
import { UserCredentialImpl } from '../user/user_credential_impl';
2727
import { _createError } from '../util/assert';
2828
import { OAuthProvider } from './oauth';
29+
import { OAuthCredential } from '../credentials/oauth';
2930

3031
describe('core/providers/oauth', () => {
32+
const callMethod = (tokenResponse: any): OAuthCredential => {
33+
return (OAuthProvider as any).oauthCredentialFromTaggedObject({
34+
_tokenResponse: tokenResponse
35+
});
36+
};
37+
3138
it('generates the correct type of oauth credential', () => {
3239
const cred = new OAuthProvider('google.com').credential({
3340
idToken: 'id-token',
@@ -125,4 +132,96 @@ describe('core/providers/oauth', () => {
125132
expect(cred.signInMethod).to.eq('foo.test');
126133
expect((cred.toJSON() as { nonce: string }).nonce).to.eq('i-am-a-nonce');
127134
});
135+
136+
it('creates OAuthCredential from valid object input', () => {
137+
const input = {
138+
providerId: 'google.com',
139+
signInMethod: 'google.com',
140+
idToken: 'id-token',
141+
accessToken: 'access-token'
142+
};
143+
144+
const credential = OAuthProvider.credentialFromJSON(input);
145+
expect(credential).to.be.instanceOf(OAuthCredential);
146+
expect(credential.providerId).to.equal('google.com');
147+
expect(credential.signInMethod).to.equal('google.com');
148+
expect(credential.idToken).to.equal('id-token');
149+
expect(credential.accessToken).to.equal('access-token');
150+
});
151+
152+
it('creates OAuthCredential from valid JSON string input', () => {
153+
const input = JSON.stringify({
154+
providerId: 'providerid',
155+
signInMethod: 'providerid',
156+
accessToken: 'access-token'
157+
});
158+
159+
const credential = OAuthProvider.credentialFromJSON(input);
160+
expect(credential).to.be.instanceOf(OAuthCredential);
161+
expect(credential.providerId).to.equal('providerid');
162+
expect(credential.signInMethod).to.equal('providerid');
163+
expect(credential.accessToken).to.equal('access-token');
164+
});
165+
166+
it('throws an error if providerId or signInMethod is missing', () => {
167+
const input = {
168+
idToken: 'missing-provider-id'
169+
};
170+
171+
expect(() => {
172+
OAuthProvider.credentialFromJSON(input);
173+
}).to.throw(AuthErrorCode.ARGUMENT_ERROR);
174+
});
175+
176+
it('throws an error if JSON string is invalid', () => {
177+
const invalidJson = '{ not valid json }';
178+
179+
expect(() => {
180+
OAuthProvider.credentialFromJSON(invalidJson);
181+
}).to.throw(SyntaxError);
182+
});
183+
184+
it('returns null if tokenResponse is missing', () => {
185+
const result = callMethod(undefined);
186+
expect(result).to.be.null;
187+
});
188+
189+
it('returns null if all tokens (idToken, accessToken, tokenSecret, pendingToken) are missing', () => {
190+
const result = callMethod({
191+
providerId: 'google.com'
192+
// all token fields missing
193+
});
194+
expect(result).to.be.null;
195+
});
196+
197+
it('returns null if providerId is missing', () => {
198+
const result = callMethod({
199+
oauthIdToken: 'id-token',
200+
oauthAccessToken: 'access-token'
201+
// providerId is missing
202+
});
203+
expect(result).to.be.null;
204+
});
205+
206+
it('returns null if OAuthProvider._credential throws', () => {
207+
const proto = OAuthProvider.prototype as any;
208+
const original = proto._credential;
209+
210+
// Temporarily replace _credential to throw an error
211+
proto._credential = () => {
212+
throw new Error('Simulated error');
213+
};
214+
215+
const result = (OAuthProvider as any).oauthCredentialFromTaggedObject({
216+
_tokenResponse: {
217+
providerId: 'google.com',
218+
oauthIdToken: 'id-token'
219+
}
220+
});
221+
222+
expect(result).to.be.null;
223+
224+
// Restore original method
225+
proto._credential = original;
226+
});
128227
});

0 commit comments

Comments
 (0)