Skip to content

Commit 2675beb

Browse files
committed
fix(listener/express): throw session required error on missing request context
1 parent 7fea5db commit 2675beb

File tree

3 files changed

+68
-16
lines changed

3 files changed

+68
-16
lines changed

src/listener/ExpressListener.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import { JwtFields, Token } from '../entity/auth/Token';
1515
import { UserRepository } from '../entity/auth/UserRepository';
1616
import { Context } from '../entity/Context';
1717
import { Message } from '../entity/Message';
18+
import { SessionRequiredError } from '../error/SessionRequiredError';
1819
import { ServiceModule } from '../module/ServiceModule';
1920
import { ServiceMetadata } from '../Service';
2021
import { Storage } from '../storage';
@@ -229,5 +230,10 @@ export class ExpressListener extends SessionListener<ExpressListenerData> implem
229230

230231
export function getRequestContext(req: Request): Context {
231232
/* tslint:disable-next-line:no-any */
232-
return mustExist<Context>(req.user as any);
233+
const user = req.user as any;
234+
if (doesExist(user)) {
235+
return user;
236+
} else {
237+
throw new SessionRequiredError();
238+
}
233239
}

src/schema/graph/index.ts

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
import express from 'express';
22
import { GraphQLList, GraphQLObjectType, GraphQLSchema, GraphQLString } from 'graphql';
3-
import { isNil } from 'lodash';
43
import { Inject } from 'noicejs';
54

65
import { INJECT_CLOCK, INJECT_SERVICES } from '../../BaseService';
76
import { BotService, BotServiceData, BotServiceOptions, INJECT_BOT, INJECT_STORAGE } from '../../BotService';
87
import { Command, CommandVerb, GRAPH_INPUT_COMMAND, GRAPH_OUTPUT_COMMAND } from '../../entity/Command';
9-
import { Context, GRAPH_INPUT_CONTEXT } from '../../entity/Context';
8+
import { GRAPH_INPUT_CONTEXT } from '../../entity/Context';
109
import { GRAPH_INPUT_MESSAGE, GRAPH_OUTPUT_MESSAGE, Message } from '../../entity/Message';
11-
import { SessionRequiredError } from '../../error/SessionRequiredError';
10+
import { getRequestContext } from '../../listener/ExpressListener';
1211
import { ServiceModule } from '../../module/ServiceModule';
1312
import { GRAPH_OUTPUT_SERVICE, ServiceMetadata } from '../../Service';
1413
import { Storage } from '../../storage';
@@ -74,13 +73,9 @@ export class GraphSchema extends BotService<GraphSchemaData> {
7473
}
7574

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

80-
if (isNil(context)) {
81-
throw new SessionRequiredError();
82-
}
83-
8479
const commands = [];
8580
for (const data of args.commands) {
8681
const { noun, verb } = data;
@@ -97,13 +92,9 @@ export class GraphSchema extends BotService<GraphSchemaData> {
9792
}
9893

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

103-
if (isNil(context)) {
104-
throw new SessionRequiredError();
105-
}
106-
10798
const messages = [];
10899
for (const data of args.messages) {
109100
const { body, type } = data;

test/schema/TestGraph.ts

Lines changed: 57 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,63 @@
1+
import { expect } from 'chai';
2+
import { Request } from 'express';
3+
import { ineeda } from 'ineeda';
4+
import { spy } from 'sinon';
5+
6+
import { Bot } from '../../src/Bot';
7+
import { INJECT_BOT } from '../../src/BotService';
8+
import { User } from '../../src/entity/auth/User';
9+
import { GraphSchema } from '../../src/schema/graph';
110
import { describeLeaks, itLeaks } from '../helpers/async';
11+
import { createService, createServiceContainer } from '../helpers/container';
212

313
describeLeaks('graph schema', async () => {
4-
itLeaks('should execute commands');
5-
itLeaks('should send messages');
14+
itLeaks('should execute commands', async () => {
15+
const { container } = await createServiceContainer();
16+
const executeCommand = spy();
17+
const graph = await createService(container, GraphSchema, {
18+
[INJECT_BOT]: ineeda<Bot>({
19+
executeCommand,
20+
}),
21+
data: {
22+
filters: [],
23+
strict: false,
24+
},
25+
metadata: {
26+
kind: 'graph-schema',
27+
name: 'test-schema',
28+
},
29+
});
30+
await graph.executeCommands({
31+
commands: [],
32+
}, ineeda<Request>({
33+
user: ineeda<User>(),
34+
}));
35+
expect(executeCommand).to.have.callCount(1);
36+
});
37+
38+
itLeaks('should send messages', async () => {
39+
const { container } = await createServiceContainer();
40+
const sendMessage = spy();
41+
const graph = await createService(container, GraphSchema, {
42+
[INJECT_BOT]: ineeda<Bot>({
43+
sendMessage,
44+
}),
45+
data: {
46+
filters: [],
47+
strict: false,
48+
},
49+
metadata: {
50+
kind: 'graph-schema',
51+
name: 'test-schema',
52+
},
53+
});
54+
await graph.sendMessages({
55+
messages: [],
56+
}, ineeda<Request>({
57+
user: ineeda<User>(),
58+
}));
59+
expect(sendMessage).to.have.callCount(1);
60+
});
661
itLeaks('should get past commands');
762
itLeaks('should get past messages');
863
itLeaks('should get existing services');

0 commit comments

Comments
 (0)