Skip to content

Commit

Permalink
add tests package
Browse files Browse the repository at this point in the history
  • Loading branch information
pvyParts committed May 25, 2024
1 parent 4a62464 commit d6be15a
Show file tree
Hide file tree
Showing 13 changed files with 3,103 additions and 15 deletions.
Binary file added .coverage
Binary file not shown.
21 changes: 21 additions & 0 deletions .coveragerc
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
[run]
branch = True
source =
aadiscordbot

omit =
*/aadiscordbot/migrations/*
*/tests/*
*/.tox/*
*/bin/*

[report]
exclude_lines =
if self.debug:
pragma: no cover
raise NotImplementedError
if __name__ == .__main__.:
def __repr__
raise AssertionError

ignore_errors = True
30 changes: 15 additions & 15 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ jobs:
steps:
- uses: actions/checkout@v2

#- uses: shogo82148/actions-setup-redis@v1
# with:
# redis-version: "latest"
- uses: shogo82148/actions-setup-redis@v1
with:
redis-version: "latest"

#- run: redis-cli ping
- run: redis-cli ping

- uses: actions/setup-python@v2
with:
Expand All @@ -39,17 +39,17 @@ jobs:
- name: install dependencies
run: make dev

#- name: Run Tests
# run: make test

#- name: Upload test coverage
# if: ${{ !env.ACT }}
# uses: actions/[email protected]
# with:
# # Artifact name
# name: Python${{ matrix.python-version }} Test Coverage
# # A file, directory or wildcard pattern that describes what to upload
# path: htmlcov/
- name: Run Tests
run: make test

- name: Upload test coverage
if: ${{ !env.ACT }}
uses: actions/[email protected]
with:
# Artifact name
name: Python${{ matrix.python-version }} Test Coverage
# A file, directory or wildcard pattern that describes what to upload
path: htmlcov/

- name: Build Projects
run: make package
51 changes: 51 additions & 0 deletions aadiscordbot/tests/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
from aadiscordmultiverse import models as dmv_models

from django.test import TestCase
from django.utils import timezone

from allianceauth.services.modules.discord import models as core_models
from allianceauth.tests.auth_utils import AuthUtils


class AuthbotTestCase(TestCase):

def setUp(cls):
AuthUtils.disconnect_signals()
cls.u1 = AuthUtils.create_user(
"user_1"
)
AuthUtils.add_main_character(
cls.u1,
"Authbot1",
1234,
5678,
"BareMetal",
"BTL"
)

AuthUtils.connect_signals()

def create_discord_user(self):
return core_models.DiscordUser.objects.create(
user=self.u1,
uid=123456789,
username="AuthBot",
activated=timezone.now()
)

def create_dmv_server(self):
return dmv_models.DiscordManagedServer.objects.create(
guild_id=12121212,
server_name="TestAuthBotServer",
)

def create_dmv_user(self, server=None):
if not server:
server = self.create_dmv_server()
return dmv_models.MultiDiscordUser.objects.create(
guild=server,
user=self.u1,
uid=123456789,
username="AuthBot",
activated=timezone.now()
)
178 changes: 178 additions & 0 deletions aadiscordbot/tests/test_utils_auth.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
from unittest.mock import MagicMock, patch

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


class TestUtilsAuth(AuthbotTestCase):

def test_dmv_active_pass(self):
self.assertTrue(dmv_active())

def test_dmv_active_fail(self):
with self.modify_settings(
INSTALLED_APPS={
"remove": [
"aadiscordmultiverse"
]
}
):
self.assertFalse(dmv_active())

def test_discord_active_pass(self):
self.assertTrue(discord_active())

def test_discord_active_fail(self):
with self.modify_settings(
INSTALLED_APPS={
"remove": [
"allianceauth.services.modules.discord"
]
}
):
self.assertFalse(discord_active())

"""
DMV User Section
"""
@patch('aadiscordbot.utils.auth.DMV_ACTIVE', False)
def test_get_dmv_discord_user_no_module(self):
self.assertIsNone(auth._get_dmv_discord_user(123, 123))

def test_get_dmv_discord_user_no_user(self):
self.create_dmv_user()
self.assertIsNone(auth._get_dmv_discord_user(123, 123))

def test_get_dmv_discord_user_valid(self):
user = self.create_dmv_user()
self.assertEqual(
auth._get_dmv_discord_user(user.uid, user.guild.guild_id),
user
)

@patch('aadiscordbot.utils.auth.DMV_ACTIVE', False)
def test_check_for_dmv_user_no_module(self):
usr = MagicMock()
usr.id = 1234
gld = MagicMock()
gld.id = 4567
self.assertFalse(auth._check_for_dmv_user(usr, gld))

def test_check_for_dmv_user_no_user(self):
self.create_dmv_user()
usr = MagicMock()
usr.id = 1234
gld = MagicMock()
gld.id = 4567
self.assertFalse(auth._check_for_dmv_user(usr, gld))

def test_check_for_dmv_user(self):
user = self.create_dmv_user()
usr = MagicMock()
usr.id = user.uid
gld = MagicMock()
gld.id = user.guild.guild_id
self.assertTrue(auth._check_for_dmv_user(usr, gld))

"""
Core User Section
"""
@patch('aadiscordbot.utils.auth.DISCORD_ACTIVE', False)
def test_get_core_discord_user_no_module(self):
self.assertIsNone(auth._get_core_discord_user(123))

def test_get_core_discord_user_no_user(self):
self.create_discord_user()
self.assertIsNone(auth._get_core_discord_user(123))

def test_get_core_discord_user_valid(self):
user = self.create_discord_user()
self.assertEqual(
auth._get_core_discord_user(user.uid),
user
)

@patch('aadiscordbot.utils.auth.DISCORD_ACTIVE', False)
def test_check_for_core_user_no_module(self):
usr = MagicMock()
usr.id = 1234
self.assertFalse(auth._check_for_core_user(usr))

def test_check_for_core_user_no_user(self):
usr = MagicMock()
usr.id = 1234
self.assertFalse(auth._check_for_core_user(usr))

def test_check_for_core_user(self):
user = self.create_discord_user()
usr = MagicMock()
usr.id = user.uid
self.assertTrue(
auth._check_for_core_user(usr),
)

"""
Server Section
"""
@patch('aadiscordbot.utils.auth.DISCORD_ACTIVE', False)
def test_guild_is_core_module_no_module(self):
self.assertFalse(auth._guild_is_core_module(1234))

def test_guild_is_core_module_not_guild(self):
self.assertFalse(auth._guild_is_core_module(1234))

def test_guild_is_core_module(self):
self.assertTrue(
auth._guild_is_core_module(1234567891011),
)

@patch('aadiscordbot.utils.auth.DMV_ACTIVE', False)
def test_guild_is_dmv_module_no_module(self):
self.assertFalse(auth._guild_is_dmv_module(1234))

def test_guild_is_dmv_module_not_guild(self):
self.assertFalse(auth._guild_is_dmv_module(1234))

def test_guild_is_dmv_module(self):
gld = self.create_dmv_server()
self.assertTrue(
auth._guild_is_dmv_module(gld.guild_id),
)

@patch('aadiscordbot.utils.auth.DMV_ACTIVE', False)
@patch('aadiscordbot.utils.auth.DISCORD_ACTIVE', False)
def test_guild_is_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):
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):
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):
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):
gld = MagicMock()
gld.id = 1234567891011
self.assertTrue(auth.is_guild_managed(gld))
Loading

0 comments on commit d6be15a

Please sign in to comment.