From 43c481c7502a4082101ad976688f7727e958e556 Mon Sep 17 00:00:00 2001 From: Shelomanov Dmitry Date: Sat, 30 Dec 2017 01:00:22 +0300 Subject: [PATCH] test(middlewares/admin-required): Add test from adminRequired and adminRequiredSilenced (#22) * test(middlewares/admin-required): Add test from adminRequired and adminRequiredSilenced (#22) * test(middlewares/admin-required): Add test from adminRequired and adminRequiredSilenced (#22) --- src/middlewares/admin-required.test.js | 98 +++++++++++++++++++++++++- 1 file changed, 96 insertions(+), 2 deletions(-) diff --git a/src/middlewares/admin-required.test.js b/src/middlewares/admin-required.test.js index 0a55c55..acefa64 100644 --- a/src/middlewares/admin-required.test.js +++ b/src/middlewares/admin-required.test.js @@ -1,6 +1,100 @@ import test from 'ava' - +import sinon from 'sinon' +import { Extra } from 'telegraf' +import { Context } from '../tests/telegraf' +import text from '../text' import { adminRequired, adminRequiredSilenced } from './admin-required' +/* eslint-disable no-param-reassign */ + +test.beforeEach((t) => { + t.context.create = () => { + const ctx = Context.create() + const res = Math.random() + const next = sinon.stub().returns(res) + + ctx.message.$from() + return { ctx, res, next } + } +}) + +test('test adminRequired for admin and type group', async (t) => { + const { ctx, res, next } = t.context.create() + + ctx.chat.isAdmin = sinon.stub().resolves(true) + ctx.chat.$type('group') + ctx.getChat = sinon.stub().returns(ctx.chat) + const resultFn = await adminRequired(ctx, next) + + t.true(ctx.getChat.called) + t.true(ctx.chat.isAdmin.called) + t.true(next.called) + t.is(resultFn, res) +}) + +test('test adminRequired for admin and type private', async (t) => { + const { ctx, next } = t.context.create() + + ctx.chat.isAdmin = sinon.stub().resolves(true) + ctx.chat.$type('private') + ctx.getChat = sinon.stub().returns(ctx.chat) + await adminRequired(ctx, next) + + t.false(ctx.getChat.called) + t.false(ctx.chat.isAdmin.called) + t.true(ctx.reply.calledWith(text.notif.groupOnly(), Extra.inReplyTo(ctx.message.message_id))) +}) + +test('test adminRequired for regular user and type group', async (t) => { + const { ctx, next } = t.context.create() + + ctx.chat.isAdmin = sinon.stub().resolves(false) + ctx.chat.$type('group') + ctx.getChat = sinon.stub().returns(ctx.chat) + await adminRequired(ctx, next) + + t.true(ctx.getChat.called) + t.true(ctx.chat.isAdmin.called) + t.true(ctx.reply.calledWith(text.notif.adminOnly(), Extra.inReplyTo(ctx.message.message_id))) +}) + +test('test adminRequired for regular user and type private', async (t) => { + const { ctx, next } = t.context.create() + + ctx.chat.isAdmin = sinon.stub().resolves(false) + ctx.chat.$type('private') + ctx.getChat = sinon.stub().returns(ctx.chat) + await adminRequired(ctx, next) + + t.false(ctx.getChat.called) + t.false(ctx.chat.isAdmin.called) + t.true(ctx.reply.calledWith(text.notif.groupOnly(), Extra.inReplyTo(ctx.message.message_id))) +}) + +test('test adminRequiredSilenced for admin and type group', async (t) => { + const { ctx, res, next } = t.context.create() + + ctx.chat.isAdmin = sinon.stub().resolves(true) + ctx.chat.$type('group') + ctx.getChat = sinon.stub().returns(ctx.chat) + const resultfn = await adminRequired(ctx, next) + + t.true(ctx.getChat.called) + t.true(ctx.chat.isAdmin.called) + t.true(next.called) + t.is(resultfn, res) +}) + +test('test adminRequiredSilenced for regular user and type group', async (t) => { + const { ctx, next } = t.context.create() + + ctx.chat.isAdmin = sinon.stub().resolves(false) + ctx.chat.$type('private') + ctx.getChat = sinon.stub().returns(ctx.chat) + const resultfn = await adminRequired(ctx, next) -test.todo('adminRequired') + t.false(ctx.getChat.called) + t.false(ctx.chat.isAdmin.called) + t.false(next.called) + t.is(resultfn, undefined) +})