Skip to content

Commit

Permalink
More tests
Browse files Browse the repository at this point in the history
  • Loading branch information
pvyParts committed May 25, 2024
1 parent d6be15a commit 2d90cf4
Show file tree
Hide file tree
Showing 4 changed files with 128 additions and 37 deletions.
Binary file modified .coverage
Binary file not shown.
102 changes: 96 additions & 6 deletions aadiscordbot/tests/test_utils_auth.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from unittest.mock import MagicMock, patch

from ..app_settings import discord_active, dmv_active
from ..models import AuthBotConfiguration
from ..utils import auth
from . import AuthbotTestCase

Expand Down Expand Up @@ -76,7 +77,7 @@ def test_check_for_dmv_user(self):
self.assertTrue(auth._check_for_dmv_user(usr, gld))

"""
Core User Section
Core Module User Section
"""
@patch('aadiscordbot.utils.auth.DISCORD_ACTIVE', False)
def test_get_core_discord_user_no_module(self):
Expand Down Expand Up @@ -142,37 +143,126 @@ def test_guild_is_dmv_module(self):

@patch('aadiscordbot.utils.auth.DMV_ACTIVE', False)
@patch('aadiscordbot.utils.auth.DISCORD_ACTIVE', False)
def test_guild_is_managed_no_modules(self):
def test_is_guild_managed_no_modules(self):
gld = MagicMock()
gld.id = 1234
self.assertFalse(auth.is_guild_managed(gld))

@patch('aadiscordbot.utils.auth.DMV_ACTIVE', True)
@patch('aadiscordbot.utils.auth.DISCORD_ACTIVE', False)
def test_guild_is_managed_dnv_not_guild(self):
def test_is_guild_managed_dnv_not_guild(self):
self.create_dmv_server()
gld = MagicMock()
gld.id = 1234
self.assertFalse(auth.is_guild_managed(gld))

@patch('aadiscordbot.utils.auth.DMV_ACTIVE', True)
@patch('aadiscordbot.utils.auth.DISCORD_ACTIVE', False)
def test_guild_is_managed_dnv_is_guild(self):
def test_is_guild_managed_dnv_is_guild(self):
guild = self.create_dmv_server()
gld = MagicMock()
gld.id = guild.guild_id
self.assertTrue(auth.is_guild_managed(gld))

@patch('aadiscordbot.utils.auth.DMV_ACTIVE', False)
@patch('aadiscordbot.utils.auth.DISCORD_ACTIVE', True)
def test_guild_is_managed_core_not_guild(self):
def test_is_guild_managed_core_not_guild(self):
gld = MagicMock()
gld.id = 1234
self.assertFalse(auth.is_guild_managed(gld))

@patch('aadiscordbot.utils.auth.DMV_ACTIVE', False)
@patch('aadiscordbot.utils.auth.DISCORD_ACTIVE', True)
def test_guild_is_managed_core_is_guild(self):
def test_is_guild_managed_core_is_guild(self):
gld = MagicMock()
gld.id = 1234567891011
self.assertTrue(auth.is_guild_managed(gld))

"""
Auth User Section
"""

@patch('aadiscordbot.utils.auth.DISCORD_ACTIVE', False)
def test_user_is_authenticated(self):
user = self.create_dmv_user()

usr = MagicMock()
usr.id = user.uid
gld = MagicMock()
gld.id = user.guild.guild_id
self.assertTrue(auth.user_is_authenticated(usr, gld))

def test_is_user_authenticated_dnv(self):
user = self.create_dmv_user()
usr = MagicMock()
usr.id = user.uid
gld = MagicMock()
gld.id = user.guild.guild_id

self.assertTrue(auth.is_user_authenticated(usr, gld))

def test_is_user_authenticated_core(self):
user = self.create_discord_user()
usr = MagicMock()
usr.id = user.uid
gld = MagicMock()
gld.id = 1234567891011

self.assertTrue(auth.is_user_authenticated(usr, gld))

def test_is_user_authenticated_none(self):
usr = MagicMock()
usr.id = 123456789
gld = MagicMock()
gld.id = 12345678910

self.assertFalse(auth.is_user_authenticated(usr, gld))

def test_get_auth_user_core(self):
user = self.create_discord_user()
usr = MagicMock()
usr.id = user.uid
gld = MagicMock()
gld.id = 1234567891011

self.assertEqual(
auth.get_auth_user(usr, gld),
self.u1
)

def test_get_auth_user_dnv(self):
user = self.create_dmv_user()
usr = MagicMock()
usr.id = user.uid
gld = MagicMock()
gld.id = user.guild.guild_id

self.assertEqual(
auth.get_auth_user(usr, gld),
self.u1
)

def test_get_auth_user_none(self):
usr = MagicMock()
usr.id = 1234
gld = MagicMock()
gld.id = 5678

self.assertIsNone(
auth.get_auth_user(usr, gld)
)

def test_bot_admin_fail(self):
user = self.create_discord_user()
usr = MagicMock()
usr.id = user.uid

self.assertFalse(auth.is_user_bot_admin(usr))

def test_bot_admin_pass(self):
user = self.create_discord_user()
AuthBotConfiguration.objects.get(id=1).admin_users.add(user)
usr = MagicMock()
usr.id = user.uid

self.assertTrue(auth.is_user_bot_admin(usr))
3 changes: 2 additions & 1 deletion aadiscordbot/utils/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,11 +129,12 @@ def _get_dmv_guild(guild_id):
def is_user_bot_admin(user: User):
"""
Is user a configured Bot Admin
TODO: Make this work with DNV somehow.
"""
if user.id in get_admins():
return True
else:
raise False
return False


