Skip to content

Commit

Permalink
Fixed broken unit tests and moved some around
Browse files Browse the repository at this point in the history
  • Loading branch information
Robbie-Microsoft committed Apr 29, 2024
1 parent a1d53eb commit 053c051
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 115 deletions.
65 changes: 0 additions & 65 deletions lib/msal-common/test/client/SilentFlowClient.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -366,71 +366,6 @@ describe("SilentFlowClient unit tests", () => {
);
});

it("acquireCachedToken does not throw when given valid claims with claimsBasedCachingEnabled", async () => {
const testScopes = [
Constants.OPENID_SCOPE,
Constants.PROFILE_SCOPE,
...TEST_CONFIG.DEFAULT_GRAPH_SCOPE,
];
testAccessTokenEntity.target = testScopes.join(" ");
sinon
.stub(
Authority.prototype,
<any>"getEndpointMetadataFromNetwork"
)
.resolves(DEFAULT_OPENID_CONFIG_RESPONSE.body);
sinon
.stub(CacheManager.prototype, "readAccountFromCache")
.returns(testAccountEntity);
sinon
.stub(CacheManager.prototype, "getIdToken")
.returns(testIdToken);
sinon
.stub(CacheManager.prototype, "getAccessToken")
.returns(testAccessTokenEntity);
sinon
.stub(CacheManager.prototype, "getRefreshToken")
.returns(testRefreshTokenEntity);
const config =
await ClientTestUtils.createTestClientConfiguration();
const client = new SilentFlowClient(
{
...config,
cacheOptions: {
...config.cacheOptions,
claimsBasedCachingEnabled: true,
},
},
stubPerformanceClient
);
sinon.stub(TimeUtils, <any>"isTokenExpired").returns(false);

const silentFlowRequest: CommonSilentFlowRequest = {
scopes: TEST_CONFIG.DEFAULT_GRAPH_SCOPE,
account: testAccount,
authority: TEST_CONFIG.validAuthority,
correlationId: TEST_CONFIG.CORRELATION_ID,
forceRefresh: false,
claims: `{ "access_token": { "xms_cc":{"values":["cp1"] } }}`,
};

const response = await client.acquireCachedToken(silentFlowRequest);
const authResult: AuthenticationResult = response[0];
expect(authResult.authority).toEqual(
`${TEST_URIS.DEFAULT_INSTANCE}${TEST_CONFIG.TENANT}/`
);
expect(authResult.uniqueId).toEqual(ID_TOKEN_CLAIMS.oid);
expect(authResult.tenantId).toEqual(ID_TOKEN_CLAIMS.tid);
expect(authResult.scopes).toEqual(testScopes);
expect(authResult.account).toEqual(testAccount);
expect(authResult.idToken).toEqual(testIdToken.secret);
expect(authResult.idTokenClaims).toEqual(ID_TOKEN_CLAIMS);
expect(authResult.accessToken).toEqual(
testAccessTokenEntity.secret
);
expect(authResult.state).toBe("");
});

