Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
ba49410
Initial Commit
tnorling May 1, 2020
e5764bc
Merge branch 'dev' into cloud-discovery
tnorling May 13, 2020
d2bf765
Merge branch 'dev' into cloud-discovery
tnorling May 19, 2020
968c1b3
Merge branch 'authority-metadata-openid-perf' of https://github.com/A…
tnorling May 19, 2020
a9b9853
Move setup functions to Factory
tnorling May 19, 2020
e90cd4e
Update unit tests
tnorling May 20, 2020
c75b961
Merge branch 'authority-metadata-openid-perf' of https://github.com/A…
tnorling May 20, 2020
453bad8
Resolve lint errors
tnorling May 20, 2020
85d1736
Move validation of Authority to Factory
tnorling May 21, 2020
e0fc794
Add TrustedHostList getter
tnorling May 21, 2020
a190f8a
Update Error message
tnorling May 21, 2020
72e213b
Separate async network call
tnorling May 21, 2020
acebe0f
Fix Trusted Check
tnorling May 21, 2020
19ab0dd
Unit Tests
tnorling May 21, 2020
dc93c20
Merge branch 'dev' of https://github.com/AzureAD/microsoft-authentica…
tnorling May 21, 2020
b179c4d
Move TrustedAuthority to its own class
tnorling May 22, 2020
4d11b85
Clean up test
tnorling May 22, 2020
ff06f8b
Fix context
tnorling May 22, 2020
79e8fce
Merge branch 'dev' into cloud-discovery
tnorling May 26, 2020
80c4b6d
Add tests
tnorling May 26, 2020
22b125e
Merge branch 'dev' into cloud-discovery
tnorling Jun 1, 2020
acb0a51
Initiate cloud discovery in Constructor
tnorling Jun 2, 2020
69c53db
Fix lint errors
tnorling Jun 2, 2020
bc50c5a
Fix tests
tnorling Jun 2, 2020
01212c6
Revert "Fix tests"
tnorling Jun 3, 2020
ea9fab0
Revert "Fix lint errors"
tnorling Jun 3, 2020
e645c9e
Revert "Initiate cloud discovery in Constructor"
tnorling Jun 3, 2020
6e098e3
Merge branch 'dev' into cloud-discovery
tnorling Jun 3, 2020
c0fa200
Merge branch 'dev' of https://github.com/AzureAD/microsoft-authentica…
tnorling Jun 15, 2020
2e16854
Addressing feedback
tnorling Jun 15, 2020
84dd6a2
Merge branch 'dev' into cloud-discovery
tnorling Jun 15, 2020
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
Prev Previous commit
Next Next commit
Add tests
  • Loading branch information
tnorling committed May 26, 2020
commit 80c4b6d2c6963d5ad883732227f9e3f1b6a70959
204 changes: 140 additions & 64 deletions lib/msal-core/test/authority/Authority.spec.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import { expect } from "chai";
import { Authority } from "../../src/authority/Authority";
import { ClientConfigurationErrorMessage } from "../../src/error/ClientConfigurationError"
import { ClientConfigurationErrorMessage, ClientConfigurationError } from "../../src/error/ClientConfigurationError"
import { TEST_CONFIG, TENANT_DISCOVERY_RESPONSE } from "../TestConstants";
import TelemetryManager from "../../src/telemetry/TelemetryManager";
import { TelemetryConfig } from "../../src/telemetry/TelemetryTypes";
import { Logger } from "../../src";
import { TrustedAuthority } from "../../src/authority/TrustedAuthority";
import sinon from "sinon";

