-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(tests): cover gitlab endpoint, base parser
- Loading branch information
Showing
5 changed files
with
257 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters