Skip to content

Commit

Permalink
fix(tests): cover tick and token entities, various error handling bra…
Browse files Browse the repository at this point in the history
…nches
  • Loading branch information
ssube committed Oct 14, 2019
1 parent 2b2d4ab commit f07a116
Show file tree
Hide file tree
Showing 8 changed files with 193 additions and 17 deletions.
1 change: 1 addition & 0 deletions src/entity/Tick.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ export class Tick extends BaseEntity {
createdAt: this.createdAt,
id: this.id,
intervalId: this.intervalId,
status: this.status,
updatedAt: this.updatedAt,
};
}
Expand Down
10 changes: 2 additions & 8 deletions src/parser/YamlParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,8 @@ export class YamlParser extends BaseParser<YamlParserData> implements Parser {
public async parse(msg: Message): Promise<Array<Command>> {
const ctx = mustExist(msg.context);
const data = await this.decode(msg);

if (isObject(data)) {
// TODO: this cast and conversion should not be necessary
const map = makeMap(data.data);
return [await this.createCommand(ctx, map)];
} else {
return [];
}
const map = makeMap(data.data);
return [await this.createCommand(ctx, map)];
}

public async decode(msg: Message): Promise<ParserOutput> {
Expand Down
65 changes: 61 additions & 4 deletions test/TestBot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,16 @@ import { LogLevel } from 'noicejs';
import { Registry } from 'prom-client';
import { spy } from 'sinon';

import { INJECT_METRICS, INJECT_SCHEMA } from '../src/BaseService';
import { INJECT_LOGGER, INJECT_METRICS, INJECT_SCHEMA } from '../src/BaseService';
import { Bot, BotData } from '../src/Bot';
import { Command, CommandVerb } from '../src/entity/Command';
import { BotModule } from '../src/module/BotModule';
import { EntityModule } from '../src/module/EntityModule';
import { MigrationModule } from '../src/module/MigrationModule';
import { ServiceModule } from '../src/module/ServiceModule';
import { Schema } from '../src/schema';
import { ServiceEvent } from '../src/Service';
import { defer } from '../src/utils/Async';
import { describeLeaks, itLeaks } from './helpers/async';
import { createContainer } from './helpers/container';
import { getTestLogger } from './helpers/logger';
Expand All @@ -36,17 +40,17 @@ const TEST_CONFIG: BotData = {
parsers: [],
process: {
pid: {
file: '../out/isolex.pid',
file: './out/test.pid',
},
},
services: {
timeout: 1000,
},
storage: {
data: {
migrate: false,
migrate: true,
orm: {
database: '',
database: './out/test.db',
type: 'sqlite',
},
},
Expand Down Expand Up @@ -97,4 +101,57 @@ describeLeaks('bot service', async () => {
});
expect(bot.isConnected).to.equal(false);
});

xit('should execute commands', async () => {
const { container } = await createContainer(new BotModule({
logger: getTestLogger(),
}), new ServiceModule({
timeout: 1000,
}), new EntityModule(), new MigrationModule());
const bot = await container.create(Bot, {
[INJECT_LOGGER]: getTestLogger(true),
[INJECT_SCHEMA]: new Schema(),
data: TEST_CONFIG,
metadata: {
kind: 'bot',
name: 'test-bot',
},
});
await bot.start();
const results = await bot.executeCommand(new Command({
data: {},
labels: {},
noun: 'test',
verb: CommandVerb.Create,
}));
await defer(50);
await bot.stop();
expect(results.length).to.equal(0);
});

xit('should send messages', async () => {
const { container } = await createContainer(new BotModule({
logger: getTestLogger(),
}), new ServiceModule({
timeout: 1000,
}), new EntityModule(), new MigrationModule());
const bot = await container.create(Bot, {
[INJECT_SCHEMA]: new Schema(),
data: TEST_CONFIG,
metadata: {
kind: 'bot',
name: 'test-bot',
},
});
await bot.start();
const results = await bot.sendMessage(/* new Message({
body: 'test',
labels: {},
reactions: [],
type: TYPE_TEXT,
}) */);
await defer(50);
await bot.stop();
expect(results.length).to.equal(0);
});
});
16 changes: 16 additions & 0 deletions test/entity/TestTick.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { expect } from 'chai';

import { Tick } from '../../src/entity/Tick';
import { describeLeaks, itLeaks } from '../helpers/async';

describeLeaks('tick entity', async () => {
itLeaks('should convert itself to JSON', async () => {
const tick = new Tick({
intervalId: '0',
status: 0,
});
const json = tick.toJSON();
expect(json).to.have.property('intervalId');
expect(json).to.have.property('status');
});
});
51 changes: 51 additions & 0 deletions test/entity/auth/TestToken.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,55 @@ describeLeaks('token entity', async () => {
const token = new Token(data);
expect(token).to.deep.include(testProps);
});

itLeaks('should sign tokens', async () => {
const token = new Token({
audience: ['test'],
createdAt: new Date(),
data: {},
expiresAt: new Date(),
grants: [],
issuer: 'test',
labels: {},
subject: 'test',
});
token.id = 'test';
const signed = token.sign('test');
expect(signed).to.match(/[-_a-zA-Z0-9]+\.[-_a-zA-Z0-9]+\.[-_a-zA-Z0-9]+/);
});

