|
1 | 1 | import { expect } from "chai";
|
2 | 2 | import { AuthorityFactory } from "../../src/authority/AuthorityFactory";
|
3 |
| -import { INetworkModule, NetworkRequestOptions } from "../../src/network/INetworkModule"; |
4 |
| -import { ClientConfigurationErrorMessage, ClientAuthErrorMessage, Constants, Authority } from "../../src"; |
| 3 | +import { |
| 4 | + INetworkModule, |
| 5 | + NetworkRequestOptions |
| 6 | +} from "../../src/network/INetworkModule"; |
| 7 | +import { |
| 8 | + ClientConfigurationErrorMessage, |
| 9 | + Constants, |
| 10 | + Authority, |
| 11 | + ClientAuthError, |
| 12 | + ClientAuthErrorMessage |
| 13 | +} from "../../src"; |
5 | 14 | import { AadAuthority } from "../../src/authority/AadAuthority";
|
| 15 | +import { B2cAuthority, B2CTrustedHostList} from "../../src/authority/B2cAuthority"; |
| 16 | +import { TEST_CONFIG } from "../utils/StringConstants"; |
6 | 17 |
|
7 | 18 | describe("AuthorityFactory.ts Class Unit Tests", () => {
|
8 |
| - |
9 | 19 | const networkInterface: INetworkModule = {
|
10 |
| - sendGetRequestAsync<T>(url: string, options?: NetworkRequestOptions): T { |
| 20 | + sendGetRequestAsync<T>( |
| 21 | + url: string, |
| 22 | + options?: NetworkRequestOptions |
| 23 | + ): T { |
11 | 24 | return null;
|
12 | 25 | },
|
13 |
| - sendPostRequestAsync<T>(url: string, options?: NetworkRequestOptions): T { |
| 26 | + sendPostRequestAsync<T>( |
| 27 | + url: string, |
| 28 | + options?: NetworkRequestOptions |
| 29 | + ): T { |
14 | 30 | return null;
|
15 | 31 | }
|
16 | 32 | };
|
17 | 33 |
|
18 |
| - it("AuthorityFactory returns null if given url is null or empty", () => { |
19 |
| - expect(() => AuthorityFactory.createInstance("", networkInterface)).to.throw(ClientConfigurationErrorMessage.urlEmptyError.desc); |
20 |
| - expect(() => AuthorityFactory.createInstance(null, networkInterface)).to.throw(ClientConfigurationErrorMessage.urlEmptyError.desc); |
| 34 | + beforeEach(() => { |
| 35 | + while (B2CTrustedHostList.length) { |
| 36 | + B2CTrustedHostList.pop(); |
| 37 | + } |
21 | 38 | });
|
22 | 39 |
|
23 |
| - it("Throws error for B2C url strings that contain tfp", () => { |
24 |
| - expect(() => AuthorityFactory.createInstance("https://contoso.b2clogin.com/tfp/contoso.onmicrosoft.com/B2C_1_signupsignin1", networkInterface)).to.throw(ClientAuthErrorMessage.invalidAuthorityType.desc); |
| 40 | + it("AuthorityFactory returns null if given url is null or empty", () => { |
| 41 | + expect(() => |
| 42 | + AuthorityFactory.createInstance("", networkInterface) |
| 43 | + ).to.throw(ClientConfigurationErrorMessage.urlEmptyError.desc); |
| 44 | + expect(() => |
| 45 | + AuthorityFactory.createInstance(null, networkInterface) |
| 46 | + ).to.throw(ClientConfigurationErrorMessage.urlEmptyError.desc); |
25 | 47 | });
|
26 | 48 |
|
27 | 49 | it("Throws error for malformed url strings", () => {
|
28 |
| - expect(() => AuthorityFactory.createInstance(`http://login.microsoftonline.com/common`, networkInterface)).to.throw(ClientConfigurationErrorMessage.authorityUriInsecure.desc); |
29 |
| - expect(() => AuthorityFactory.createInstance(`https://login.microsoftonline.com/`, networkInterface)).to.throw(ClientConfigurationErrorMessage.urlParseError.desc); |
30 |
| - expect(() => AuthorityFactory.createInstance("This is not a URI", networkInterface)).to.throw(ClientConfigurationErrorMessage.urlParseError.desc); |
31 |
| - expect(() => AuthorityFactory.createInstance("", networkInterface)).to.throw(ClientConfigurationErrorMessage.urlEmptyError.desc); |
| 50 | + expect(() => |
| 51 | + AuthorityFactory.createInstance( |
| 52 | + `http://login.microsoftonline.com/common`, |
| 53 | + networkInterface |
| 54 | + ) |
| 55 | + ).to.throw(ClientConfigurationErrorMessage.authorityUriInsecure.desc); |
| 56 | + expect(() => |
| 57 | + AuthorityFactory.createInstance( |
| 58 | + `https://login.microsoftonline.com/`, |
| 59 | + networkInterface |
| 60 | + ) |
| 61 | + ).to.throw(ClientConfigurationErrorMessage.urlParseError.desc); |
| 62 | + expect(() => |
| 63 | + AuthorityFactory.createInstance( |
| 64 | + "This is not a URI", |
| 65 | + networkInterface |
| 66 | + ) |
| 67 | + ).to.throw(ClientConfigurationErrorMessage.urlParseError.desc); |
| 68 | + expect(() => |
| 69 | + AuthorityFactory.createInstance("", networkInterface) |
| 70 | + ).to.throw(ClientConfigurationErrorMessage.urlEmptyError.desc); |
32 | 71 | });
|
33 | 72 |
|
34 |
| - it("createInstance returns an AAD instance for any valid url string that does not contain a tfp", () => { |
35 |
| - const authorityInstance = AuthorityFactory.createInstance(Constants.DEFAULT_AUTHORITY, networkInterface); |
| 73 | + it("createInstance returns an AAD instance if knownAuthorities not provided", () => { |
| 74 | + const authorityInstance = AuthorityFactory.createInstance( |
| 75 | + Constants.DEFAULT_AUTHORITY, |
| 76 | + networkInterface |
| 77 | + ); |
36 | 78 | expect(authorityInstance instanceof AadAuthority);
|
37 | 79 | expect(authorityInstance instanceof Authority);
|
38 | 80 | });
|
| 81 | + |
| 82 | + it("createInstance returns B2C instance if knownAuthorities is provided", () => { |
| 83 | + AuthorityFactory.setKnownAuthorities(["fabrikamb2c.b2clogin.com"]); |
| 84 | + const authorityInstance = AuthorityFactory.createInstance( |
| 85 | + TEST_CONFIG.b2cValidAuthority, |
| 86 | + networkInterface |
| 87 | + ); |
| 88 | + expect(authorityInstance instanceof B2cAuthority); |
| 89 | + expect(authorityInstance instanceof Authority); |
| 90 | + |
| 91 | + |
| 92 | + }); |
| 93 | + |
| 94 | + it("Do not add additional authorities to trusted host list if it has already been populated", () => { |
| 95 | + AuthorityFactory.setKnownAuthorities(["fabrikamb2c.b2clogin.com"]); |
| 96 | + AuthorityFactory.setKnownAuthorities(["fake.b2clogin.com"]); |
| 97 | + |
| 98 | + expect(B2CTrustedHostList).to.include("fabrikamb2c.b2clogin.com"); |
| 99 | + expect(B2CTrustedHostList).not.to.include("fake.b2clogin.com"); |
| 100 | + expect(B2CTrustedHostList.length).to.equal(1); |
| 101 | + }); |
| 102 | + |
| 103 | + it("Throws error if AuthorityType is not AAD or B2C", done => { |
| 104 | + //Right now only way to throw this is to send adfs authority. This will need to change when we implement ADFS |
| 105 | + const errorAuthority = "https://login.microsoftonline.com/adfs"; |
| 106 | + try { |
| 107 | + const authorityInstance = AuthorityFactory.createInstance( |
| 108 | + errorAuthority, |
| 109 | + networkInterface |
| 110 | + ); |
| 111 | + } catch (e) { |
| 112 | + expect(e).to.be.instanceOf(ClientAuthError); |
| 113 | + expect(e.errorCode).to.be.eql( |
| 114 | + ClientAuthErrorMessage.invalidAuthorityType.code |
| 115 | + ); |
| 116 | + expect(e.errorMessage).to.be.eql( |
| 117 | + `${ClientAuthErrorMessage.invalidAuthorityType.desc} Given Url: ${errorAuthority}` |
| 118 | + ); |
| 119 | + done(); |
| 120 | + } |
| 121 | + }); |
39 | 122 | });
|
0 commit comments