const stubbedTelemetryConfig: TelemetryConfig = {
clientId: TEST_CONFIG.MSAL_CLIENT_ID,
Expand All @@ -25,82 +27,156 @@ describe("Authority.ts Class", function () {

afterEach(function () {
authority = null;
sinon.restore();
});

it("tests initialization of Authority", function() {
expect(authority).to.be.instanceOf(Authority);
describe("Constructor", () => {
it("tests initialization of Authority", function() {
expect(authority).to.be.instanceOf(Authority);
});

it("throws invalidAuthorityType on init if authority is not url", function () {
try {
authority = new Authority("", true);
}
catch(e) {
expect(e).to.be.equal(ClientConfigurationErrorMessage.invalidAuthorityType)
}
});

it("throws authorityUriInsecure on init if not https", function () {
try {
authority = new Authority("http://login.microsoftonline.com/common", true);
}
catch(e) {
expect(e).to.be.equal(ClientConfigurationErrorMessage.authorityUriInsecure)
}
});

it("throws authorityUriInvalidPath on init if there is no path", function () {
try {
authority = new Authority("https://login.microsoftonline.com", true);
}
catch(e) {
expect(e).to.be.equal(ClientConfigurationErrorMessage.authorityUriInvalidPath)
}
});
});

it("throws error if ResolveEndpointsAsync hasn't been called yet", function () {
try {
const authEndpoint = authority.AuthorizationEndpoint
}
catch(e) {
expect(e).to.be.equal("Please call ResolveEndpointsAsync first");
}
describe("get AuthoritzationEndpoint", () => {
it("throws error if ResolveEndpointsAsync hasn't been called yet", function () {
try {
const authEndpoint = authority.AuthorizationEndpoint
}
catch(e) {
expect(e).to.be.equal("Please call ResolveEndpointsAsync first");
}
});

it("tests AuthorizationEndpoint", async function () {
const response = await authority.resolveEndpointsAsync(stubbedTelemetryManager, TEST_CONFIG.CorrelationId);

expect(authority.AuthorizationEndpoint).to.equal("https://login.microsoftonline.com/common/oauth2/v2.0/authorize")
});
});

it("tests EndSessionEndpoint", async function () {
const response = await authority.resolveEndpointsAsync(stubbedTelemetryManager, TEST_CONFIG.CorrelationId);

expect(authority.EndSessionEndpoint).to.equal("https://login.microsoftonline.com/common/oauth2/v2.0/logout")
});

it("tests SelfSignedJwtAudience", async function () {
const response = await authority.resolveEndpointsAsync(stubbedTelemetryManager, TEST_CONFIG.CorrelationId);

expect(authority.SelfSignedJwtAudience).to.equal("https://login.microsoftonline.com/common/v2.0")
describe("get EndSessionEndpoint", () => {
it("throws error if ResolveEndpointsAsync hasn't been called yet", function () {
try {
const authEndpoint = authority.EndSessionEndpoint
}
catch(e) {
expect(e).to.be.equal("Please call ResolveEndpointsAsync first");
}
});

it("tests EndSessionEndpoint", async function () {
const response = await authority.resolveEndpointsAsync(stubbedTelemetryManager, TEST_CONFIG.CorrelationId);

expect(authority.EndSessionEndpoint).to.equal("https://login.microsoftonline.com/common/oauth2/v2.0/logout")
});
});

it("throws invalidAuthorityType on init if authority is not url", function () {
try {
authority = new Authority("", true);
}
catch(e) {
expect(e).to.be.equal(ClientConfigurationErrorMessage.invalidAuthorityType)
}
describe("get SelfSignedJwtAudience", () => {
it("throws error if ResolveEndpointsAsync hasn't been called yet", function () {
try {
const authEndpoint = authority.SelfSignedJwtAudience
}
catch(e) {
expect(e).to.be.equal("Please call ResolveEndpointsAsync first");
}
});

it("tests SelfSignedJwtAudience", async function () {
const response = await authority.resolveEndpointsAsync(stubbedTelemetryManager, TEST_CONFIG.CorrelationId);

expect(authority.SelfSignedJwtAudience).to.equal("https://login.microsoftonline.com/common/v2.0")
});
});

it("throws authorityUriInsecure on init if not https", function () {
try {
authority = new Authority("http://login.microsoftonline.com/common", true);
}
catch(e) {
expect(e).to.be.equal(ClientConfigurationErrorMessage.authorityUriInsecure)
}
describe("resolveEndpointsAsync", () => {
it("returns authority metadata", async function () {
const endpoints = await authority.resolveEndpointsAsync(stubbedTelemetryManager, TEST_CONFIG.CorrelationId);

expect(endpoints.EndSessionEndpoint).to.not.be.undefined;
expect(endpoints.AuthorizationEndpoint).to.not.be.undefined;
expect(endpoints.Issuer).to.not.be.undefined;
});

it("Calls Instance Discovery Endpoint if TrustedHostList not set", async function () {
// Testing of setTrustedAuthoritiesFromNetwork done in another test
let setFromNetworkCalled = false;
sinon.stub(TrustedAuthority, "IsInTrustedHostList").returns(true);
sinon.stub(TrustedAuthority, "getTrustedHostList").returns([]);
sinon.stub(TrustedAuthority, "setTrustedAuthoritiesFromNetwork").callsFake(async function() {
setFromNetworkCalled = true;
});

await authority.resolveEndpointsAsync(stubbedTelemetryManager, TEST_CONFIG.CorrelationId);
expect(setFromNetworkCalled).to.be.true;
});

it("Throws error if authority is not in TrustedHostList", async function () {
sinon.stub(TrustedAuthority, "IsInTrustedHostList").returns(false);
let err = null;
try {
const endpoints = await authority.resolveEndpointsAsync(stubbedTelemetryManager, TEST_CONFIG.CorrelationId);
} catch(e) {
expect(e).to.be.instanceOf(ClientConfigurationError);
err = e;
}

expect(err.errorCode).to.equal(ClientConfigurationErrorMessage.untrustedAuthority.code);
expect(err.errorMessage).to.contain(ClientConfigurationErrorMessage.untrustedAuthority.desc);
});
});

it("throws authorityUriInvalidPath on init if there is no path", function () {
try {
authority = new Authority("https://login.microsoftonline.com", true);
}
catch(e) {
expect(e).to.be.equal(ClientConfigurationErrorMessage.authorityUriInvalidPath)
}
describe("hasCachedMetadata", () => {
it("returns false if metadata no fetched", () => {
expect(authority.hasCachedMetadata()).to.be.false;
});

it("returns true when metadata is provided", () => {
const testAuthorityWithMetadata = new Authority(TEST_CONFIG.validAuthority, true, TENANT_DISCOVERY_RESPONSE);

expect(testAuthorityWithMetadata.hasCachedMetadata()).to.be.true;
});
});

it("hasCachedMetadata returns false if metadata no fetched", () => {
expect(authority.hasCachedMetadata()).to.be.false;
});

it("hasCachedMetadata returns true when metadata is provided", () => {
const testAuthorityWithMetadata = new Authority(TEST_CONFIG.validAuthority, true, TENANT_DISCOVERY_RESPONSE);

expect(testAuthorityWithMetadata.hasCachedMetadata()).to.be.true;
});

it("GetOpenIdConfigurationEndpoint returns well-known endpoint", async function () {
const endpoint = authority.GetOpenIdConfigurationEndpoint();

expect(endpoint).to.include("/v2.0/.well-known/openid-configuration");
expect(endpoint).to.include(TEST_CONFIG.validAuthority);
});

it("GetOpenIdConfigurationEndpoint returns well-known endpoint", async function () {
authority = new Authority(TEST_CONFIG.alternateValidAuthority, true);
const endpoint = authority.GetOpenIdConfigurationEndpoint();

expect(endpoint).to.include("/v2.0/.well-known/openid-configuration");
expect(endpoint).to.include(TEST_CONFIG.alternateValidAuthority);
describe("GetOpenIdConfigurationEndpoint", () => {
it("returns well-known endpoint", async function () {
const endpoint = authority.GetOpenIdConfigurationEndpoint();

expect(endpoint).to.include("/v2.0/.well-known/openid-configuration");
expect(endpoint).to.include(TEST_CONFIG.validAuthority);
});

it("returns well-known endpoint, alternate authority", async function () {
authority = new Authority(TEST_CONFIG.alternateValidAuthority, true);
const endpoint = authority.GetOpenIdConfigurationEndpoint();

expect(endpoint).to.include("/v2.0/.well-known/openid-configuration");
expect(endpoint).to.include(TEST_CONFIG.alternateValidAuthority);
});
});
});
21 changes: 20 additions & 1 deletion lib/msal-core/test/authority/AuthorityFactory.spec.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { expect } from "chai";
import { ClientConfigurationError, ClientConfigurationErrorMessage } from "../../src/error/ClientConfigurationError";
import { ClientConfigurationError } from "../../src/error/ClientConfigurationError";
import { AuthorityFactory } from "../../src/authority/AuthorityFactory";
import { TEST_CONFIG, OPENID_CONFIGURATION, TENANT_DISCOVERY_RESPONSE } from "../TestConstants";
import sinon from "sinon";
import { Authority } from "../../src/authority/Authority";


describe("AuthorityFactory.ts Class", function () {
Expand All @@ -16,6 +17,24 @@ describe("AuthorityFactory.ts Class", function () {

expect(authority).to.be.null;
});

it("Creates Authority Instance", function () {
let authority = AuthorityFactory.CreateInstance(TEST_CONFIG.validAuthority, false);

expect(authority).to.be.instanceOf(Authority);
});

it("calls saveMetadataFromConfig if metadata provided", function (done) {
// Verification of saved metadata is done in separate tests below
const testMetadata = JSON.stringify(OPENID_CONFIGURATION)
sinon.stub(AuthorityFactory, "saveMetadataFromConfig").callsFake(function (authorityUrl, metadata) {
expect(authorityUrl).to.equal(TEST_CONFIG.validAuthority);
expect(metadata).to.equal(testMetadata);
done();
});

AuthorityFactory.CreateInstance(TEST_CONFIG.validAuthority, false, testMetadata);
});
});

describe("saveMetadataFromConfig", () => {
Expand Down
13 changes: 11 additions & 2 deletions lib/msal-core/test/authority/TrustedAuthority.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ describe("TrustedAuthority.ts Class", function () {
});

describe("setTrustedAuthoritiesFromConfig", () => {
it("Sets TrustedHostList with Known Authorities", async () => {
it("Sets TrustedHostList with Known Authorities", () => {
sinon.stub(TrustedAuthority, "getTrustedHostList").returns([]);
TrustedAuthority.setTrustedAuthoritiesFromConfig(true, TEST_CONFIG.knownAuthorities);

Expand All @@ -31,11 +31,20 @@ describe("TrustedAuthority.ts Class", function () {
});
});

it("Do not add additional authorities to trusted host list if it has already been populated", async () => {
it("Do not add additional authorities to trusted host list if it has already been populated", () => {
sinon.stub(TrustedAuthority, "getTrustedHostList").returns(["login.microsoftonline.com"]);
TrustedAuthority.setTrustedAuthoritiesFromConfig(true, ["contoso.b2clogin.com"]);

expect(TrustedAuthority.IsInTrustedHostList("contoso.b2clogin.com")).to.be.false;
});
});

describe("setTrustedAuthoritiesFromNetwork", () => {
it("Sets TrustedHostList with Authorities known to Microsoft via Instance Discovery Network Call", async () => {
const countBefore = TrustedAuthority.getTrustedHostList().length;
await TrustedAuthority.setTrustedAuthoritiesFromNetwork(stubbedTelemetryManager);
const countAfter = TrustedAuthority.getTrustedHostList().length;
expect(countBefore).to.be.lessThan(countAfter);
});
});
});