botium-core
Version:
The Selenium for Chatbots
259 lines (231 loc) • 8.01 kB
JavaScript
const path = require('path')
const chai = require('chai')
const chaiAsPromised = require('chai-as-promised')
chai.use(chaiAsPromised)
const assert = chai.assert
const BotDriver = require('../../').BotDriver
const Capabilities = require('../../').Capabilities
const HookUtils = require('../../src/helpers/HookUtils')
const myCapsSimpleRest = {
[]: 'simplerest',
[]: 'http://my-non-existing-botium-host.com/api/endpoint',
[]: 'POST',
[]: false,
[]: true,
[]: ['$']
}
const _getSimpleRestCaps = (caps) => {
return Object.assign(
{},
myCapsSimpleRest,
caps || {}
)
}
const emptyMsg = {}
const functionConnector = ({ queueBotSays }) => {
return {
UserSays (msg) {
const botMsg = {
sender: 'bot',
sourceData: msg.sourceData,
messageText: `Response of ${msg.messageText}`
}
queueBotSays(botMsg)
}
}
}
describe('security.allowUnsafe', function () {
describe('hook utils', function () {
it('should accept javascript hook in safe mode', async function () {
HookUtils.getHook({
[]: false
}, () => null)
})
it('should not accept file hook in safe mode', async function () {
try {
HookUtils.getHook({
[]: false
}, 'test/security/resources/hook-as-file.js')
assert.fail('should have failed')
} catch (err) {
console.log(err.message)
assert.isTrue(err.message.indexOf('invalid') >= 0)
}
})
it('should accept file hook from safe dir in unsafe mode', async function () {
HookUtils.getHook({
[]: true,
[]: 'test/security/'
}, 'resources/hook-as-file.js')
})
it('should not accept file hook from outside safe dir', async function () {
try {
HookUtils.getHook({
[]: true,
[]: 'test/security/'
}, '../hook-as-file.js')
assert.fail('should have failed')
} catch (err) {
assert.isTrue(err.message.indexOf('invalid') >= 0)
}
})
it('should accept require hook in unsafe mode', async function () {
HookUtils.getHook({
[]: true
}, 'hook-as-file')
})
})
describe('simple rest, scripting memory', function () {
it('should use variables succesful', async function () {
const myCaps = _getSimpleRestCaps(
{
[]: {
FUNCTION_WITHOUT_PARAM: '{{fnc.year}}'
}
}
)
const driver = new BotDriver(myCaps)
const container = await driver.Build()
await container.Start()
await container.pluginInstance._buildRequest(emptyMsg)
await container.Clean()
})
it('should use env variables succesful', async function () {
const driver = new BotDriver(_getSimpleRestCaps(
{
[]: {
SAMPLE_ENV: '{{#fnc.env}}SAMPLE_ENV{{/fnc.env}}'
}
}
))
const container = await driver.Build()
await container.Start()
await container.pluginInstance._buildRequest(emptyMsg)
await container.Clean()
})
})
describe('simple rest, hooks', function () {
it('should create and use simplerest with function hooks', async function () {
const driver = new BotDriver(_getSimpleRestCaps(
{
[]: () => 2
}
))
const compiler = driver.BuildCompiler()
const container = await driver.Build()
await container.Start()
compiler.ReadScript(path.resolve(__dirname, 'convos'), 'dummy.convo.txt')
})
})
describe('logic hook, asserter', function () {
it('should load asserter from file', async function () {
const driver = new BotDriver(_getSimpleRestCaps(
{
[]: [
{
ref: 'as-file'
}
]
}
))
driver.BuildCompiler()
})
})
describe('connectors', function () {
it('should create simplerest', async function () {
const driver = new BotDriver(myCapsSimpleRest)
await driver.Build()
})
it('should create any connector from file/dir with added botium-connector prefix', async function () {
const driver = new BotDriver({
[]: 'as-file',
[]: false
})
await driver.Build()
})
it('should create any connector from safedir', async function () {
const driver = new BotDriver({
[]: 'test/security/',
[]: 'resources/botium-connector-as-file.js',
[]: false
})
await driver.Build()
})
it('should create any function connectors', async function () {
const myCapsFunction = {
[]: 'security.allowUnsafe.connectors',
[]: functionConnector,
[]: true,
[]: false
}
const driver = new BotDriver(myCapsFunction)
await driver.Build()
})
})
describe('media input', function () {
it('should fail for downloadMedia global arg without baseDir', async function () {
const args = {
downloadMedia: true
}
const driver = new BotDriver(_getSimpleRestCaps({
[]: [
{
ref: 'MEDIA',
src: 'MediaInput',
args
}
]
}))
const compiler = driver.BuildCompiler()
const container = await driver.Build()
await container.Start()
try {
compiler.ReadScript(path.resolve(__dirname, 'convos'), 'media.convo.txt')
await compiler.convos[0].Run(container)
assert.fail('should have failed')
} catch (err) {
assert.isTrue(err.message.indexOf('Security Error. Using base dir global argument in MediaInput is required') >= 0)
}
await container && container.Clean()
})
it('should fail for downloadMedia global arg as file', async function () {
const args = {
downloadMedia: true
}
const driver = new BotDriver(_getSimpleRestCaps({
[]: [
{
ref: 'MEDIA',
src: 'MediaInput',
args
}
]
}))
const compiler = driver.BuildCompiler()
const container = await driver.Build()
await container.Start()
compiler.ReadScript(path.resolve(__dirname, 'convos'), 'mediaasfile.convo.txt')
try {
await compiler.convos[0].Run(container)
assert.fail('should have failed')
} catch (err) {
assert.isTrue(err.message.indexOf('Security Error. Using base dir global argument in MediaInput is required') >= 0)
}
await container && container.Clean()
})
it('should fail for wildcard arg', async function () {
const driver = new BotDriver(_getSimpleRestCaps())
const compiler = driver.BuildCompiler()
const container = await driver.Build()
await container.Start()
compiler.ReadScript(path.resolve(__dirname, 'convos'), 'mediawildcard.convo.txt')
try {
compiler.ExpandConvos()
assert.fail('should have failed')
} catch (err) {
assert.isTrue(err.message.indexOf('Security Error. Using base dir global argument in MediaInput is required') >= 0)
}
await container && container.Clean()
})
})
})