Skip to content

Commit 4f15a91

Browse files
committed
Add tests
1 parent 4aead0a commit 4f15a91

File tree

2 files changed

+80
-3
lines changed

2 files changed

+80
-3
lines changed

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

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,15 @@ import sinon from "sinon";
33
import { Authority } from "../../src/authority/Authority";
44
import { INetworkModule, NetworkRequestOptions } from "../../src/network/INetworkModule";
55
import { Constants } from "../../src/utils/Constants";
6-
import { AuthorityType } from "../../src/authority/AuthorityType";
76
import {
8-
DEFAULT_TENANT_DISCOVERY_RESPONSE,
97
TEST_URIS,
108
RANDOM_TEST_GUID,
119
DEFAULT_OPENID_CONFIG_RESPONSE
1210
} from "../utils/StringConstants";
13-
import { ClientConfigurationErrorMessage } from "../../src/error/ClientConfigurationError";
11+
import { ClientConfigurationErrorMessage, ClientConfigurationError } from "../../src/error/ClientConfigurationError";
1412
import { ClientAuthErrorMessage } from "../../src";
1513
import { ClientTestUtils } from "../client/ClientTestUtils";
14+
import { TrustedAuthority } from "../../src/authority/TrustedAuthority";
1615

1716
describe("Authority.ts Class Unit Tests", () => {
1817
afterEach(() => {
@@ -123,6 +122,7 @@ describe("Authority.ts Class Unit Tests", () => {
123122
expect(() => authority.authorizationEndpoint).to.throw(ClientAuthErrorMessage.endpointResolutionError.desc);
124123
expect(() => authority.tokenEndpoint).to.throw(ClientAuthErrorMessage.endpointResolutionError.desc);
125124
expect(() => authority.endSessionEndpoint).to.throw(ClientAuthErrorMessage.endpointResolutionError.desc);
125+
expect(() => authority.deviceCodeEndpoint).to.throw(ClientAuthErrorMessage.endpointResolutionError.desc);
126126
expect(() => authority.selfSignedJwtAudience).to.throw(ClientAuthErrorMessage.endpointResolutionError.desc);
127127
});
128128
});
@@ -169,5 +169,34 @@ describe("Authority.ts Class Unit Tests", () => {
169169
expect(authority.endSessionEndpoint).to.be.eq(DEFAULT_OPENID_CONFIG_RESPONSE.body.end_session_endpoint.replace("{tenant}", "common"));
170170
expect(authority.selfSignedJwtAudience).to.be.eq(DEFAULT_OPENID_CONFIG_RESPONSE.body.issuer.replace("{tenant}", "common"));
171171
});
172+
173+
it("Attempts to set instance metadata from network if not set", async () => {
174+
sinon.restore();
175+
let setFromNetwork = false;
176+
sinon.stub(TrustedAuthority, "getTrustedHostList").returns([]);
177+
sinon.stub(TrustedAuthority, "IsInTrustedHostList").returns(true);
178+
sinon.stub(TrustedAuthority, "setTrustedAuthoritiesFromNetwork").callsFake(async () => {
179+
setFromNetwork = true;
180+
});
181+
182+
await authority.resolveEndpointsAsync();
183+
expect(setFromNetwork).to.be.true;
184+
});
185+
186+
it("throws untrustedAuthority error if host is not in TrustedHostList", async () => {
187+
sinon.restore();
188+
sinon.stub(TrustedAuthority, "getTrustedHostList").returns(["testAuthority"]);
189+
sinon.stub(TrustedAuthority, "IsInTrustedHostList").returns(false);
190+
191+
let err = null;
192+
try {
193+
await authority.resolveEndpointsAsync();
194+
} catch (e) {
195+
expect(e).to.be.instanceOf(ClientConfigurationError);
196+
err = e;
197+
}
198+
expect(err.errorMessage).to.equal(ClientConfigurationErrorMessage.untrustedAuthority.desc);
199+
expect(err.errorCode).to.equal(ClientConfigurationErrorMessage.untrustedAuthority.code);
200+
});
172201
});
173202
});
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import { expect } from "chai";
2+
import sinon from "sinon";
3+
import { TrustedAuthority } from "../../src/authority/TrustedAuthority";
4+
import { NetworkRequestOptions } from "../../src/network/INetworkModule";
5+
import { NetworkResponse } from "../../src/network/NetworkManager";
6+
import { OpenIdConfigResponse } from "../../src/authority/OpenIdConfigResponse";
7+
8+
describe("TrustedAuthority.ts Class", function () {
9+
const knownAuthorities = ["fabrikamb2c.b2clogin.com"];
10+
const instanceMetadata = [{
11+
preferred_cache: "fabrikamb2c.b2clogin.com",
12+
preferred_network: "fabrikamb2c.b2clogin.com",
13+
aliases: ["fabrikamb2c.b2clogin.com"]
14+
}];
15+
16+
afterEach(function() {
17+
sinon.restore();
18+
});
19+
20+
describe("setTrustedAuthoritiesFromConfig", () => {
21+
it("Sets TrustedHostList with Known Authorities", () => {
22+
sinon.stub(TrustedAuthority, "getTrustedHostList").returns([]);
23+
TrustedAuthority.setTrustedAuthoritiesFromConfig(knownAuthorities, []);
24+
25+
knownAuthorities.forEach(function(authority) {
26+
expect(TrustedAuthority.IsInTrustedHostList(authority)).to.be.true;
27+
});
28+
});
29+
30+
it("Do not add additional authorities to trusted host list if it has already been populated", () => {
31+
sinon.stub(TrustedAuthority, "getTrustedHostList").returns(["login.microsoftonline.com"]);
32+
TrustedAuthority.setTrustedAuthoritiesFromConfig(["contoso.b2clogin.com"], []);
33+
34+
expect(TrustedAuthority.IsInTrustedHostList("contoso.b2clogin.com")).to.be.false;
35+
});
36+
37+
it("Throws error if both knownAuthorities and instanceMetadata are passed", (done) => {
38+
sinon.stub(TrustedAuthority, "getTrustedHostList").returns([]);
39+
40+
try {
41+
TrustedAuthority.setTrustedAuthoritiesFromConfig(knownAuthorities, instanceMetadata);
42+
} catch (e) {
43+
expect(e).to.eq("Cannot pass both knownAuthorities and instanceMetadata");
44+
done();
45+
}
46+
});
47+
});
48+
});

0 commit comments

Comments
 (0)