From 88db66c36028a987b3bf04886869b29bbc738546 Mon Sep 17 00:00:00 2001 From: ilyazheprog Date: Thu, 7 Dec 2023 18:02:04 +0700 Subject: [PATCH] =?UTF-8?q?=D0=B4=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=D1=8B=20=D0=BC=D0=BE=D0=B4=D1=83=D0=BB=D0=B8=20config,?= =?UTF-8?q?=20role,=20=D0=B8=D1=81=D0=BF=D1=80=D0=B0=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=D0=B0=20=D0=B1=D0=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/core/config.yaml | 7 +- src/core/main.py | 2 +- src/modules/standart/config/__init__.py | 0 .../standart/config}/config.py | 1 + src/modules/standart/config/info.json | 6 ++ src/modules/standart/config/tests/config.yaml | 7 ++ .../standart/config/tests/test_config.py | 35 +++++++ src/modules/standart/database/models/chats.py | 2 +- .../standart/database/tests/test_db.py | 95 +++++++++++-------- src/modules/standart/roles/__init__.py | 0 src/modules/standart/roles/api.py | 43 +++++++++ src/modules/standart/roles/info.json | 6 ++ src/modules/standart/roles/tests/__init__.py | 0 src/modules/standart/roles/tests/config.yaml | 7 ++ .../standart/roles/tests/test_roles.py | 35 +++++++ 15 files changed, 201 insertions(+), 45 deletions(-) create mode 100644 src/modules/standart/config/__init__.py rename src/{core => modules/standart/config}/config.py (98%) create mode 100644 src/modules/standart/config/info.json create mode 100644 src/modules/standart/config/tests/config.yaml create mode 100644 src/modules/standart/config/tests/test_config.py create mode 100644 src/modules/standart/roles/__init__.py create mode 100644 src/modules/standart/roles/api.py create mode 100644 src/modules/standart/roles/info.json create mode 100644 src/modules/standart/roles/tests/__init__.py create mode 100644 src/modules/standart/roles/tests/config.yaml create mode 100644 src/modules/standart/roles/tests/test_roles.py diff --git a/src/core/config.yaml b/src/core/config.yaml index f2c57f1..65a4d10 100644 --- a/src/core/config.yaml +++ b/src/core/config.yaml @@ -1,2 +1,7 @@ TELEGRAM: - TOKEN: xxxxxxxxxxxxxxxxxxxx \ No newline at end of file + TOKEN: xxxxxxxxxxxxxxxxxxxx +ROLES: + ADMIN: 0 + MODERATOR: 1 + USER: 2 + BOT: 3 \ No newline at end of file diff --git a/src/core/main.py b/src/core/main.py index c4e2992..f83a1de 100644 --- a/src/core/main.py +++ b/src/core/main.py @@ -1,4 +1,4 @@ -from .config import yaml_load +from src.modules.standart.config.config import yaml_load from asyncio import run from aiogram import Bot, Dispatcher diff --git a/src/modules/standart/config/__init__.py b/src/modules/standart/config/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/src/core/config.py b/src/modules/standart/config/config.py similarity index 98% rename from src/core/config.py rename to src/modules/standart/config/config.py index ff83769..2591a84 100644 --- a/src/core/config.py +++ b/src/modules/standart/config/config.py @@ -1,4 +1,5 @@ import yaml + with open('config.yaml', 'r') as file: yaml_load = yaml.full_load(file) diff --git a/src/modules/standart/config/info.json b/src/modules/standart/config/info.json new file mode 100644 index 0000000..b190cc1 --- /dev/null +++ b/src/modules/standart/config/info.json @@ -0,0 +1,6 @@ +{ + "name": "Config YAML", + "description": "Модуль для работы с конфигурационным файлом бота (YAML)", + "author": "OCAB Team", + "version": "1.0" +} \ No newline at end of file diff --git a/src/modules/standart/config/tests/config.yaml b/src/modules/standart/config/tests/config.yaml new file mode 100644 index 0000000..65a4d10 --- /dev/null +++ b/src/modules/standart/config/tests/config.yaml @@ -0,0 +1,7 @@ +TELEGRAM: + TOKEN: xxxxxxxxxxxxxxxxxxxx +ROLES: + ADMIN: 0 + MODERATOR: 1 + USER: 2 + BOT: 3 \ No newline at end of file diff --git a/src/modules/standart/config/tests/test_config.py b/src/modules/standart/config/tests/test_config.py new file mode 100644 index 0000000..12b891f --- /dev/null +++ b/src/modules/standart/config/tests/test_config.py @@ -0,0 +1,35 @@ +from src.modules.standart.config.config import yaml_load +import unittest + + +class TestConfig(unittest.TestCase): + def test_yaml_load_correctness(self): + self.assertIsNotNone(yaml_load) + self.assertIn("TELEGRAM", yaml_load) + self.assertIn("TOKEN", yaml_load["TELEGRAM"]) + self.assertIn("ROLES", yaml_load) + self.assertIn("ADMIN", yaml_load["ROLES"]) + self.assertIn("MODERATOR", yaml_load["ROLES"]) + self.assertIn("USER", yaml_load["ROLES"]) + self.assertIn("BOT", yaml_load["ROLES"]) + + def test_yaml_keys_existence(self): + self.assertTrue(all(key in yaml_load for key in ["TELEGRAM", "ROLES"])) + self.assertIn("TOKEN", yaml_load["TELEGRAM"]) + self.assertTrue(all(role in yaml_load["ROLES"] for role in ["ADMIN", "MODERATOR", "USER"])) + + def test_yaml_yaml_load_types(self): + self.assertIsInstance(yaml_load["TELEGRAM"]["TOKEN"], str) + self.assertTrue(all(isinstance(yaml_load["ROLES"][role], int) for role in ["ADMIN", "MODERATOR", "USER"])) + + def test_yaml_values(self): + expected_token = 'xxxxxxxxxxxxxxxxxxxx' + expected_role_values = {'ADMIN': 0, 'MODERATOR': 1, 'USER': 2, 'BOT': 3} + + self.assertEqual(yaml_load["TELEGRAM"]["TOKEN"], expected_token) + for role, value in expected_role_values.items(): + self.assertEqual(yaml_load["ROLES"][role], value) + + +if __name__ == '__main__': + unittest.main() diff --git a/src/modules/standart/database/models/chats.py b/src/modules/standart/database/models/chats.py index 2d8da3b..c3a2e6d 100644 --- a/src/modules/standart/database/models/chats.py +++ b/src/modules/standart/database/models/chats.py @@ -4,6 +4,6 @@ import peewee as pw class Chats(BaseModel): - chat_role = pw.IntegerField(null=False) + chat_role = pw.IntegerField(null=False, default=1) chat_stats = pw.IntegerField(null=False) chat_federation = pw.IntegerField(null=False) diff --git a/src/modules/standart/database/tests/test_db.py b/src/modules/standart/database/tests/test_db.py index cc7da4b..c0b4bda 100644 --- a/src/modules/standart/database/tests/test_db.py +++ b/src/modules/standart/database/tests/test_db.py @@ -7,59 +7,70 @@ create_tables(database) class TestDatabaseAPI(unittest.TestCase): + def test_add_and_get_chat(self): - add_chat(chat_id=21, chat_role=1, chat_stats=0, chat_federation=0) - chat = get_chat(21) - self.assertIsNotNone(chat) - self.assertEqual(chat.id, 21) - self.assertEqual(chat.chat_role, 1) + add_chat(chat_id=21, chat_role=0, chat_stats=0, chat_federation=0) + add_chat(chat_id=22, chat_role=1, chat_stats=100, chat_federation=1) + + chat1 = get_chat(21) + self.assertIsNotNone(chat1) + self.assertEqual(chat1.id, 21) + self.assertEqual(chat1.chat_role, 0) + + chat2 = get_chat(22) + self.assertIsNotNone(chat2) + self.assertEqual(chat2.id, 22) + self.assertEqual(chat2.chat_role, 1) def test_add_and_get_message(self): - add_message(message_id=1, message_text="Test Message", message_sender=1, answer_id=2) - message = get_message(1) - self.assertIsNotNone(message) - self.assertEqual(message.id, 1) - self.assertEqual(message.message_text, "Test Message") + add_message(message_id=1, message_text="Test Message 1", message_sender=1, answer_id=2) + add_message(message_id=2, message_text="Test Message 2", message_sender=2, answer_id=1) + + message1 = get_message(1) + self.assertIsNotNone(message1) + self.assertEqual(message1.id, 1) + self.assertEqual(message1.message_text, "Test Message 1") + + message2 = get_message(2) + self.assertIsNotNone(message2) + self.assertEqual(message2.id, 2) + self.assertEqual(message2.message_text, "Test Message 2") def test_add_and_get_user(self): - add_user(user_id=100, user_name="TestUser", user_tag="TestTag", user_role=2, user_stats=10, user_rep=5) - user = get_user(100) - self.assertIsNotNone(user) - self.assertEqual(user.id, 100) - self.assertEqual(user.user_name, "TestUser") + add_user(user_id=100, user_name="TestUser1", user_tag="TestTag1", user_role=0, user_stats=10, user_rep=5) + add_user(user_id=101, user_name="TestUser2", user_tag="TestTag2", user_role=1, user_stats=20, user_rep=10) + + user1 = get_user(100) + self.assertIsNotNone(user1) + self.assertEqual(user1.id, 100) + self.assertEqual(user1.user_name, "TestUser1") + + user2 = get_user(101) + self.assertIsNotNone(user2) + self.assertEqual(user2.id, 101) + self.assertEqual(user2.user_name, "TestUser2") def test_get_user_role(self): - add_user(user_id=101, user_name="TestUser2", user_tag="TestTag2", user_role=3, user_stats=20, user_rep=10) - user_role = get_user_role(101) - self.assertEqual(user_role, 3) + add_user(user_id=102, user_name="TestUser3", user_tag="TestTag3", user_role=0, user_stats=30, user_rep=15) + add_user(user_id=103, user_name="TestUser4", user_tag="TestTag4", user_role=1, user_stats=40, user_rep=20) + + user_role1 = get_user_role(102) + self.assertEqual(user_role1, 0) + + user_role2 = get_user_role(103) + self.assertEqual(user_role2, 1) def test_change_user_name(self): - add_user(user_id=102, user_name="OldName", user_tag="TestTag3", user_role=4, user_stats=30, user_rep=15) - change_user_name(102, "NewName") - updated_user = get_user(102) - self.assertEqual(updated_user.user_name, "NewName") + add_user(user_id=104, user_name="OldName1", user_tag="TestTag5", user_role=0, user_stats=50, user_rep=25) + change_user_name(104, "NewName1") + updated_user1 = get_user(104) + self.assertEqual(updated_user1.user_name, "NewName1") - # Дополнительные тесты для других функций обновления... + add_user(user_id=105, user_name="OldName2", user_tag="TestTag6", user_role=1, user_stats=60, user_rep=30) + change_user_name(105, "NewName2") + updated_user2 = get_user(105) + self.assertEqual(updated_user2.user_name, "NewName2") - def test_update_user_stats(self): - add_user(user_id=103, user_name="UserStats", user_tag="TestTag4", user_role=5, user_stats=40, user_rep=20) - update_user_stats() - updated_user = get_user(103) - self.assertEqual(updated_user.user_stats, 41) - - def test_update_user_rep(self): - add_user(user_id=104, user_name="UserRep", user_tag="TestTag5", user_role=6, user_stats=50, user_rep=25) - update_user_rep() - updated_user = get_user(104) - self.assertEqual(updated_user.user_rep, 26) - - def test_update_chat_stats(self): - add_chat(chat_id=22, chat_role=2, chat_stats=100, chat_federation=0) - update_chat_stats() - updated_chat = get_chat(22) - self.assertEqual(updated_chat.chat_stats, 101) - - # Дополнительные тесты для других функций... if __name__ == '__main__': unittest.main() diff --git a/src/modules/standart/roles/__init__.py b/src/modules/standart/roles/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/src/modules/standart/roles/api.py b/src/modules/standart/roles/api.py new file mode 100644 index 0000000..06dbc12 --- /dev/null +++ b/src/modules/standart/roles/api.py @@ -0,0 +1,43 @@ +from src.modules.standart.database import get_user_role +from src.modules.standart.config.config import yaml_load + + +class Roles: + user = "USER" + moderator = "MODERATOR" + admin = "ADMIN" + bot = "BOT" + __roles = yaml_load["ROLES"] + + def __init__(self): + self.user_role_id = self.__roles[self.user] + self.moderator_role_id = self.__roles[self.moderator] + self.admin_role_id = self.__roles[self.admin] + self.bot_role_id = self.__roles[self.bot] + + async def check_admin_permission(self, user_id): + match get_user_role(user_id): + case self.admin_role_id: + return True + case _: + return False + + async def check_moderator_permission(self, user_id): + match get_user_role(user_id): + case self.moderator_role_id: + return True + case _: + return False + + async def get_role_name(self, role_id): + match role_id: + case self.admin_role_id: + return self.admin + case self.moderator_role_id: + return self.moderator + case self.user_role_id: + return self.user + case self.bot_role_id: + return self.bot + case _: + raise ValueError(f"Нет роли с id={role_id}") diff --git a/src/modules/standart/roles/info.json b/src/modules/standart/roles/info.json new file mode 100644 index 0000000..80d3b3d --- /dev/null +++ b/src/modules/standart/roles/info.json @@ -0,0 +1,6 @@ +{ + "name": "Roles", + "description": "Модуль для работы с ролями", + "author": "OCAB Team", + "version": "1.0" +} \ No newline at end of file diff --git a/src/modules/standart/roles/tests/__init__.py b/src/modules/standart/roles/tests/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/src/modules/standart/roles/tests/config.yaml b/src/modules/standart/roles/tests/config.yaml new file mode 100644 index 0000000..65a4d10 --- /dev/null +++ b/src/modules/standart/roles/tests/config.yaml @@ -0,0 +1,7 @@ +TELEGRAM: + TOKEN: xxxxxxxxxxxxxxxxxxxx +ROLES: + ADMIN: 0 + MODERATOR: 1 + USER: 2 + BOT: 3 \ No newline at end of file diff --git a/src/modules/standart/roles/tests/test_roles.py b/src/modules/standart/roles/tests/test_roles.py new file mode 100644 index 0000000..cb93ba4 --- /dev/null +++ b/src/modules/standart/roles/tests/test_roles.py @@ -0,0 +1,35 @@ +import unittest +from src.modules.standart.database.api import create_tables, add_user +from src.modules.standart.database.models.base import database +from src.modules.standart.roles.api import Roles +import asyncio + +create_tables(database) + +class TestRoles(unittest.IsolatedAsyncioTestCase): + async def asyncSetUp(self): + self.roles = Roles() + + async def test_check_admin_permission(self): + self.assertTrue(await self.roles.check_admin_permission(1)) + self.assertFalse(await self.roles.check_admin_permission(2)) + self.assertFalse(await self.roles.check_admin_permission(3)) + self.assertFalse(await self.roles.check_admin_permission(4)) + + async def test_check_moderator_permission(self): + self.assertTrue(await self.roles.check_moderator_permission(2)) + self.assertFalse(await self.roles.check_moderator_permission(0)) + self.assertFalse(await self.roles.check_moderator_permission(1)) + self.assertFalse(await self.roles.check_moderator_permission(3)) + + async def test_get_role_name(self): + self.assertEqual(await self.roles.get_role_name(self.roles.admin_role_id), "ADMIN") + self.assertEqual(await self.roles.get_role_name(self.roles.moderator_role_id), "MODERATOR") + self.assertEqual(await self.roles.get_role_name(self.roles.user_role_id), "USER") + self.assertEqual(await self.roles.get_role_name(self.roles.bot_role_id), "BOT") + with self.assertRaises(ValueError): + await self.roles.get_role_name(999) # Несуществующий ID роли + + +if __name__ == '__main__': + unittest.main()