Skip to content

Commit 53d373d

Browse files
author
Kerry Archibald
committed
test validateWellKnownAuthentication
1 parent 7e7cfd1 commit 53d373d

File tree

1 file changed

+103
-0
lines changed

1 file changed

+103
-0
lines changed

spec/unit/oidc/validate.spec.ts

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
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 { M_AUTHENTICATION } from "../../../src";
18+
import { OidcDiscoveryError, validateWellKnownAuthentication } from "../../../src/oidc/validate";
19+
20+
describe('validateWellKnownAuthentication()', () => {
21+
const baseWk = {
22+
"m.homeserver" : {
23+
base_url: "https://hs.org"
24+
}
25+
}
26+
it('should throw not supported error when wellKnown has no m.authentication section', () => {
27+
expect(() => validateWellKnownAuthentication(baseWk)).toThrow(OidcDiscoveryError.NotSupported);
28+
});
29+
30+
it('should throw misconfigured error when authentication issuer is not a string', () => {
31+
const wk = {
32+
...baseWk,
33+
[M_AUTHENTICATION.stable!]: {
34+
issuer: { url: 'test.com' }
35+
}
36+
}
37+
expect(() => validateWellKnownAuthentication(wk)).toThrow(OidcDiscoveryError.Misconfigured);
38+
});
39+
40+
it('should throw misconfigured error when authentication account is not a string', () => {
41+
const wk = {
42+
...baseWk,
43+
[M_AUTHENTICATION.stable!]: {
44+
issuer: "test.com",
45+
account: { url: "test" }
46+
}
47+
}
48+
expect(() => validateWellKnownAuthentication(wk)).toThrow(OidcDiscoveryError.Misconfigured);
49+
});
50+
51+
it('should return valid config when wk uses stable m.authentication', () => {
52+
const wk = {
53+
...baseWk,
54+
[M_AUTHENTICATION.stable!]: {
55+
issuer: "test.com",
56+
account: "account.com",
57+
}
58+
}
59+
expect(validateWellKnownAuthentication(wk)).toEqual({
60+
issuer: "test.com",
61+
account: "account.com"
62+
});
63+
});
64+
65+
it('should return valid config when m.authentication account is falsy', () => {
66+
const wk = {
67+
...baseWk,
68+
[M_AUTHENTICATION.stable!]: {
69+
issuer: "test.com",
70+
}
71+
}
72+
expect(validateWellKnownAuthentication(wk)).toEqual({
73+
issuer: "test.com",
74+
});
75+
});
76+
77+
it('should remove unexpected properties', () => {
78+
const wk = {
79+
...baseWk,
80+
[M_AUTHENTICATION.stable!]: {
81+
issuer: "test.com",
82+
somethingElse: "test"
83+
}
84+
}
85+
expect(validateWellKnownAuthentication(wk)).toEqual({
86+
issuer: "test.com",
87+
});
88+
});
89+
90+
it('should return valid config when wk uses unstable prefix for m.authentication', () => {
91+
const wk = {
92+
...baseWk,
93+
[M_AUTHENTICATION.unstable!]: {
94+
issuer: "test.com",
95+
account: "account.com",
96+
}
97+
}
98+
expect(validateWellKnownAuthentication(wk)).toEqual({
99+
issuer: "test.com",
100+
account: "account.com"
101+
});
102+
});
103+
});

0 commit comments

Comments
 (0)