diff --git a/src/middlewares/allowed-chat.js b/src/middlewares/allowed-chat.js index e5ecab0..4082efe 100644 --- a/src/middlewares/allowed-chat.js +++ b/src/middlewares/allowed-chat.js @@ -3,13 +3,12 @@ const debug = require('debug')('rubot:middlewares:allowed-chat') const allowWhiteListChat = async ({ chat, isChatInWhiteList }, next) => { debug('allowWhiteListChat', chat.id, chat.type) - if (chat.type !== 'private') { - if (!isChatInWhiteList(chat)) { - return null - } + + if (isChatInWhiteList(chat)) { + return next() } - return next() + return null } module.exports = { diff --git a/src/middlewares/allowed-chat.test.js b/src/middlewares/allowed-chat.test.js new file mode 100644 index 0000000..9829f65 --- /dev/null +++ b/src/middlewares/allowed-chat.test.js @@ -0,0 +1,37 @@ +import test from 'ava' +import sinon from 'sinon' +import { Context } from '../tests/telegraf' +import { allowWhiteListChat } from './allowed-chat' + +/* eslint-disable no-param-reassign */ + +test.beforeEach((t) => { + t.context.create = ({ inWhitelist }) => { + const ctx = Context.create() + const res = Math.random() + const next = sinon.stub().returns(res) + + ctx.isChatInWhiteList = sinon.stub().returns(inWhitelist) + + return { ctx, res, next } + } +}) + +test('should pass in whitelist', async (t) => { + const { ctx, res, next } = t.context.create({ inWhitelist: true }) + const result = await allowWhiteListChat(ctx, next) + + t.true(next.called) + t.is(result, res) + t.true(ctx.isChatInWhiteList.calledWith(ctx.chat)) +}) + +test('should stop not in whitelist', async (t) => { + const { ctx, next } = t.context.create({ inWhitelist: false }) + const result = await allowWhiteListChat(ctx, next) + + t.false(next.called) + t.is(result, null) + t.true(ctx.isChatInWhiteList.calledWith(ctx.chat)) +}) + diff --git a/src/tests/telegraf.js b/src/tests/telegraf.js index 471b619..d2ea178 100644 --- a/src/tests/telegraf.js +++ b/src/tests/telegraf.js @@ -10,6 +10,8 @@ let ID_USER = ID_USER_START let ID_MESSAGE = ID_MESSAGE_START let ID_CHAT = ID_CHAT_START +/* eslint-disable class-methods-use-this */ + class User { constructor() { this.id = ++ID_USER @@ -77,6 +79,7 @@ class Context { } } + module.exports = { Context, Message,