@@ -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,82 @@ describe('Service', function() {
236241 } ) ;
237242 } ) ;
238243 } ) ;
244+
245+ describe ( '#exchangeGrant_()' , function ( ) {
246+ var getValueCaseInsensitive_ = OAuth2 . getValueCaseInsensitive_ ;
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 (
252+ getValueCaseInsensitive_ ( urlOptions . headers , 'Authorization' ) ) ;
253+ done ( ) ;
254+ } ;
255+ var service = OAuth2 . createService ( 'test' )
256+ . setGrantType ( 'fake' )
257+ . setTokenUrl ( 'http://www.example.com' ) ;
258+ service . exchangeGrant_ ( ) ;
259+ } ) ;
260+
261+ it ( 'should not set auth header if the client ID is not set' ,
262+ function ( done ) {
263+ mocks . UrlFetchApp . resultFunction = function ( url , urlOptions ) {
264+ assert . isUndefined (
265+ getValueCaseInsensitive_ ( urlOptions . headers , 'Authorization' ) ) ;
266+ done ( ) ;
267+ } ;
268+ var service = OAuth2 . createService ( 'test' )
269+ . setGrantType ( 'client_credentials' )
270+ . setTokenUrl ( 'http://www.example.com' ) ;
271+ service . exchangeGrant_ ( ) ;
272+ } ) ;
273+
274+ it ( 'should not set auth header if the client secret is not set' ,
275+ function ( done ) {
276+ mocks . UrlFetchApp . resultFunction = function ( url , urlOptions ) {
277+ assert . isUndefined (
278+ getValueCaseInsensitive_ ( urlOptions . headers , 'Authorization' ) ) ;
279+ done ( ) ;
280+ } ;
281+ var service = OAuth2 . createService ( 'test' )
282+ . setGrantType ( 'client_credentials' )
283+ . setTokenUrl ( 'http://www.example.com' )
284+ . setClientId ( 'abc' ) ;
285+ service . exchangeGrant_ ( ) ;
286+ } ) ;
287+
288+ it ( 'should not set auth header if it is already set' ,
289+ function ( done ) {
290+ mocks . UrlFetchApp . resultFunction = function ( url , urlOptions ) {
291+ assert . equal ( urlOptions . headers . Authorization , 'something' ) ;
292+ done ( ) ;
293+ } ;
294+ var service = OAuth2 . createService ( 'test' )
295+ . setGrantType ( 'client_credentials' )
296+ . setTokenUrl ( 'http://www.example.com' )
297+ . setClientId ( 'abc' )
298+ . setClientSecret ( 'def' )
299+ . setTokenHeaders ( {
300+ Authorization : 'something'
301+ } ) ;
302+ service . exchangeGrant_ ( ) ;
303+ } ) ;
304+
305+ it ( 'should set the auth header for the client_credentials grant type, if ' +
306+ 'the client ID and client secret are set and the authorization header' +
307+ 'is not already set' , function ( done ) {
308+ mocks . UrlFetchApp . resultFunction = function ( url , urlOptions ) {
309+ assert . equal ( urlOptions . headers . Authorization , 'Basic YWJjOmRlZg==' ) ;
310+ done ( ) ;
311+ } ;
312+ var service = OAuth2 . createService ( 'test' )
313+ . setGrantType ( 'client_credentials' )
314+ . setTokenUrl ( 'http://www.example.com' )
315+ . setClientId ( 'abc' )
316+ . setClientSecret ( 'def' ) ;
317+ service . exchangeGrant_ ( ) ;
318+ } ) ;
319+ } ) ;
239320} ) ;
240321
241322describe ( 'Utilities' , function ( ) {
@@ -255,4 +336,35 @@ describe('Utilities', function() {
255336 assert . deepEqual ( o , { foo : [ 100 ] , bar : 2 , baz : { } } ) ;
256337 } ) ;
257338 } ) ;
339+
340+ describe ( '#getValueCaseInsensitive_()' , function ( ) {
341+ var getValueCaseInsensitive_ = OAuth2 . getValueCaseInsensitive_ ;
342+
343+ it ( 'should find identical keys' , function ( ) {
344+ assert . isTrue ( getValueCaseInsensitive_ ( { 'a' : true } , 'a' ) ) ;
345+ assert . isTrue ( getValueCaseInsensitive_ ( { 'A' : true } , 'A' ) ) ;
346+ assert . isTrue ( getValueCaseInsensitive_ ( { 'Ab' : true } , 'Ab' ) ) ;
347+ } ) ;
348+
349+ it ( 'should find matching keys of different cases' , function ( ) {
350+ assert . isTrue ( getValueCaseInsensitive_ ( { 'a' : true } , 'A' ) ) ;
351+ assert . isTrue ( getValueCaseInsensitive_ ( { 'A' : true } , 'a' ) ) ;
352+ assert . isTrue ( getValueCaseInsensitive_ ( { 'Ab' : true } , 'aB' ) ) ;
353+ assert . isTrue ( getValueCaseInsensitive_ ( { 'a2' : true } , 'A2' ) ) ;
354+ } ) ;
355+
356+ it ( 'should work with non-alphabetic keys' , function ( ) {
357+ assert . isTrue ( getValueCaseInsensitive_ ( { 'A2' : true } , 'a2' ) ) ;
358+ assert . isTrue ( getValueCaseInsensitive_ ( { '2' : true } , '2' ) ) ;
359+ assert . isTrue ( getValueCaseInsensitive_ ( { 2 : true } , 2 ) ) ;
360+ assert . isTrue ( getValueCaseInsensitive_ ( { '!@#' : true } , '!@#' ) ) ;
361+ } ) ;
362+
363+ it ( 'should work null and undefined' , function ( ) {
364+ assert . isUndefined ( getValueCaseInsensitive_ ( null , 'key' ) ) ;
365+ assert . isUndefined ( getValueCaseInsensitive_ ( undefined , 'key' ) ) ;
366+ assert . isUndefined ( getValueCaseInsensitive_ ( { 'a' : true } , null ) ) ;
367+ assert . isUndefined ( getValueCaseInsensitive_ ( { 'a' : true } , undefined ) ) ;
368+ } ) ;
369+ } ) ;
258370} ) ;
0 commit comments