@@ -2,18 +2,21 @@ import { expect } from "chai";
2
2
import sinon from "sinon" ;
3
3
import { BrowserStorage } from "../src/cache/BrowserStorage" ;
4
4
import { AuthCache } from "../src/cache/AuthCache" ;
5
- import { Constants } from "../src" ;
5
+ import { Constants , AuthError } from "../src" ;
6
6
import { CacheKeys } from "../src/utils/Constants" ;
7
7
import { AccessTokenKey } from "../src/cache/AccessTokenKey" ;
8
8
import { AccessTokenValue } from "../src/cache/AccessTokenValue" ;
9
9
import { Account } from "../src/Account" ;
10
+ import { AuthErrorMessage } from "../src/error/AuthError" ;
11
+ import { ClientConfigurationErrorMessage , ClientConfigurationError } from "../src/error/ClientConfigurationError" ;
10
12
11
13
describe ( "CacheStorage.ts Class - Local Storage" , function ( ) {
12
- let TEST_KEY = "test_key" ;
13
- let TEST_VALUE = "test value" ;
14
- let TEST_ACCOUNT_ID = "1234" ;
15
- let TEST_STATE = "state5678" ;
16
- let TEST_STATE2 = "state9012" ;
14
+ const TEST_KEY = "test_key" ;
15
+ const TEST_VALUE = "test value" ;
16
+ const TEST_ACCOUNT_ID = "1234" ;
17
+ const TEST_STATE = "state5678" ;
18
+ const TEST_STATE2 = "state9012" ;
19
+ const LOCAL_STORAGE = "localStorage" ;
17
20
let cacheStorage : BrowserStorage ;
18
21
let ACCESS_TOKEN_KEY : AccessTokenKey ;
19
22
let ACCESS_TOKEN_VALUE : AccessTokenValue ;
@@ -59,29 +62,58 @@ describe("CacheStorage.ts Class - Local Storage", function () {
59
62
} ) ;
60
63
61
64
it ( "parses the cache location correctly" , function ( ) {
62
- cacheStorage = new BrowserStorage ( "localStorage" ) ;
65
+ cacheStorage = new BrowserStorage ( LOCAL_STORAGE ) ;
63
66
cacheStorage . setItem ( TEST_KEY , TEST_VALUE ) ;
64
67
expect ( window . localStorage . getItem ( TEST_KEY ) ) . to . be . eq ( TEST_VALUE ) ;
65
68
} ) ;
66
69
67
70
it ( "throws error if cache location is not supported" , function ( ) {
68
- // Cannot test with current tooling - will need to take a look
69
- // Possibly wrapple as an option here? https://github.com/mroderick/wrapple
70
- } ) ;
71
-
72
- it ( "uses previous storage instance if one already exists" , function ( ) {
73
- let oldCacheStorage = new BrowserStorage ( Constants . cacheLocationLocal ) ;
74
- cacheStorage = new BrowserStorage ( Constants . cacheLocationSession ) ;
75
- expect ( cacheStorage ) . to . deep . eq ( oldCacheStorage ) ;
71
+ sinon . stub ( window , LOCAL_STORAGE ) . value ( null ) ;
72
+ console . log ( window . localStorage ) ;
73
+ let authErr ;
74
+ try {
75
+ cacheStorage = new BrowserStorage ( LOCAL_STORAGE ) ;
76
+ } catch ( e ) {
77
+ authErr = e ;
78
+ }
79
+ expect ( authErr instanceof ClientConfigurationError ) . to . be . true ;
80
+ expect ( authErr instanceof Error ) . to . be . true ;
81
+ expect ( authErr . errorCode ) . to . equal ( ClientConfigurationErrorMessage . storageNotSupported . code ) ;
82
+ expect ( authErr . errorMessage ) . to . include ( ClientConfigurationErrorMessage . storageNotSupported . desc ) ;
83
+ expect ( authErr . message ) . to . include ( ClientConfigurationErrorMessage . storageNotSupported . desc ) ;
84
+ expect ( authErr . errorMessage ) . to . include ( LOCAL_STORAGE ) ;
85
+ expect ( authErr . message ) . to . include ( LOCAL_STORAGE ) ;
86
+ expect ( authErr . name ) . to . equal ( "ClientConfigurationError" ) ;
87
+ expect ( authErr . stack ) . to . include ( "Storage.localStorage.spec.ts" ) ;
88
+ sinon . restore ( ) ;
76
89
} ) ;
77
90
91
+ it ( "throws error if window object does not exist" , function ( ) {
92
+ let authErr ;
93
+ const oldWindow = window ;
94
+ window = null ;
95
+ try {
96
+ cacheStorage = new BrowserStorage ( LOCAL_STORAGE ) ;
97
+ } catch ( e ) {
98
+ authErr = e ;
99
+ }
100
+ expect ( authErr instanceof AuthError ) . to . be . true ;
101
+ expect ( authErr instanceof Error ) . to . be . true ;
102
+ expect ( authErr . errorCode ) . to . equal ( AuthErrorMessage . noWindowObjectError . code ) ;
103
+ expect ( authErr . errorMessage ) . to . include ( AuthErrorMessage . noWindowObjectError . desc ) ;
104
+ expect ( authErr . message ) . to . include ( AuthErrorMessage . noWindowObjectError . desc ) ;
105
+ expect ( authErr . name ) . to . equal ( "AuthError" ) ;
106
+ expect ( authErr . stack ) . to . include ( "Storage.localStorage.spec.ts" ) ;
107
+ window = oldWindow ;
108
+ } )
78
109
} ) ;
79
110
80
111
describe ( "localStorage access functions" , function ( ) {
81
112
82
113
beforeEach ( function ( ) {
83
- cacheStorage = new BrowserStorage ( "localStorage" ) ;
114
+ cacheStorage = new BrowserStorage ( LOCAL_STORAGE ) ;
84
115
setTestCacheItems ( ) ;
116
+ document . cookie = "" ;
85
117
} ) ;
86
118
87
119
afterEach ( function ( ) {
@@ -118,13 +150,15 @@ describe("CacheStorage.ts Class - Local Storage", function () {
118
150
cacheStorage . setItemCookie ( CacheKeys . NONCE_IDTOKEN , idTokenNonceString ) ;
119
151
expect ( document . cookie ) . to . include ( CacheKeys . NONCE_IDTOKEN ) ;
120
152
expect ( document . cookie ) . to . include ( idTokenNonceString ) ;
153
+ cacheStorage . clearItemCookie ( CacheKeys . NONCE_IDTOKEN ) ;
121
154
} ) ;
122
155
123
156
it ( "tests getItemCookie " , function ( ) {
124
157
let idTokenNonceString = "idTokenNonce" ;
125
158
cacheStorage . setItemCookie ( CacheKeys . NONCE_IDTOKEN , idTokenNonceString ) ;
126
159
let retrievedItem = cacheStorage . getItemCookie ( CacheKeys . NONCE_IDTOKEN ) ;
127
160
expect ( retrievedItem ) . to . include ( idTokenNonceString ) ;
161
+ cacheStorage . clearItemCookie ( CacheKeys . NONCE_IDTOKEN ) ;
128
162
} ) ;
129
163
130
164
it ( "tests getCookieExpirationTime" , function ( ) {
@@ -173,8 +207,9 @@ describe("CacheStorage.ts Class - Local Storage", function () {
173
207
let msalCacheStorage : AuthCache ;
174
208
175
209
beforeEach ( function ( ) {
176
- msalCacheStorage = new AuthCache ( MSAL_CLIENT_ID , "localStorage" , true ) ;
210
+ msalCacheStorage = new AuthCache ( MSAL_CLIENT_ID , LOCAL_STORAGE , true ) ;
177
211
setTestCacheItems ( ) ;
212
+ document . cookie = "" ;
178
213
} ) ;
179
214
180
215
afterEach ( function ( ) {
@@ -217,8 +252,9 @@ describe("CacheStorage.ts Class - Local Storage", function () {
217
252
it ( "removeAcquireTokenEntries removes any acquireToken or authorityKey entries in the cache" , function ( ) {
218
253
let acquireTokenAccountKey = AuthCache . generateAcquireTokenAccountKey ( TEST_ACCOUNT_ID , TEST_STATE ) ;
219
254
let authorityKey = AuthCache . generateAuthorityKey ( TEST_STATE ) ;
220
- window . localStorage . setItem ( acquireTokenAccountKey , JSON . stringify ( ACCOUNT ) ) ;
221
- window . localStorage . setItem ( authorityKey , validAuthority ) ;
255
+
256
+ window . localStorage . setItem ( `${ CacheKeys . PREFIX } .${ MSAL_CLIENT_ID } .${ acquireTokenAccountKey } ` , JSON . stringify ( ACCOUNT ) ) ;
257
+ window . localStorage . setItem ( `${ CacheKeys . PREFIX } .${ MSAL_CLIENT_ID } .${ authorityKey } ` , validAuthority ) ;
222
258
223
259
expect ( msalCacheStorage . getItem ( acquireTokenAccountKey ) ) . to . be . eq ( JSON . stringify ( ACCOUNT ) ) ;
224
260
expect ( msalCacheStorage . getItem ( authorityKey ) ) . to . be . eq ( validAuthority ) ;
@@ -235,10 +271,10 @@ describe("CacheStorage.ts Class - Local Storage", function () {
235
271
236
272
let acquireTokenAccountKey2 = AuthCache . generateAcquireTokenAccountKey ( TEST_ACCOUNT_ID , TEST_STATE2 ) ;
237
273
let authorityKey2 = AuthCache . generateAuthorityKey ( TEST_STATE2 ) ;
238
- window . localStorage . setItem ( acquireTokenAccountKey , JSON . stringify ( ACCOUNT ) ) ;
239
- window . localStorage . setItem ( authorityKey , validAuthority ) ;
240
- window . localStorage . setItem ( acquireTokenAccountKey2 , JSON . stringify ( ACCOUNT ) ) ;
241
- window . localStorage . setItem ( authorityKey2 , validAuthority ) ;
274
+ window . localStorage . setItem ( ` ${ CacheKeys . PREFIX } . ${ MSAL_CLIENT_ID } . ${ acquireTokenAccountKey } ` , JSON . stringify ( ACCOUNT ) ) ;
275
+ window . localStorage . setItem ( ` ${ CacheKeys . PREFIX } . ${ MSAL_CLIENT_ID } . ${ authorityKey } ` , validAuthority ) ;
276
+ window . localStorage . setItem ( ` ${ CacheKeys . PREFIX } . ${ MSAL_CLIENT_ID } . ${ acquireTokenAccountKey2 } ` , JSON . stringify ( ACCOUNT ) ) ;
277
+ window . localStorage . setItem ( ` ${ CacheKeys . PREFIX } . ${ MSAL_CLIENT_ID } . ${ authorityKey2 } ` , validAuthority ) ;
242
278
243
279
expect ( msalCacheStorage . getItem ( acquireTokenAccountKey ) ) . to . be . eq ( JSON . stringify ( ACCOUNT ) ) ;
244
280
expect ( msalCacheStorage . getItem ( authorityKey ) ) . to . be . eq ( validAuthority ) ;
@@ -263,11 +299,17 @@ describe("CacheStorage.ts Class - Local Storage", function () {
263
299
let stateLoginString = "stateLogin" ;
264
300
let loginRequestString = "loginRequest" ;
265
301
let stateAcquireTokenString = "stateAcquireToken" ;
302
+ console . log ( document . cookie ) ;
266
303
msalCacheStorage . setItemCookie ( CacheKeys . NONCE_IDTOKEN , idTokenNonceString ) ;
304
+ console . log ( document . cookie ) ;
267
305
msalCacheStorage . setItemCookie ( CacheKeys . STATE_LOGIN , stateLoginString ) ;
306
+ console . log ( document . cookie ) ;
268
307
msalCacheStorage . setItemCookie ( CacheKeys . LOGIN_REQUEST , loginRequestString ) ;
308
+ console . log ( document . cookie ) ;
269
309
msalCacheStorage . setItemCookie ( CacheKeys . STATE_ACQ_TOKEN , stateAcquireTokenString ) ;
310
+ console . log ( document . cookie ) ;
270
311
msalCacheStorage . clearMsalCookie ( ) ;
312
+ console . log ( document . cookie ) ;
271
313
expect ( document . cookie ) . to . be . empty ;
272
314
} ) ;
273
315
0 commit comments