@@ -40,15 +40,65 @@ test('Create an instance', t => {
4040 'getFile' ,
4141 'upload' ,
4242 'setToken' ,
43- 'clearToken'
43+ 'clearToken' ,
44+ 'isBrowser'
4445 ]
4546 ) ;
4647
47- t . deepEqual ( Object . getOwnPropertyNames ( t . context . strapi ) , [ 'axios' ] ) ;
48+ t . deepEqual ( Object . getOwnPropertyNames ( t . context . strapi ) , [
49+ 'axios' ,
50+ 'storeConfig'
51+ ] ) ;
4852
4953 t . deepEqual ( t . context . strapi . axios . defaults . baseURL , 'http://strapi-host' ) ;
5054} ) ;
5155
56+ test . serial ( 'Create an instance with existing token on localStorage' , t => {
57+ browserEnv ( [ 'window' ] ) ;
58+ const globalAny : any = global ;
59+ globalAny . window . localStorage = storageMock ( ) ;
60+ const setItem = sinon . spy ( globalAny . window . localStorage , 'setItem' ) ;
61+ globalAny . window . localStorage . setItem ( 'jwt' , '"XXX"' ) ;
62+ const strapi = new Strapi ( 'http://strapi-host' , {
63+ cookie : false
64+ } ) ;
65+
66+ t . is ( strapi . axios . defaults . headers . common . Authorization , 'Bearer XXX' ) ;
67+ t . true ( setItem . calledWith ( 'jwt' , '"XXX"' ) ) ;
68+ delete strapi . axios . defaults . headers . common . Authorization ;
69+ delete globalAny . window ;
70+ } ) ;
71+
72+ test ( 'Create an instance with existing token on cookies' , t => {
73+ browserEnv ( [ 'window' , 'document' ] ) ;
74+ const Cookies = require ( 'js-cookie' ) ;
75+ const globalAny : any = global ;
76+ Cookies . set ( 'jwt' , 'XXX' ) ;
77+ // const CookieGet = sinon.spy(Cookies)
78+
79+ const strapi = new Strapi ( 'http://strapi-host' , {
80+ localStorage : false
81+ } ) ;
82+
83+ t . is ( strapi . axios . defaults . headers . common . Authorization , 'Bearer XXX' ) ;
84+ // TODO: Mock Cookies
85+ // t.true(CookieGet.calledWith('jwt'));
86+ delete strapi . axios . defaults . headers . common . Authorization ;
87+ delete globalAny . window ;
88+ } ) ;
89+
90+ test . serial ( 'Create an instance without token' , t => {
91+ browserEnv ( [ 'window' ] ) ;
92+ const globalAny : any = global ;
93+ const strapi = new Strapi ( 'http://strapi-host' , {
94+ cookie : false ,
95+ localStorage : false
96+ } ) ;
97+
98+ t . is ( strapi . axios . defaults . headers . common . Authorization , undefined ) ;
99+ delete globalAny . window ;
100+ } ) ;
101+
52102test ( 'Make a request' , async t => {
53103 t . context . axiosRequest . resolves ( {
54104 data : [ { foo : 'bar' } ]
@@ -243,19 +293,18 @@ test('Provider authentication on Node.js', async t => {
243293} ) ;
244294
245295test . serial ( 'Provider authentication on browser' , async t => {
246- const globalAny : any = global ;
247296 browserEnv ( [ 'window' ] , {
248297 url : 'http://localhost?access_token=XXX'
249298 } ) ;
299+ const globalAny : any = global ;
300+ globalAny . window . localStorage = storageMock ( ) ;
250301 t . context . axiosRequest . resolves ( {
251302 data : {
252303 jwt : 'foo' ,
253304 user : { }
254305 }
255306 } ) ;
256- const authentication = await t . context . strapi . authenticateProvider (
257- 'github'
258- ) ;
307+ const authentication = await t . context . strapi . authenticateProvider ( 'github' ) ;
259308
260309 t . true (
261310 t . context . axiosRequest . calledWithExactly ( {
@@ -401,3 +450,53 @@ test('Set token', t => {
401450 'Bearer foo'
402451 ) ;
403452} ) ;
453+
454+ test ( 'Set token on Node.js' , t => {
455+ browserEnv ( [ 'window' , 'document' ] ) ;
456+ // const Cookies = require('js-cookie');
457+ const globalAny : any = global ;
458+ globalAny . window . localStorage = storageMock ( ) ;
459+ const setItem = sinon . spy ( globalAny . window . localStorage , 'setItem' ) ;
460+ // const CookieSet = sinon.spy(Cookies, 'set')
461+
462+ const strapi = new Strapi ( 'http://strapi-host' , {
463+ cookie : false ,
464+ localStorage : false
465+ } ) ;
466+ strapi . setToken ( 'XXX' ) ;
467+
468+ t . is ( strapi . axios . defaults . headers . common . Authorization , 'Bearer XXX' ) ;
469+ t . true ( setItem . notCalled ) ;
470+ // t.true(CookieSet.notCalled)
471+ delete globalAny . window ;
472+ } ) ;
473+
474+ test ( 'Clear token without storage' , t => {
475+ browserEnv ( [ 'window' ] ) ;
476+ const globalAny : any = global ;
477+ globalAny . window . localStorage = storageMock ( ) ;
478+ const setItem = sinon . spy ( globalAny . window . localStorage , 'setItem' ) ;
479+ const strapi = new Strapi ( 'http://strapi-host' , {
480+ cookie : false ,
481+ localStorage : false
482+ } ) ;
483+ strapi . axios . defaults . headers . common . Authorization = 'Bearer XXX' ;
484+ strapi . clearToken ( ) ;
485+ t . true ( setItem . notCalled ) ;
486+ t . is ( strapi . axios . defaults . headers . common . Authorization , undefined ) ;
487+ } ) ;
488+
489+ function storageMock ( ) {
490+ const storage : any = { } ;
491+ return {
492+ setItem ( key : string , value : string ) {
493+ storage [ key ] = value || '' ;
494+ } ,
495+ getItem ( key : string ) {
496+ return key in storage ? storage [ key ] : null ;
497+ } ,
498+ removeItem ( key : string ) {
499+ delete storage [ key ] ;
500+ }
501+ } ;
502+ }
0 commit comments