@@ -6,7 +6,7 @@ const passport = require("passport");
66const app = require ( "../../server" ) ;
77const cleanDb = require ( "../utils/cleanDb" ) ;
88const { generateGithubAuthRedirectUrl } = require ( "..//utils/github" ) ;
9- const { generateGoogleAuthRedirectUrl } = require ( "..//utils/googleauth" ) ;
9+ const { generateGoogleAuthRedirectUrl, stubPassportAuthenticate } = require ( "..//utils/googleauth" ) ;
1010const { addUserToDBForTest } = require ( "../../utils/users" ) ;
1111const userData = require ( "../fixtures/user/user" ) ( ) ;
1212
@@ -308,11 +308,7 @@ describe("auth", function () {
308308
309309 it ( "should redirect the google user to new sign up flow if they are have incomplete user details true" , async function ( ) {
310310 const redirectURL = "https://my.realdevsquad.com/new-signup" ;
311-
312- sinon . stub ( passport , "authenticate" ) . callsFake ( ( strategy , options , callback ) => {
313- callback ( null , "accessToken" , googleUserInfo [ 0 ] ) ;
314- return ( req , res , next ) => { } ;
315- } ) ;
311+ stubPassportAuthenticate ( googleUserInfo [ 0 ] ) ;
316312
317313 const res = await chai
318314 . request ( app )
@@ -326,10 +322,7 @@ describe("auth", function () {
326322 it ( "should redirect the google user to the goto page on successful login, if user has incomplete user details false" , async function ( ) {
327323 await addUserToDBForTest ( googleUserInfo [ 1 ] ) ;
328324 const rdsUiUrl = new URL ( config . get ( "services.rdsUi.baseUrl" ) ) . href ;
329- sinon . stub ( passport , "authenticate" ) . callsFake ( ( strategy , options , callback ) => {
330- callback ( null , "accessToken" , googleUserInfo [ 0 ] ) ;
331- return ( req , res , next ) => { } ;
332- } ) ;
325+ stubPassportAuthenticate ( googleUserInfo [ 0 ] ) ;
333326
334327 const res = await chai
335328 . request ( app )
@@ -343,11 +336,7 @@ describe("auth", function () {
343336 it ( "should redirect the google user to the redirect URL provided on successful login, if user has incomplete user details false" , async function ( ) {
344337 await addUserToDBForTest ( googleUserInfo [ 1 ] ) ;
345338 const rdsUrl = new URL ( "https://dashboard.realdevsquad.com" ) . href ;
346- sinon . stub ( passport , "authenticate" ) . callsFake ( ( strategy , options , callback ) => {
347- callback ( null , "accessToken" , googleUserInfo [ 0 ] ) ;
348- return ( req , res , next ) => { } ;
349- } ) ;
350-
339+ stubPassportAuthenticate ( googleUserInfo [ 0 ] ) ;
351340 const res = await chai
352341 . request ( app )
353342 . get ( `/auth/google/callback` )
@@ -361,10 +350,7 @@ describe("auth", function () {
361350 await addUserToDBForTest ( googleUserInfo [ 1 ] ) ;
362351 const invalidRedirectUrl = new URL ( "https://google.com" ) . href ;
363352 const rdsUiUrl = new URL ( config . get ( "services.rdsUi.baseUrl" ) ) . href ;
364- sinon . stub ( passport , "authenticate" ) . callsFake ( ( strategy , options , callback ) => {
365- callback ( null , "accessToken" , googleUserInfo [ 0 ] ) ;
366- return ( req , res , next ) => { } ;
367- } ) ;
353+ stubPassportAuthenticate ( googleUserInfo [ 0 ] ) ;
368354
369355 const res = await chai
370356 . request ( app )
@@ -379,11 +365,7 @@ describe("auth", function () {
379365 await addUserToDBForTest ( googleUserInfo [ 1 ] ) ;
380366 const invalidRedirectUrl = "invalidURL" ;
381367 const rdsUiUrl = new URL ( config . get ( "services.rdsUi.baseUrl" ) ) . href ;
382- sinon . stub ( passport , "authenticate" ) . callsFake ( ( strategy , options , callback ) => {
383- callback ( null , "accessToken" , googleUserInfo [ 0 ] ) ;
384- return ( req , res , next ) => { } ;
385- } ) ;
386-
368+ stubPassportAuthenticate ( googleUserInfo [ 0 ] ) ;
387369 const res = await chai
388370 . request ( app )
389371 . get ( `/auth/google/callback` )
@@ -393,13 +375,10 @@ describe("auth", function () {
393375 expect ( res . headers . location ) . to . equal ( rdsUiUrl ) ;
394376 } ) ;
395377
396- it ( "should send a cookie with JWT in the response for google user " , function ( done ) {
378+ it ( "should issue JWT cookie on using google login " , function ( done ) {
397379 const rdsUiUrl = new URL ( config . get ( "services.rdsUi.baseUrl" ) ) ;
398380
399- sinon . stub ( passport , "authenticate" ) . callsFake ( ( strategy , options , callback ) => {
400- callback ( null , "accessToken" , googleUserInfo [ 0 ] ) ;
401- return ( req , res , next ) => { } ;
402- } ) ;
381+ stubPassportAuthenticate ( googleUserInfo [ 0 ] ) ;
403382
404383 chai
405384 . request ( app )
@@ -428,10 +407,7 @@ describe("auth", function () {
428407 it ( "should redirect the google user to login page if the user is a developer" , async function ( ) {
429408 await addUserToDBForTest ( googleUserInfo [ 3 ] ) ;
430409 const rdsUiUrl = new URL ( config . get ( "services.rdsUi.baseUrl" ) ) . href ;
431- sinon . stub ( passport , "authenticate" ) . callsFake ( ( strategy , options , callback ) => {
432- callback ( null , "accessToken" , googleUserInfo [ 2 ] ) ;
433- return ( req , res , next ) => { } ;
434- } ) ;
410+ stubPassportAuthenticate ( googleUserInfo [ 2 ] ) ;
435411
436412 const res = await chai
437413 . request ( app )
@@ -444,17 +420,35 @@ describe("auth", function () {
444420 expect ( res . headers . location ) . to . equal ( expectedUrl ) ;
445421 } ) ;
446422
447- it ( "should recognize existing user by email when logging in via different OAuth provider" , async function ( ) {
423+ it ( "should log in existing google user with same email via github OAuth" , async function ( ) {
424+ await addUserToDBForTest ( googleUserInfo [ 1 ] ) ;
425+ const rdsUiUrl = new URL ( config . get ( "services.rdsUi.baseUrl" ) ) . href ;
426+ const userInfoFromGitHub = {
427+ ...githubUserInfo [ 0 ] ,
428+ _json : {
429+ ...githubUserInfo [ 0 ] . _json ,
430+ 431+ } ,
432+ } ;
433+ stubPassportAuthenticate ( userInfoFromGitHub ) ;
434+
435+ const res = await chai
436+ . request ( app )
437+ . get ( "/auth/github/callback" )
438+ . query ( { code : "codeReturnedByGithub" , state : rdsUiUrl } )
439+ . redirects ( 0 ) ;
440+ expect ( res ) . to . have . status ( 302 ) ;
441+ expect ( res . headers . location ) . to . equal ( rdsUiUrl ) ;
442+ } ) ;
443+
444+ it ( "should log in existing github user with same email via google OAuth" , async function ( ) {
448445 await addUserToDBForTest ( userData [ 0 ] ) ;
449446 const rdsUiUrl = new URL ( config . get ( "services.rdsUi.baseUrl" ) ) . href ;
450- sinon . stub ( passport , "authenticate" ) . callsFake ( ( strategy , options , callback ) => {
451- const userInfoFromGoogle = {
452- ...googleUserInfo [ 0 ] ,
453- emails :
[ { value :
"[email protected] " , verified :
true } ] , 454- } ;
455- callback ( null , "accessToken" , userInfoFromGoogle ) ;
456- return ( req , res , next ) => { } ;
457- } ) ;
447+ const userInfoFromGoogle = {
448+ ...googleUserInfo [ 0 ] ,
449+ emails :
[ { value :
"[email protected] " , verified :
true } ] , 450+ } ;
451+ stubPassportAuthenticate ( userInfoFromGoogle ) ;
458452
459453 const res = await chai
460454 . request ( app )
@@ -468,17 +462,14 @@ describe("auth", function () {
468462 it ( "should get the verified email and redirect the google user to the goto page on successful login" , async function ( ) {
469463 await addUserToDBForTest ( googleUserInfo [ 1 ] ) ;
470464 const rdsUiUrl = new URL ( config . get ( "services.rdsUi.baseUrl" ) ) . href ;
471- sinon . stub ( passport , "authenticate" ) . callsFake ( ( strategy , options , callback ) => {
472- const googleUser = {
473- ...googleUserInfo [ 0 ] ,
474- emails : [
475- { value :
"[email protected] " , verified :
false } , 476- { value :
"[email protected] " , verified :
true } , 477- ] ,
478- } ;
479- callback ( null , "accessToken" , googleUser ) ;
480- return ( req , res , next ) => { } ;
481- } ) ;
465+ const googleUser = {
466+ ...googleUserInfo [ 0 ] ,
467+ emails : [
468+ { value :
"[email protected] " , verified :
false } , 469+ { value :
"[email protected] " , verified :
true } , 470+ ] ,
471+ } ;
472+ stubPassportAuthenticate ( googleUser ) ;
482473
483474 const res = await chai
484475 . request ( app )
@@ -489,16 +480,21 @@ describe("auth", function () {
489480 expect ( res . headers . location ) . to . equal ( rdsUiUrl ) ;
490481 } ) ;
491482
483+ it ( "should return 404 if dev feature flag is not enabled" , async function ( ) {
484+ const res = await chai . request ( app ) . get ( "/auth/google/login" ) ;
485+
486+ expect ( res ) . to . have . status ( 404 ) ;
487+ expect ( res . body . message ) . to . equal ( "Route not found" ) ;
488+ } ) ;
489+
492490 it ( "should return 401 if google email does not exist" , async function ( ) {
493491 const rdsUiUrl = new URL ( config . get ( "services.rdsUi.baseUrl" ) ) . href ;
494- sinon . stub ( passport , "authenticate" ) . callsFake ( ( strategy , options , callback ) => {
495- const userInfoWithoutEmail = {
496- ...googleUserInfo [ 0 ] ,
497- emails : [ ] ,
498- } ;
499- callback ( null , "accessToken" , userInfoWithoutEmail ) ;
500- return ( req , res , next ) => { } ;
501- } ) ;
492+ const userInfoWithoutEmail = {
493+ ...googleUserInfo [ 0 ] ,
494+ emails : [ ] ,
495+ } ;
496+
497+ stubPassportAuthenticate ( userInfoWithoutEmail ) ;
502498
503499 const res = await chai
504500 . request ( app )
@@ -512,15 +508,11 @@ describe("auth", function () {
512508
513509 it ( "should return 401 if no verified email exists" , async function ( ) {
514510 const rdsUiUrl = new URL ( config . get ( "services.rdsUi.baseUrl" ) ) . href ;
515- sinon . stub ( passport , "authenticate" ) . callsFake ( ( strategy , options , callback ) => {
516- const userInfoWithUnverifiedEmail = {
517- ...googleUserInfo [ 0 ] ,
518- emails :
[ { value :
"[email protected] " , verified :
false } ] , 519- } ;
520- callback ( null , "accessToken" , userInfoWithUnverifiedEmail ) ;
521- return ( req , res , next ) => { } ;
522- } ) ;
523-
511+ const userInfoWithUnverifiedEmail = {
512+ ...googleUserInfo [ 0 ] ,
513+ emails :
[ { value :
"[email protected] " , verified :
false } ] , 514+ } ;
515+ stubPassportAuthenticate ( userInfoWithUnverifiedEmail ) ;
524516 const res = await chai
525517 . request ( app )
526518 . get ( "/auth/google/callback" )
0 commit comments