Skip to content

Increase unit test coverage for saml.ts to 100% #9131

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 2 commits into
base: main
Choose a base branch
from
Open
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',
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