itLeaks('should convert itself to json', async () => {
const token = new Token({
audience: ['test'],
createdAt: new Date(),
data: {},
expiresAt: new Date(),
grants: [],
issuer: 'test',
labels: {},
subject: 'test',
});
const json = token.toJSON();
expect(json).to.have.property('audience');
expect(json).to.have.property('issuer');
expect(json).to.have.property('subject');
});

itLeaks('should check grants', async () => {
const token = new Token({
audience: ['test'],
createdAt: new Date(),
data: {},
expiresAt: new Date(),
grants: ['test:*'],
issuer: 'test',
labels: {},
subject: 'test',
});
expect(token.permit(['fail:foo'])).to.equal(false);
expect(token.permit(['test:foo'])).to.equal(true);
});

itLeaks('should issue sessions with a user');
itLeaks('should not issue sessions without a user');
});
28 changes: 26 additions & 2 deletions test/filter/TestMessageFilter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { Context } from '../../src/entity/Context';
import { Message } from '../../src/entity/Message';
import { FilterBehavior } from '../../src/filter';
import { MessageFilter, MessageFilterData } from '../../src/filter/MessageFilter';
import { RuleOperator } from '../../src/utils/match';
import { TYPE_TEXT } from '../../src/utils/Mime';
import { describeLeaks, itLeaks } from '../helpers/async';
import { createService, createServiceContainer } from '../helpers/container';
Expand Down Expand Up @@ -44,8 +45,7 @@ describeLeaks('message filter', async () => {
expect(result).to.equal(FilterBehavior.Allow);
});

xit('should drop other messages');
it('should ignore other entities', async () => {
itLeaks('should ignore other entities', async () => {
const { filter } = await createFilter({
filters: [],
match: {
Expand All @@ -62,4 +62,28 @@ describeLeaks('message filter', async () => {
}));
expect(result).to.equal(FilterBehavior.Ignore);
});

it('should reject unmatched messages', async () => {
const { filter } = await createFilter({
filters: [],
match: {
rules: [{
key: '$.type',
operator: RuleOperator.Every,
values: [{
string: 'jpeg',
}],
}],
},
strict: true,
});
const result = await filter.check(new Message({
body: '',
context: ineeda<Context>(),
labels: {},
reactions: [],
type: TYPE_TEXT,
}));
expect(result).to.equal(FilterBehavior.Drop);
});
});
22 changes: 22 additions & 0 deletions test/parser/TestYamlParser.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { expect } from 'chai';
import { ineeda } from 'ineeda';
import { BaseError } from 'noicejs';
import { Repository } from 'typeorm';

import { INJECT_STORAGE } from '../../src/BotService';
Expand Down Expand Up @@ -88,4 +89,25 @@ describeLeaks('yaml parser', async () => {
});
return expect(svc.parse(msg)).to.eventually.be.rejectedWith(MimeTypeError);
});

it('should throw when the root value is not an object', async () => {
const { container } = await createServiceContainer();
const svc = await createService(container, YamlParser, {
[INJECT_STORAGE]: TEST_STORAGE,
data: TEST_CONFIG,
metadata: {
kind: 'test',
name: 'test',
},
});

const msg = new Message({
body: '[]',
context: ineeda<Context>(),
labels: {},
reactions: [],
type: TYPE_JPEG,
});
return expect(svc.parse(msg)).to.eventually.be.rejectedWith(BaseError);
});
});
17 changes: 14 additions & 3 deletions test/schema/TestGraph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@ import { Repository } from 'typeorm';
import { Bot } from '../../src/Bot';
import { INJECT_BOT, INJECT_STORAGE } from '../../src/BotService';
import { User } from '../../src/entity/auth/User';
import { Command } from '../../src/entity/Command';
import { Command, CommandVerb } from '../../src/entity/Command';
import { Message } from '../../src/entity/Message';
import { GraphSchema } from '../../src/schema/graph';
import { Service } from '../../src/Service';
import { Storage } from '../../src/storage';
import { TYPE_TEXT } from '../../src/utils/Mime';
import { describeLeaks, itLeaks } from '../helpers/async';
import { createService, createServiceContainer } from '../helpers/container';

Expand All @@ -36,7 +37,12 @@ describeLeaks('graph schema', async () => {
metadata: TEST_SCHEMA,
});
await graph.executeCommands({
commands: [],
commands: [{
data: [],
labels: [],
noun: 'test',
verb: CommandVerb.Create,
}],
}, ineeda<Request>({
user: ineeda<User>(),
}));
Expand All @@ -57,7 +63,12 @@ describeLeaks('graph schema', async () => {
metadata: TEST_SCHEMA,
});
await graph.sendMessages({
messages: [],
messages: [{
body: 'msg',
labels: [],
reactions: [],
type: TYPE_TEXT,
}],
}, ineeda<Request>({
user: ineeda<User>(),
}));
Expand Down

0 comments on commit f07a116

Please sign in to comment.