-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(middlewares/admin-required): Add test from adminRequired and adm…
…inRequiredSilenced (#22) * test(middlewares/admin-required): Add test from adminRequired and adminRequiredSilenced (#22) * test(middlewares/admin-required): Add test from adminRequired and adminRequiredSilenced (#22)
- Loading branch information
1 parent
778713b
commit 43c481c
Showing
1 changed file
with
96 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
}) |