def is_guild_managed(guild: Guild):
Expand Down
60 changes: 30 additions & 30 deletions coverage.xml
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<?xml version="1.0" ?>
<coverage version="7.5.1" timestamp="1716632699455" lines-valid="2460" lines-covered="240" line-rate="0.09756" branches-valid="682" branches-covered="30" branch-rate="0.04399" complexity="0">
<coverage version="7.5.1" timestamp="1716639678517" lines-valid="2460" lines-covered="261" line-rate="0.1061" branches-valid="682" branches-covered="42" branch-rate="0.06158" complexity="0">
<!-- Generated by coverage.py: https://coverage.readthedocs.io/en/7.5.1 -->
<!-- Based on https://raw.githubusercontent.com/cobertura/web/master/htdocs/xml/coverage-04.dtd -->
<sources>
<source>/home/aaronkable/allianceserver/working/allianceauth-discordbot/aadiscordbot</source>
</sources>
<packages>
<package name="." line-rate="0.2613" branch-rate="0.09756" complexity="0">
<package name="." line-rate="0.2642" branch-rate="0.09756" complexity="0">
<classes>
<class name="__init__.py" filename="__init__.py" complexity="0" line-rate="1" branch-rate="1">
<methods/>
Expand Down Expand Up @@ -75,7 +75,7 @@
<line number="83" hits="1"/>
</lines>
</class>
<class name="app_settings.py" filename="app_settings.py" complexity="0" line-rate="0.6167" branch-rate="0">
<class name="app_settings.py" filename="app_settings.py" complexity="0" line-rate="0.65" branch-rate="0">
<methods/>
<lines>
<line number="1" hits="1"/>
Expand All @@ -95,8 +95,8 @@
<line number="26" hits="1"/>
<line number="27" hits="0"/>
<line number="30" hits="1"/>
<line number="31" hits="0"/>
<line number="32" hits="0"/>
<line number="31" hits="1"/>
<line number="32" hits="1"/>
<line number="35" hits="1"/>
<line number="36" hits="0"/>
<line number="37" hits="0" branch="true" condition-coverage="0% (0/2)" missing-branches="exit,38"/>
Expand Down Expand Up @@ -2592,13 +2592,13 @@
</class>
</classes>
</package>
<package name="utils" line-rate="0.7262" branch-rate="0.5" complexity="0">
<package name="utils" line-rate="0.9524" branch-rate="0.9286" complexity="0">
<classes>
<class name="__init__.py" filename="utils/__init__.py" complexity="0" line-rate="1" branch-rate="1">
<methods/>
<lines/>
</class>
<class name="auth.py" filename="utils/auth.py" complexity="0" line-rate="0.7262" branch-rate="0.5">
<class name="auth.py" filename="utils/auth.py" complexity="0" line-rate="0.9524" branch-rate="0.9286">
<methods/>
<lines>
<line number="12" hits="1"/>
Expand Down Expand Up @@ -2659,32 +2659,32 @@
<line number="124" hits="1"/>
<line number="126" hits="1"/>
<line number="129" hits="1"/>
<line number="133" hits="0" branch="true" condition-coverage="0% (0/2)" missing-branches="134,136"/>
<line number="134" hits="0"/>
<line number="136" hits="0"/>
<line number="139" hits="1"/>
<line number="134" hits="1" branch="true" condition-coverage="100% (2/2)"/>
<line number="135" hits="1"/>
<line number="137" hits="1"/>
<line number="140" hits="1"/>
<line number="141" hits="1"/>
<line number="142" hits="1"/>
<line number="145" hits="1"/>
<line number="146" hits="0"/>
<line number="151" hits="0"/>
<line number="152" hits="0"/>
<line number="155" hits="1"/>
<line number="162" hits="0" branch="true" condition-coverage="0% (0/2)" missing-branches="163,165"/>
<line number="163" hits="0"/>
<line number="165" hits="0" branch="true" condition-coverage="0% (0/2)" missing-branches="166,169"/>
<line number="166" hits="0"/>
<line number="169" hits="0"/>
<line number="172" hits="1"/>
<line number="176" hits="0"/>
<line number="177" hits="0" branch="true" condition-coverage="0% (0/2)" missing-branches="178,180"/>
<line number="178" hits="0"/>
<line number="180" hits="0" branch="true" condition-coverage="0% (0/2)" missing-branches="181,183"/>
<line number="181" hits="0"/>
<line number="183" hits="0" branch="true" condition-coverage="0% (0/2)" missing-branches="184,186"/>
<line number="184" hits="0"/>
<line number="186" hits="0"/>
<line number="143" hits="1"/>
<line number="146" hits="1"/>
<line number="147" hits="1"/>
<line number="152" hits="1"/>
<line number="153" hits="1"/>
<line number="156" hits="1"/>
<line number="163" hits="1" branch="true" condition-coverage="100% (2/2)"/>
<line number="164" hits="1"/>
<line number="166" hits="1" branch="true" condition-coverage="100% (2/2)"/>
<line number="167" hits="1"/>
<line number="170" hits="1"/>
<line number="173" hits="1"/>
<line number="177" hits="1"/>
<line number="178" hits="1" branch="true" condition-coverage="100% (2/2)"/>
<line number="179" hits="1"/>
<line number="181" hits="1" branch="true" condition-coverage="100% (2/2)"/>
<line number="182" hits="1"/>
<line number="184" hits="1" branch="true" condition-coverage="100% (2/2)"/>
<line number="185" hits="1"/>
<line number="187" hits="1"/>
</lines>
</class>
</classes>
Expand Down

0 comments on commit 2d90cf4

Please sign in to comment.