tests for makeProvider

This commit is contained in:
Raghunandan
2020-05-20 11:27:35 +02:00
parent 99a360bdef
commit 2c6b883006
4 changed files with 111 additions and 8 deletions
@@ -0,0 +1,22 @@
/*
* Copyright 2020 Spotify AB
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { AuthProviderFactories } from './types';
import { GoogleAuthProvider } from './google/provider';
export const providerFactories: AuthProviderFactories = {
google: GoogleAuthProvider,
};
@@ -0,0 +1,80 @@
/*
* Copyright 2020 Spotify AB
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import passport from 'passport';
import express from 'express';
import { makeProvider } from '.';
import { AuthProvider, AuthProviderRouteHandlers } from './types';
class MyAuthProvider implements AuthProvider, AuthProviderRouteHandlers {
strategy(): passport.Strategy {
return new passport.Strategy();
}
start(
req: express.Request,
res: express.Response,
next: express.NextFunction,
): Promise<any> {
return new Promise((res, rej) => res());
}
frameHandler(
req: express.Request,
res: express.Response,
next: express.NextFunction,
): express.Response<any> {
return res.send('frameHandler');
}
logout(
req: express.Request,
res: express.Response,
next: express.NextFunction,
): express.Response<any> {
return res.send('logout');
}
}
const providerFactories = {
a: MyAuthProvider,
};
const providerConfig = {
provider: 'a',
options: {
somekey: 'somevalue',
},
};
const providerConfigInvalid = {
provider: 'b',
options: {
somekey: 'somevalue',
},
};
describe('makeProvider', () => {
it('makes a provider for Myauthprovider', () => {
const provider = makeProvider(providerFactories, providerConfig);
expect(provider.providerId).toEqual('a');
expect(provider.strategy).toBeDefined();
expect(provider.providerRouter).toBeDefined();
});
it('throws an error when provider implementation does not exist', () => {
expect(() => {
makeProvider(providerFactories, providerConfigInvalid);
}).toThrow('Provider Implementation missing for : b auth provider');
});
});
+7 -8
View File
@@ -21,12 +21,6 @@ import {
AuthProviderConfig,
} from './types';
import { GoogleAuthProvider } from './google/provider';
const providerFactories: AuthProviderFactories = {
google: GoogleAuthProvider,
};
export const defaultRouter = (provider: AuthProviderRouteHandlers) => {
const router = Router();
router.get('/start', provider.start);
@@ -38,11 +32,16 @@ export const defaultRouter = (provider: AuthProviderRouteHandlers) => {
return router;
};
export const makeProvider = (config: AuthProviderConfig) => {
export const makeProvider = (
providerFactories: AuthProviderFactories,
config: any,
) => {
const providerId = config.provider;
const ProviderImpl = providerFactories[providerId];
if (!ProviderImpl) {
throw Error(`Provider Implementation missing for provider: ${providerId}`);
throw Error(
`Provider Implementation missing for : ${providerId} auth provider`,
);
}
const providerInstance = new ProviderImpl(config);
const strategy = providerInstance.strategy();
@@ -19,6 +19,7 @@ import Router from 'express-promise-router';
import passport from 'passport';
import { Logger } from 'winston';
import { providers } from './../providers/config';
import { providerFactories } from './../providers/factories';
import { makeProvider } from '../providers';
export interface RouterOptions {
@@ -35,6 +36,7 @@ export async function createRouter(
// configure all the providers
for (const providerConfig of providers) {
const { providerId, strategy, providerRouter } = makeProvider(
providerFactories,
providerConfig,
);
logger.info(`Configuring provider: ${providerId}`);