|
| 1 | +/* |
| 2 | +Copyright 2023 The Matrix.org Foundation C.I.C. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +import { AutoDiscovery, AutoDiscoveryAction, ClientConfig } from "matrix-js-sdk/src/autodiscovery"; |
| 18 | +import { logger } from "matrix-js-sdk/src/logger"; |
| 19 | + |
| 20 | +import AutoDiscoveryUtils from "../../src/utils/AutoDiscoveryUtils"; |
| 21 | + |
| 22 | +describe("AutoDiscoveryUtils", () => { |
| 23 | + describe("buildValidatedConfigFromDiscovery()", () => { |
| 24 | + const serverName = "my-server"; |
| 25 | + |
| 26 | + beforeEach(() => { |
| 27 | + // don't litter console with expected errors |
| 28 | + jest.spyOn(logger, "error") |
| 29 | + .mockClear() |
| 30 | + .mockImplementation(() => {}); |
| 31 | + }); |
| 32 | + |
| 33 | + afterAll(() => { |
| 34 | + jest.spyOn(logger, "error").mockRestore(); |
| 35 | + }); |
| 36 | + |
| 37 | + const validIsConfig = { |
| 38 | + "m.identity_server": { |
| 39 | + state: AutoDiscoveryAction.SUCCESS, |
| 40 | + base_url: "identity.com", |
| 41 | + }, |
| 42 | + }; |
| 43 | + const validHsConfig = { |
| 44 | + "m.homeserver": { |
| 45 | + state: AutoDiscoveryAction.SUCCESS, |
| 46 | + base_url: "https://matrix.org", |
| 47 | + }, |
| 48 | + }; |
| 49 | + |
| 50 | + const expectedValidatedConfig = { |
| 51 | + hsName: serverName, |
| 52 | + hsNameIsDifferent: true, |
| 53 | + hsUrl: "https://matrix.org", |
| 54 | + isDefault: false, |
| 55 | + isNameResolvable: true, |
| 56 | + isUrl: "identity.com", |
| 57 | + }; |
| 58 | + |
| 59 | + it("throws an error when discovery result is falsy", () => { |
| 60 | + expect(() => AutoDiscoveryUtils.buildValidatedConfigFromDiscovery(serverName, undefined as any)).toThrow( |
| 61 | + "Unexpected error resolving homeserver configuration", |
| 62 | + ); |
| 63 | + expect(logger.error).toHaveBeenCalled(); |
| 64 | + }); |
| 65 | + |
| 66 | + it("throws an error when discovery result does not include homeserver config", () => { |
| 67 | + const discoveryResult = { |
| 68 | + ...validIsConfig, |
| 69 | + } as unknown as ClientConfig; |
| 70 | + expect(() => AutoDiscoveryUtils.buildValidatedConfigFromDiscovery(serverName, discoveryResult)).toThrow( |
| 71 | + "Unexpected error resolving homeserver configuration", |
| 72 | + ); |
| 73 | + expect(logger.error).toHaveBeenCalled(); |
| 74 | + }); |
| 75 | + |
| 76 | + it("throws an error when identity server config has fail error and recognised error string", () => { |
| 77 | + const discoveryResult = { |
| 78 | + ...validHsConfig, |
| 79 | + "m.identity_server": { |
| 80 | + state: AutoDiscoveryAction.FAIL_ERROR, |
| 81 | + error: "GenericFailure", |
| 82 | + }, |
| 83 | + }; |
| 84 | + expect(() => AutoDiscoveryUtils.buildValidatedConfigFromDiscovery(serverName, discoveryResult)).toThrow( |
| 85 | + "GenericFailure", |
| 86 | + ); |
| 87 | + expect(logger.error).toHaveBeenCalled(); |
| 88 | + }); |
| 89 | + |
| 90 | + it("throws an error with fallback message identity server config has fail error", () => { |
| 91 | + const discoveryResult = { |
| 92 | + ...validHsConfig, |
| 93 | + "m.identity_server": { |
| 94 | + state: AutoDiscoveryAction.FAIL_ERROR, |
| 95 | + }, |
| 96 | + }; |
| 97 | + expect(() => AutoDiscoveryUtils.buildValidatedConfigFromDiscovery(serverName, discoveryResult)).toThrow( |
| 98 | + "Unexpected error resolving identity server configuration", |
| 99 | + ); |
| 100 | + }); |
| 101 | + |
| 102 | + it("throws an error when error is ERROR_INVALID_HOMESERVER", () => { |
| 103 | + const discoveryResult = { |
| 104 | + ...validIsConfig, |
| 105 | + "m.homeserver": { |
| 106 | + state: AutoDiscoveryAction.FAIL_ERROR, |
| 107 | + error: AutoDiscovery.ERROR_INVALID_HOMESERVER, |
| 108 | + }, |
| 109 | + }; |
| 110 | + expect(() => AutoDiscoveryUtils.buildValidatedConfigFromDiscovery(serverName, discoveryResult)).toThrow( |
| 111 | + "Unexpected error resolving homeserver configuration", |
| 112 | + ); |
| 113 | + }); |
| 114 | + |
| 115 | + it("throws an error when homeserver base_url is falsy", () => { |
| 116 | + const discoveryResult = { |
| 117 | + ...validIsConfig, |
| 118 | + "m.homeserver": { |
| 119 | + state: AutoDiscoveryAction.SUCCESS, |
| 120 | + base_url: "", |
| 121 | + }, |
| 122 | + }; |
| 123 | + expect(() => AutoDiscoveryUtils.buildValidatedConfigFromDiscovery(serverName, discoveryResult)).toThrow( |
| 124 | + "Unexpected error resolving homeserver configuration", |
| 125 | + ); |
| 126 | + expect(logger.error).toHaveBeenCalledWith("No homeserver URL configured"); |
| 127 | + }); |
| 128 | + |
| 129 | + it("throws an error when homeserver base_url is not a valid URL", () => { |
| 130 | + const discoveryResult = { |
| 131 | + ...validIsConfig, |
| 132 | + "m.homeserver": { |
| 133 | + state: AutoDiscoveryAction.SUCCESS, |
| 134 | + base_url: "banana", |
| 135 | + }, |
| 136 | + }; |
| 137 | + expect(() => AutoDiscoveryUtils.buildValidatedConfigFromDiscovery(serverName, discoveryResult)).toThrow( |
| 138 | + "Invalid URL: banana", |
| 139 | + ); |
| 140 | + }); |
| 141 | + |
| 142 | + it("uses hs url hostname when serverName is falsy in args and config", () => { |
| 143 | + const discoveryResult = { |
| 144 | + ...validIsConfig, |
| 145 | + ...validHsConfig, |
| 146 | + }; |
| 147 | + expect(AutoDiscoveryUtils.buildValidatedConfigFromDiscovery("", discoveryResult)).toEqual({ |
| 148 | + ...expectedValidatedConfig, |
| 149 | + hsNameIsDifferent: false, |
| 150 | + hsName: "matrix.org", |
| 151 | + }); |
| 152 | + }); |
| 153 | + |
| 154 | + it("uses serverName from props", () => { |
| 155 | + const discoveryResult = { |
| 156 | + ...validIsConfig, |
| 157 | + "m.homeserver": { |
| 158 | + ...validHsConfig["m.homeserver"], |
| 159 | + server_name: "should not use this name", |
| 160 | + }, |
| 161 | + }; |
| 162 | + const syntaxOnly = true; |
| 163 | + expect( |
| 164 | + AutoDiscoveryUtils.buildValidatedConfigFromDiscovery(serverName, discoveryResult, syntaxOnly), |
| 165 | + ).toEqual({ |
| 166 | + ...expectedValidatedConfig, |
| 167 | + hsNameIsDifferent: true, |
| 168 | + hsName: serverName, |
| 169 | + }); |
| 170 | + }); |
| 171 | + |
| 172 | + it("ignores liveliness error when checking syntax only", () => { |
| 173 | + const discoveryResult = { |
| 174 | + ...validIsConfig, |
| 175 | + "m.homeserver": { |
| 176 | + ...validHsConfig["m.homeserver"], |
| 177 | + state: AutoDiscoveryAction.FAIL_ERROR, |
| 178 | + error: AutoDiscovery.ERROR_INVALID_HOMESERVER, |
| 179 | + }, |
| 180 | + }; |
| 181 | + const syntaxOnly = true; |
| 182 | + expect( |
| 183 | + AutoDiscoveryUtils.buildValidatedConfigFromDiscovery(serverName, discoveryResult, syntaxOnly), |
| 184 | + ).toEqual({ |
| 185 | + ...expectedValidatedConfig, |
| 186 | + warning: "Homeserver URL does not appear to be a valid Matrix homeserver", |
| 187 | + }); |
| 188 | + }); |
| 189 | + }); |
| 190 | +}); |
0 commit comments