Skip to content

Commit fd4945d

Browse files
committed
fix(tests): cover gitlab endpoint, base parser
1 parent 2675beb commit fd4945d

File tree

5 files changed

+257
-20
lines changed

5 files changed

+257
-20
lines changed

src/endpoint/GitlabEndpoint.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ export interface GitlabEndpointData extends HookEndpointData {
9595

9696
const STATUS_SUCCESS = 200;
9797
const STATUS_ERROR = 500;
98-
const STATUS_UNKNOWN = 404;
98+
export const STATUS_UNKNOWN = 404;
9999

100100
export class GitlabEndpoint extends HookEndpoint<GitlabEndpointData> implements Endpoint {
101101
constructor(options: BaseEndpointOptions<GitlabEndpointData>) {

test/endpoint/TestGithubEndpoint.ts

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,15 @@ describeLeaks('github endpoint', async () => {
3737
});
3838

3939
describeLeaks('webhook route', async () => {
40-
const endpoint = await createEndpoint(GithubEndpoint, false, false, TEST_DATA);
41-
const sendStatus = spy();
42-
await endpoint.postWebhook(ineeda<Request>({
43-
header: stub().returns([]),
44-
}), ineeda<Response>({
45-
sendStatus,
46-
}));
47-
expect(sendStatus).to.have.been.calledOnce.and.calledWithExactly(STATUS_SUCCESS);
40+
itLeaks('should succeed', async () => {
41+
const endpoint = await createEndpoint(GithubEndpoint, false, false, TEST_DATA);
42+
const sendStatus = spy();
43+
await endpoint.postWebhook(ineeda<Request>({
44+
header: stub().returns([]),
45+
}), ineeda<Response>({
46+
sendStatus,
47+
}));
48+
expect(sendStatus).to.have.been.calledOnce.and.calledWithExactly(STATUS_SUCCESS);
49+
});
4850
});
4951
});

test/endpoint/TestGitlabEndpoint.ts

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import { expect } from 'chai';
2+
import { Request, Response, Router } from 'express';
3+
import { ineeda } from 'ineeda';
4+
import passport from 'passport';
5+
import { spy, stub } from 'sinon';
6+
7+
import { GitlabEndpoint, GitlabEndpointData, STATUS_UNKNOWN } from '../../src/endpoint/GitlabEndpoint';
8+
import { CommandVerb } from '../../src/entity/Command';
9+
import { describeLeaks, itLeaks } from '../helpers/async';
10+
import { createEndpoint } from '../helpers/request';
11+
12+
const TEST_DATA: GitlabEndpointData = {
13+
defaultCommand: {
14+
data: {},
15+
labels: {},
16+
noun: '',
17+
verb: CommandVerb.Create,
18+
},
19+
filters: [],
20+
hookUser: '',
21+
strict: false,
22+
};
23+
24+
describeLeaks('gitlab endpoint', async () => {
25+
itLeaks('should have paths', async () => {
26+
const endpoint = await createEndpoint(GitlabEndpoint, false, false, TEST_DATA);
27+
expect(endpoint.paths.length).to.equal(3);
28+
expect(endpoint.paths).to.include('/gitlab');
29+
});
30+
31+
itLeaks('should configure a router', async () => {
32+
const endpoint = await createEndpoint(GitlabEndpoint, false, false, TEST_DATA);
33+
const post = spy();
34+
const router = ineeda<Router>({
35+
post,
36+
});
37+
const result = await endpoint.createRouter({
38+
passport: ineeda<passport.Authenticator>(),
39+
router,
40+
});
41+
expect(result).to.equal(router, 'must return the passed router');
42+
expect(post).to.have.callCount(1);
43+
});
44+
45+
describeLeaks('webhook route', async () => {
46+
itLeaks('should fail without a body', async () => {
47+
const endpoint = await createEndpoint(GitlabEndpoint, false, false, TEST_DATA);
48+
const sendStatus = spy();
49+
await endpoint.hookSwitch(ineeda<Request>({
50+
header: stub().returns([]),
51+
}), ineeda<Response>({
52+
sendStatus,
53+
}));
54+
expect(sendStatus).to.have.been.calledOnce.and.calledWithExactly(STATUS_UNKNOWN);
55+
});
56+
});
57+
});

test/parser/TestBaseParser.ts

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
import { expect } from 'chai';
2+
import { ineeda } from 'ineeda';
3+
import { Repository } from 'typeorm';
4+
5+
import { BotServiceOptions, INJECT_STORAGE } from '../../src/BotService';
6+
import { User } from '../../src/entity/auth/User';
7+
import { Command, CommandVerb } from '../../src/entity/Command';
8+
import { Context } from '../../src/entity/Context';
9+
import { Fragment } from '../../src/entity/Fragment';
10+
import { Message } from '../../src/entity/Message';
11+
import { ParserData, ParserOutput } from '../../src/parser';
12+
import { BaseParser } from '../../src/parser/BaseParser';
13+
import { Storage } from '../../src/storage';
14+
import { describeLeaks, itLeaks } from '../helpers/async';
15+
import { createService, createServiceContainer } from '../helpers/container';
16+
17+
const TEST_PARSER = 'test-parser';
18+
19+
class TestParser extends BaseParser<ParserData> {
20+
constructor(options: BotServiceOptions<ParserData>) {
21+
super(options, 'isolex#/definitions/service-parser');
22+
}
23+
24+
public async decode(msg: Message): Promise<ParserOutput> {
25+
return {
26+
data: {},
27+
};
28+
}
29+
30+
public async parse(msg: Message): Promise<Array<Command>> {
31+
return [];
32+
}
33+
}
34+
35+
describeLeaks('base parser', async () => {
36+
itLeaks('should match messages', async () => {
37+
const { container } = await createServiceContainer();
38+
const parser = await createService(container, TestParser, {
39+
data: {
40+
defaultCommand: {
41+
data: {},
42+
labels: {},
43+
noun: '',
44+
verb: CommandVerb.Create,
45+
},
46+
filters: [],
47+
match: {
48+
rules: [],
49+
},
50+
preferData: false,
51+
strict: false,
52+
},
53+
metadata: {
54+
kind: TEST_PARSER,
55+
name: TEST_PARSER,
56+
},
57+
});
58+
expect(await parser.match(ineeda<Message>({
59+
body: '',
60+
}))).to.equal(true);
61+
});
62+
63+
itLeaks('should complete fragments', async () => {
64+
const { container } = await createServiceContainer();
65+
const parser = await createService(container, TestParser, {
66+
[INJECT_STORAGE]: ineeda<Storage>({
67+
getRepository: () => {
68+
return ineeda<Repository<Context>>({
69+
save(ctx: Context) {
70+
return ctx;
71+
}
72+
});
73+
},
74+
}),
75+
data: {
76+
defaultCommand: {
77+
data: {},
78+
labels: {},
79+
noun: '',
80+
verb: CommandVerb.Create,
81+
},
82+
filters: [],
83+
match: {
84+
rules: [],
85+
},
86+
preferData: false,
87+
strict: false,
88+
},
89+
metadata: {
90+
kind: TEST_PARSER,
91+
name: TEST_PARSER,
92+
},
93+
});
94+
const results = await parser.complete(new Context({
95+
channel: {
96+
id: '',
97+
thread: '',
98+
},
99+
name: 'test',
100+
uid: '0',
101+
user: ineeda<User>(),
102+
}), new Fragment({
103+
data: {},
104+
key: '',
105+
labels: {},
106+
noun: '',
107+
parserId: '',
108+
userId: '',
109+
verb: CommandVerb.Create,
110+
}), []);
111+
expect(results.length).to.be.greaterThan(0);
112+
});
113+
114+
itLeaks('should prefer the data command');
115+
itLeaks('should prefer the command');
116+
});

test/schema/TestGraph.ts

Lines changed: 73 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,23 @@ import { expect } from 'chai';
22
import { Request } from 'express';
33
import { ineeda } from 'ineeda';
44
import { spy } from 'sinon';
5+
import { Repository } from 'typeorm';
56

67
import { Bot } from '../../src/Bot';
7-
import { INJECT_BOT } from '../../src/BotService';
8+
import { INJECT_BOT, INJECT_STORAGE } from '../../src/BotService';
89
import { User } from '../../src/entity/auth/User';
10+
import { Command } from '../../src/entity/Command';
11+
import { Message } from '../../src/entity/Message';
912
import { GraphSchema } from '../../src/schema/graph';
13+
import { Storage } from '../../src/storage';
1014
import { describeLeaks, itLeaks } from '../helpers/async';
1115
import { createService, createServiceContainer } from '../helpers/container';
1216

17+
const TEST_SCHEMA = {
18+
kind: 'graph-schema',
19+
name: 'test-schema',
20+
};
21+
1322
describeLeaks('graph schema', async () => {
1423
itLeaks('should execute commands', async () => {
1524
const { container } = await createServiceContainer();
@@ -22,10 +31,7 @@ describeLeaks('graph schema', async () => {
2231
filters: [],
2332
strict: false,
2433
},
25-
metadata: {
26-
kind: 'graph-schema',
27-
name: 'test-schema',
28-
},
34+
metadata: TEST_SCHEMA,
2935
});
3036
await graph.executeCommands({
3137
commands: [],
@@ -46,10 +52,7 @@ describeLeaks('graph schema', async () => {
4652
filters: [],
4753
strict: false,
4854
},
49-
metadata: {
50-
kind: 'graph-schema',
51-
name: 'test-schema',
52-
},
55+
metadata: TEST_SCHEMA,
5356
});
5457
await graph.sendMessages({
5558
messages: [],
@@ -58,8 +61,67 @@ describeLeaks('graph schema', async () => {
5861
}));
5962
expect(sendMessage).to.have.callCount(1);
6063
});
61-
itLeaks('should get past commands');
62-
itLeaks('should get past messages');
64+
65+
itLeaks('should get past commands', async () => {
66+
const { container } = await createServiceContainer();
67+
const sendMessage = spy();
68+
const graph = await createService(container, GraphSchema, {
69+
[INJECT_BOT]: ineeda<Bot>({
70+
sendMessage,
71+
}),
72+
[INJECT_STORAGE]: ineeda<Storage>({
73+
getRepository: () => {
74+
return ineeda<Repository<Command>>({
75+
async findOne(id: string) {
76+
return ineeda.instanceof(Command);
77+
}
78+
});
79+
},
80+
}),
81+
data: {
82+
filters: [],
83+
strict: false,
84+
},
85+
metadata: TEST_SCHEMA,
86+
});
87+
const command = await graph.getCommand({
88+
id: '0',
89+
}, ineeda<Request>({
90+
user: ineeda<User>(),
91+
}));
92+
expect(Command.isCommand(command)).to.equal(true);
93+
});
94+
95+
itLeaks('should get past messages', async () => {
96+
const { container } = await createServiceContainer();
97+
const sendMessage = spy();
98+
const graph = await createService(container, GraphSchema, {
99+
[INJECT_BOT]: ineeda<Bot>({
100+
sendMessage,
101+
}),
102+
[INJECT_STORAGE]: ineeda<Storage>({
103+
getRepository: () => {
104+
return ineeda<Repository<Message>>({
105+
async findOne(id: string) {
106+
return ineeda.instanceof(Message);
107+
}
108+
});
109+
},
110+
}),
111+
data: {
112+
filters: [],
113+
strict: false,
114+
},
115+
metadata: TEST_SCHEMA,
116+
});
117+
const message = await graph.getMessage({
118+
id: '0',
119+
}, ineeda<Request>({
120+
user: ineeda<User>(),
121+
}));
122+
expect(Message.isMessage(message)).to.equal(true);
123+
});
124+
63125
itLeaks('should get existing services');
64126
itLeaks('should get a single service');
65127
});

0 commit comments

Comments
 (0)