Skip to content

Commit

Permalink
feat : add validations
Browse files Browse the repository at this point in the history
  • Loading branch information
epicadk committed May 8, 2021
1 parent ee009cf commit b34348c
Show file tree
Hide file tree
Showing 6 changed files with 121 additions and 4 deletions.
9 changes: 9 additions & 0 deletions app/database/models/mentorship_relation.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from app.database.models.tasks_list import TasksListModel
from app.database.models.user import UserModel
from app.database.sqlalchemy_extension import db
from sqlalchemy.orm import validates
from app.utils.enum_utils import MentorshipRelationState


Expand Down Expand Up @@ -123,3 +124,11 @@ def delete_from_db(self) -> None:
self.tasks_list.delete_from_db()
db.session.delete(self)
db.session.commit()

@validates("notes")
def validate(self, key, value):
if key == "notes":
assert value is not None
value = str(value).strip()
assert len(value.strip()) > 2
return value
9 changes: 9 additions & 0 deletions app/database/models/task_comment.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from app.api.validations.task_comment import COMMENT_MAX_LENGTH
from app.database.sqlalchemy_extension import db
from sqlalchemy.orm import validates


class TaskCommentModel(db.Model):
Expand Down Expand Up @@ -105,3 +106,11 @@ def delete_from_db(self):
"""Deletes a comment task from the database."""
db.session.delete(self)
db.session.commit()

@validates("comment")
def validate(self, key, value):
if key == "comment":
assert value is not None
value = str(value).strip()
assert len(value) > 2
return value
22 changes: 21 additions & 1 deletion app/database/models/user.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
from werkzeug.security import generate_password_hash, check_password_hash
import time

from sqlalchemy.orm import validates
from werkzeug.security import generate_password_hash, check_password_hash

from app.database.sqlalchemy_extension import db
from app.utils.validation_utils import (
is_name_valid,
is_email_valid,
is_username_valid,
)


class UserModel(db.Model):
Expand Down Expand Up @@ -155,3 +163,15 @@ def delete_from_db(self) -> None:
"""Deletes a user from the database."""
db.session.delete(self)
db.session.commit()

@validates("username", "name", "email", "terms_and_conditions_checked")
def validate(self, key, value):
if key == "username":
assert is_username_valid(value)
elif key == "name":
assert is_name_valid(value)
elif key == "email":
assert is_email_valid(value)
elif key == "terms_and_conditions_checked":
assert value is True
return value
14 changes: 14 additions & 0 deletions tests/mentorship_relation/test_database_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,20 @@ def test_empty_table(self):
db.session.commit()
self.assertTrue(MentorshipRelationModel.is_empty())

def test__validations_comment(self):
self.assertRaises(
AssertionError,
MentorshipRelationModel,
action_user_id=self.first_user.id,
mentor_user=self.first_user,
mentee_user=self.second_user,
creation_date=self.now_datetime,
end_date=self.end_date_example,
state=MentorshipRelationState.PENDING,
notes=" s ",
tasks_list=TasksListModel(),
)


if __name__ == "__main__":
unittest.main()
22 changes: 22 additions & 0 deletions tests/task_comments/test_database_model.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
from app.database.models.task_comment import TaskCommentModel
from tests.base_test_case import BaseTestCase


class TestAdminUserModel(BaseTestCase):
def test_task_validations_comment(self):
self.assertRaises(
AssertionError,
TaskCommentModel,
user_id=1,
task_id=1,
relation_id=1,
comment=""
)

def test_task_validations_comment_strip(self):
comment = TaskCommentModel(
user_id=1,
task_id=1,
relation_id=1,
comment=" user ")
self.assertEqual(comment.comment, "user")
49 changes: 46 additions & 3 deletions tests/users/test_database_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,16 @@

from tests.test_data import test_admin_user


# Testing User database model
#
# TODO tests:
# - User insertion/deletion/update/read
# - Check if first user is an admin


class TestAdminUserModel(BaseTestCase):
class TestUserModel(BaseTestCase):
def test_is_first_user_admin(self):

user = UserModel.query.filter_by(email=test_admin_user["email"]).first()
self.assertTrue(user is not None)
self.assertTrue(user.id is not None)
Expand All @@ -31,7 +31,6 @@ def test_is_first_user_admin(self):
)

def test_second_user_cannot_be_admin(self):

user = UserModel(
name="User1",
email="[email protected]",
Expand All @@ -55,6 +54,50 @@ def test_second_user_cannot_be_admin(self):
self.assertIsInstance(user.registration_date, float)
self.assertFalse(user.is_email_verified)

def test_user_validations_name(self):
self.assertRaises(
AssertionError,
UserModel,
name="User@$1",
email="[email protected]",
username="user",
password="user1_password",
terms_and_conditions_checked=True,
)

def test_user_validations_email(self):
self.assertRaises(
AssertionError,
UserModel,
name="User1",
email="user1",
username="user",
password="user1_password",
terms_and_conditions_checked=True,
)

def test_user_validations_username(self):
self.assertRaises(
AssertionError,
UserModel,
name="User",
email="[email protected]",
username="user_not$$_admin",
password="user1_password",
terms_and_conditions_checked=True,
)

def test_user_validations(self):
self.assertRaises(
AssertionError,
UserModel,
name="User",
email="[email protected]",
username="user_not_admin",
password="user1_password",
terms_and_conditions_checked=False,
)


if __name__ == "__main__":
unittest.main()

0 comments on commit b34348c

Please sign in to comment.