UNPKG

botmaster-test

Version:

test utility for end to end testing with botmaster

165 lines (153 loc) 5.26 kB
'use strict'; var nock = require('nock'); var request = require('supertest'); var debug = require('debug')('botmaster:test'); var telegramToken = 'abc'; var telegramWebhookEndpoint = 'boo124/'; var telegramSettings = { credentials: { authToken: telegramToken }, webhookEndpoint: telegramWebhookEndpoint }; var telegramBots = function telegramBots(botmaster) { return botmaster.getBots('telegram'); }; var telegramBot = function telegramBot(botmaster) { return telegramBots(botmaster)[0]; }; var telegramNock = function telegramNock() { return nock('https://api.telegram.org/bot' + telegramToken).log(debug); }; /** * have bot master response done with a text. Chain it by calling it response(botmaster)(text) * @param {object} botmaster - botmaster to work with * @param {String} text - bot text to send */ var respond = function respond(botmaster) { return function (text) { botmaster.use({ type: 'incoming', name: 'send message as bot', controller: function controller(bot) { return bot.sendMessage(incomingUpdate(text)); } }); }; }; /** * generate a new telegram incoming message for use with botmaster * @param {string} text optional - the users text * @return {Object} a mock telegram incoming message to use with telegramMock.sendMessage */ var incomingMessage = function incomingMessage() { var text = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 'hi'; return { update_id: 123, message: { message_id: 123, from: { id: 123, first_name: 'testerbot' }, date: 631152000, // 1/1/1990 text: text } }; }; /** * generate an incoming update * @param {String} [text] - the bots text * @return {Object} mock botmaster update after update handler */ var incomingUpdate = function incomingUpdate() { var text = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 'the users test'; return { raw: 'some raw object data', sender: { id: 123 }, recipient: { id: 123 }, timestamp: 1468325836000, message: { mid: '4666071', seq: 1, text: text } }; }; /** * generate an outgoing message * @param {String} [text] - the bots text * @return {Object} message to send with bot.sendMessage */ var outgoingMessage = function outgoingMessage() { var text = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 'the bots text'; return { recipient: { id: 'invalidId' }, message: { text: text } }; }; /** * A chainable mock for telegram that can send and expect messages. Construct it by calling it with these params. * @param {Object} botmaster - the botmaster object being tested. we use this to get the app for use with supertest. * @param {Object} [mock] - a nock scope * @return {Object} mock object with methods */ var telegramMock = function telegramMock(botmaster) { var mock = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : telegramNock(); return { /** * mock telegram sending botmaster an update * @param {object} update - telegram update * @param {Function} cb - error-first callback with response object from botmaster * @return {Object} the telegraMock object for chaining */ sendUpdate: function sendUpdate(update, cb) { request(botmaster.server).post('/telegram/' + telegramWebhookEndpoint).send(incomingMessage(update)).expect(200).end(cb); return telegramMock(botmaster, mock); }, /** * expect botmaster to send certain responses * @param {Array} responses a series of botmaster responses to expect in order * @param {Function} cb error-first callback * @return {Object} the telegraMock object for chaining */ expect: function expect(responses, cb) { // return this to prevent errors in botmaster var telegramResponse = { result: {} }; responses.forEach(function (response, i) { mock.post('/sendMessage', function (body) { return body.text == response || cb(new Error(body.text + ' does not match ' + response)); }).reply(200, function () { if (i + 1 == responses.length) cb(); return telegramResponse; }); }); return telegramMock(botmaster, mock); }, /** * sugar syntax for nock.cleanAll() to remove any existing mocks */ cleanAll: function cleanAll() { return nock.cleanAll(); } }; }; module.exports = { telegramMock: telegramMock, incomingMessage: incomingMessage, incomingUpdate: incomingUpdate, outgoingMessage: outgoingMessage, telegramSettings: telegramSettings, respond: respond, telegramBot: telegramBot };