Commit 0ac8a9eb authored by Jeremy Pallats's avatar Jeremy Pallats 💬
Browse files

Add tests to cover admin add/remove subcommands.

parent 053877e5
Loading
Loading
Loading
Loading
Loading
+2 −2
Original line number Diff line number Diff line
@@ -174,7 +174,7 @@ class Admin(Action):
                                             self.msg.channel_mentions[0])
                response = "Channel permission added."

            elif self.args.role:
            else:
                cogdb.query.add_role_perm(self.session, self.args.rule_cmd,
                                          self.msg.channel.guild,
                                          self.msg.role_mentions[0])
@@ -203,7 +203,7 @@ class Admin(Action):
                                                self.msg.channel_mentions[0])
                response = "Channel permission removed."

            elif self.args.role:
            else:
                cogdb.query.remove_role_perm(self.session, self.args.rule_cmd,
                                             self.msg.channel.guild,
                                             self.msg.role_mentions[0])
+2 −1
Original line number Diff line number Diff line
@@ -1102,7 +1102,8 @@ def empty_tables(session, *, perm=False):
    """
    Drop all tables.
    """
    classes = [FortDrop, UMHold, FortSystem, UMSystem, FortUser, UMUser, KOS]
    classes = [FortDrop, UMHold, FortSystem, UMSystem, FortUser, UMUser, KOS,
               KOS, TrackSystem, TrackSystemCached, TrackByID, AdminPerm, ChannelPerm, RolePerm]
    if perm:
        classes += [DiscordUser]

+105 −0
Original line number Diff line number Diff line
@@ -7,6 +7,7 @@ import re

import aiomock
import pytest
import sqlalchemy as sqla

import cog.actions
import cog.bot
@@ -16,6 +17,7 @@ from cogdb.eddb import HUDSON_CONTROLS
from cogdb.side import SystemAge
from cogdb.schema import (DiscordUser, FortSystem, FortDrop, FortUser,
                          FortOrder, UMSystem, UMUser, UMHold,
                          AdminPerm, RolePerm, ChannelPerm,
                          TrackSystem, TrackSystemCached, TrackByID)

from tests.conftest import fake_msg_gears, fake_msg_newuser
@@ -132,6 +134,109 @@ async def test_cmd_invalid_flag(f_bot):
        await action_map(msg, f_bot).execute()


@pytest.mark.asyncio
async def test_cmd_admin_add_admin(f_dusers, f_admins, f_bot, db_cleanup, session):
    new_admin = tc.Member(f_dusers[-1].display_name, None, id=f_dusers[-1].id)
    msg = fake_msg_gears("!admin add", mentions=[new_admin])

    await action_map(msg, f_bot).execute()

    session.close()
    last = session.query(AdminPerm).order_by(AdminPerm.id.desc()).limit(1).one()
    assert last.id == 3


@pytest.mark.asyncio
async def test_cmd_admin_remove_admin(f_dusers, f_admins, f_bot, db_cleanup, session):
    new_admin = tc.Member(f_dusers[1].display_name, None, id=f_dusers[1].id)
    msg = fake_msg_gears("!admin remove", mentions=[new_admin])

    await action_map(msg, f_bot).execute()

    session.close()
    with pytest.raises(sqla.exc.NoResultFound):
        session.query(AdminPerm).filter(AdminPerm.id == f_dusers[1].id).limit(1).one()


@pytest.mark.asyncio
async def test_cmd_admin_add_chan(f_dusers, f_admins, f_bot, db_cleanup, session):
    srv = tc.fake_servers()[0]
    chan = srv.channels[1]
    msg = fake_msg_gears("!admin add BGS", channel_mentions=[chan])

    await action_map(msg, f_bot).execute()

    session.close()
    added = session.query(ChannelPerm).filter(ChannelPerm.cmd == 'BGS').one()
    assert added.cmd == "BGS"


