From e190463539e03c48598e0ad2aca45ec4326effd9 Mon Sep 17 00:00:00 2001 From: Raghunandan Date: Fri, 22 May 2020 16:01:35 +0200 Subject: [PATCH] update tests --- .../src/providers/google/provider.test.ts | 42 ++++++------------- .../src/providers/google/provider.ts | 4 +- plugins/auth-backend/src/setupTests.ts | 2 - 3 files changed, 14 insertions(+), 34 deletions(-) diff --git a/plugins/auth-backend/src/providers/google/provider.test.ts b/plugins/auth-backend/src/providers/google/provider.test.ts index e9b6243fa1..4881b1dd93 100644 --- a/plugins/auth-backend/src/providers/google/provider.test.ts +++ b/plugins/auth-backend/src/providers/google/provider.test.ts @@ -18,7 +18,7 @@ import { GoogleAuthProvider } from './provider'; import passport from 'passport'; import express from 'express'; import * as passportGoogleAuth20 from 'passport-google-oauth20'; -// import * as utils from './../utils'; +import * as utils from './../utils'; const googleAuthProviderConfig = { provider: 'google', @@ -76,7 +76,7 @@ describe('GoogleAuthProvider', () => { it('should initiate authenticate request with provided scopes', () => { const mockRequest = ({ query: { - scopes: 'a,b', + scope: 'a,b', }, } as unknown) as express.Request; @@ -90,7 +90,7 @@ describe('GoogleAuthProvider', () => { googleAuthProvider.start(mockRequest, mockResponse, mockNext); expect(spyPassport).toBeCalledTimes(1); expect(spyPassport).toBeCalledWith('google', { - scope: ['a', 'b'], + scope: 'a,b', accessType: 'offline', prompt: 'consent', }); @@ -106,7 +106,7 @@ describe('GoogleAuthProvider', () => { ); expect(() => { googleAuthProvider.start(mockRequest, mockResponse, mockNext); - }).toThrowError('Scopes should be specified'); + }).toThrowError('missing scope parameter'); }); }); @@ -140,10 +140,9 @@ describe('GoogleAuthProvider', () => { const mockNext: express.NextFunction = jest.fn(); it('should call authenticate and post a response ', () => { - // TODO: Unable to verify if post message is being called - // const spyPostMessage = jest - // .spyOn(utils, 'postMessageResponse') - // .mockImplementation(() => jest.fn()); + const spyPostMessage = jest + .spyOn(utils, 'postMessageResponse') + .mockImplementation(() => jest.fn()); const spyPassport = jest .spyOn(passport, 'authenticate') @@ -154,8 +153,10 @@ describe('GoogleAuthProvider', () => { ); googleAuthProvider.frameHandler(mockRequest, mockResponse, mockNext); + const callbackFunc = spyPassport.mock.calls[0][1] as Function; + callbackFunc(); expect(spyPassport).toBeCalledTimes(1); - // expect(spyPostMessage).toBeCalledTimes(1); + expect(spyPostMessage).toBeCalledTimes(1); }); }); @@ -165,35 +166,16 @@ describe('GoogleAuthProvider', () => { googleAuthProviderConfig, ); - // TODO: how to test the callback function? expect(googleAuthProvider.strategy()).toBeInstanceOf(passport.Strategy); }); - it('should return a valid passport strategy if secrets in env', () => { - const googleAuthProvider = new GoogleAuthProvider( - googleAuthProviderConfigFromEnv, - ); - - expect(googleAuthProvider.strategy()).toBeInstanceOf(passport.Strategy); - }); - - // TODO: The two tests below should throw errors because either - // the options are missing in env or not present at all - // but they aren't throwing errors - it('should throw an error if secrets missing in env', () => { - expect(() => { - const googleAuthProvider = new GoogleAuthProvider( - googleAuthProviderConfigMissingInEnv, - ); - }); - }); - it('should throw an error for invalid options', () => { expect(() => { const googleAuthProvider = new GoogleAuthProvider( googleAuthProviderConfigInvalidOptions, ); - }); + googleAuthProvider.strategy(); + }).toThrow(); }); }); }); diff --git a/plugins/auth-backend/src/providers/google/provider.ts b/plugins/auth-backend/src/providers/google/provider.ts index a6b8b0ebec..7f3ae85089 100644 --- a/plugins/auth-backend/src/providers/google/provider.ts +++ b/plugins/auth-backend/src/providers/google/provider.ts @@ -77,9 +77,9 @@ export class GoogleAuthProvider refreshToken: any, params: any, profile: any, - cb: any, + done: any, ) => { - cb(undefined, { + done(undefined, { profile, idToken: params.id_token, accessToken, diff --git a/plugins/auth-backend/src/setupTests.ts b/plugins/auth-backend/src/setupTests.ts index 07f6384b3b..3fa7cb04b4 100644 --- a/plugins/auth-backend/src/setupTests.ts +++ b/plugins/auth-backend/src/setupTests.ts @@ -15,5 +15,3 @@ */ require('jest-fetch-mock').enableMocks(); -process.env.AUTH_GOOGLE_CLIENT_ID = 'a'; -process.env.AUTH_GOOGLE_CLIENT_SECRET = 'b';