Skip to content

Commit d258da4

Browse files
committed
Increase unit test coverage for oauth.ts to 100%
1 parent ab5c2a0 commit d258da4

File tree

1 file changed

+101
-0
lines changed

1 file changed

+101
-0
lines changed

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

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,17 @@ 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';
30+
import sinon from 'sinon';
2931

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

0 commit comments

Comments
 (0)