forked from cerc-io/ipld-eth-server
Add Postgraphile subscription exposure
- Add automation for TypeScript project build and tests via Webpack, tsc, and Jasmine - Add custom Express application to serve Postgraphile subscription endpoints - Add automated test suite for subscription endpoints
This commit is contained in:
@@ -0,0 +1,76 @@
|
||||
import { parseConfig } from '../../src/config/parse';
|
||||
|
||||
describe('parseConfig', () => {
|
||||
let configPath: string;
|
||||
let tomlContents: string;
|
||||
let parsedToml: object;
|
||||
let readCallback: jasmine.Spy;
|
||||
let tomlParseCallback: jasmine.Spy;
|
||||
|
||||
beforeEach(() => {
|
||||
configPath = '/example/config/path.toml';
|
||||
tomlContents = `[database]\nname = 'example_database'\n `
|
||||
+ `hostname = 'example.com'\nport = 1234`;
|
||||
|
||||
parsedToml = {
|
||||
database: {
|
||||
hostname: 'example.com',
|
||||
name: 'example_database',
|
||||
port: '1234'
|
||||
}
|
||||
};
|
||||
|
||||
readCallback = jasmine.createSpy('readCallback');
|
||||
readCallback.and.returnValue(tomlContents);
|
||||
|
||||
tomlParseCallback = jasmine.createSpy('tomlParseCallback');
|
||||
tomlParseCallback.and.returnValue(parsedToml);
|
||||
});
|
||||
|
||||
it('provides the database host', () => {
|
||||
const databaseConfig = parseConfig(
|
||||
readCallback, tomlParseCallback, configPath);
|
||||
|
||||
expect(databaseConfig.host).toEqual('postgres://example.com:1234');
|
||||
});
|
||||
|
||||
it('provides the database name', () => {
|
||||
const databaseConfig = parseConfig(
|
||||
readCallback, tomlParseCallback, configPath);
|
||||
|
||||
expect(databaseConfig.database).toEqual('example_database');
|
||||
});
|
||||
|
||||
it('handles a missing config path', () => {
|
||||
const failingCall = () =>
|
||||
parseConfig(readCallback, tomlParseCallback, '');
|
||||
|
||||
tomlParseCallback.and.returnValue({
|
||||
database: { hostname: 'example.com', name: 'example_database' }
|
||||
});
|
||||
|
||||
expect(failingCall).toThrow();
|
||||
});
|
||||
|
||||
it('handles a missing database host', () => {
|
||||
const failingCall = () =>
|
||||
parseConfig(readCallback, tomlParseCallback, configPath);
|
||||
|
||||
tomlParseCallback.and.returnValue({
|
||||
database: { hostname: '', name: 'example_database' }
|
||||
});
|
||||
|
||||
expect(failingCall).toThrow();
|
||||
});
|
||||
|
||||
it('handles a missing database name', () => {
|
||||
const failingCall = () =>
|
||||
parseConfig(readCallback, tomlParseCallback, configPath);
|
||||
|
||||
tomlParseCallback.and.returnValue({
|
||||
database: { hostname: 'example.com', name: '', port: '1234' }
|
||||
});
|
||||
|
||||
expect(failingCall).toThrow();
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,7 @@
|
||||
// NOTE (jchristie@8thlight.com) This file helps Jasmine
|
||||
// comprehend TS sourcemaps by installing a reporter
|
||||
// specifically for TypeScript
|
||||
const TSConsoleReporter = require('jasmine-ts-console-reporter');
|
||||
|
||||
jasmine.getEnv().clearReporters();
|
||||
jasmine.getEnv().addReporter(new TSConsoleReporter());
|
||||
@@ -0,0 +1,104 @@
|
||||
import { PassportStatic } from 'passport';
|
||||
|
||||
import { PostgraphileMiddleware } from '../../src/adapters/postgraphile';
|
||||
import { buildServerConfig } from '../../src/server/config';
|
||||
|
||||
import {
|
||||
DatabaseConfig,
|
||||
ServerConfig,
|
||||
ServerUtilities
|
||||
} from '../../src/server/interface';
|
||||
|
||||
describe('buildServerConfig', () => {
|
||||
let configParser: jasmine.Spy;
|
||||
let postgraphileMiddleware: PostgraphileMiddleware;
|
||||
let expressSessionHandler: jasmine.Spy;
|
||||
let passportInitializer: jasmine.Spy;
|
||||
let passportSessionHandler: jasmine.Spy;
|
||||
let serverConfig: ServerConfig;
|
||||
|
||||
let serverUtilities: ServerUtilities;
|
||||
let databaseConfig: DatabaseConfig;
|
||||
|
||||
beforeEach(() => {
|
||||
databaseConfig = { host: 'example.com', database: 'example_database' };
|
||||
|
||||
postgraphileMiddleware = jasmine
|
||||
.createSpyObj<PostgraphileMiddleware>(['call']),
|
||||
|
||||
serverUtilities = {
|
||||
enableSubscriptions: jasmine.createSpy('enableSubscriptions'),
|
||||
express: jasmine.createSpy('express'),
|
||||
expressSession: jasmine.createSpy('expressSession'),
|
||||
httpServerFactory: jasmine.createSpy('httpServerFactory'),
|
||||
passport: jasmine.createSpyObj<PassportStatic>(['initialize', 'session']),
|
||||
postgraphile: jasmine.createSpy('postgraphile')
|
||||
};
|
||||
|
||||
const rawConfig: object = { exampleOption: 'example value' };
|
||||
|
||||
configParser = jasmine.createSpy('configParser');
|
||||
configParser.and.returnValue(rawConfig);
|
||||
|
||||
expressSessionHandler = jasmine.createSpy('expressSessionHandler');
|
||||
passportInitializer = jasmine.createSpy('passportInitializer');
|
||||
passportSessionHandler = jasmine.createSpy('passportSessionHandler');
|
||||
|
||||
(serverUtilities.postgraphile as jasmine.Spy)
|
||||
.and.returnValue(postgraphileMiddleware);
|
||||
(serverUtilities.expressSession as jasmine.Spy)
|
||||
.and.returnValue(expressSessionHandler);
|
||||
(serverUtilities.passport.initialize as jasmine.Spy)
|
||||
.and.returnValue(passportInitializer);
|
||||
(serverUtilities.passport.session as jasmine.Spy)
|
||||
.and.returnValue(passportSessionHandler);
|
||||
|
||||
serverConfig = buildServerConfig(
|
||||
serverUtilities, databaseConfig, undefined);
|
||||
});
|
||||
|
||||
it('provides the Postgraphile options', () => {
|
||||
expect(serverConfig.options).not.toBeNull();
|
||||
});
|
||||
|
||||
it('enables simple subscriptions', () => {
|
||||
expect(serverConfig.options.simpleSubscriptions).toBe(true);
|
||||
});
|
||||
|
||||
it('it adds the express session handler as the first middleware', () => {
|
||||
expect(serverConfig.options.webSocketMiddlewares[0])
|
||||
.toBe(expressSessionHandler);
|
||||
});
|
||||
|
||||
it('it adds the passport initializer as the second middleware', () => {
|
||||
expect(serverConfig.options.webSocketMiddlewares[1])
|
||||
.toBe(passportInitializer);
|
||||
});
|
||||
|
||||
it('it adds the passport session handler as the third middleware', () => {
|
||||
expect(serverConfig.options.webSocketMiddlewares[2])
|
||||
.toBe(passportSessionHandler);
|
||||
});
|
||||
|
||||
it('provides the database config to Postgraphile', () => {
|
||||
expect(serverUtilities.postgraphile).toHaveBeenCalledWith(
|
||||
databaseConfig.host,
|
||||
databaseConfig.database,
|
||||
jasmine.any(Object));
|
||||
});
|
||||
|
||||
it('provides the Postgraphile middleware', () => {
|
||||
expect(serverConfig.middleware).toBe(postgraphileMiddleware);
|
||||
});
|
||||
|
||||
it('sets the default server port', () => {
|
||||
expect(serverConfig.port).toEqual(3000);
|
||||
});
|
||||
|
||||
it('sets an explicit server port', () => {
|
||||
const serverConfigWithPort = buildServerConfig(
|
||||
serverUtilities, databaseConfig, '1234');
|
||||
|
||||
expect(serverConfigWithPort.port).toEqual(1234);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,64 @@
|
||||
import { Express } from 'express';
|
||||
import { PassportStatic } from 'passport';
|
||||
import { Server } from 'http';
|
||||
|
||||
import { ServerUtilities, ServerConfig } from '../../src/server/interface';
|
||||
import { bootServer } from '../../src/server/runtime';
|
||||
import { PostgraphileMiddleware } from '../../src/adapters/postgraphile';
|
||||
|
||||
describe('bootServer', () => {
|
||||
let serverUtilities: ServerUtilities;
|
||||
let serverConfig: ServerConfig;
|
||||
let mockExpressApp: Express;
|
||||
let mockHttpServer: Server;
|
||||
|
||||
beforeEach(() => {
|
||||
serverUtilities = {
|
||||
enableSubscriptions: jasmine.createSpy('enableSubscriptions'),
|
||||
express: jasmine.createSpy('express'),
|
||||
expressSession: jasmine.createSpy('expressSession'),
|
||||
httpServerFactory: jasmine.createSpy('httpServerFactory'),
|
||||
passport: jasmine.createSpyObj<PassportStatic>(['initialize', 'session']),
|
||||
postgraphile: jasmine.createSpy('postgraphile')
|
||||
};
|
||||
|
||||
serverConfig = {
|
||||
middleware: jasmine.createSpyObj<PostgraphileMiddleware>(['call']),
|
||||
options: { simpleSubscriptions: true, webSocketMiddlewares: [] },
|
||||
port: 5678
|
||||
};
|
||||
|
||||
mockExpressApp = jasmine.createSpyObj<Express>(['use']);
|
||||
(serverUtilities.express as jasmine.Spy)
|
||||
.and.returnValue(mockExpressApp);
|
||||
|
||||
mockHttpServer = jasmine.createSpyObj<Server>(['listen']);
|
||||
(serverUtilities.httpServerFactory as jasmine.Spy)
|
||||
.and.returnValue(mockHttpServer);
|
||||
|
||||
bootServer(serverUtilities, serverConfig);
|
||||
});
|
||||
|
||||
it('builds a new, Node HTTP server', () => {
|
||||
expect(serverUtilities.httpServerFactory)
|
||||
.toHaveBeenCalledWith(mockExpressApp);
|
||||
});
|
||||
|
||||
it('provides Postgraphile middleware to the Express app', () => {
|
||||
const useSpy = mockExpressApp.use as jasmine.Spy;
|
||||
expect(useSpy).toHaveBeenCalledWith(serverConfig.middleware);
|
||||
});
|
||||
|
||||
it('enahances the Node HTTP server with Postgraphile subscriptions', () => {
|
||||
expect(serverUtilities.enableSubscriptions)
|
||||
.toHaveBeenCalledWith(
|
||||
mockHttpServer,
|
||||
serverConfig.middleware,
|
||||
serverConfig.options);
|
||||
});
|
||||
|
||||
it('instructs the server to listen on the given port', () => {
|
||||
const listenSpy = mockHttpServer.listen as jasmine.Spy;
|
||||
expect(listenSpy).toHaveBeenCalledWith(serverConfig.port);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,11 @@
|
||||
{
|
||||
"spec_dir": "build/spec/",
|
||||
"spec_files": [
|
||||
"**/*[sS]pec.js"
|
||||
],
|
||||
"helpers": [
|
||||
"helpers/*.js"
|
||||
],
|
||||
"stopSpecOnExpectationFailure": false,
|
||||
"random": true
|
||||
}
|
||||
Reference in New Issue
Block a user