11'use strict'
22
3- require ( 'localstorage-polyfill' ) // exports 'localStorage' global
43global . URL = require ( 'url' ) . URL
54global . URLSearchParams = require ( 'url' ) . URLSearchParams
65
6+ const localStorage = require ( 'localstorage-memory' )
7+
78const chai = require ( 'chai' )
89const sinon = require ( 'sinon' )
910const sinonChai = require ( 'sinon-chai' )
@@ -18,18 +19,15 @@ const expect = chai.expect
1819const SolidAuthOIDC = require ( '../src/index' )
1920
2021describe ( 'SolidAuthOIDC' , ( ) => {
22+ var auth
23+ const providerUri = 'https://provider.example.com'
24+
2125 beforeEach ( ( ) => {
2226 localStorage . clear ( )
27+ auth = new SolidAuthOIDC ( { window : { location : { } } , store : localStorage } )
2328 } )
2429
2530 describe ( 'login()' , ( ) => {
26- let auth , providerUri
27-
28- beforeEach ( ( ) => {
29- auth = new SolidAuthOIDC ( )
30- providerUri = 'https://provider.example.com'
31- } )
32-
3331 it ( 'should invoke selectProvider() if provider uri is not given' , ( ) => {
3432 let selectProvider = sinon . stub ( auth , 'selectProvider' ) . resolves ( null )
3533
@@ -74,12 +72,6 @@ describe('SolidAuthOIDC', () => {
7472 } )
7573
7674 describe ( 'logout()' , ( ) => {
77- let auth
78-
79- beforeEach ( ( ) => {
80- auth = new SolidAuthOIDC ( )
81- } )
82-
8375 it ( 'should clear the current user' , ( ) => {
8476 let clearCurrentUser = sinon . spy ( auth , 'clearCurrentUser' )
8577
@@ -129,11 +121,6 @@ describe('SolidAuthOIDC', () => {
129121 } )
130122
131123 describe ( 'providerFromCurrentUri()' , ( ) => {
132- var auth
133- beforeEach ( ( ) => {
134- auth = new SolidAuthOIDC ( { window : { location : { } } } )
135- } )
136-
137124 it ( 'should return null when no state param present' , ( ) => {
138125 auth . window . location . href = 'https://client-app.example.com'
139126 let providerUri = auth . providerFromCurrentUri ( )
@@ -163,8 +150,6 @@ describe('SolidAuthOIDC', () => {
163150
164151 describe ( 'provider persistence' , ( ) => {
165152 it ( 'should store and load provider uri, by state' , ( ) => {
166- let auth = new SolidAuthOIDC ( )
167- let providerUri = 'https://provider.example.com'
168153 let state = 'abcd'
169154 // Check to see that provider doesn't exist initially
170155 expect ( auth . loadProvider ( state ) ) . to . not . exist ( )
@@ -178,12 +163,6 @@ describe('SolidAuthOIDC', () => {
178163 } )
179164
180165 describe ( 'extractState()' , ( ) => {
181- var auth
182-
183- beforeEach ( ( ) => {
184- auth = new SolidAuthOIDC ( )
185- } )
186-
187166 it ( 'should return null when no uri is provided' , ( ) => {
188167 let state = auth . extractState ( )
189168
@@ -223,15 +202,10 @@ describe('SolidAuthOIDC', () => {
223202
224203 describe ( 'selectProvider()' , ( ) => {
225204 it ( 'should pass through a given providerUri' , ( ) => {
226- let auth = new SolidAuthOIDC ( )
227- let providerUri = 'https://provider.example.com'
228-
229205 expect ( auth . selectProvider ( providerUri ) ) . to . eventually . equal ( providerUri )
230206 } )
231207
232208 it ( 'should derive a provider from the current uri' , ( ) => {
233- let auth = new SolidAuthOIDC ( )
234- let providerUri = 'https://provider.example.com'
235209 auth . providerFromCurrentUri = sinon . stub ( ) . returns ( providerUri )
236210
237211 return auth . selectProvider ( )
@@ -242,8 +216,6 @@ describe('SolidAuthOIDC', () => {
242216 } )
243217
244218 it ( 'should obtain provider from UI, if not present or cached' , ( ) => {
245- let auth = new SolidAuthOIDC ( )
246- let providerUri = 'https://provider.example.com'
247219 auth . providerFromCurrentUri = sinon . stub ( ) . returns ( null )
248220 auth . providerFromUI = sinon . stub ( ) . resolves ( providerUri )
249221
@@ -256,17 +228,11 @@ describe('SolidAuthOIDC', () => {
256228 } )
257229
258230 describe ( 'client persistence' , ( ) => {
259- let providerUri = 'https://provider.example.com'
260231 let clientConfig = { provider : { url : providerUri } }
261232 let mockClient = {
262233 provider : { url : providerUri } ,
263234 serialize : ( ) => { return clientConfig }
264235 }
265- var auth
266-
267- beforeEach ( ( ) => {
268- auth = new SolidAuthOIDC ( )
269- } )
270236
271237 describe ( 'loadClient()' , ( ) => {
272238 it ( 'should throw an error if no providerUri given' , ( ) => {
@@ -290,8 +256,6 @@ describe('SolidAuthOIDC', () => {
290256 } )
291257
292258 it ( 'should store and load serialized clients' , ( ) => {
293- let auth = new SolidAuthOIDC ( )
294-
295259 auth . storeClient ( mockClient , providerUri )
296260 // Storing a client should cache it in the auth client
297261 expect ( auth . currentClient ) . to . equal ( mockClient )
@@ -305,21 +269,18 @@ describe('SolidAuthOIDC', () => {
305269
306270 describe ( 'currentLocation()' , ( ) => {
307271 it ( 'should return the current window uri' , ( ) => {
272+ localStorage . clear ( )
273+
308274 let currentUri = 'https://client-app.example.com'
309- let auth = new SolidAuthOIDC ( { window : { location : { href : currentUri } } } )
275+ let auth = new SolidAuthOIDC ( {
276+ window : { location : { href : currentUri } } , store : localStorage
277+ } )
310278
311279 expect ( auth . currentLocation ( ) ) . to . equal ( currentUri )
312280 } )
313281 } )
314282
315283 describe ( 'validateOrSendAuthRequest()' , ( ) => {
316- var auth
317-
318- beforeEach ( ( ) => {
319- localStorage . clear ( )
320- auth = new SolidAuthOIDC ( { window : { location : { } } } )
321- } )
322-
323284 it ( 'should throw an error when no client is given' , ( ) => {
324285 expect ( auth . validateOrSendAuthRequest ( ) )
325286 . to . be . rejectedWith ( / C o u l d n o t l o a d o r r e g i s t e r a R e l y i n g P a r t y c l i e n t / )
@@ -353,13 +314,6 @@ describe('SolidAuthOIDC', () => {
353314 } )
354315
355316 describe ( 'initUserFromResponse()' , ( ) => {
356- var auth
357-
358- beforeEach ( ( ) => {
359- localStorage . clear ( )
360- auth = new SolidAuthOIDC ( { window : { location : { } } } )
361- } )
362-
363317 it ( 'should validate the auth response' , ( ) => {
364318 let aliceWebId = 'https://alice.example.com/'
365319 let authResponse = {
@@ -386,7 +340,6 @@ describe('SolidAuthOIDC', () => {
386340
387341 describe ( 'sendAuthRequest()' , ( ) => {
388342 it ( 'should compose an auth request uri, save provider, and redirect' , ( ) => {
389- let auth = new SolidAuthOIDC ( { window : { location : { } } } )
390343 let state = 'abcd'
391344 let providerUri = 'https://provider.example.com'
392345 let authUri = `https://provider.example.com/authorize?state=${ state } `
@@ -409,22 +362,18 @@ describe('SolidAuthOIDC', () => {
409362 describe ( 'currentUser()' , ( ) => {
410363 it ( 'should return cached webId if present' , ( ) => {
411364 let aliceWebId = 'https://alice.example.com'
412- let auth = new SolidAuthOIDC ( )
413365 auth . webId = aliceWebId
414366
415367 expect ( auth . currentUser ( ) ) . to . eventually . equal ( aliceWebId )
416368 } )
417369
418370 it ( 'should return null if no cached webId and no current state param' , ( ) => {
419- let auth = new SolidAuthOIDC ( { window : { location : { } } } )
420-
421371 expect ( auth . currentUser ( ) ) . to . eventually . not . exist ( )
422372 } )
423373
424374 it ( 'should automatically login if current uri has state param' , ( ) => {
425375 let state = 'abcd'
426376 let providerUri = 'https://provider.example.com'
427- let auth = new SolidAuthOIDC ( { window : { location : { } } } )
428377 auth . saveProviderByState ( state , providerUri )
429378
430379 auth . window . location . href = `https://client-app.example.com#state=${ state } `
@@ -441,12 +390,6 @@ describe('SolidAuthOIDC', () => {
441390 } )
442391
443392 describe ( 'providerEndSessionEndpoint()' , ( ) => {
444- let auth
445-
446- beforeEach ( ( ) => {
447- auth = new SolidAuthOIDC ( )
448- } )
449-
450393 it ( 'should return null if no current client' , ( ) => {
451394 auth . currentClient = null
452395
@@ -479,7 +422,7 @@ describe('SolidAuthOIDC', () => {
479422 expect ( url ) . to . equal ( null )
480423 } )
481424
482- it ( 'should return null if current provider end session endpoint' , ( ) => {
425+ it ( 'should return the provider end session endpoint' , ( ) => {
483426 auth . currentClient = {
484427 provider : {
485428 configuration : {
@@ -496,8 +439,6 @@ describe('SolidAuthOIDC', () => {
496439
497440 describe ( 'clearAuthResponseFromUrl()' , ( ) => {
498441 it ( 'should replace the current url with a no-hash cleared one' , ( ) => {
499- let auth = new SolidAuthOIDC ( )
500-
501442 let clearedUrl = 'https://rp.com'
502443
503444 auth . currentLocationNoHash = sinon . stub ( ) . returns ( clearedUrl )
0 commit comments