Skip to content

Commit f07a116

Browse files
committed
fix(tests): cover tick and token entities, various error handling branches
1 parent 2b2d4ab commit f07a116

File tree

8 files changed

+193
-17
lines changed

8 files changed

+193
-17
lines changed

src/entity/Tick.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ export class Tick extends BaseEntity {
3636
createdAt: this.createdAt,
3737
id: this.id,
3838
intervalId: this.intervalId,
39+
status: this.status,
3940
updatedAt: this.updatedAt,
4041
};
4142
}

src/parser/YamlParser.ts

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,8 @@ export class YamlParser extends BaseParser<YamlParserData> implements Parser {
2424
public async parse(msg: Message): Promise<Array<Command>> {
2525
const ctx = mustExist(msg.context);
2626
const data = await this.decode(msg);
27-
28-
if (isObject(data)) {
29-
// TODO: this cast and conversion should not be necessary
30-
const map = makeMap(data.data);
31-
return [await this.createCommand(ctx, map)];
32-
} else {
33-
return [];
34-
}
27+
const map = makeMap(data.data);
28+
return [await this.createCommand(ctx, map)];
3529
}
3630

3731
public async decode(msg: Message): Promise<ParserOutput> {

test/TestBot.ts

Lines changed: 61 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,16 @@ import { LogLevel } from 'noicejs';
44
import { Registry } from 'prom-client';
55
import { spy } from 'sinon';
66

7-
import { INJECT_METRICS, INJECT_SCHEMA } from '../src/BaseService';
7+
import { INJECT_LOGGER, INJECT_METRICS, INJECT_SCHEMA } from '../src/BaseService';
88
import { Bot, BotData } from '../src/Bot';
9+
import { Command, CommandVerb } from '../src/entity/Command';
910
import { BotModule } from '../src/module/BotModule';
11+
import { EntityModule } from '../src/module/EntityModule';
12+
import { MigrationModule } from '../src/module/MigrationModule';
1013
import { ServiceModule } from '../src/module/ServiceModule';
1114
import { Schema } from '../src/schema';
1215
import { ServiceEvent } from '../src/Service';
16+
import { defer } from '../src/utils/Async';
1317
import { describeLeaks, itLeaks } from './helpers/async';
1418
import { createContainer } from './helpers/container';
1519
import { getTestLogger } from './helpers/logger';
@@ -36,17 +40,17 @@ const TEST_CONFIG: BotData = {
3640
parsers: [],
3741
process: {
3842
pid: {
39-
file: '../out/isolex.pid',
43+
file: './out/test.pid',
4044
},
4145
},
4246
services: {
4347
timeout: 1000,
4448
},
4549
storage: {
4650
data: {
47-
migrate: false,
51+
migrate: true,
4852
orm: {
49-
database: '',
53+
database: './out/test.db',
5054
type: 'sqlite',
5155
},
5256
},
@@ -97,4 +101,57 @@ describeLeaks('bot service', async () => {
97101
});
98102
expect(bot.isConnected).to.equal(false);
99103
});
104+
105+
xit('should execute commands', async () => {
106+
const { container } = await createContainer(new BotModule({
107+
logger: getTestLogger(),
108+
}), new ServiceModule({
109+
timeout: 1000,
110+
}), new EntityModule(), new MigrationModule());
111+
const bot = await container.create(Bot, {
112+
[INJECT_LOGGER]: getTestLogger(true),
113+
[INJECT_SCHEMA]: new Schema(),
114+
data: TEST_CONFIG,
115+
metadata: {
116+
kind: 'bot',
117+
name: 'test-bot',
118+
},
119+
});
120+
await bot.start();
121+
const results = await bot.executeCommand(new Command({
122+
data: {},
123+
labels: {},
124+
noun: 'test',
125+
verb: CommandVerb.Create,
126+
}));
127+
await defer(50);
128+
await bot.stop();
129+
expect(results.length).to.equal(0);
130+
});
131+
132+
xit('should send messages', async () => {
133+
const { container } = await createContainer(new BotModule({
134+
logger: getTestLogger(),
135+
}), new ServiceModule({
136+
timeout: 1000,
137+
}), new EntityModule(), new MigrationModule());
138+
const bot = await container.create(Bot, {
139+
[INJECT_SCHEMA]: new Schema(),
140+
data: TEST_CONFIG,
141+
metadata: {
142+
kind: 'bot',
143+
name: 'test-bot',
144+
},
145+
});
146+
await bot.start();
147+
const results = await bot.sendMessage(/* new Message({
148+
body: 'test',
149+
labels: {},
150+
reactions: [],
151+
type: TYPE_TEXT,
152+
}) */);
153+
await defer(50);
154+
await bot.stop();
155+
expect(results.length).to.equal(0);
156+
});
100157
});