@pytest.mark.asyncio
async def test_cmd_admin_add_chan_raises(f_dusers, f_admins, f_bot, db_cleanup, session):
    srv = tc.fake_servers()[0]
    chan = srv.channels[1]
    msg = fake_msg_gears("!admin add Invalid", channel_mentions=[chan])

    with pytest.raises(cog.exc.InvalidCommandArgs):
        await action_map(msg, f_bot).execute()


@pytest.mark.asyncio
async def test_cmd_admin_remove_chan(f_dusers, f_admins, f_bot, db_cleanup, session):
    srv = tc.fake_servers()[0]
    chan = srv.channels[1]

    msg = fake_msg_gears("!admin add BGS", channel_mentions=[chan])
    await action_map(msg, f_bot).execute()
    msg = fake_msg_gears("!admin remove BGS", channel_mentions=[chan])
    await action_map(msg, f_bot).execute()

    session.close()
    with pytest.raises(sqla.exc.NoResultFound):
        session.query(ChannelPerm).filter(ChannelPerm.cmd == 'BGS').one()


@pytest.mark.asyncio
async def test_cmd_admin_add_role(f_dusers, f_admins, f_bot, db_cleanup, session):
    srv = tc.fake_servers()[0]
    role = srv.get_member(1).roles[0]
    role.id = 90
    msg = fake_msg_gears("!admin add BGS", role_mentions=[role])

    await action_map(msg, f_bot).execute()

    session.close()
    added = session.query(RolePerm).filter(RolePerm.cmd == 'BGS').one()
    assert added.role_id == role.id


@pytest.mark.asyncio
async def test_cmd_admin_add_role_raises(f_dusers, f_admins, f_bot, db_cleanup, session):
    srv = tc.fake_servers()[0]
    role = srv.get_member(1).roles[0]
    role.id = 90
    msg = fake_msg_gears("!admin add Invalid", role_mentions=[role])

    with pytest.raises(cog.exc.InvalidCommandArgs):
        await action_map(msg, f_bot).execute()


@pytest.mark.asyncio
async def test_cmd_admin_remove_role(f_dusers, f_admins, f_bot, db_cleanup, session):
    srv = tc.fake_servers()[0]
    role = srv.get_member(1).roles[0]
    role.id = 90

    msg = fake_msg_gears("!admin add BGS", role_mentions=[role])
    await action_map(msg, f_bot).execute()
    msg = fake_msg_gears("!admin remove BGS", role_mentions=[role])
    await action_map(msg, f_bot).execute()

    session.close()
    with pytest.raises(sqla.exc.NoResultFound):
        session.query(ChannelPerm).filter(ChannelPerm.cmd == 'BGS').one()


@pytest.mark.asyncio
async def test_cmd_admin_removeum_fail(f_admins, f_bot, db_cleanup):
    msg = fake_msg_gears("!admin removeum Sol")
+4 −4
Original line number Diff line number Diff line
@@ -133,14 +133,14 @@ def test_check_reply():
        cog.inara.check_reply(None)

    with pytest.raises(cog.exc.CmdAborted):
        cog.inara.check_reply(Message('!status', None, None, None, None))
        cog.inara.check_reply(Message('!status', None, None, None))

    with pytest.raises(cog.exc.CmdAborted):
        cog.inara.check_reply(Message('stop', None, None, None, None))
        cog.inara.check_reply(Message('stop', None, None, None))

    assert cog.inara.check_reply(Message('2', None, None, None, None)) == 2
    assert cog.inara.check_reply(Message('2', None, None, None)) == 2

    assert cog.inara.check_reply(Message('cmdr 5', None, None, None, None)) == 5
    assert cog.inara.check_reply(Message('cmdr 5', None, None, None)) == 5


def test_extract_inara_systems():
+1 −1
Original line number Diff line number Diff line
@@ -399,7 +399,7 @@ def test_check_perms(session, f_cperms, f_rperms):
    ops_channel.server = server
    roles = [Role('FRC Member', id=3001), Role('Winters', id=3002)]
    author = Member('User1', roles)
    msg = Message('!drop', author, server, ops_channel, None)
    msg = Message('!drop', author, server, ops_channel)
    msg.channel = ops_channel
    msg.channel.guild = server

Loading