Skip to content

Commit

Permalink
fix(tests): cover gitlab endpoint, base parser
Browse files Browse the repository at this point in the history
  • Loading branch information
ssube committed Oct 11, 2019
1 parent 2675beb commit fd4945d
Show file tree
Hide file tree
Showing 5 changed files with 257 additions and 20 deletions.
2 changes: 1 addition & 1 deletion src/endpoint/GitlabEndpoint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ export interface GitlabEndpointData extends HookEndpointData {

const STATUS_SUCCESS = 200;
const STATUS_ERROR = 500;
const STATUS_UNKNOWN = 404;
export const STATUS_UNKNOWN = 404;

export class GitlabEndpoint extends HookEndpoint<GitlabEndpointData> implements Endpoint {
constructor(options: BaseEndpointOptions<GitlabEndpointData>) {
Expand Down
18 changes: 10 additions & 8 deletions test/endpoint/TestGithubEndpoint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,15 @@ describeLeaks('github endpoint', async () => {
});

describeLeaks('webhook route', async () => {
const endpoint = await createEndpoint(GithubEndpoint, false, false, TEST_DATA);
const sendStatus = spy();
await endpoint.postWebhook(ineeda<Request>({
header: stub().returns([]),
}), ineeda<Response>({
sendStatus,
}));
expect(sendStatus).to.have.been.calledOnce.and.calledWithExactly(STATUS_SUCCESS);
itLeaks('should succeed', async () => {
const endpoint = await createEndpoint(GithubEndpoint, false, false, TEST_DATA);
const sendStatus = spy();
await endpoint.postWebhook(ineeda<Request>({
header: stub().returns([]),
}), ineeda<Response>({
sendStatus,
}));
expect(sendStatus).to.have.been.calledOnce.and.calledWithExactly(STATUS_SUCCESS);
});
});
});
57 changes: 57 additions & 0 deletions test/endpoint/TestGitlabEndpoint.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import { expect } from 'chai';
import { Request, Response, Router } from 'express';
import { ineeda } from 'ineeda';
import passport from 'passport';
import { spy, stub } from 'sinon';

import { GitlabEndpoint, GitlabEndpointData, STATUS_UNKNOWN } from '../../src/endpoint/GitlabEndpoint';
import { CommandVerb } from '../../src/entity/Command';
import { describeLeaks, itLeaks } from '../helpers/async';
import { createEndpoint } from '../helpers/request';

const TEST_DATA: GitlabEndpointData = {
defaultCommand: {
data: {},
labels: {},
noun: '',
verb: CommandVerb.Create,
},
filters: [],
hookUser: '',
strict: false,
};

describeLeaks('gitlab endpoint', async () => {
itLeaks('should have paths', async () => {
const endpoint = await createEndpoint(GitlabEndpoint, false, false, TEST_DATA);
expect(endpoint.paths.length).to.equal(3);
expect(endpoint.paths).to.include('/gitlab');
});

itLeaks('should configure a router', async () => {
const endpoint = await createEndpoint(GitlabEndpoint, false, false, TEST_DATA);
const post = spy();
const router = ineeda<Router>({
post,
});
const result = await endpoint.createRouter({
passport: ineeda<passport.Authenticator>(),
router,
});
expect(result).to.equal(router, 'must return the passed router');
expect(post).to.have.callCount(1);
});

describeLeaks('webhook route', async () => {
itLeaks('should fail without a body', async () => {
const endpoint = await createEndpoint(GitlabEndpoint, false, false, TEST_DATA);
const sendStatus = spy();
await endpoint.hookSwitch(ineeda<Request>({
header: stub().returns([]),
}), ineeda<Response>({
sendStatus,
}));
expect(sendStatus).to.have.been.calledOnce.and.calledWithExactly(STATUS_UNKNOWN);
});
});
});
116 changes: 116 additions & 0 deletions test/parser/TestBaseParser.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
import { expect } from 'chai';
import { ineeda } from 'ineeda';
import { Repository } from 'typeorm';

import { BotServiceOptions, INJECT_STORAGE } from '../../src/BotService';
import { User } from '../../src/entity/auth/User';
import { Command, CommandVerb } from '../../src/entity/Command';
import { Context } from '../../src/entity/Context';
import { Fragment } from '../../src/entity/Fragment';
import { Message } from '../../src/entity/Message';
import { ParserData, ParserOutput } from '../../src/parser';
import { BaseParser } from '../../src/parser/BaseParser';
import { Storage } from '../../src/storage';
import { describeLeaks, itLeaks } from '../helpers/async';
import { createService, createServiceContainer } from '../helpers/container';

const TEST_PARSER = 'test-parser';

class TestParser extends BaseParser<ParserData> {
constructor(options: BotServiceOptions<ParserData>) {
super(options, 'isolex#/definitions/service-parser');
}

public async decode(msg: Message): Promise<ParserOutput> {
return {
data: {},
};
}

public async parse(msg: Message): Promise<Array<Command>> {
return [];
}
}

describeLeaks('base parser', async () => {
itLeaks('should match messages', async () => {
const { container } = await createServiceContainer();
const parser = await createService(container, TestParser, {
data: {
defaultCommand: {
data: {},
labels: {},
noun: '',
verb: CommandVerb.Create,
},
filters: [],
match: {
rules: [],
},
preferData: false,
strict: false,
},
metadata: {
kind: TEST_PARSER,
name: TEST_PARSER,
},
});
expect(await parser.match(ineeda<Message>({
body: '',
}))).to.equal(true);
});

itLeaks('should complete fragments', async () => {
const { container } = await createServiceContainer();
const parser = await createService(container, TestParser, {
[INJECT_STORAGE]: ineeda<Storage>({
getRepository: () => {
return ineeda<Repository<Context>>({
save(ctx: Context) {
return ctx;
}
});
},
}),
data: {
defaultCommand: {
data: {},
labels: {},
noun: '',
verb: CommandVerb.Create,
},
filters: [],
match: {
rules: [],
},
preferData: false,
strict: false,
},
metadata: {
kind: TEST_PARSER,
name: TEST_PARSER,
},
});
const results = await parser.complete(new Context({
channel: {
id: '',
thread: '',
},
name: 'test',
uid: '0',
user: ineeda<User>(),
}), new Fragment({
data: {},
key: '',
labels: {},
noun: '',
parserId: '',
userId: '',
verb: CommandVerb.Create,
}), []);
expect(results.length).to.be.greaterThan(0);
});

itLeaks('should prefer the data command');
itLeaks('should prefer the command');
});
84 changes: 73 additions & 11 deletions test/schema/TestGraph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,23 @@ import { expect } from 'chai';
import { Request } from 'express';
import { ineeda } from 'ineeda';
import { spy } from 'sinon';
import { Repository } from 'typeorm';

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

const TEST_SCHEMA = {
kind: 'graph-schema',
name: 'test-schema',
};

describeLeaks('graph schema', async () => {
itLeaks('should execute commands', async () => {
const { container } = await createServiceContainer();
Expand All @@ -22,10 +31,7 @@ describeLeaks('graph schema', async () => {
filters: [],
strict: false,
},
metadata: {
kind: 'graph-schema',
name: 'test-schema',
},
metadata: TEST_SCHEMA,
});
await graph.executeCommands({
commands: [],
Expand All @@ -46,10 +52,7 @@ describeLeaks('graph schema', async () => {
filters: [],
strict: false,
},
metadata: {
kind: 'graph-schema',
name: 'test-schema',
},
metadata: TEST_SCHEMA,
});
await graph.sendMessages({
messages: [],
Expand All @@ -58,8 +61,67 @@ describeLeaks('graph schema', async () => {
}));
expect(sendMessage).to.have.callCount(1);
});
itLeaks('should get past commands');
itLeaks('should get past messages');

itLeaks('should get past commands', async () => {
const { container } = await createServiceContainer();
const sendMessage = spy();
const graph = await createService(container, GraphSchema, {
[INJECT_BOT]: ineeda<Bot>({
sendMessage,
}),
[INJECT_STORAGE]: ineeda<Storage>({
getRepository: () => {
return ineeda<Repository<Command>>({
async findOne(id: string) {
return ineeda.instanceof(Command);
}
});
},
}),
data: {
filters: [],
strict: false,
},
metadata: TEST_SCHEMA,
});
const command = await graph.getCommand({
id: '0',
}, ineeda<Request>({
user: ineeda<User>(),
}));
expect(Command.isCommand(command)).to.equal(true);
});

itLeaks('should get past messages', async () => {
const { container } = await createServiceContainer();
const sendMessage = spy();
const graph = await createService(container, GraphSchema, {
[INJECT_BOT]: ineeda<Bot>({
sendMessage,
}),
[INJECT_STORAGE]: ineeda<Storage>({
getRepository: () => {
return ineeda<Repository<Message>>({
async findOne(id: string) {
return ineeda.instanceof(Message);
}
});
},
}),
data: {
filters: [],
strict: false,
},
metadata: TEST_SCHEMA,
});
const message = await graph.getMessage({
id: '0',
}, ineeda<Request>({
user: ineeda<User>(),
}));
expect(Message.isMessage(message)).to.equal(true);
});

itLeaks('should get existing services');
itLeaks('should get a single service');
});

0 comments on commit fd4945d

Please sign in to comment.