@@ -13,6 +13,11 @@ var mocks = {
1313 }
1414 } ,
1515 UrlFetchApp : new MockUrlFetchApp ( ) ,
16+ Utilities : {
17+ base64Encode : function ( data ) {
18+ return Buffer . from ( data ) . toString ( 'base64' ) ;
19+ }
20+ } ,
1621 __proto__ : gas . globalMockDefault
1722} ;
1823var OAuth2 = gas . require ( './src' , mocks ) ;
@@ -236,6 +241,81 @@ describe('Service', function() {
236241 } ) ;
237242 } ) ;
238243 } ) ;
244+
245+ describe ( '#exchangeGrant_()' , function ( ) {
246+ var toLowerCaseKeys_ = OAuth2 . toLowerCaseKeys_ ;
247+
248+ it ( 'should not set auth header if the grant type is not client_credentials' ,
249+ function ( done ) {
250+ mocks . UrlFetchApp . resultFunction = function ( url , urlOptions ) {
251+ assert . isUndefined ( toLowerCaseKeys_ ( urlOptions . headers ) . authorization ) ;
252+ done ( ) ;
253+ } ;
254+ var service = OAuth2 . createService ( 'test' )
255+ . setGrantType ( 'fake' )
256+ . setTokenUrl ( 'http://www.example.com' ) ;
257+ service . exchangeGrant_ ( ) ;
258+ } ) ;
259+
260+ it ( 'should not set auth header if the client ID is not set' ,
261+ function ( done ) {
262+ mocks . UrlFetchApp . resultFunction = function ( url , urlOptions ) {
263+ assert . isUndefined ( toLowerCaseKeys_ ( urlOptions . headers ) . authorization ) ;
264+ done ( ) ;
265+ } ;
266+ var service = OAuth2 . createService ( 'test' )
267+ . setGrantType ( 'client_credentials' )
268+ . setTokenUrl ( 'http://www.example.com' ) ;
269+ service . exchangeGrant_ ( ) ;
270+ } ) ;
271+
272+ it ( 'should not set auth header if the client secret is not set' ,
273+ function ( done ) {
274+ mocks . UrlFetchApp . resultFunction = function ( url , urlOptions ) {
275+ assert . isUndefined ( toLowerCaseKeys_ ( urlOptions . headers ) . authorization ) ;
276+ done ( ) ;
277+ } ;
278+ var service = OAuth2 . createService ( 'test' )
279+ . setGrantType ( 'client_credentials' )
280+ . setTokenUrl ( 'http://www.example.com' )
281+ . setClientId ( 'abc' ) ;
282+ service . exchangeGrant_ ( ) ;
283+ } ) ;
284+
285+ it ( 'should not set auth header if it is already set' ,
286+ function ( done ) {
287+ mocks . UrlFetchApp . resultFunction = function ( url , urlOptions ) {
288+ assert . equal ( toLowerCaseKeys_ ( urlOptions . headers ) . authorization ,
289+ 'something' ) ;
290+ done ( ) ;
291+ } ;
292+ var service = OAuth2 . createService ( 'test' )
293+ . setGrantType ( 'client_credentials' )
294+ . setTokenUrl ( 'http://www.example.com' )
295+ . setClientId ( 'abc' )
296+ . setClientSecret ( 'def' )
297+ . setTokenHeaders ( {
298+ authorization : 'something'
299+ } ) ;
300+ service . exchangeGrant_ ( ) ;
301+ } ) ;
302+
303+ it ( 'should set the auth header for the client_credentials grant type, if ' +
304+ 'the client ID and client secret are set and the authorization header' +
305+ 'is not already set' , function ( done ) {
306+ mocks . UrlFetchApp . resultFunction = function ( url , urlOptions ) {
307+ assert . equal ( toLowerCaseKeys_ ( urlOptions . headers ) . authorization ,
308+ 'Basic YWJjOmRlZg==' ) ;
309+ done ( ) ;
310+ } ;
311+ var service = OAuth2 . createService ( 'test' )
312+ . setGrantType ( 'client_credentials' )
313+ . setTokenUrl ( 'http://www.example.com' )
314+ . setClientId ( 'abc' )
315+ . setClientSecret ( 'def' ) ;
316+ service . exchangeGrant_ ( ) ;
317+ } ) ;
318+ } ) ;
239319} ) ;
240320
241321describe ( 'Utilities' , function ( ) {
@@ -255,4 +335,33 @@ describe('Utilities', function() {
255335 assert . deepEqual ( o , { foo : [ 100 ] , bar : 2 , baz : { } } ) ;
256336 } ) ;
257337 } ) ;
338+
339+ describe ( '#toLowerCaseKeys_()' , function ( ) {
340+ var toLowerCaseKeys_ = OAuth2 . toLowerCaseKeys_ ;
341+
342+ it ( 'should contain only lower-case keys' , function ( ) {
343+ var data = {
344+ 'a' : true ,
345+ 'A' : true ,
346+ 'B' : true ,
347+ 'Cc' : true ,
348+ 'D2' : true ,
349+ 'E!@#' : true
350+ } ;
351+ var lowerCaseData = toLowerCaseKeys_ ( data ) ;
352+ assert . deepEqual ( lowerCaseData , {
353+ 'a' : true ,
354+ 'b' : true ,
355+ 'cc' : true ,
356+ 'd2' : true ,
357+ 'e!@#' : true
358+ } ) ;
359+ } ) ;
360+
361+ it ( 'should handle null, undefined, and empty objects' , function ( ) {
362+ assert . isNull ( toLowerCaseKeys_ ( null ) ) ;
363+ assert . isUndefined ( toLowerCaseKeys_ ( undefined ) ) ;
364+ assert . isEmpty ( toLowerCaseKeys_ ( { } ) ) ;
365+ } ) ;
366+ } ) ;
258367} ) ;
0 commit comments