Skip to content

Commit

Permalink
test(middlewares/allowed-chat): Add test for allowWhiteListChat (#22)
Browse files Browse the repository at this point in the history
  • Loading branch information
Shelomanov authored and sergeysova committed Dec 3, 2017
1 parent ffb40b0 commit f89faa4
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 5 deletions.
9 changes: 4 additions & 5 deletions src/middlewares/allowed-chat.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down
37 changes: 37 additions & 0 deletions src/middlewares/allowed-chat.test.js
Original file line number Diff line number Diff line change
@@ -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(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(false)
const result = await allowWhiteListChat(ctx, next)

t.false(next.called)
t.is(result, null)
t.true(ctx.isChatInWhiteList.calledWith(ctx.chat))
})

3 changes: 3 additions & 0 deletions src/tests/telegraf.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -77,6 +79,7 @@ class Context {
}
}


module.exports = {
Context,
Message,
Expand Down

0 comments on commit f89faa4

Please sign in to comment.