@@ -3,9 +3,19 @@ const Config = require("../lib/Config");
33const defaultColumns = require ( '../lib/Controllers/SchemaController' ) . defaultColumns ;
44const authenticationLoader = require ( '../lib/Adapters/Auth' ) ;
55const path = require ( 'path' ) ;
6+ const responses = {
7+ instagram : { data : { id : 'userId' } } ,
8+ janrainengage : { stat : 'ok' , profile : { identifier : 'userId' } } ,
9+ janraincapture : { stat : 'ok' , result : 'userId' } ,
10+ vkontakte : { response : { user_id : 'userId' } } ,
11+ google : { sub : 'userId' } ,
12+ wechat : { errcode : 0 } ,
13+ weibo : { uid : 'userId' } ,
14+ qq : 'callback( {"openid":"userId"} );' // yes it's like that, run eval in the client :P
15+ }
616
717describe ( 'AuthenticationProviders' , function ( ) {
8- [ "facebook" , "facebookaccountkit" , "github" , "instagram" , "google" , "linkedin" , "meetup" , "twitter" , "janrainengage" , "janraincapture" , "vkontakte" ] . map ( function ( providerName ) {
18+ [ "facebook" , "facebookaccountkit" , "github" , "instagram" , "google" , "linkedin" , "meetup" , "twitter" , "janrainengage" , "janraincapture" , "vkontakte" , "qq" , "spotify" , "wechat" , "weibo" ] . map ( function ( providerName ) {
919 it ( "Should validate structure of " + providerName , ( done ) => {
1020 const provider = require ( "../lib/Adapters/Auth/" + providerName ) ;
1121 jequal ( typeof provider . validateAuthData , "function" ) ;
@@ -18,6 +28,32 @@ describe('AuthenticationProviders', function() {
1828 validateAppIdPromise . then ( ( ) => { } , ( ) => { } ) ;
1929 done ( ) ;
2030 } ) ;
31+
32+ it ( `should provide the right responses for adapter ${ providerName } ` , async ( ) => {
33+ if ( providerName === 'twitter' ) {
34+ return ;
35+ }
36+ spyOn ( require ( '../lib/Adapters/Auth/httpsRequest' ) , 'get' ) . and . callFake ( ( options ) => {
37+ if ( options === "https://oauth.vk.com/access_token?client_id=appId&client_secret=appSecret&v=5.59&grant_type=client_credentials" ) {
38+ return {
39+ access_token : 'access_token'
40+ }
41+ }
42+ return Promise . resolve ( responses [ providerName ] || { id : 'userId' } ) ;
43+ } ) ;
44+ spyOn ( require ( '../lib/Adapters/Auth/httpsRequest' ) , 'request' ) . and . callFake ( ( ) => {
45+ return Promise . resolve ( responses [ providerName ] || { id : 'userId' } ) ;
46+ } ) ;
47+ const provider = require ( "../lib/Adapters/Auth/" + providerName ) ;
48+ let params = { } ;
49+ if ( providerName === 'vkontakte' ) {
50+ params = {
51+ appIds : 'appId' ,
52+ appSecret : 'appSecret'
53+ }
54+ }
55+ await provider . validateAuthData ( { id : 'userId' } , params ) ;
56+ } ) ;
2157 } ) ;
2258
2359 const getMockMyOauthProvider = function ( ) {
@@ -388,3 +424,60 @@ describe('AuthenticationProviders', function() {
388424 } )
389425 } ) ;
390426} ) ;
427+
428+ describe ( 'google auth adapter' , ( ) => {
429+ const google = require ( '../lib/Adapters/Auth/google' ) ;
430+ const httpsRequest = require ( '../lib/Adapters/Auth/httpsRequest' ) ;
431+
432+ it ( 'should use id_token for validation is passed' , async ( ) => {
433+ spyOn ( httpsRequest , 'request' ) . and . callFake ( ( ) => {
434+ return Promise . resolve ( { sub : 'userId' } ) ;
435+ } ) ;
436+ await google . validateAuthData ( { id : 'userId' , id_token : 'the_token' } , { } ) ;
437+ } ) ;
438+
439+ it ( 'should use id_token for validation is passed and responds with user_id' , async ( ) => {
440+ spyOn ( httpsRequest , 'request' ) . and . callFake ( ( ) => {
441+ return Promise . resolve ( { user_id : 'userId' } ) ;
442+ } ) ;
443+ await google . validateAuthData ( { id : 'userId' , id_token : 'the_token' } , { } ) ;
444+ } ) ;
445+
446+ it ( 'should use access_token for validation is passed and responds with user_id' , async ( ) => {
447+ spyOn ( httpsRequest , 'request' ) . and . callFake ( ( ) => {
448+ return Promise . resolve ( { user_id : 'userId' } ) ;
449+ } ) ;
450+ await google . validateAuthData ( { id : 'userId' , access_token : 'the_token' } , { } ) ;
451+ } ) ;
452+
453+ it ( 'should use access_token for validation is passed with sub' , async ( ) => {
454+ spyOn ( httpsRequest , 'request' ) . and . callFake ( ( ) => {
455+ return Promise . resolve ( { sub : 'userId' } ) ;
456+ } ) ;
457+ await google . validateAuthData ( { id : 'userId' , id_token : 'the_token' } , { } ) ;
458+ } ) ;
459+
460+ it ( 'should fail when the id_token is invalid' , async ( ) => {
461+ spyOn ( httpsRequest , 'request' ) . and . callFake ( ( ) => {
462+ return Promise . resolve ( { sub : 'badId' } ) ;
463+ } ) ;
464+ try {
465+ await google . validateAuthData ( { id : 'userId' , id_token : 'the_token' } , { } ) ;
466+ fail ( )
467+ } catch ( e ) {
468+ expect ( e . message ) . toBe ( 'Google auth is invalid for this user.' ) ;
469+ }
470+ } ) ;
471+
472+ it ( 'should fail when the access_token is invalid' , async ( ) => {
473+ spyOn ( httpsRequest , 'request' ) . and . callFake ( ( ) => {
474+ return Promise . resolve ( { sub : 'badId' } ) ;
475+ } ) ;
476+ try {
477+ await google . validateAuthData ( { id : 'userId' , access_token : 'the_token' } , { } ) ;
478+ fail ( )
479+ } catch ( e ) {
480+ expect ( e . message ) . toBe ( 'Google auth is invalid for this user.' ) ;
481+ }
482+ } ) ;
483+ } ) ;
0 commit comments