UNPKG

25.3 kBJavaScriptView Raw
1/*
2Copyright 2018 New Vector Ltd
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15*/
16"use strict";
17
18import 'source-map-support/register';
19import Promise from 'bluebird';
20const sdk = require("../..");
21const utils = require("../test-utils");
22
23const AutoDiscovery = sdk.AutoDiscovery;
24
25import expect from 'expect';
26import MockHttpBackend from "matrix-mock-request";
27
28
29describe("AutoDiscovery", function() {
30 let httpBackend = null;
31
32 beforeEach(function() {
33 utils.beforeEach(this); // eslint-disable-line babel/no-invalid-this
34 httpBackend = new MockHttpBackend();
35 sdk.request(httpBackend.requestFn);
36 });
37
38 it("should throw an error when no domain is specified", function() {
39 return Promise.all([
40 AutoDiscovery.findClientConfig(/* no args */).then(() => {
41 throw new Error("Expected a failure, not success with no args");
42 }, () => {
43 return true;
44 }),
45
46 AutoDiscovery.findClientConfig("").then(() => {
47 throw new Error("Expected a failure, not success with an empty string");
48 }, () => {
49 return true;
50 }),
51
52 AutoDiscovery.findClientConfig(null).then(() => {
53 throw new Error("Expected a failure, not success with null");
54 }, () => {
55 return true;
56 }),
57
58 AutoDiscovery.findClientConfig(true).then(() => {
59 throw new Error("Expected a failure, not success with a non-string");
60 }, () => {
61 return true;
62 }),
63 ]);
64 });
65
66 it("should return PROMPT when .well-known 404s", function() {
67 httpBackend.when("GET", "/.well-known/matrix/client").respond(404, {});
68 return Promise.all([
69 httpBackend.flushAllExpected(),
70 AutoDiscovery.findClientConfig("example.org").then((conf) => {
71 const expected = {
72 "m.homeserver": {
73 state: "PROMPT",
74 error: null,
75 base_url: null,
76 },
77 "m.identity_server": {
78 state: "PROMPT",
79 error: null,
80 base_url: null,
81 },
82 };
83
84 expect(conf).toEqual(expected);
85 }),
86 ]);
87 });
88
89 it("should return FAIL_PROMPT when .well-known returns a 500 error", function() {
90 httpBackend.when("GET", "/.well-known/matrix/client").respond(500, {});
91 return Promise.all([
92 httpBackend.flushAllExpected(),
93 AutoDiscovery.findClientConfig("example.org").then((conf) => {
94 const expected = {
95 "m.homeserver": {
96 state: "FAIL_PROMPT",
97 error: AutoDiscovery.ERROR_INVALID,
98 base_url: null,
99 },
100 "m.identity_server": {
101 state: "PROMPT",
102 error: null,
103 base_url: null,
104 },
105 };
106
107 expect(conf).toEqual(expected);
108 }),
109 ]);
110 });
111
112 it("should return FAIL_PROMPT when .well-known returns a 400 error", function() {
113 httpBackend.when("GET", "/.well-known/matrix/client").respond(400, {});
114 return Promise.all([
115 httpBackend.flushAllExpected(),
116 AutoDiscovery.findClientConfig("example.org").then((conf) => {
117 const expected = {
118 "m.homeserver": {
119 state: "FAIL_PROMPT",
120 error: AutoDiscovery.ERROR_INVALID,
121 base_url: null,
122 },
123 "m.identity_server": {
124 state: "PROMPT",
125 error: null,
126 base_url: null,
127 },
128 };
129
130 expect(conf).toEqual(expected);
131 }),
132 ]);
133 });
134
135 it("should return FAIL_PROMPT when .well-known returns an empty body", function() {
136 httpBackend.when("GET", "/.well-known/matrix/client").respond(200, "");
137 return Promise.all([
138 httpBackend.flushAllExpected(),
139 AutoDiscovery.findClientConfig("example.org").then((conf) => {
140 const expected = {
141 "m.homeserver": {
142 state: "FAIL_PROMPT",
143 error: AutoDiscovery.ERROR_INVALID,
144 base_url: null,
145 },
146 "m.identity_server": {
147 state: "PROMPT",
148 error: null,
149 base_url: null,
150 },
151 };
152
153 expect(conf).toEqual(expected);
154 }),
155 ]);
156 });
157
158 it("should return FAIL_PROMPT when .well-known returns not-JSON", function() {
159 httpBackend.when("GET", "/.well-known/matrix/client").respond(200, "abc");
160 return Promise.all([
161 httpBackend.flushAllExpected(),
162 AutoDiscovery.findClientConfig("example.org").then((conf) => {
163 const expected = {
164 "m.homeserver": {
165 state: "FAIL_PROMPT",
166 error: AutoDiscovery.ERROR_INVALID,
167 base_url: null,
168 },
169 "m.identity_server": {
170 state: "PROMPT",
171 error: null,
172 base_url: null,
173 },
174 };
175
176 expect(conf).toEqual(expected);
177 }),
178 ]);
179 });
180
181 it("should return FAIL_PROMPT when .well-known does not have a base_url for " +
182 "m.homeserver (empty string)", function() {
183 httpBackend.when("GET", "/.well-known/matrix/client").respond(200, {
184 "m.homeserver": {
185 base_url: "",
186 },
187 });
188 return Promise.all([
189 httpBackend.flushAllExpected(),
190 AutoDiscovery.findClientConfig("example.org").then((conf) => {
191 const expected = {
192 "m.homeserver": {
193 state: "FAIL_PROMPT",
194 error: AutoDiscovery.ERROR_INVALID_HS_BASE_URL,
195 base_url: null,
196 },
197 "m.identity_server": {
198 state: "PROMPT",
199 error: null,
200 base_url: null,
201 },
202 };
203
204 expect(conf).toEqual(expected);
205 }),
206 ]);
207 });
208
209 it("should return FAIL_PROMPT when .well-known does not have a base_url for " +
210 "m.homeserver (no property)", function() {
211 httpBackend.when("GET", "/.well-known/matrix/client").respond(200, {
212 "m.homeserver": {},
213 });
214 return Promise.all([
215 httpBackend.flushAllExpected(),
216 AutoDiscovery.findClientConfig("example.org").then((conf) => {
217 const expected = {
218 "m.homeserver": {
219 state: "FAIL_PROMPT",
220 error: AutoDiscovery.ERROR_INVALID_HS_BASE_URL,
221 base_url: null,
222 },
223 "m.identity_server": {
224 state: "PROMPT",
225 error: null,
226 base_url: null,
227 },
228 };
229
230 expect(conf).toEqual(expected);
231 }),
232 ]);
233 });
234
235 it("should return FAIL_ERROR when .well-known has an invalid base_url for " +
236 "m.homeserver (disallowed scheme)", function() {
237 httpBackend.when("GET", "/.well-known/matrix/client").respond(200, {
238 "m.homeserver": {
239 base_url: "mxc://example.org",
240 },
241 });
242 return Promise.all([
243 httpBackend.flushAllExpected(),
244 AutoDiscovery.findClientConfig("example.org").then((conf) => {
245 const expected = {
246 "m.homeserver": {
247 state: "FAIL_ERROR",
248 error: AutoDiscovery.ERROR_INVALID_HS_BASE_URL,
249 base_url: null,
250 },
251 "m.identity_server": {
252 state: "PROMPT",
253 error: null,
254 base_url: null,
255 },
256 };
257
258 expect(conf).toEqual(expected);
259 }),
260 ]);
261 });
262
263 it("should return FAIL_ERROR when .well-known has an invalid base_url for " +
264 "m.homeserver (verification failure: 404)", function() {
265 httpBackend.when("GET", "/_matrix/client/versions").respond(404, {});
266 httpBackend.when("GET", "/.well-known/matrix/client").respond(200, {
267 "m.homeserver": {
268 base_url: "https://example.org",
269 },
270 });
271 return Promise.all([
272 httpBackend.flushAllExpected(),
273 AutoDiscovery.findClientConfig("example.org").then((conf) => {
274 const expected = {
275 "m.homeserver": {
276 state: "FAIL_ERROR",
277 error: AutoDiscovery.ERROR_INVALID_HOMESERVER,
278 base_url: "https://example.org",
279 },
280 "m.identity_server": {
281 state: "PROMPT",
282 error: null,
283 base_url: null,
284 },
285 };
286
287 expect(conf).toEqual(expected);
288 }),
289 ]);
290 });
291
292 it("should return FAIL_ERROR when .well-known has an invalid base_url for " +
293 "m.homeserver (verification failure: 500)", function() {
294 httpBackend.when("GET", "/_matrix/client/versions").respond(500, {});
295 httpBackend.when("GET", "/.well-known/matrix/client").respond(200, {
296 "m.homeserver": {
297 base_url: "https://example.org",
298 },
299 });
300 return Promise.all([
301 httpBackend.flushAllExpected(),
302 AutoDiscovery.findClientConfig("example.org").then((conf) => {
303 const expected = {
304 "m.homeserver": {
305 state: "FAIL_ERROR",
306 error: AutoDiscovery.ERROR_INVALID_HOMESERVER,
307 base_url: "https://example.org",
308 },
309 "m.identity_server": {
310 state: "PROMPT",
311 error: null,
312 base_url: null,
313 },
314 };
315
316 expect(conf).toEqual(expected);
317 }),
318 ]);
319 });
320
321 it("should return FAIL_ERROR when .well-known has an invalid base_url for " +
322 "m.homeserver (verification failure: 200 but wrong content)", function() {
323 httpBackend.when("GET", "/_matrix/client/versions").respond(200, {
324 not_matrix_versions: ["r0.0.1"],
325 });
326 httpBackend.when("GET", "/.well-known/matrix/client").respond(200, {
327 "m.homeserver": {
328 base_url: "https://example.org",
329 },
330 });
331 return Promise.all([
332 httpBackend.flushAllExpected(),
333 AutoDiscovery.findClientConfig("example.org").then((conf) => {
334 const expected = {
335 "m.homeserver": {
336 state: "FAIL_ERROR",
337 error: AutoDiscovery.ERROR_INVALID_HOMESERVER,
338 base_url: "https://example.org",
339 },
340 "m.identity_server": {
341 state: "PROMPT",
342 error: null,
343 base_url: null,
344 },
345 };
346
347 expect(conf).toEqual(expected);
348 }),
349 ]);
350 });
351
352 it("should return SUCCESS when .well-known has a verifiably accurate base_url for " +
353 "m.homeserver", function() {
354 httpBackend.when("GET", "/_matrix/client/versions").check((req) => {
355 expect(req.opts.uri).toEqual("https://example.org/_matrix/client/versions");
356 }).respond(200, {
357 versions: ["r0.0.1"],
358 });
359 httpBackend.when("GET", "/.well-known/matrix/client").respond(200, {
360 "m.homeserver": {
361 base_url: "https://example.org",
362 },
363 });
364 return Promise.all([
365 httpBackend.flushAllExpected(),
366 AutoDiscovery.findClientConfig("example.org").then((conf) => {
367 const expected = {
368 "m.homeserver": {
369 state: "SUCCESS",
370 error: null,
371 base_url: "https://example.org",
372 },
373 "m.identity_server": {
374 state: "PROMPT",
375 error: null,
376 base_url: null,
377 },
378 };
379
380 expect(conf).toEqual(expected);
381 }),
382 ]);
383 });
384
385 it("should return SUCCESS with the right homeserver URL", function() {
386 httpBackend.when("GET", "/_matrix/client/versions").check((req) => {
387 expect(req.opts.uri)
388 .toEqual("https://chat.example.org/_matrix/client/versions");
389 }).respond(200, {
390 versions: ["r0.0.1"],
391 });
392 httpBackend.when("GET", "/.well-known/matrix/client").respond(200, {
393 "m.homeserver": {
394 // Note: we also expect this test to trim the trailing slash
395 base_url: "https://chat.example.org/",
396 },
397 });
398 return Promise.all([
399 httpBackend.flushAllExpected(),
400 AutoDiscovery.findClientConfig("example.org").then((conf) => {
401 const expected = {
402 "m.homeserver": {
403 state: "SUCCESS",
404 error: null,
405 base_url: "https://chat.example.org",
406 },
407 "m.identity_server": {
408 state: "PROMPT",
409 error: null,
410 base_url: null,
411 },
412 };
413
414 expect(conf).toEqual(expected);
415 }),
416 ]);
417 });
418
419 it("should return FAIL_ERROR when the identity server configuration is wrong " +
420 "(missing base_url)", function() {
421 httpBackend.when("GET", "/_matrix/client/versions").check((req) => {
422 expect(req.opts.uri)
423 .toEqual("https://chat.example.org/_matrix/client/versions");
424 }).respond(200, {
425 versions: ["r0.0.1"],
426 });
427 httpBackend.when("GET", "/.well-known/matrix/client").respond(200, {
428 "m.homeserver": {
429 // Note: we also expect this test to trim the trailing slash
430 base_url: "https://chat.example.org/",
431 },
432 "m.identity_server": {
433 not_base_url: "https://identity.example.org",
434 },
435 });
436 return Promise.all([
437 httpBackend.flushAllExpected(),
438 AutoDiscovery.findClientConfig("example.org").then((conf) => {
439 const expected = {
440 "m.homeserver": {
441 state: "FAIL_ERROR",
442 error: AutoDiscovery.ERROR_INVALID_IS,
443
444 // We still expect the base_url to be here for debugging purposes.
445 base_url: "https://chat.example.org",
446 },
447 "m.identity_server": {
448 state: "FAIL_ERROR",
449 error: AutoDiscovery.ERROR_INVALID_IS_BASE_URL,
450 base_url: null,
451 },
452 };
453
454 expect(conf).toEqual(expected);
455 }),
456 ]);
457 });
458
459 it("should return FAIL_ERROR when the identity server configuration is wrong " +
460 "(empty base_url)", function() {
461 httpBackend.when("GET", "/_matrix/client/versions").check((req) => {
462 expect(req.opts.uri)
463 .toEqual("https://chat.example.org/_matrix/client/versions");
464 }).respond(200, {
465 versions: ["r0.0.1"],
466 });
467 httpBackend.when("GET", "/.well-known/matrix/client").respond(200, {
468 "m.homeserver": {
469 // Note: we also expect this test to trim the trailing slash
470 base_url: "https://chat.example.org/",
471 },
472 "m.identity_server": {
473 base_url: "",
474 },
475 });
476 return Promise.all([
477 httpBackend.flushAllExpected(),
478 AutoDiscovery.findClientConfig("example.org").then((conf) => {
479 const expected = {
480 "m.homeserver": {
481 state: "FAIL_ERROR",
482 error: AutoDiscovery.ERROR_INVALID_IS,
483
484 // We still expect the base_url to be here for debugging purposes.
485 base_url: "https://chat.example.org",
486 },
487 "m.identity_server": {
488 state: "FAIL_ERROR",
489 error: AutoDiscovery.ERROR_INVALID_IS_BASE_URL,
490 base_url: null,
491 },
492 };
493
494 expect(conf).toEqual(expected);
495 }),
496 ]);
497 });
498
499 it("should return FAIL_ERROR when the identity server configuration is wrong " +
500 "(validation error: 404)", function() {
501 httpBackend.when("GET", "/_matrix/client/versions").check((req) => {
502 expect(req.opts.uri)
503 .toEqual("https://chat.example.org/_matrix/client/versions");
504 }).respond(200, {
505 versions: ["r0.0.1"],
506 });
507 httpBackend.when("GET", "/_matrix/identity/api/v1").respond(404, {});
508 httpBackend.when("GET", "/.well-known/matrix/client").respond(200, {
509 "m.homeserver": {
510 // Note: we also expect this test to trim the trailing slash
511 base_url: "https://chat.example.org/",
512 },
513 "m.identity_server": {
514 base_url: "https://identity.example.org",
515 },
516 });
517 return Promise.all([
518 httpBackend.flushAllExpected(),
519 AutoDiscovery.findClientConfig("example.org").then((conf) => {
520 const expected = {
521 "m.homeserver": {
522 state: "FAIL_ERROR",
523 error: AutoDiscovery.ERROR_INVALID_IS,
524
525 // We still expect the base_url to be here for debugging purposes.
526 base_url: "https://chat.example.org",
527 },
528 "m.identity_server": {
529 state: "FAIL_ERROR",
530 error: AutoDiscovery.ERROR_INVALID_IDENTITY_SERVER,
531 base_url: "https://identity.example.org",
532 },
533 };
534
535 expect(conf).toEqual(expected);
536 }),
537 ]);
538 });
539
540 it("should return FAIL_ERROR when the identity server configuration is wrong " +
541 "(validation error: 500)", function() {
542 httpBackend.when("GET", "/_matrix/client/versions").check((req) => {
543 expect(req.opts.uri)
544 .toEqual("https://chat.example.org/_matrix/client/versions");
545 }).respond(200, {
546 versions: ["r0.0.1"],
547 });
548 httpBackend.when("GET", "/_matrix/identity/api/v1").respond(500, {});
549 httpBackend.when("GET", "/.well-known/matrix/client").respond(200, {
550 "m.homeserver": {
551 // Note: we also expect this test to trim the trailing slash
552 base_url: "https://chat.example.org/",
553 },
554 "m.identity_server": {
555 base_url: "https://identity.example.org",
556 },
557 });
558 return Promise.all([
559 httpBackend.flushAllExpected(),
560 AutoDiscovery.findClientConfig("example.org").then((conf) => {
561 const expected = {
562 "m.homeserver": {
563 state: "FAIL_ERROR",
564 error: AutoDiscovery.ERROR_INVALID_IS,
565
566 // We still expect the base_url to be here for debugging purposes
567 base_url: "https://chat.example.org",
568 },
569 "m.identity_server": {
570 state: "FAIL_ERROR",
571 error: AutoDiscovery.ERROR_INVALID_IDENTITY_SERVER,
572 base_url: "https://identity.example.org",
573 },
574 };
575
576 expect(conf).toEqual(expected);
577 }),
578 ]);
579 });
580
581 it("should return SUCCESS when the identity server configuration is " +
582 "verifiably accurate", function() {
583 httpBackend.when("GET", "/_matrix/client/versions").check((req) => {
584 expect(req.opts.uri)
585 .toEqual("https://chat.example.org/_matrix/client/versions");
586 }).respond(200, {
587 versions: ["r0.0.1"],
588 });
589 httpBackend.when("GET", "/_matrix/identity/api/v1").check((req) => {
590 expect(req.opts.uri)
591 .toEqual("https://identity.example.org/_matrix/identity/api/v1");
592 }).respond(200, {});
593 httpBackend.when("GET", "/.well-known/matrix/client").respond(200, {
594 "m.homeserver": {
595 // Note: we also expect this test to trim the trailing slash
596 base_url: "https://chat.example.org/",
597 },
598 "m.identity_server": {
599 base_url: "https://identity.example.org",
600 },
601 });
602 return Promise.all([
603 httpBackend.flushAllExpected(),
604 AutoDiscovery.findClientConfig("example.org").then((conf) => {
605 const expected = {
606 "m.homeserver": {
607 state: "SUCCESS",
608 error: null,
609 base_url: "https://chat.example.org",
610 },
611 "m.identity_server": {
612 state: "SUCCESS",
613 error: null,
614 base_url: "https://identity.example.org",
615 },
616 };
617
618 expect(conf).toEqual(expected);
619 }),
620 ]);
621 });
622
623 it("should return SUCCESS and preserve non-standard keys from the " +
624 ".well-known response", function() {
625 httpBackend.when("GET", "/_matrix/client/versions").check((req) => {
626 expect(req.opts.uri)
627 .toEqual("https://chat.example.org/_matrix/client/versions");
628 }).respond(200, {
629 versions: ["r0.0.1"],
630 });
631 httpBackend.when("GET", "/_matrix/identity/api/v1").check((req) => {
632 expect(req.opts.uri)
633 .toEqual("https://identity.example.org/_matrix/identity/api/v1");
634 }).respond(200, {});
635 httpBackend.when("GET", "/.well-known/matrix/client").respond(200, {
636 "m.homeserver": {
637 // Note: we also expect this test to trim the trailing slash
638 base_url: "https://chat.example.org/",
639 },
640 "m.identity_server": {
641 base_url: "https://identity.example.org",
642 },
643 "org.example.custom.property": {
644 cupcakes: "yes",
645 },
646 });
647 return Promise.all([
648 httpBackend.flushAllExpected(),
649 AutoDiscovery.findClientConfig("example.org").then((conf) => {
650 const expected = {
651 "m.homeserver": {
652 state: "SUCCESS",
653 error: null,
654 base_url: "https://chat.example.org",
655 },
656 "m.identity_server": {
657 state: "SUCCESS",
658 error: null,
659 base_url: "https://identity.example.org",
660 },
661 "org.example.custom.property": {
662 cupcakes: "yes",
663 },
664 };
665
666 expect(conf).toEqual(expected);
667 }),
668 ]);
669 });
670});