@@ -160,6 +160,18 @@ describe('DropboxAuth', () => {
160160 } ) ;
161161 } ) ;
162162
163+ describe ( 'dataOnBody' , ( ) => {
164+ it ( 'can be set in the constructor' , ( ) => {
165+ const dbx = new DropboxAuth ( { dataOnBody : true } ) ;
166+ chai . assert . equal ( dbx . dataOnBody , true ) ;
167+ } ) ;
168+
169+ it ( 'is undefined if not set in constructor' , ( ) => {
170+ const dbx = new DropboxAuth ( ) ;
171+ chai . assert . equal ( dbx . dataOnBody , undefined ) ;
172+ } ) ;
173+ } ) ;
174+
163175 describe ( 'refreshToken' , ( ) => {
164176 it ( 'can be set in the constructor' , ( ) => {
165177 const dbxAuth = new DropboxAuth ( { refreshToken : 'foo' } ) ;
@@ -366,9 +378,7 @@ describe('DropboxAuth', () => {
366378 ) ;
367379 } ) ;
368380
369- const testRefreshUrl = 'https://api.dropboxapi.com/oauth2/token?grant_type=refresh_token&refresh_token=undefined&client_id=foo&client_secret=bar' ;
370-
371- it ( 'sets the correct refresh url (no scope passed)' , ( ) => {
381+ it ( 'sets request content type to json' , ( ) => {
372382 const dbxAuth = new DropboxAuth ( {
373383 clientId : 'foo' ,
374384 clientSecret : 'bar' ,
@@ -377,26 +387,97 @@ describe('DropboxAuth', () => {
377387 const fetchSpy = sinon . spy ( dbxAuth , 'fetch' ) ;
378388 dbxAuth . refreshAccessToken ( ) ;
379389 chai . assert . isTrue ( fetchSpy . calledOnce ) ;
380- const refreshUrl = dbxAuth . fetch . getCall ( 0 ) . args [ 0 ] ;
390+
381391 const { headers } = dbxAuth . fetch . getCall ( 0 ) . args [ 1 ] ;
382- chai . assert . equal ( refreshUrl , testRefreshUrl ) ;
383392 chai . assert . equal ( headers [ 'Content-Type' ] , 'application/json' ) ;
384393 } ) ;
385394
386- it ( 'sets the correct refresh url (scope passed)' , ( ) => {
387- const dbxAuth = new DropboxAuth ( {
388- clientId : 'foo' ,
389- clientSecret : 'bar' ,
395+ describe ( 'when dataOnBody flag is enabled' , ( ) => {
396+ const dataOnBody = true ;
397+
398+ it ( 'does the request without URL parameters' , ( ) => {
399+ const dbxAuth = new DropboxAuth ( {
400+ clientId : 'foo' ,
401+ clientSecret : 'bar' ,
402+ dataOnBody,
403+ } ) ;
404+
405+ const fetchSpy = sinon . spy ( dbxAuth , 'fetch' ) ;
406+ dbxAuth . refreshAccessToken ( [ 'files.metadata.read' ] ) ;
407+ chai . assert . isTrue ( fetchSpy . calledOnce ) ;
408+ const refreshUrl = dbxAuth . fetch . getCall ( 0 ) . args [ 0 ] ;
409+
410+ chai . assert . equal ( refreshUrl , 'https://api.dropboxapi.com/oauth2/token' ) ;
390411 } ) ;
391412
392- const fetchSpy = sinon . spy ( dbxAuth , 'fetch' ) ;
393- dbxAuth . refreshAccessToken ( [ 'files.metadata.read' ] ) ;
394- chai . assert . isTrue ( fetchSpy . calledOnce ) ;
395- const refreshUrl = dbxAuth . fetch . getCall ( 0 ) . args [ 0 ] ;
396- const { headers } = dbxAuth . fetch . getCall ( 0 ) . args [ 1 ] ;
397- const testScopeUrl = `${ testRefreshUrl } &scope=files.metadata.read` ;
398- chai . assert . equal ( refreshUrl , testScopeUrl ) ;
399- chai . assert . equal ( headers [ 'Content-Type' ] , 'application/json' ) ;
413+ it ( 'sends the client id and secret in the body' , ( ) => {
414+ const dbxAuth = new DropboxAuth ( {
415+ clientId : 'foo' ,
416+ clientSecret : 'bar' ,
417+ dataOnBody,
418+ } ) ;
419+
420+ const fetchSpy = sinon . spy ( dbxAuth , 'fetch' ) ;
421+ dbxAuth . refreshAccessToken ( ) ;
422+ chai . assert . isTrue ( fetchSpy . calledOnce ) ;
423+
424+ const { body } = dbxAuth . fetch . getCall ( 0 ) . args [ 1 ] ;
425+ chai . assert . equal ( body . client_id , 'foo' ) ;
426+ chai . assert . equal ( body . client_secret , 'bar' ) ;
427+ } ) ;
428+
429+ it ( 'sends the scope in the body when passed' , ( ) => {
430+ const dbxAuth = new DropboxAuth ( {
431+ clientId : 'foo' ,
432+ clientSecret : 'bar' ,
433+ dataOnBody,
434+ } ) ;
435+
436+ const fetchSpy = sinon . spy ( dbxAuth , 'fetch' ) ;
437+ dbxAuth . refreshAccessToken ( [ 'files.metadata.read' ] ) ;
438+ chai . assert . isTrue ( fetchSpy . calledOnce ) ;
439+
440+ const { body } = dbxAuth . fetch . getCall ( 0 ) . args [ 1 ] ;
441+ chai . assert . equal ( body . scope , 'files.metadata.read' ) ;
442+ } ) ;
443+ } ) ;
444+
445+ describe ( 'when dataOnBody flag is disabled' , ( ) => {
446+ const dataOnBody = false ;
447+ const testRefreshUrl = 'https://api.dropboxapi.com/oauth2/token?grant_type=refresh_token&refresh_token=undefined&client_id=foo&client_secret=bar' ;
448+
449+ it ( 'sets the correct refresh url (no scope passed)' , ( ) => {
450+ const dbxAuth = new DropboxAuth ( {
451+ clientId : 'foo' ,
452+ clientSecret : 'bar' ,
453+ dataOnBody,
454+ } ) ;
455+
456+ const fetchSpy = sinon . spy ( dbxAuth , 'fetch' ) ;
457+ dbxAuth . refreshAccessToken ( ) ;
458+ chai . assert . isTrue ( fetchSpy . calledOnce ) ;
459+ const refreshUrl = dbxAuth . fetch . getCall ( 0 ) . args [ 0 ] ;
460+ const { headers } = dbxAuth . fetch . getCall ( 0 ) . args [ 1 ] ;
461+ chai . assert . equal ( refreshUrl , testRefreshUrl ) ;
462+ chai . assert . equal ( headers [ 'Content-Type' ] , 'application/json' ) ;
463+ } ) ;
464+
465+ it ( 'sets the correct refresh url (scope passed)' , ( ) => {
466+ const dbxAuth = new DropboxAuth ( {
467+ clientId : 'foo' ,
468+ clientSecret : 'bar' ,
469+ dataOnBody,
470+ } ) ;
471+
472+ const fetchSpy = sinon . spy ( dbxAuth , 'fetch' ) ;
473+ dbxAuth . refreshAccessToken ( [ 'files.metadata.read' ] ) ;
474+ chai . assert . isTrue ( fetchSpy . calledOnce ) ;
475+ const refreshUrl = dbxAuth . fetch . getCall ( 0 ) . args [ 0 ] ;
476+ const { headers } = dbxAuth . fetch . getCall ( 0 ) . args [ 1 ] ;
477+ const testScopeUrl = `${ testRefreshUrl } &scope=files.metadata.read` ;
478+ chai . assert . equal ( refreshUrl , testScopeUrl ) ;
479+ chai . assert . equal ( headers [ 'Content-Type' ] , 'application/json' ) ;
480+ } ) ;
400481 } ) ;
401482 } ) ;
402483} ) ;
0 commit comments