test/entity/TestTick.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { expect } from 'chai';
2+
3+
import { Tick } from '../../src/entity/Tick';
4+
import { describeLeaks, itLeaks } from '../helpers/async';
5+
6+
describeLeaks('tick entity', async () => {
7+
itLeaks('should convert itself to JSON', async () => {
8+
const tick = new Tick({
9+
intervalId: '0',
10+
status: 0,
11+
});
12+
const json = tick.toJSON();
13+
expect(json).to.have.property('intervalId');
14+
expect(json).to.have.property('status');
15+
});
16+
});

test/entity/auth/TestToken.ts

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,55 @@ describeLeaks('token entity', async () => {
2020
const token = new Token(data);
2121
expect(token).to.deep.include(testProps);
2222
});
23+
24+
itLeaks('should sign tokens', async () => {
25+
const token = new Token({
26+
audience: ['test'],
27+
createdAt: new Date(),
28+
data: {},
29+
expiresAt: new Date(),
30+
grants: [],
31+
issuer: 'test',
32+
labels: {},
33+
subject: 'test',
34+
});
35+
token.id = 'test';
36+
const signed = token.sign('test');
37+
expect(signed).to.match(/[-_a-zA-Z0-9]+\.[-_a-zA-Z0-9]+\.[-_a-zA-Z0-9]+/);
38+
});
39+
40+
itLeaks('should convert itself to json', async () => {
41+
const token = new Token({
42+
audience: ['test'],
43+
createdAt: new Date(),
44+
data: {},
45+
expiresAt: new Date(),
46+
grants: [],
47+
issuer: 'test',
48+
labels: {},
49+
subject: 'test',
50+
});
51+
const json = token.toJSON();
52+
expect(json).to.have.property('audience');
53+
expect(json).to.have.property('issuer');
54+
expect(json).to.have.property('subject');
55+
});
56+
57+
itLeaks('should check grants', async () => {
58+
const token = new Token({
59+
audience: ['test'],
60+
createdAt: new Date(),
61+
data: {},
62+
expiresAt: new Date(),
63+
grants: ['test:*'],
64+
issuer: 'test',
65+
labels: {},
66+
subject: 'test',
67+
});
68+
expect(token.permit(['fail:foo'])).to.equal(false);
69+
expect(token.permit(['test:foo'])).to.equal(true);
70+
});
71+
72+
itLeaks('should issue sessions with a user');
73+
itLeaks('should not issue sessions without a user');
2374
});

test/filter/TestMessageFilter.ts

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { Context } from '../../src/entity/Context';
66
import { Message } from '../../src/entity/Message';
77
import { FilterBehavior } from '../../src/filter';
88
import { MessageFilter, MessageFilterData } from '../../src/filter/MessageFilter';
9+
import { RuleOperator } from '../../src/utils/match';
910
import { TYPE_TEXT } from '../../src/utils/Mime';
1011
import { describeLeaks, itLeaks } from '../helpers/async';
1112
import { createService, createServiceContainer } from '../helpers/container';
@@ -44,8 +45,7 @@ describeLeaks('message filter', async () => {
4445
expect(result).to.equal(FilterBehavior.Allow);
4546
});
4647

