Skip to content

Commit

Permalink
fix(listener/express): throw session required error on missing reques…
Browse files Browse the repository at this point in the history
…t context
  • Loading branch information
ssube committed Oct 11, 2019
1 parent 7fea5db commit 2675beb
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 16 deletions.
8 changes: 7 additions & 1 deletion src/listener/ExpressListener.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { JwtFields, Token } from '../entity/auth/Token';
import { UserRepository } from '../entity/auth/UserRepository';
import { Context } from '../entity/Context';
import { Message } from '../entity/Message';
import { SessionRequiredError } from '../error/SessionRequiredError';
import { ServiceModule } from '../module/ServiceModule';
import { ServiceMetadata } from '../Service';
import { Storage } from '../storage';
Expand Down Expand Up @@ -229,5 +230,10 @@ export class ExpressListener extends SessionListener<ExpressListenerData> implem

export function getRequestContext(req: Request): Context {
/* tslint:disable-next-line:no-any */
return mustExist<Context>(req.user as any);
const user = req.user as any;
if (doesExist(user)) {
return user;
} else {
throw new SessionRequiredError();
}
}
17 changes: 4 additions & 13 deletions src/schema/graph/index.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import express from 'express';
import { GraphQLList, GraphQLObjectType, GraphQLSchema, GraphQLString } from 'graphql';
import { isNil } from 'lodash';
import { Inject } from 'noicejs';

import { INJECT_CLOCK, INJECT_SERVICES } from '../../BaseService';
import { BotService, BotServiceData, BotServiceOptions, INJECT_BOT, INJECT_STORAGE } from '../../BotService';
import { Command, CommandVerb, GRAPH_INPUT_COMMAND, GRAPH_OUTPUT_COMMAND } from '../../entity/Command';
import { Context, GRAPH_INPUT_CONTEXT } from '../../entity/Context';
import { GRAPH_INPUT_CONTEXT } from '../../entity/Context';
import { GRAPH_INPUT_MESSAGE, GRAPH_OUTPUT_MESSAGE, Message } from '../../entity/Message';
import { SessionRequiredError } from '../../error/SessionRequiredError';
import { getRequestContext } from '../../listener/ExpressListener';
import { ServiceModule } from '../../module/ServiceModule';
import { GRAPH_OUTPUT_SERVICE, ServiceMetadata } from '../../Service';
import { Storage } from '../../storage';
Expand Down Expand Up @@ -74,13 +73,9 @@ export class GraphSchema extends BotService<GraphSchemaData> {
}

public async executeCommands(args: GraphCommandOptions, req: express.Request) {
const context = req.user as Context | undefined;
const context = getRequestContext(req);
this.logger.debug({ args, context }, 'execute commands');

if (isNil(context)) {
throw new SessionRequiredError();
}

const commands = [];
for (const data of args.commands) {
const { noun, verb } = data;
Expand All @@ -97,13 +92,9 @@ export class GraphSchema extends BotService<GraphSchemaData> {
}

public async sendMessages(args: GraphMessageOptions, req: express.Request) {
const context = req.user as Context | undefined;
const context = getRequestContext(req);
this.logger.debug({ args, context }, 'send messages');

if (isNil(context)) {
throw new SessionRequiredError();
}

const messages = [];
for (const data of args.messages) {
const { body, type } = data;
Expand Down
59 changes: 57 additions & 2 deletions test/schema/TestGraph.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,63 @@
import { expect } from 'chai';
import { Request } from 'express';
import { ineeda } from 'ineeda';
import { spy } from 'sinon';

import { Bot } from '../../src/Bot';
import { INJECT_BOT } from '../../src/BotService';
import { User } from '../../src/entity/auth/User';
import { GraphSchema } from '../../src/schema/graph';
import { describeLeaks, itLeaks } from '../helpers/async';
import { createService, createServiceContainer } from '../helpers/container';

describeLeaks('graph schema', async () => {
itLeaks('should execute commands');
itLeaks('should send messages');
itLeaks('should execute commands', async () => {
const { container } = await createServiceContainer();
const executeCommand = spy();
const graph = await createService(container, GraphSchema, {
[INJECT_BOT]: ineeda<Bot>({
executeCommand,
}),
data: {
filters: [],
strict: false,
},
metadata: {
kind: 'graph-schema',
name: 'test-schema',
},
});
await graph.executeCommands({
commands: [],
}, ineeda<Request>({
user: ineeda<User>(),
}));
expect(executeCommand).to.have.callCount(1);
});

itLeaks('should send messages', async () => {
const { container } = await createServiceContainer();
const sendMessage = spy();
const graph = await createService(container, GraphSchema, {
[INJECT_BOT]: ineeda<Bot>({
sendMessage,
}),
data: {
filters: [],
strict: false,
},
metadata: {
kind: 'graph-schema',
name: 'test-schema',
},
});
await graph.sendMessages({
messages: [],
}, ineeda<Request>({
user: ineeda<User>(),
}));
expect(sendMessage).to.have.callCount(1);
});
itLeaks('should get past commands');
itLeaks('should get past messages');
itLeaks('should get existing services');
Expand Down

0 comments on commit 2675beb

Please sign in to comment.