Skip to content
Merged
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
86 changes: 86 additions & 0 deletions packages/auth/src/core/providers/saml.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import { OperationType } from '../../model/enums';
import { TEST_ID_TOKEN_RESPONSE } from '../../../test/helpers/id_token_response';
import { testUser, testAuth } from '../../../test/helpers/mock_auth';
import { TaggedWithTokenResponse } from '../../model/id_token';
import { SAMLAuthCredential } from '../credentials/saml';
import { AuthErrorCode } from '../errors';
import { UserCredentialImpl } from '../user/user_credential_impl';
import { _createError } from '../util/assert';
Expand All @@ -45,6 +46,17 @@ describe('core/providers/saml', () => {
expect(cred.signInMethod).to.eq('saml.provider');
});

it('generates SAML provider', () => {
const provider = new SAMLAuthProvider('saml.provider');
expect(provider.providerId).to.eq('saml.provider');
});

it('returns error for invalid SAML provdier', () => {
expect(() => {
new SAMLAuthProvider('provider');
}).throw(/auth\/argument-error/);
});

it('credentialFromResult returns null if provider ID not specified', async () => {
const auth = await testAuth();
const userCred = new UserCredentialImpl({
Expand Down Expand Up @@ -73,4 +85,78 @@ describe('core/providers/saml', () => {
expect(cred.providerId).to.eq('saml.provider');
expect(cred.signInMethod).to.eq('saml.provider');
});

it('credentialFromJSON returns SAML credential from valid object', () => {
const json = {
providerId: 'saml.provider',

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is it possible providerId and signInMethod are different? maybe we should use a different string for assertion.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey Liubin,

We have a validation in the SamlAuthCredential.fromJson method to validate that providerId === signInMethod - https://github.com/firebase/firebase-js-sdk/blob/main/packages/auth/src/core/credentials/saml.ts#L95

Hence using the same string.

signInMethod: 'saml.provider',
pendingToken: 'fake-pending-token'
};

const credential = SAMLAuthProvider.credentialFromJSON(json);
expect(credential.providerId).to.eq('saml.provider');
expect(credential.signInMethod).to.eq('saml.provider');
expect((credential as any).pendingToken).to.eq('fake-pending-token');
});

it('returns null when _tokenResponse is missing (undefined)', () => {
const error = _createError(AuthErrorCode.NEED_CONFIRMATION, {
appName: 'test-app'
});

error.customData = {}; // _tokenResponse missing
const credential = SAMLAuthProvider.credentialFromError(error);
expect(credential).to.be.null;
});

it('returns null when _tokenResponse is missing oauthAccessToken key', () => {
const error = _createError(AuthErrorCode.NEED_CONFIRMATION, {
appName: 'foo'
});
error.customData = {
_tokenResponse: {
// intentionally missing oauthAccessToken
idToken: 'some-id-token',
oauthAccessToken: null
}
};

const cred = SAMLAuthProvider.credentialFromError(error);
expect(cred).to.be.null;
});

it('returns null if _create throws internally', () => {
const originalCreate = (SAMLAuthCredential as any)._create;

(SAMLAuthCredential as any)._create = () => {
throw new Error('Simulated error');
};

const error = _createError(AuthErrorCode.NEED_CONFIRMATION, {
appName: 'test-app'
});

error.customData = {
_tokenResponse: {
pendingToken: 'valid-token',
providerId: 'saml.my-provider'
}
};

const cred = SAMLAuthProvider.credentialFromError(error);
expect(cred).to.be.null;

(SAMLAuthCredential as any)._create = originalCreate;
});

it('returns null when customData is undefined (falls back to empty object)', () => {
const error = _createError(AuthErrorCode.NEED_CONFIRMATION, {
appName: 'test-app'
});

delete (error as any).customData;

const credential = SAMLAuthProvider.credentialFromError(error);
expect(credential).to.be.null;
});
});
Loading