it("acquireCachedToken returns correct token when max age is provided and has not transpired yet", async () => {
const testScopes = [
Constants.OPENID_SCOPE,
Expand Down
54 changes: 35 additions & 19 deletions lib/msal-common/test/config/ClientConfiguration.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,12 @@ import {
import { MockStorageClass, mockCrypto } from "../client/ClientTestUtils";
import { MockCache } from "../cache/entities/cacheConstants";
import { Constants } from "../../src/utils/Constants";
import { ClientAuthErrorCodes, createClientAuthError } from "../../src";
import {
ClientAuthErrorCodes,
ClientConfigurationErrorCodes,
createClientAuthError,
createClientConfigurationError,
} from "../../src";

describe("ClientConfiguration.ts Class Unit Tests", () => {
it("buildConfiguration assigns default functions", async () => {
Expand All @@ -31,21 +36,21 @@ describe("ClientConfiguration.ts Class Unit Tests", () => {
expect(emptyConfig.cryptoInterface.base64Decode).not.toBeNull();
expect(() =>
emptyConfig.cryptoInterface.base64Decode("test input")
).toThrowError(
).toThrow(
createClientAuthError(ClientAuthErrorCodes.methodNotImplemented)
);
expect(() =>
emptyConfig.cryptoInterface.base64Decode("test input")
).toThrowError(AuthError);
).toThrow(AuthError);
expect(emptyConfig.cryptoInterface.base64Encode).not.toBeNull();
expect(() =>
emptyConfig.cryptoInterface.base64Encode("test input")
).toThrowError(
).toThrow(
createClientAuthError(ClientAuthErrorCodes.methodNotImplemented)
);
expect(() =>
emptyConfig.cryptoInterface.base64Encode("test input")
).toThrowError(AuthError);
).toThrow(AuthError);
// Storage interface checks
expect(emptyConfig.storageInterface).not.toBeNull();
expect(emptyConfig.storageInterface.clear).not.toBeNull();
Expand All @@ -57,37 +62,35 @@ describe("ClientConfiguration.ts Class Unit Tests", () => {
expect(emptyConfig.storageInterface.getAccount).not.toBeNull();
expect(() =>
emptyConfig.storageInterface.getAccount("testKey")
).toThrowError(
).toThrow(
createClientAuthError(ClientAuthErrorCodes.methodNotImplemented)
);
expect(() =>
emptyConfig.storageInterface.getAccount("testKey")
).toThrowError(AuthError);
).toThrow(AuthError);
expect(emptyConfig.storageInterface.getKeys).not.toBeNull();
expect(() => emptyConfig.storageInterface.getKeys()).toThrowError(
expect(() => emptyConfig.storageInterface.getKeys()).toThrow(
createClientAuthError(ClientAuthErrorCodes.methodNotImplemented)
);
expect(() => emptyConfig.storageInterface.getKeys()).toThrowError(
AuthError
);
expect(() => emptyConfig.storageInterface.getKeys()).toThrow(AuthError);
expect(emptyConfig.storageInterface.removeItem).not.toBeNull();
expect(() =>
emptyConfig.storageInterface.removeItem("testKey")
).toThrowError(
).toThrow(
createClientAuthError(ClientAuthErrorCodes.methodNotImplemented)
);
expect(() =>
emptyConfig.storageInterface.removeItem("testKey")
).toThrowError(AuthError);
).toThrow(AuthError);
expect(emptyConfig.storageInterface.setAccount).not.toBeNull();
expect(() =>
emptyConfig.storageInterface.setAccount(MockCache.acc)
).toThrowError(
).toThrow(
createClientAuthError(ClientAuthErrorCodes.methodNotImplemented)
);
expect(() =>
emptyConfig.storageInterface.setAccount(MockCache.acc)
).toThrowError(AuthError);
).toThrow(AuthError);
// Network interface checks
expect(emptyConfig.networkInterface).not.toBeNull();
expect(emptyConfig.networkInterface.sendGetRequestAsync).not.toBeNull();
Expand Down Expand Up @@ -191,9 +194,6 @@ describe("ClientConfiguration.ts Class Unit Tests", () => {
): void => {},
piiLoggingEnabled: true,
},
cacheOptions: {
claimsBasedCachingEnabled: true,
},
libraryInfo: {
sku: TEST_CONFIG.TEST_SKU,
version: TEST_CONFIG.TEST_VERSION,
Expand Down Expand Up @@ -265,7 +265,7 @@ describe("ClientConfiguration.ts Class Unit Tests", () => {
expect(newConfig.loggerOptions.piiLoggingEnabled).toBe(true);
// Cache options tests
expect(newConfig.cacheOptions).not.toBeNull();
expect(newConfig.cacheOptions.claimsBasedCachingEnabled).toBe(true);
expect(newConfig.cacheOptions.claimsBasedCachingEnabled).toBe(false);
// Client info tests
expect(newConfig.libraryInfo.sku).toBe(TEST_CONFIG.TEST_SKU);
expect(newConfig.libraryInfo.version).toBe(TEST_CONFIG.TEST_VERSION);
Expand All @@ -279,4 +279,20 @@ describe("ClientConfiguration.ts Class Unit Tests", () => {
TEST_CONFIG.TEST_APP_VER
);
});

test("throws an error when claimsBasedCaching is enabled", async () => {
expect(() => {
buildClientConfiguration({
//@ts-ignore
authOptions: {
clientId: TEST_CONFIG.MSAL_CLIENT_ID,
},
cacheOptions: { claimsBasedCachingEnabled: true },
});
}).toThrow(
createClientConfigurationError(
ClientConfigurationErrorCodes.claimsBasedCachingEnabled
)
);
});
});
16 changes: 0 additions & 16 deletions lib/msal-node/test/client/ConfidentialClientApplication.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ import {
CommonClientCredentialRequest,
createClientAuthError,
ClientAuthErrorCodes,
createClientConfigurationError,
ClientConfigurationErrorCodes,
} from "@azure/msal-common";
import { TEST_CONSTANTS } from "../utils/TestConstants";
import {
Expand Down Expand Up @@ -387,18 +385,4 @@ describe("ConfidentialClientApplication", () => {
const authApp = new ConfidentialClientApplication(appConfig);
await authApp.acquireTokenByClientCredential(request);
});

it("An error is thrown when claims based caching is enabled", async () => {
expect(() => {
// will use msal-node's buildAppConfiguration
new ConfidentialClientApplication({
...appConfig,
cache: { claimsBasedCachingEnabled: true },
});
}).toThrow(
createClientConfigurationError(
ClientConfigurationErrorCodes.claimsBasedCachingEnabled
)
);
});
});
15 changes: 0 additions & 15 deletions lib/msal-node/test/client/PublicClientApplication.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ import {
CacheHelpers,
AuthorityFactory,
ProtocolMode,
createClientConfigurationError,
ClientConfigurationErrorCodes,
} from "@azure/msal-common";
import {
Configuration,
Expand Down Expand Up @@ -870,19 +868,6 @@ describe("PublicClientApplication", () => {
});
});

test("throws an error when claimsBasedCaching is enabled", async () => {
expect(() => {
new PublicClientApplication({
...appConfig,
cache: { claimsBasedCachingEnabled: true },
});
}).toThrow(
createClientConfigurationError(
ClientConfigurationErrorCodes.claimsBasedCachingEnabled
)
);
});

test("initializeBaseRequest doesn't pass a claims hash to acquireToken when claimsBasedHashing is disabled by default", async () => {
const account: AccountInfo = {
homeAccountId: "",
Expand Down
17 changes: 17 additions & 0 deletions lib/msal-node/test/config/ClientConfiguration.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import {
LogLevel,
NetworkRequestOptions,
AzureCloudInstance,
createClientConfigurationError,
ClientConfigurationErrorCodes,
} from "@azure/msal-common";
import {
ClientCredentialRequest,
Expand Down Expand Up @@ -264,4 +266,19 @@ describe("ClientConfiguration tests", () => {
})
);
});

test("throws an error when claimsBasedCaching is enabled", async () => {
expect(() => {
buildAppConfiguration({
auth: {
clientId: TEST_CONSTANTS.CLIENT_ID,
},
cache: { claimsBasedCachingEnabled: true },
});
}).toThrow(
createClientConfigurationError(
ClientConfigurationErrorCodes.claimsBasedCachingEnabled
)
);
});
});

0 comments on commit 053c051

Please sign in to comment.