From 88b0c8538d899c9a8db62bffd3c63fd3ebbe15aa Mon Sep 17 00:00:00 2001 From: ssube Date: Sun, 11 Apr 2021 21:58:41 -0500 Subject: [PATCH] fix: make context source required, add test helper for context data --- src/BaseService.ts | 9 +++- src/endpoint/BaseEndpoint.ts | 3 +- src/endpoint/GithubEndpoint.ts | 4 ++ src/endpoint/GitlabEndpoint.ts | 11 +++++ src/endpoint/HookEndpoint.ts | 1 + src/entity/Context.ts | 31 ++++++------- src/listener/BaseListener.ts | 4 +- src/listener/DiscordListener.ts | 1 + src/listener/ExpressListener.ts | 1 + src/listener/GithubListener.ts | 1 + src/listener/SlackListener.ts | 1 + src/parser/BaseParser.ts | 1 + src/utils/ContextRedirect.ts | 14 +++++- test/controller/TestAccountController.ts | 11 ++--- test/controller/TestWeatherController.ts | 2 + .../github/TestApproveController.ts | 11 +++-- test/endpoint/TestGithubEndpoint.ts | 14 +++++- test/endpoint/TestGitlabEndpoint.ts | 15 +++++-- test/entity/TestCommand.ts | 12 +---- test/entity/TestContext.ts | 44 ++++++++++++++++--- test/generator/TestCommandGenerator.ts | 12 +---- test/generator/TestEventGenerator.ts | 12 +---- test/generator/TestMessageGenerator.ts | 12 +---- test/generator/TestMetricsGenerator.ts | 12 +---- test/helpers/context.ts | 18 ++++++++ test/listener/TestLoopbackListener.ts | 6 +-- test/parser/TestArgsParser.ts | 23 ++-------- test/parser/TestBaseParser.ts | 10 +---- test/parser/TestEchoParser.ts | 12 +---- test/parser/TestRegexParser.ts | 12 +---- test/parser/TestSplitParser.ts | 23 ++-------- test/parser/TestYamlParser.ts | 12 +---- test/utils/TestTemplateCompiler.ts | 14 ++---- 33 files changed, 182 insertions(+), 187 deletions(-) create mode 100644 test/helpers/context.ts diff --git a/src/BaseService.ts b/src/BaseService.ts index 36c2c0eaf..574084b5f 100644 --- a/src/BaseService.ts +++ b/src/BaseService.ts @@ -7,7 +7,7 @@ import { SchemaError } from './error/SchemaError'; import { serviceLogger } from './logger'; import { ServiceModule } from './module/ServiceModule'; import { Schema } from './schema'; -import { Service, ServiceDefinition, ServiceEvent } from './Service'; +import { Service, ServiceDefinition, ServiceEvent, ServiceMetadata } from './Service'; import { Clock } from './utils/Clock'; import { JsonPath } from './utils/JsonPath'; import { MathFactory } from './utils/Math'; @@ -95,4 +95,11 @@ export abstract class BaseService implements Serv return this.id; } } + + public getMetadata(): ServiceMetadata { + return { + kind: this.kind, + name: this.name, + }; + } } diff --git a/src/endpoint/BaseEndpoint.ts b/src/endpoint/BaseEndpoint.ts index 18270eb8a..60d92542b 100644 --- a/src/endpoint/BaseEndpoint.ts +++ b/src/endpoint/BaseEndpoint.ts @@ -95,7 +95,8 @@ export abstract class BaseEndpoint extends BotServic protected async createContext(options: ContextOptions): Promise { const ctx = await this.contextRepository.save(new Context({ ...options, - // TODO: does this need source/target? + source: this.getMetadata(), + // TODO: does this need target? })); this.logger.debug({ ctx }, 'endpoint saved new context'); return ctx; diff --git a/src/endpoint/GithubEndpoint.ts b/src/endpoint/GithubEndpoint.ts index 8f54f7755..06ac6fc95 100644 --- a/src/endpoint/GithubEndpoint.ts +++ b/src/endpoint/GithubEndpoint.ts @@ -81,6 +81,7 @@ export class GithubEndpoint extends HookEndpoint { id: data.repository.full_name, thread: data.check_run.head_sha, }, + source: this.getMetadata(), sourceUser: { name: data.sender.login, uid: this.data.hookUser, @@ -100,6 +101,7 @@ export class GithubEndpoint extends HookEndpoint { id: data.repository.full_name, thread: data.check_suite.head_sha, }, + source: this.getMetadata(), sourceUser: { name: data.sender.login, uid: this.data.hookUser, @@ -119,6 +121,7 @@ export class GithubEndpoint extends HookEndpoint { id: data.repository.full_name, thread: data.pull_request.number, }, + source: this.getMetadata(), sourceUser: { name: data.sender.login, uid: this.data.hookUser, @@ -138,6 +141,7 @@ export class GithubEndpoint extends HookEndpoint { id: data.repository.full_name, thread: data.sha, }, + source: this.getMetadata(), sourceUser: { name: data.sender.login, uid: this.data.hookUser, diff --git a/src/endpoint/GitlabEndpoint.ts b/src/endpoint/GitlabEndpoint.ts index 4a08baa9a..4094fe2d4 100644 --- a/src/endpoint/GitlabEndpoint.ts +++ b/src/endpoint/GitlabEndpoint.ts @@ -162,6 +162,10 @@ export class GitlabEndpoint extends HookEndpoint implements id: data.project_name, thread: data.ref, }, + source: { + kind: this.kind, + name: this.name, + }, sourceUser: { name: user.name, uid: this.data.hookUser, @@ -183,6 +187,10 @@ export class GitlabEndpoint extends HookEndpoint implements id: data.project.id, thread: data.object_attributes.id, }, + source: { + kind: this.kind, + name: this.name, + }, sourceUser: { name: user.name, uid: this.data.hookUser, @@ -204,6 +212,7 @@ export class GitlabEndpoint extends HookEndpoint implements id: data.project.id, thread: data.object_attributes.id, }, + source: this.getMetadata(), sourceUser: { name: user.name, uid: this.data.hookUser, @@ -223,6 +232,7 @@ export class GitlabEndpoint extends HookEndpoint implements id: data.project.web_url, thread: data.object_attributes.ref, }, + source: this.getMetadata(), sourceUser: { name: data.user.name, uid: data.user.username, @@ -242,6 +252,7 @@ export class GitlabEndpoint extends HookEndpoint implements id: data.project.web_url, thread: data.ref, }, + source: this.getMetadata(), sourceUser: { name: data.user_name, uid: data.user_username, diff --git a/src/endpoint/HookEndpoint.ts b/src/endpoint/HookEndpoint.ts index 3670e2a92..956994773 100644 --- a/src/endpoint/HookEndpoint.ts +++ b/src/endpoint/HookEndpoint.ts @@ -46,6 +46,7 @@ export class HookEndpoint extends BaseEndpoint extends BotServic protected async createContext(options: ContextOptions): Promise { const ctx = await this.contextRepository.save(new Context({ ...options, - source: this, - target: this, + source: this.getMetadata(), + target: this.getMetadata(), })); this.logger.debug({ ctx }, 'listener saved new context'); return ctx; diff --git a/src/listener/DiscordListener.ts b/src/listener/DiscordListener.ts index c47c8470a..ccd6308f9 100644 --- a/src/listener/DiscordListener.ts +++ b/src/listener/DiscordListener.ts @@ -226,6 +226,7 @@ export class DiscordListener extends SessionListener implem id: msg.channel.id, thread: msg.id, }, + source: this.getMetadata(), sourceUser: { name: msg.author.username, uid: msg.author.id, diff --git a/src/listener/ExpressListener.ts b/src/listener/ExpressListener.ts index 76625d2a0..eee5c3580 100644 --- a/src/listener/ExpressListener.ts +++ b/src/listener/ExpressListener.ts @@ -152,6 +152,7 @@ export class ExpressListener extends SessionListener implem id: '', thread: '', }, + source: this.getMetadata(), sourceUser: { name: token.user.name, uid, diff --git a/src/listener/GithubListener.ts b/src/listener/GithubListener.ts index 7a470a083..64a165349 100644 --- a/src/listener/GithubListener.ts +++ b/src/listener/GithubListener.ts @@ -129,6 +129,7 @@ export class GithubListener extends SessionListener { id: `${repo.owner}/${repo.repo}`, thread, }, + source: this.getMetadata(), sourceUser: { name: msg.user.login, uid, diff --git a/src/listener/SlackListener.ts b/src/listener/SlackListener.ts index 2e6eacbc8..0d516ac8b 100644 --- a/src/listener/SlackListener.ts +++ b/src/listener/SlackListener.ts @@ -230,6 +230,7 @@ export class SlackListener extends SessionListener implements id: channel, thread: ts, }, + source: this.getMetadata(), sourceUser: { name: uid, uid, diff --git a/src/parser/BaseParser.ts b/src/parser/BaseParser.ts index 169b63c42..6d0c278f7 100644 --- a/src/parser/BaseParser.ts +++ b/src/parser/BaseParser.ts @@ -58,6 +58,7 @@ export abstract class BaseParser extends BotService(...args: Array>): T | undefined { + for (const a of args) { + if (doesExist(a)) { + return a; + } + } + + return undefined; +} + export function redirectContext(original: Context, redirect: ContextRedirect): Context { const channel = mustCoalesce(redirect.forces.channel, original.channel, redirect.defaults.channel); - const target = mustCoalesce(redirect.forces.target, original.target, redirect.defaults.target); + const target = mayCoalesce(redirect.forces.target, original.target, redirect.defaults.target); const loopTarget = defaultWhen(redirect.forces.loopback === true, original.source, target); const user = original.user; diff --git a/test/controller/TestAccountController.ts b/test/controller/TestAccountController.ts index ebee4571a..19d7ebdee 100644 --- a/test/controller/TestAccountController.ts +++ b/test/controller/TestAccountController.ts @@ -11,6 +11,7 @@ import { Context } from '../../src/entity/Context'; import { Locale } from '../../src/locale'; import { Schema } from '../../src/schema'; import { createService, createServiceContainer } from '../helpers/container'; +import { getTestContextData } from '../helpers/context'; const TEST_DATA = { filters: [], @@ -60,11 +61,8 @@ describe('account controller', async () => { await ctrl.getGrant(ineeda({ get: () => tvals, }), ineeda({ + ...getTestContextData(), checkGrants, - sourceUser: { - name: 'test', - uid: 'test', - }, })); expect(checkGrants).to.have.callCount(tvals.length); @@ -125,10 +123,7 @@ describe('account controller', async () => { }); const context = ineeda({ - sourceUser: { - name: 'test', - uid: 'test', - }, + ...getTestContextData(), user: { locale: { lang: 'test', diff --git a/test/controller/TestWeatherController.ts b/test/controller/TestWeatherController.ts index 216dab192..4ce3c9060 100644 --- a/test/controller/TestWeatherController.ts +++ b/test/controller/TestWeatherController.ts @@ -14,6 +14,7 @@ import { TransformModule } from '../../src/module/TransformModule'; import { Transform } from '../../src/transform'; import { RequestFactory } from '../../src/utils/Request'; import { createService, createServiceContainer } from '../helpers/container'; +import { getTestContextData } from '../helpers/context'; describe('weather controller', async () => { it('should send a message', async () => { @@ -76,6 +77,7 @@ describe('weather controller', async () => { const cmd = new Command({ context: ineeda({ + ...getTestContextData(), checkGrants: () => true, getGrants: () => ['*:test'], user: ineeda(), diff --git a/test/controller/github/TestApproveController.ts b/test/controller/github/TestApproveController.ts index db2771d36..45c42de16 100644 --- a/test/controller/github/TestApproveController.ts +++ b/test/controller/github/TestApproveController.ts @@ -9,13 +9,12 @@ import { GithubApproveController } from '../../../src/controller/github/ApproveC import { STATUS_SUCCESS } from '../../../src/endpoint/BaseEndpoint'; import { Command, CommandVerb } from '../../../src/entity/Command'; import { Context } from '../../../src/entity/Context'; -import { Transform } from '../../../src/transform'; import { GithubClient } from '../../../src/utils/github'; import { createService, createServiceContainer } from '../../helpers/container'; +import { getTestContextData } from '../../helpers/context'; /* eslint-disable @typescript-eslint/naming-convention,@typescript-eslint/no-explicit-any, camelcase */ -const TEST_TRANSFORM = 'test-transform'; const TEST_DATA = { client: { agent: '', @@ -70,7 +69,7 @@ function pullGetResponse(pulls: Array): ClientResponse { labels: {}, noun: '', verb: CommandVerb.Create, - }), ineeda()); + }), new Context(getTestContextData())); expect(sendMessage).to.have.callCount(1); expect(sendMessage).to.have.been.calledWithMatch({ @@ -210,7 +209,7 @@ describe('github approve controller', async () => { labels: {}, noun: '', verb: CommandVerb.Create, - }), ineeda()); + }), new Context(getTestContextData())); expect(sendMessage).to.have.callCount(1); expect(sendMessage).to.have.been.calledWithMatch({ @@ -392,7 +391,7 @@ describe('github approve controller', async () => { labels: {}, noun: '', verb: CommandVerb.Create, - }), ineeda()); + }), new Context(getTestContextData())); expect(sendMessage).to.have.callCount(pulls.length); }); diff --git a/test/endpoint/TestGithubEndpoint.ts b/test/endpoint/TestGithubEndpoint.ts index 9ad8394eb..3f93ad3d6 100644 --- a/test/endpoint/TestGithubEndpoint.ts +++ b/test/endpoint/TestGithubEndpoint.ts @@ -17,7 +17,7 @@ const TEST_DATA: GithubEndpointData = { verb: CommandVerb.Get, }, filters: [], - hookUser: '', + hookUser: 'test', secret: '', strict: false, transforms: [], @@ -71,6 +71,12 @@ describe('github endpoint', async () => { const sendStatus = spy(); await endpoint.postWebhook(ineeda({ + body: { + sender: { + /* eslint-disable-next-line */ + login: 'test' as any, + }, + }, header: stub().withArgs('X-GitHub-Event').returns('pull_request_review'), }), ineeda({ sendStatus, @@ -85,6 +91,12 @@ describe('github endpoint', async () => { const sendStatus = spy(); await endpoint.postWebhook(ineeda({ + body: { + sender: { + /* eslint-disable-next-line */ + login: 'test' as any, + }, + }, header: stub().withArgs('X-GitHub-Event').returns('status'), }), ineeda({ sendStatus, diff --git a/test/endpoint/TestGitlabEndpoint.ts b/test/endpoint/TestGitlabEndpoint.ts index 119289de8..f217bc613 100644 --- a/test/endpoint/TestGitlabEndpoint.ts +++ b/test/endpoint/TestGitlabEndpoint.ts @@ -19,7 +19,7 @@ const TEST_DATA: GitlabEndpointData = { verb: CommandVerb.Create, }, filters: [], - hookUser: '', + hookUser: 'test', strict: false, transforms: [], }; @@ -34,10 +34,19 @@ const TEST_EVENTS = [{ data: {}, name: 'note', }, { - data: {}, + data: { + user: { + name: 'test', + username: 'test', + }, + }, name: 'pipeline', }, { - data: {}, + data: { + /* eslint-disable camelcase */ + user_name: 'test', + user_username: 'test', + }, name: 'push', }]; diff --git a/test/entity/TestCommand.ts b/test/entity/TestCommand.ts index 853f0e7cf..28d197d0a 100644 --- a/test/entity/TestCommand.ts +++ b/test/entity/TestCommand.ts @@ -3,6 +3,7 @@ import { ineeda } from 'ineeda'; import { Command, CommandVerb } from '../../src/entity/Command'; import { Context } from '../../src/entity/Context'; +import { getTestContextData } from '../helpers/context'; describe('command', async () => { it('should copy data', async () => { @@ -56,16 +57,7 @@ describe('command', async () => { it('should convert its context to JSON', async () => { const cmd = new Command({ - context: new Context({ - channel: { - id: '', - thread: '', - }, - sourceUser: { - name: '', - uid: '', - }, - }), + context: new Context(getTestContextData()), data: {}, labels: {}, noun: 'test', diff --git a/test/entity/TestContext.ts b/test/entity/TestContext.ts index 4a6902f5e..0b336a8b9 100644 --- a/test/entity/TestContext.ts +++ b/test/entity/TestContext.ts @@ -1,6 +1,7 @@ import { NotFoundError } from '@apextoaster/js-utils'; import { expect } from 'chai'; import { ineeda } from 'ineeda'; +import { MissingValueError } from 'noicejs'; import { Role } from '../../src/entity/auth/Role'; import { Token } from '../../src/entity/auth/Token'; @@ -8,6 +9,11 @@ import { LOCALE_DEFAULT, User } from '../../src/entity/auth/User'; import { Context } from '../../src/entity/Context'; import { Listener } from '../../src/listener'; +const SOURCE_SERVICE = { + kind: 'test-source', + name: 'test-source', +}; + describe('context entity', async () => { it('should allow matching permissions', async () => { const grants = ['foo:bar', 'if:else,elif:end']; @@ -129,6 +135,7 @@ describe('context entity', async () => { id: '', thread: '', }, + source: SOURCE_SERVICE, sourceUser: { name: 'test', uid: 'uid', @@ -144,6 +151,7 @@ describe('context entity', async () => { id: '', thread: '', }, + source: SOURCE_SERVICE, sourceUser: { name: 'test', uid: 'uid', @@ -158,6 +166,7 @@ describe('context entity', async () => { id: '', thread: '', }, + source: SOURCE_SERVICE, sourceUser: { name: 'test', uid: 'uid', @@ -172,6 +181,7 @@ describe('context entity', async () => { id: '', thread: '', }, + source: SOURCE_SERVICE, sourceUser: { name: 'test', uid: 'uid', @@ -189,6 +199,7 @@ describe('context entity', async () => { id: '', thread: '', }, + source: SOURCE_SERVICE, sourceUser: { name: 'test', uid: 'uid', @@ -199,12 +210,35 @@ describe('context entity', async () => { }); expect(context.checkGrants(['test'])).to.equal(false); }); -}); -const SOURCE_SERVICE = { - kind: 'test-source', - name: 'test-source', -}; + it('should throw when created with a nameless source user', () => { + expect(() => new Context({ + channel: { + id: '', + thread: '', + }, + source: SOURCE_SERVICE, + sourceUser: { + name: '', + uid: 'test', + }, + })).to.throw(MissingValueError); + }); + + it('should throw when created with an unidentified source user', () => { + expect(() => new Context({ + channel: { + id: '', + thread: '', + }, + source: SOURCE_SERVICE, + sourceUser: { + name: 'test', + uid: '', + }, + })).to.throw(MissingValueError); + }); +}); const TARGET_SERVICE = { kind: 'test-target', diff --git a/test/generator/TestCommandGenerator.ts b/test/generator/TestCommandGenerator.ts index 95a93d702..5f0c2d363 100644 --- a/test/generator/TestCommandGenerator.ts +++ b/test/generator/TestCommandGenerator.ts @@ -9,6 +9,7 @@ import { Context } from '../../src/entity/Context'; import { Tick } from '../../src/entity/Tick'; import { CommandGenerator } from '../../src/generator/CommandGenerator'; import { createService, createServiceContainer } from '../helpers/container'; +import { getTestContextData } from '../helpers/context'; describe('command generator', async () => { it('should notify target services', async () => { @@ -19,16 +20,7 @@ describe('command generator', async () => { executeCommand, }), data: { - context: { - channel: { - id: '', - thread: '', - }, - sourceUser: { - name: '', - uid: '', - }, - }, + context: getTestContextData(), defaultCommand: { data: {}, labels: {}, diff --git a/test/generator/TestEventGenerator.ts b/test/generator/TestEventGenerator.ts index eee721059..dd1d91e84 100644 --- a/test/generator/TestEventGenerator.ts +++ b/test/generator/TestEventGenerator.ts @@ -7,6 +7,7 @@ import { Tick } from '../../src/entity/Tick'; import { EventGenerator } from '../../src/generator/EventGenerator'; import { Service } from '../../src/Service'; import { createService, createServiceContainer } from '../helpers/container'; +import { getTestContextData } from '../helpers/context'; const TEST_SVC = 'some-service'; @@ -21,16 +22,7 @@ describe('event generator', async () => { })); const interval = await createService(container, EventGenerator, { data: { - context: { - channel: { - id: '', - thread: '', - }, - sourceUser: { - name: '', - uid: '', - }, - }, + context: getTestContextData(), filters: [], frequency: { time: '30s', diff --git a/test/generator/TestMessageGenerator.ts b/test/generator/TestMessageGenerator.ts index 98c75a45c..89ad85334 100644 --- a/test/generator/TestMessageGenerator.ts +++ b/test/generator/TestMessageGenerator.ts @@ -17,6 +17,7 @@ import { Service } from '../../src/Service'; import { Clock } from '../../src/utils/Clock'; import { TYPE_TEXT } from '../../src/utils/Mime'; import { createService, createServiceContainer } from '../helpers/container'; +import { getTestContextData } from '../helpers/context'; const TEST_SVC = 'some-service'; const TEST_SVC2 = 'test-service'; @@ -24,16 +25,7 @@ const TEST_TARGET = 'test-target'; const TEST_CONFIG = { data: { - context: { - channel: { - id: '', - thread: '', - }, - sourceUser: { - name: '', - uid: '', - }, - }, + context: getTestContextData(), defaultMessage: { body: '', labels: {}, diff --git a/test/generator/TestMetricsGenerator.ts b/test/generator/TestMetricsGenerator.ts index 3c513495d..919319f6f 100644 --- a/test/generator/TestMetricsGenerator.ts +++ b/test/generator/TestMetricsGenerator.ts @@ -9,21 +9,13 @@ import { Tick } from '../../src/entity/Tick'; import { MetricsGenerator, MetricsGeneratorData } from '../../src/generator/MetricsGenerator'; import { Clock } from '../../src/utils/Clock'; import { createService, createServiceContainer } from '../helpers/container'; +import { getTestContextData } from '../helpers/context'; const TEST_DELAY = 50; const TEST_TARGET = 'test-target'; const TEST_GENERATOR = 'metrics-generator'; const TEST_DATA: MetricsGeneratorData = { - context: { - channel: { - id: '', - thread: '', - }, - sourceUser: { - name: '', - uid: '', - }, - }, + context: getTestContextData(), filters: [], frequency: { time: '100ms', diff --git a/test/helpers/context.ts b/test/helpers/context.ts new file mode 100644 index 000000000..fbff7a74e --- /dev/null +++ b/test/helpers/context.ts @@ -0,0 +1,18 @@ +import { ContextData } from '../../src/entity/Context'; + +export function getTestContextData(): ContextData { + return { + channel: { + id: 'test', + thread: 'test', + }, + source: { + kind: 'test', + name: 'test', + }, + sourceUser: { + name: 'test', + uid: 'test', + }, + }; +} diff --git a/test/listener/TestLoopbackListener.ts b/test/listener/TestLoopbackListener.ts index 217482af3..5a10f47dd 100644 --- a/test/listener/TestLoopbackListener.ts +++ b/test/listener/TestLoopbackListener.ts @@ -12,6 +12,7 @@ import { LoopbackListener } from '../../src/listener/LoopbackListener'; import { Service } from '../../src/Service'; import { TYPE_TEXT } from '../../src/utils/Mime'; import { createService, createServiceContainer } from '../helpers/container'; +import { getTestContextData } from '../helpers/context'; const TEST_METADATA = { kind: 'loopback-listener', @@ -62,10 +63,7 @@ describe('loopback listener', async () => { const msg = new Message({ context: new Context({ - channel: { - id: '', - thread: '', - }, + ...getTestContextData(), ...ctxOptions, }), labels: {}, diff --git a/test/parser/TestArgsParser.ts b/test/parser/TestArgsParser.ts index b458e9127..5afad6479 100644 --- a/test/parser/TestArgsParser.ts +++ b/test/parser/TestArgsParser.ts @@ -13,6 +13,7 @@ import { ArgsParser } from '../../src/parser/ArgsParser'; import { Storage } from '../../src/storage'; import { TYPE_JPEG, TYPE_TEXT } from '../../src/utils/Mime'; import { createService, createServiceContainer } from '../helpers/container'; +import { getTestContextData } from '../helpers/context'; const TEST_CONFIG = { args: { @@ -80,16 +81,7 @@ describe('args parser', async () => { const commands = await svc.parse(new Message({ body: '--foo=1', - context: new Context({ - channel: { - id: 'test', - thread: 'test', - }, - sourceUser: { - name: 'test', - uid: 'test', - }, - }), + context: new Context(getTestContextData()), labels: {}, reactions: [], type: TYPE_TEXT, @@ -109,16 +101,7 @@ describe('args parser', async () => { metadata: TEST_METADATA, }); - const commands = await svc.complete(new Context({ - channel: { - id: 'test', - thread: 'test', - }, - sourceUser: { - name: 'test', - uid: 'test', - }, - }), new Fragment({ + const commands = await svc.complete(new Context(getTestContextData()), new Fragment({ data: { foo: ['1'], }, diff --git a/test/parser/TestBaseParser.ts b/test/parser/TestBaseParser.ts index 3b4b72c40..378c7b7c3 100644 --- a/test/parser/TestBaseParser.ts +++ b/test/parser/TestBaseParser.ts @@ -12,6 +12,7 @@ import { ParserData, ParserOutput } from '../../src/parser'; import { BaseParser } from '../../src/parser/BaseParser'; import { Storage } from '../../src/storage'; import { createService, createServiceContainer } from '../helpers/container'; +import { getTestContextData } from '../helpers/context'; const TEST_PARSER = 'test-parser'; @@ -89,14 +90,7 @@ describe('base parser', async () => { }, }); const results = await parser.complete(new Context({ - channel: { - id: '', - thread: '', - }, - sourceUser: { - name: 'test', - uid: '0', - }, + ...getTestContextData(), user: ineeda(), }), new Fragment({ data: {}, diff --git a/test/parser/TestEchoParser.ts b/test/parser/TestEchoParser.ts index c11cbb107..6176a701b 100644 --- a/test/parser/TestEchoParser.ts +++ b/test/parser/TestEchoParser.ts @@ -11,6 +11,7 @@ import { EchoParser } from '../../src/parser/EchoParser'; import { Storage } from '../../src/storage'; import { TYPE_JPEG, TYPE_TEXT } from '../../src/utils/Mime'; import { createService, createServiceContainer } from '../helpers/container'; +import { getTestContextData } from '../helpers/context'; const TEST_CONFIG = { dataMapper: { @@ -61,16 +62,7 @@ describe('echo parser', async () => { const [cmd] = await svc.parse(new Message({ body: 'test message', - context: new Context({ - channel: { - id: 'test', - thread: 'test', - }, - sourceUser: { - name: 'test', - uid: 'test', - }, - }), + context: new Context(getTestContextData()), labels: {}, reactions: [], type: TYPE_TEXT, diff --git a/test/parser/TestRegexParser.ts b/test/parser/TestRegexParser.ts index 20230e1c3..32827912a 100644 --- a/test/parser/TestRegexParser.ts +++ b/test/parser/TestRegexParser.ts @@ -12,6 +12,7 @@ import { RegexParser } from '../../src/parser/RegexParser'; import { Storage } from '../../src/storage'; import { TYPE_JPEG, TYPE_TEXT } from '../../src/utils/Mime'; import { createService, createServiceContainer } from '../helpers/container'; +import { getTestContextData } from '../helpers/context'; const TEST_CONFIG = { dataMapper: { @@ -59,16 +60,7 @@ describe('regex parser', async () => { const body = '0123456789 abcdefghij'; const [cmd] = await svc.parse(new Message({ body, - context: new Context({ - channel: { - id: 'test', - thread: 'test', - }, - sourceUser: { - name: 'test', - uid: 'test', - }, - }), + context: new Context(getTestContextData()), labels: {}, reactions: [], type: TYPE_TEXT, diff --git a/test/parser/TestSplitParser.ts b/test/parser/TestSplitParser.ts index a4a34fd25..8be526f02 100644 --- a/test/parser/TestSplitParser.ts +++ b/test/parser/TestSplitParser.ts @@ -12,6 +12,7 @@ import { Storage } from '../../src/storage'; import { RuleOperator } from '../../src/utils/MatchRules'; import { TYPE_JPEG, TYPE_TEXT } from '../../src/utils/Mime'; import { createService, createServiceContainer } from '../helpers/container'; +import { getTestContextData } from '../helpers/context'; const TEST_CONFIG = { dataMapper: { @@ -67,16 +68,7 @@ describe('split parser', async () => { const commands = await svc.parse(new Message({ body: 'test message', - context: new Context({ - channel: { - id: 'test', - thread: 'test', - }, - sourceUser: { - name: 'test', - uid: 'test', - }, - }), + context: new Context(getTestContextData()), labels: {}, reactions: [], type: TYPE_TEXT, @@ -100,16 +92,7 @@ describe('split parser', async () => { const commands = await svc.parse(new Message({ body: 'test (message group) [second group] bits "third group"', - context: new Context({ - channel: { - id: 'test', - thread: 'test', - }, - sourceUser: { - name: 'test', - uid: 'test', - }, - }), + context: new Context(getTestContextData()), labels: {}, reactions: [], type: TYPE_TEXT, diff --git a/test/parser/TestYamlParser.ts b/test/parser/TestYamlParser.ts index 156b42193..b33989bfe 100644 --- a/test/parser/TestYamlParser.ts +++ b/test/parser/TestYamlParser.ts @@ -12,6 +12,7 @@ import { YamlParser } from '../../src/parser/YamlParser'; import { Storage } from '../../src/storage'; import { TYPE_JPEG, TYPE_YAML } from '../../src/utils/Mime'; import { createService, createServiceContainer } from '../helpers/container'; +import { getTestContextData } from '../helpers/context'; const TEST_CONFIG = { defaultCommand: { @@ -52,16 +53,7 @@ describe('yaml parser', async () => { const [cmd] = await svc.parse(new Message({ body: '{foo: ["1"], bar: ["2"]}', - context: new Context({ - channel: { - id: 'test', - thread: 'test', - }, - sourceUser: { - name: 'test', - uid: 'test', - }, - }), + context: new Context(getTestContextData()), labels: {}, reactions: [], type: TYPE_YAML, diff --git a/test/utils/TestTemplateCompiler.ts b/test/utils/TestTemplateCompiler.ts index 3f03dc123..d7c787441 100644 --- a/test/utils/TestTemplateCompiler.ts +++ b/test/utils/TestTemplateCompiler.ts @@ -4,23 +4,15 @@ import { stub } from 'sinon'; import { Context } from '../../src/entity/Context'; import { TemplateCompiler, testEq } from '../../src/utils/TemplateCompiler'; import { createContainer } from '../helpers/container'; +import { getTestContextData } from '../helpers/context'; describe('template compiler', async () => { describe('format context helper', async () => { it('should prefix a name', async () => { const { container } = await createContainer(); const compiler = await container.create(TemplateCompiler); - const ctx = new Context({ - channel: { - id: '', - thread: '', - }, - sourceUser: { - name: 'foo', - uid: '', - }, - }); - expect(compiler.formatContext(ctx)).to.equal('@foo'); + const ctx = new Context(getTestContextData()); + expect(compiler.formatContext(ctx)).to.equal('@test'); }); });