Skip to content

Commit

Permalink
feat: add models tests
Browse files Browse the repository at this point in the history
relates to #9
  • Loading branch information
vugonz committed Nov 14, 2024
1 parent accee0a commit ce1fa64
Show file tree
Hide file tree
Showing 7 changed files with 104 additions and 30 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/unittests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,5 @@ jobs:
pip install -r requirements.txt
- name: Test with unittest module
run: |
python -m unittest tests/roles/roles_handler.py
python -m unittest discover -s tests
2 changes: 0 additions & 2 deletions app/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,7 @@ class Config:
PHOTOS_DIR = os.path.join(basedir, _get_env_or_default("PHOTOS_DIR", "data/photos/")).rstrip("/")
MAX_CONTENT_LENGTH = _get_int_env_or_default("MAX_PHOTO_SIZE", 16 * 1000 * 1000) ## max pohto size

print(_get_env_or_default("LOGS_PATH", ""))
LOGS_PATH = os.path.join(basedir, _get_env_or_default("LOGS_PATH", ""))
print(LOGS_PATH)
LOG_LEVEL = _get_env_or_default("LOG_LEVEL", "INFO")

ADMIN_USERNAME = _get_env_or_default("ADMIN_USERNAME", "")
Expand Down
40 changes: 20 additions & 20 deletions app/models/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,10 @@ def __init__(
self.member_number = member_number
self.name = name
self.join_date = join_date
self.exit_date = exit_date
self.course = course
self.description = description
self.email = email
self.exit_date = exit_date
self.description = description
self.extra = extra
self.roles = roles
self.check_invariants()
Expand Down Expand Up @@ -110,40 +110,40 @@ def check_invariants(self):
if not isinstance(self.name, str) or not self.name:
raise ValueError("Field 'name' must be a non-empty string.")

# email
if not isinstance(self.email, str) or not self.email:
raise ValueError("Field 'email' must be a non-empty string.")
# join_date
if not isinstance(self.join_date, str) or not self.join_date:
raise ValueError("Field 'join_date' must be a non-empty string.")
_validate_date_string(self.join_date, "join_date")

# course
if not isinstance(self.course, str) or not self.course:
raise ValueError("Field 'course' must be a non-empty string.")

# email
if not isinstance(self.email, str) or not self.email:
raise ValueError("Field 'email' must be a non-empty string.")

# description
# exit_date
if not isinstance(self.exit_date, str):
raise ValueError("Field 'exit_date' must be a string.")
if self.exit_date != "":
_validate_date_string(self.exit_date, "exit_date")

# description
if not isinstance(self.description, str):
raise ValueError("Field 'description' must be a string.")

# extra
if not isinstance(self.extra, str):
raise ValueError("Field 'extra' must be a string.")

# roles
if not isinstance(self.roles, list) or len(self.roles) == 0:
# roles
if not isinstance(self.roles, list) or (len(self.roles) == 1 and self.roles[0] == ""):
raise ValueError("Field 'roles' must have at least 1 role")
for role in self.roles:
if role not in roles_handler.roles.keys():
if not roles_handler.exists_role(role):
raise ValueError(f"Unknown role '{role}'")

# join_date
if not isinstance(self.join_date, str) or not self.join_date:
raise ValueError("Field 'join_date' must be a non-empty string.")
_validate_date_string(self.join_date, "join_date")

# exit_date
if not isinstance(self.exit_date, str):
raise ValueError("Field 'exit_date' must be a string.")
if self.exit_date != "":
_validate_date_string(self.exit_date, "exit_date")

def to_dict(self):
return {
"ist_id": self.ist_id,
Expand Down
6 changes: 3 additions & 3 deletions app/roles/roles_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ def _get_highest(self, roles_list: list) -> Tuple[str, int]:

return highest_role, highest_lvl

def exists_role(self, role):
return role in self.roles.keys()

def has_permission(self, roles_list: List[str], permission: str):
""" Checks wether any role in `roles_list` has `permission`"""
for role in roles_list:
Expand All @@ -42,9 +45,6 @@ def has_permission(self, roles_list: List[str], permission: str):
def has_higher_level(self, roles_list: List[str], role: str):
"""" Checks whether any role in `roles_list` has higher level than `role`. """
_, highest_lvl = self._get_highest(roles_list)
print(highest_lvl)
print(role)
print(self._get_role_level(role))
if highest_lvl == 0:
return True # level 0 has all permissions
return highest_lvl < self._get_role_level(role) # < instead of <= means we cannot add "horizontally"
Expand Down
8 changes: 4 additions & 4 deletions app/services/member_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@
from app.models import Member

def create_member(
username: str,
password: str,
ist_id: str,
member_number: int,
name: str,
username: str,
password: str,
join_date: str,
course: str,
email: str,
Expand All @@ -23,11 +23,11 @@ def create_member(
extra: str = "",
) -> Member :
new_member = Member(
username=username,
password=password,
ist_id=ist_id,
member_number=member_number,
name=name,
username=username,
password=password,
join_date=join_date,
course=course,
email=email,
Expand Down
76 changes: 76 additions & 0 deletions tests/models/test_models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import unittest
from unittest.mock import Mock, patch

from app.models import Member
from app.extensions import roles_handler

class MemberModelTest(unittest.TestCase):
def test_invalid_username(self):
with self.assertRaises(ValueError):
Member(
username="ççç_invalid",
password="password",
ist_id="istid",
name="name",
member_number=10,
join_date="2014-01-01",
course="course",
email="[email protected]",
description="",
exit_date="",
extra="",
roles=["member"]
)

def test_no_roles(self):
with self.assertRaises(ValueError):
Member(
username="username",
password="password",
ist_id="istid",
name="name",
member_number=1,
join_date="2014-01-01",
course="course",
email="[email protected]",
description="",
exit_date="",
extra="",
roles=[]
)

@patch.object(roles_handler, 'exists_role')
def test_non_existant_role(self, exists_role):
exists_role.return_value = False # make exists_role return false
with self.assertRaises(ValueError):
Member(
username="username",
password="password",
ist_id="istid",
name="name",
member_number=1,
join_date="2014-01-01",
course="course",
email="[email protected]",
description="",
exit_date="",
extra="",
roles=["random role"]
)

def test_invalid_join_date(self):
with self.assertRaises(ValueError):
Member(
username="username",
password="password",
ist_id="istid",
name="name",
member_number=1,
join_date="01-01-1000",
course="course",
email="[email protected]",
description="",
exit_date="",
extra="",
roles=["member"]
)
File renamed without changes.

0 comments on commit ce1fa64

Please sign in to comment.