47-
xit('should drop other messages');
48-
it('should ignore other entities', async () => {
48+
itLeaks('should ignore other entities', async () => {
4949
const { filter } = await createFilter({
5050
filters: [],
5151
match: {
@@ -62,4 +62,28 @@ describeLeaks('message filter', async () => {
6262
}));
6363
expect(result).to.equal(FilterBehavior.Ignore);
6464
});
65+
66+
it('should reject unmatched messages', async () => {
67+
const { filter } = await createFilter({
68+
filters: [],
69+
match: {
70+
rules: [{
71+
key: '$.type',
72+
operator: RuleOperator.Every,
73+
values: [{
74+
string: 'jpeg',
75+
}],
76+
}],
77+
},
78+
strict: true,
79+
});
80+
const result = await filter.check(new Message({
81+
body: '',
82+
context: ineeda<Context>(),
83+
labels: {},
84+
reactions: [],
85+
type: TYPE_TEXT,
86+
}));
87+
expect(result).to.equal(FilterBehavior.Drop);
88+
});
6589
});

test/parser/TestYamlParser.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { expect } from 'chai';
22
import { ineeda } from 'ineeda';
3+
import { BaseError } from 'noicejs';
34
import { Repository } from 'typeorm';
45

56
import { INJECT_STORAGE } from '../../src/BotService';
@@ -88,4 +89,25 @@ describeLeaks('yaml parser', async () => {
8889
});
8990
return expect(svc.parse(msg)).to.eventually.be.rejectedWith(MimeTypeError);
9091
});
92+
93+
it('should throw when the root value is not an object', async () => {
94+
const { container } = await createServiceContainer();
95+
const svc = await createService(container, YamlParser, {
96+
[INJECT_STORAGE]: TEST_STORAGE,
97+
data: TEST_CONFIG,
98+
metadata: {
99+
kind: 'test',
100+
name: 'test',
101+
},
102+
});
103+
104+
const msg = new Message({
105+
body: '[]',
106+
context: ineeda<Context>(),
107+
labels: {},
108+
reactions: [],
109+
type: TYPE_JPEG,
110+
});
111+
return expect(svc.parse(msg)).to.eventually.be.rejectedWith(BaseError);
112+
});
91113
});

test/schema/TestGraph.ts

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,12 @@ import { Repository } from 'typeorm';
77
import { Bot } from '../../src/Bot';
88
import { INJECT_BOT, INJECT_STORAGE } from '../../src/BotService';
99
import { User } from '../../src/entity/auth/User';
10-
import { Command } from '../../src/entity/Command';
10+
import { Command, CommandVerb } from '../../src/entity/Command';
1111
import { Message } from '../../src/entity/Message';
1212
import { GraphSchema } from '../../src/schema/graph';
1313
import { Service } from '../../src/Service';
1414
import { Storage } from '../../src/storage';
15+
import { TYPE_TEXT } from '../../src/utils/Mime';
1516
import { describeLeaks, itLeaks } from '../helpers/async';
1617
import { createService, createServiceContainer } from '../helpers/container';
1718

@@ -36,7 +37,12 @@ describeLeaks('graph schema', async () => {
3637
metadata: TEST_SCHEMA,
3738
});
3839
await graph.executeCommands({
39-
commands: [],
40+
commands: [{
41+
data: [],
42+
labels: [],
43+
noun: 'test',
44+
verb: CommandVerb.Create,
45+
}],
4046
}, ineeda<Request>({
4147
user: ineeda<User>(),
4248
}));
@@ -57,7 +63,12 @@ describeLeaks('graph schema', async () => {
5763
metadata: TEST_SCHEMA,
5864
});
5965
await graph.sendMessages({
60-
messages: [],
66+
messages: [{
67+
body: 'msg',
68+
labels: [],
69+
reactions: [],
70+
type: TYPE_TEXT,
71+
}],
6172
}, ineeda<Request>({
6273
user: ineeda<User>(),
6374
}));

0 commit comments

Comments
 (0)