update tests

This commit is contained in:
Raghunandan
2020-05-22 16:01:35 +02:00
parent 64c743edde
commit e190463539
3 changed files with 14 additions and 34 deletions
@@ -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();
});
});
});
@@ -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,
-2
View File
@@ -15,5 +15,3 @@
*/
require('jest-fetch-mock').enableMocks();
process.env.AUTH_GOOGLE_CLIENT_ID = 'a';
process.env.AUTH_GOOGLE_CLIENT_SECRET = 'b';