Skip to content

Commit b947eed

Browse files
committed
Update tests for skipAuthorityMetadataCache
1 parent ba6f56e commit b947eed

File tree

1 file changed

+22
-185
lines changed

1 file changed

+22
-185
lines changed

lib/msal-common/test/authority/Authority.spec.ts

+22-185
Original file line numberDiff line numberDiff line change
@@ -1143,7 +1143,6 @@ describe("Authority.ts Class Unit Tests", () => {
11431143
knownAuthorities: [Constants.DEFAULT_AUTHORITY_HOST],
11441144
cloudDiscoveryMetadata: "",
11451145
authorityMetadata: "",
1146-
skipAuthorityMetadataCache: false,
11471146
};
11481147

11491148
networkInterface.sendGetRequestAsync = (
@@ -1208,7 +1207,6 @@ describe("Authority.ts Class Unit Tests", () => {
12081207
knownAuthorities: [],
12091208
cloudDiscoveryMetadata: "",
12101209
authorityMetadata: "",
1211-
skipAuthorityMetadataCache: false,
12121210
};
12131211

12141212
const tenantId = "fake-tenant-id";
@@ -1267,7 +1265,6 @@ describe("Authority.ts Class Unit Tests", () => {
12671265
knownAuthorities: [Constants.DEFAULT_AUTHORITY_HOST],
12681266
cloudDiscoveryMetadata: "",
12691267
authorityMetadata: "",
1270-
skipAuthorityMetadataCache: false,
12711268
azureRegionConfiguration: {
12721269
azureRegion: "westus2",
12731270
environmentRegion: undefined,
@@ -1423,91 +1420,6 @@ describe("Authority.ts Class Unit Tests", () => {
14231420
).not.toHaveBeenCalled();
14241421
});
14251422

1426-
it("Gets endpoints from cache skipping hardcoded metadata if skipAuthorityMetadataCache is set to true", async () => {
1427-
const key = `authority-metadata-${TEST_CONFIG.MSAL_CLIENT_ID}-${Constants.DEFAULT_AUTHORITY_HOST}`;
1428-
mockStorage.setAuthorityMetadata(
1429-
key,
1430-
authorityMetadataCacheValue
1431-
);
1432-
1433-
authority = new Authority(
1434-
Constants.DEFAULT_AUTHORITY,
1435-
networkInterface,
1436-
mockStorage,
1437-
{ ...authorityOptions, skipAuthorityMetadataCache: true },
1438-
logger,
1439-
TEST_CONFIG.CORRELATION_ID
1440-
);
1441-
1442-
// Force hardcoded metadata to return null
1443-
getEndpointMetadataFromHarcodedValuesSpy.mockReturnValue(null);
1444-
await authority.resolveEndpointsAsync();
1445-
1446-
expect(authority.discoveryComplete()).toBe(true);
1447-
expect(authority.authorizationEndpoint).toBe(
1448-
DEFAULT_OPENID_CONFIG_RESPONSE.body.authorization_endpoint.replace(
1449-
"{tenant}",
1450-
"common"
1451-
)
1452-
);
1453-
expect(authority.tokenEndpoint).toBe(
1454-
DEFAULT_OPENID_CONFIG_RESPONSE.body.token_endpoint.replace(
1455-
"{tenant}",
1456-
"common"
1457-
)
1458-
);
1459-
expect(authority.deviceCodeEndpoint).toBe(
1460-
authority.tokenEndpoint.replace("/token", "/devicecode")
1461-
);
1462-
expect(authority.endSessionEndpoint).toBe(
1463-
DEFAULT_OPENID_CONFIG_RESPONSE.body.end_session_endpoint.replace(
1464-
"{tenant}",
1465-
"common"
1466-
)
1467-
);
1468-
expect(authority.selfSignedJwtAudience).toBe(
1469-
DEFAULT_OPENID_CONFIG_RESPONSE.body.issuer.replace(
1470-
"{tenant}",
1471-
"common"
1472-
)
1473-
);
1474-
1475-
// Test that the metadata is cached
1476-
const cachedAuthorityMetadata =
1477-
mockStorage.getAuthorityMetadata(key);
1478-
if (!cachedAuthorityMetadata) {
1479-
throw Error("Cached AuthorityMetadata should not be null!");
1480-
} else {
1481-
expect(cachedAuthorityMetadata.authorization_endpoint).toBe(
1482-
DEFAULT_OPENID_CONFIG_RESPONSE.body
1483-
.authorization_endpoint
1484-
);
1485-
expect(cachedAuthorityMetadata.token_endpoint).toBe(
1486-
DEFAULT_OPENID_CONFIG_RESPONSE.body.token_endpoint
1487-
);
1488-
expect(cachedAuthorityMetadata.end_session_endpoint).toBe(
1489-
DEFAULT_OPENID_CONFIG_RESPONSE.body.end_session_endpoint
1490-
);
1491-
expect(cachedAuthorityMetadata.issuer).toBe(
1492-
DEFAULT_OPENID_CONFIG_RESPONSE.body.issuer
1493-
);
1494-
expect(cachedAuthorityMetadata.jwks_uri).toBe(
1495-
DEFAULT_OPENID_CONFIG_RESPONSE.body.jwks_uri
1496-
);
1497-
expect(cachedAuthorityMetadata.endpointsFromNetwork).toBe(
1498-
true
1499-
);
1500-
}
1501-
1502-
expect(getEndpointMetadataFromConfigSpy).toHaveBeenCalled();
1503-
expect(
1504-
getEndpointMetadataFromHarcodedValuesSpy
1505-
).not.toHaveBeenCalled();
1506-
expect(
1507-
getEndpointMetadataFromNetworkSpy
1508-
).not.toHaveBeenCalled();
1509-
});
1510-
15111423
it("Gets endpoints from network if cached metadata is expired and metadata was not included in configuration or hardcoded values", async () => {
15121424
const key = `authority-metadata-${TEST_CONFIG.MSAL_CLIENT_ID}-${Constants.DEFAULT_AUTHORITY_HOST}`;
15131425
mockStorage.setAuthorityMetadata(key, {
@@ -1692,10 +1604,13 @@ describe("Authority.ts Class Unit Tests", () => {
16921604
Constants.DEFAULT_AUTHORITY,
16931605
networkInterface,
16941606
mockStorage,
1695-
{ ...authorityOptions, skipAuthorityMetadataCache: true },
1607+
authorityOptions,
16961608
logger,
16971609
TEST_CONFIG.CORRELATION_ID
16981610
);
1611+
// Force hardcoded metadata to return null
1612+
getEndpointMetadataFromHarcodedValuesSpy.mockReturnValue(null);
1613+
16991614
authority.resolveEndpointsAsync().catch((e) => {
17001615
expect(e).toBeInstanceOf(ClientAuthError);
17011616
expect(e.errorCode).toBe(
@@ -2026,100 +1941,6 @@ describe("Authority.ts Class Unit Tests", () => {
20261941
).not.toHaveBeenCalled();
20271942
});
20281943

2029-
it("Sets instance metadata from cache skipping hardcoded values if skipAuthorityMetadataCache is set to true", async () => {
2030-
const authorityOptions: AuthorityOptions = {
2031-
protocolMode: ProtocolMode.AAD,
2032-
knownAuthorities: [],
2033-
cloudDiscoveryMetadata: "",
2034-
authorityMetadata: "",
2035-
};
2036-
2037-
const tenantDiscoveryResponseBody =
2038-
DEFAULT_TENANT_DISCOVERY_RESPONSE.body;
2039-
2040-
const expectedCloudDiscoveryMetadata =
2041-
tenantDiscoveryResponseBody.metadata[0];
2042-
2043-
const configAliases =
2044-
expectedCloudDiscoveryMetadata.aliases;
2045-
2046-
const key = `authority-metadata-${TEST_CONFIG.MSAL_CLIENT_ID}-sts.windows.net`;
2047-
mockStorage.setAuthorityMetadata(
2048-
key,
2049-
authorityMetadataCacheValue
2050-
);
2051-
jest.spyOn(
2052-
Authority.prototype,
2053-
<any>"updateEndpointMetadata"
2054-
).mockResolvedValue("cache");
2055-
authority = new Authority(
2056-
Constants.DEFAULT_AUTHORITY,
2057-
networkInterface,
2058-
mockStorage,
2059-
{
2060-
...authorityOptions,
2061-
skipAuthorityMetadataCache: true,
2062-
},
2063-
logger,
2064-
TEST_CONFIG.CORRELATION_ID
2065-
);
2066-
2067-
getCloudDiscoveryMetadataFromHarcodedValuesSpy.mockReturnValue(
2068-
null
2069-
);
2070-
2071-
await authority.resolveEndpointsAsync();
2072-
expect(authority.isAlias(configAliases[0])).toBe(true);
2073-
expect(authority.isAlias(configAliases[1])).toBe(true);
2074-
expect(authority.isAlias(configAliases[2])).toBe(true);
2075-
expect(authority.getPreferredCache()).toBe(
2076-
expectedCloudDiscoveryMetadata.preferred_cache
2077-
);
2078-
expect(
2079-
authority.canonicalAuthority.includes(
2080-
expectedCloudDiscoveryMetadata.preferred_network
2081-
)
2082-
).toBe(true);
2083-
2084-
// Test that the metadata is cached
2085-
const cachedAuthorityMetadata =
2086-
mockStorage.getAuthorityMetadata(key);
2087-
if (!cachedAuthorityMetadata) {
2088-
throw Error(
2089-
"Cached AuthorityMetadata should not be null!"
2090-
);
2091-
} else {
2092-
expect(cachedAuthorityMetadata.aliases).toContain(
2093-
configAliases[0]
2094-
);
2095-
expect(cachedAuthorityMetadata.aliases).toContain(
2096-
configAliases[1]
2097-
);
2098-
expect(cachedAuthorityMetadata.aliases).toContain(
2099-
configAliases[2]
2100-
);
2101-
expect(cachedAuthorityMetadata.preferred_cache).toBe(
2102-
expectedCloudDiscoveryMetadata.preferred_cache
2103-
);
2104-
expect(cachedAuthorityMetadata.preferred_network).toBe(
2105-
expectedCloudDiscoveryMetadata.preferred_network
2106-
);
2107-
expect(cachedAuthorityMetadata.aliasesFromNetwork).toBe(
2108-
true
2109-
);
2110-
}
2111-
2112-
expect(
2113-
getCloudDiscoveryMetadataFromConfigSpy
2114-
).toHaveBeenCalled();
2115-
expect(
2116-
getCloudDiscoveryMetadataFromHarcodedValuesSpy
2117-
).not.toHaveBeenCalled();
2118-
expect(
2119-
getCloudDiscoveryMetadataFromNetworkSpy
2120-
).not.toHaveBeenCalled();
2121-
});
2122-
21231944
it("Sets instance metadata from cache when not present in configuration or hardcoded values", async () => {
21241945
const authorityOptions: AuthorityOptions = {
21251946
protocolMode: ProtocolMode.AAD,
@@ -2505,12 +2326,20 @@ describe("Authority.ts Class Unit Tests", () => {
25052326
});
25062327

25072328
it("throws untrustedAuthority error if host is not part of knownAuthorities, cloudDiscoveryMetadata and instance discovery network call fails", (done) => {
2329+
const getCloudDiscoveryMetadataFromHarcodedValuesSpy: jest.SpyInstance = jest.spyOn(
2330+
authorityMetadata,
2331+
"getCloudDiscoveryMetadataFromHardcodedValues"
2332+
);
2333+
2334+
getCloudDiscoveryMetadataFromHarcodedValuesSpy.mockReturnValue(
2335+
null
2336+
);
2337+
25082338
const authorityOptions: AuthorityOptions = {
25092339
protocolMode: ProtocolMode.AAD,
25102340
knownAuthorities: [],
25112341
cloudDiscoveryMetadata: "",
25122342
authorityMetadata: "",
2513-
skipAuthorityMetadataCache: true,
25142343
};
25152344
networkInterface.sendGetRequestAsync = (
25162345
url: string,
@@ -2542,12 +2371,20 @@ describe("Authority.ts Class Unit Tests", () => {
25422371
});
25432372

25442373
it("throws untrustedAuthority error if host is not part of knownAuthorities, cloudDiscoveryMetadata and instance discovery network call doesn't return metadata, and the error returned from AAD is 'invalid_instance'", (done) => {
2374+
const getCloudDiscoveryMetadataFromHarcodedValuesSpy: jest.SpyInstance = jest.spyOn(
2375+
authorityMetadata,
2376+
"getCloudDiscoveryMetadataFromHardcodedValues"
2377+
);
2378+
2379+
getCloudDiscoveryMetadataFromHarcodedValuesSpy.mockReturnValue(
2380+
null
2381+
);
2382+
25452383
const authorityOptions: AuthorityOptions = {
25462384
protocolMode: ProtocolMode.AAD,
25472385
knownAuthorities: [],
25482386
cloudDiscoveryMetadata: "",
25492387
authorityMetadata: "",
2550-
skipAuthorityMetadataCache: true,
25512388
};
25522389
networkInterface.sendGetRequestAsync = (
25532390
url: string,

0 commit comments

Comments
 (0)