@@ -96,51 +96,6 @@ describe('Dropbox', () => {
9696 chai . assert . deepEqual ( { } , dbx . rpcRequest . getCall ( 0 ) . args [ 1 ] ) ;
9797 } ) ;
9898
99- it ( 'completes a multiauth RPC request with user auth when supplied with an accessToken' , ( ) => {
100- const dbxAuth = new DropboxAuth ( { accessToken : 'foo' } ) ;
101- const dbx = new Dropbox ( { auth : dbxAuth } ) ;
102- const rpcSpy = sinon . spy ( dbx , 'rpcRequest' ) ;
103- dbx . request ( 'path' , { } , 'user, app' , 'api' , RPC )
104- . catch ( ( error ) => {
105- fail ( error ) ;
106- } ) ;
107- chai . assert . isTrue ( rpcSpy . calledOnce ) ;
108- chai . assert . equal ( 'path' , dbx . rpcRequest . getCall ( 0 ) . args [ 0 ] ) ;
109- chai . assert . deepEqual ( { } , dbx . rpcRequest . getCall ( 0 ) . args [ 1 ] ) ;
110- chai . assert . equal ( USER_AUTH , dbx . rpcRequest . getCall ( 0 ) . args [ 2 ] ) ;
111- } ) ;
112-
113- it ( 'completes a multiauth RPC request with team auth when supplied with an accessToken' , ( ) => {
114- const dbxAuth = new DropboxAuth ( { accessToken : 'foo' } ) ;
115- const dbx = new Dropbox ( { auth : dbxAuth } ) ;
116- const rpcSpy = sinon . spy ( dbx , 'rpcRequest' ) ;
117- dbx . request ( 'path' , { } , 'team, app' , 'api' , RPC )
118- . catch ( ( error ) => {
119- fail ( error ) ;
120- } ) ;
121- chai . assert . isTrue ( rpcSpy . calledOnce ) ;
122- chai . assert . equal ( 'path' , dbx . rpcRequest . getCall ( 0 ) . args [ 0 ] ) ;
123- chai . assert . deepEqual ( { } , dbx . rpcRequest . getCall ( 0 ) . args [ 1 ] ) ;
124- chai . assert . equal ( TEAM_AUTH , dbx . rpcRequest . getCall ( 0 ) . args [ 2 ] ) ;
125- } ) ;
126-
127- it ( 'completes a multiauth RPC request with app auth when not supplied with an accessToken' , ( ) => {
128- const dbxAuth = new DropboxAuth ( {
129- clientID : 'foo' ,
130- clientSecret : 'bar' ,
131- } ) ;
132- const dbx = new Dropbox ( { auth : dbxAuth } ) ;
133- const rpcSpy = sinon . spy ( dbx , 'rpcRequest' ) ;
134- dbx . request ( 'path' , { } , 'user, app' , 'api' , RPC )
135- . catch ( ( error ) => {
136- fail ( error ) ;
137- } ) ;
138- chai . assert . isTrue ( rpcSpy . calledOnce ) ;
139- chai . assert . equal ( 'path' , dbx . rpcRequest . getCall ( 0 ) . args [ 0 ] ) ;
140- chai . assert . deepEqual ( { } , dbx . rpcRequest . getCall ( 0 ) . args [ 1 ] ) ;
141- chai . assert . equal ( APP_AUTH , dbx . rpcRequest . getCall ( 0 ) . args [ 2 ] ) ;
142- } ) ;
143-
14499 it ( 'completes a cookie auth RPC request' , ( ) => {
145100 const dbxAuth = new DropboxAuth ( ) ;
146101 const dbx = new Dropbox ( { auth : dbxAuth } ) ;
@@ -248,4 +203,52 @@ describe('Dropbox', () => {
248203 chai . assert . equal ( headers . cookie , 'hash' ) ;
249204 } ) ;
250205 } ) ;
206+
207+ describe ( 'setAuthHeaders' , ( ) => {
208+ const authTypes = [ 'user' , 'app' , 'team' , 'noauth' , 'user, app' , 'team, app' , 'cookie' ] ;
209+ for ( const auth of authTypes ) {
210+ for ( const hasAccessToken of [ true , false ] ) {
211+ for ( const hasAppKeys of [ true , false ] ) {
212+ it ( `correctly sets auth headers given '${ auth } ' auth and ${ hasAccessToken ? 'has' : 'does not have' } an access token` , ( ) => {
213+ const dbx = new Dropbox ( {
214+ accessToken : hasAccessToken ? 'token' : undefined ,
215+ clientId : hasAppKeys ? 'app_key' : undefined ,
216+ clientSecret : hasAppKeys ? 'app_secret' : undefined ,
217+ } ) ;
218+
219+ const fetchOptions = {
220+ headers : { } ,
221+ } ;
222+
223+ const isExpectedToHaveTokenHeader = hasAccessToken && ( auth . includes ( 'user' ) || auth . includes ( 'team' ) ) ;
224+ const isExpectedToHaveAppHeader = ( ( auth === 'app' ) || ( auth . includes ( 'app' ) && ! hasAccessToken ) ) && hasAppKeys ;
225+
226+ dbx . setAuthHeaders ( auth , fetchOptions ) ;
227+
228+ const { headers } = fetchOptions ;
229+ if ( isExpectedToHaveAppHeader ) {
230+ chai . assert . isTrue ( headers . Authorization . includes ( 'Basic' ) ) ;
231+ } else if ( isExpectedToHaveTokenHeader ) {
232+ chai . assert . isTrue ( headers . Authorization . includes ( 'Bearer' ) ) ;
233+ } else {
234+ chai . assert . deepEqual ( headers , { } ) ;
235+ }
236+ } ) ;
237+ }
238+ }
239+ }
240+
241+ it ( 'throws an error on an invalid auth type' , ( ) => {
242+ const dbx = new Dropbox ( ) ;
243+
244+ const fetchOptions = {
245+ headers : { } ,
246+ } ;
247+
248+ chai . assert . throws (
249+ Dropbox . prototype . setAuthHeaders . bind ( Dropbox , 'bad auth type' , fetchOptions ) ,
250+ Error ,
251+ ) ;
252+ } ) ;
253+ } ) ;
251254} ) ;
0 commit comments