@@ -15,14 +15,19 @@ limitations under the License.
15
15
*/
16
16
17
17
import { IdTokenClaims } from "oidc-client-ts" ;
18
+ import { decodeIdToken } from "matrix-js-sdk/src/matrix" ;
19
+ import { mocked } from "jest-mock" ;
18
20
19
21
import {
20
22
getStoredOidcClientId ,
23
+ getStoredOidcIdToken ,
21
24
getStoredOidcIdTokenClaims ,
22
25
getStoredOidcTokenIssuer ,
23
26
persistOidcAuthenticatedSettings ,
24
27
} from "../../../src/utils/oidc/persistOidcSettings" ;
25
28
29
+ jest . mock ( "matrix-js-sdk/src/matrix" ) ;
30
+
26
31
describe ( "persist OIDC settings" , ( ) => {
27
32
jest . spyOn ( Storage . prototype , "getItem" ) ;
28
33
jest . spyOn ( Storage . prototype , "setItem" ) ;
@@ -33,6 +38,7 @@ describe("persist OIDC settings", () => {
33
38
34
39
const clientId = "test-client-id" ;
35
40
const issuer = "https://auth.org/" ;
41
+ const idToken = "test-id-token" ;
36
42
const idTokenClaims : IdTokenClaims = {
37
43
// audience is this client
38
44
aud : "123" ,
@@ -44,45 +50,65 @@ describe("persist OIDC settings", () => {
44
50
} ;
45
51
46
52
describe ( "persistOidcAuthenticatedSettings" , ( ) => {
47
- it ( "should set clientId and issuer in session storage " , ( ) => {
48
- persistOidcAuthenticatedSettings ( clientId , issuer , idTokenClaims ) ;
53
+ it ( "should set clientId and issuer in localStorage " , ( ) => {
54
+ persistOidcAuthenticatedSettings ( clientId , issuer , idToken ) ;
49
55
expect ( localStorage . setItem ) . toHaveBeenCalledWith ( "mx_oidc_client_id" , clientId ) ;
50
56
expect ( localStorage . setItem ) . toHaveBeenCalledWith ( "mx_oidc_token_issuer" , issuer ) ;
51
- expect ( localStorage . setItem ) . toHaveBeenCalledWith ( "mx_oidc_id_token_claims " , JSON . stringify ( idTokenClaims ) ) ;
57
+ expect ( localStorage . setItem ) . toHaveBeenCalledWith ( "mx_oidc_id_token " , idToken ) ;
52
58
} ) ;
53
59
} ) ;
54
60
55
61
describe ( "getStoredOidcTokenIssuer()" , ( ) => {
56
- it ( "should return issuer from session storage " , ( ) => {
62
+ it ( "should return issuer from localStorage " , ( ) => {
57
63
localStorage . setItem ( "mx_oidc_token_issuer" , issuer ) ;
58
64
expect ( getStoredOidcTokenIssuer ( ) ) . toEqual ( issuer ) ;
59
65
expect ( localStorage . getItem ) . toHaveBeenCalledWith ( "mx_oidc_token_issuer" ) ;
60
66
} ) ;
61
67
62
- it ( "should return undefined when no issuer in session storage " , ( ) => {
68
+ it ( "should return undefined when no issuer in localStorage " , ( ) => {
63
69
expect ( getStoredOidcTokenIssuer ( ) ) . toBeUndefined ( ) ;
64
70
} ) ;
65
71
} ) ;
66
72
67
73
describe ( "getStoredOidcClientId()" , ( ) => {
68
- it ( "should return clientId from session storage " , ( ) => {
74
+ it ( "should return clientId from localStorage " , ( ) => {
69
75
localStorage . setItem ( "mx_oidc_client_id" , clientId ) ;
70
76
expect ( getStoredOidcClientId ( ) ) . toEqual ( clientId ) ;
71
77
expect ( localStorage . getItem ) . toHaveBeenCalledWith ( "mx_oidc_client_id" ) ;
72
78
} ) ;
73
- it ( "should throw when no clientId in session storage " , ( ) => {
79
+ it ( "should throw when no clientId in localStorage " , ( ) => {
74
80
expect ( ( ) => getStoredOidcClientId ( ) ) . toThrow ( "Oidc client id not found in storage" ) ;
75
81
} ) ;
76
82
} ) ;
77
83
84
+ describe ( "getStoredOidcIdToken()" , ( ) => {
85
+ it ( "should return token from localStorage" , ( ) => {
86
+ localStorage . setItem ( "mx_oidc_id_token" , idToken ) ;
87
+ expect ( getStoredOidcIdToken ( ) ) . toEqual ( idToken ) ;
88
+ expect ( localStorage . getItem ) . toHaveBeenCalledWith ( "mx_oidc_id_token" ) ;
89
+ } ) ;
90
+
91
+ it ( "should return undefined when no token in localStorage" , ( ) => {
92
+ expect ( getStoredOidcIdToken ( ) ) . toBeUndefined ( ) ;
93
+ } ) ;
94
+ } ) ;
95
+
78
96
describe ( "getStoredOidcIdTokenClaims()" , ( ) => {
79
- it ( "should return issuer from session storage " , ( ) => {
97
+ it ( "should return claims from localStorage " , ( ) => {
80
98
localStorage . setItem ( "mx_oidc_id_token_claims" , JSON . stringify ( idTokenClaims ) ) ;
81
99
expect ( getStoredOidcIdTokenClaims ( ) ) . toEqual ( idTokenClaims ) ;
82
100
expect ( localStorage . getItem ) . toHaveBeenCalledWith ( "mx_oidc_id_token_claims" ) ;
83
101
} ) ;
84
102
85
- it ( "should return undefined when no issuer in session storage" , ( ) => {
103
+ it ( "should return claims extracted from id_token in localStorage" , ( ) => {
104
+ localStorage . setItem ( "mx_oidc_id_token" , idToken ) ;
105
+ mocked ( decodeIdToken ) . mockReturnValue ( idTokenClaims ) ;
106
+ expect ( getStoredOidcIdTokenClaims ( ) ) . toEqual ( idTokenClaims ) ;
107
+ expect ( decodeIdToken ) . toHaveBeenCalledWith ( idToken ) ;
108
+ expect ( localStorage . getItem ) . toHaveBeenCalledWith ( "mx_oidc_id_token_claims" ) ;
109
+ } ) ;
110
+
111
+ it ( "should return undefined when no claims in localStorage" , ( ) => {
86
112
expect ( getStoredOidcIdTokenClaims ( ) ) . toBeUndefined ( ) ;
87
113
} ) ;
88
114
} ) ;
0 commit comments