From b34348c05bca0f83f1da7cf14151e47001746d98 Mon Sep 17 00:00:00 2001 From: epicadk Date: Sat, 8 May 2021 22:54:56 +0530 Subject: [PATCH] feat : add validations --- app/database/models/mentorship_relation.py | 9 ++++ app/database/models/task_comment.py | 9 ++++ app/database/models/user.py | 22 ++++++++- .../test_database_model.py | 14 ++++++ tests/task_comments/test_database_model.py | 22 +++++++++ tests/users/test_database_model.py | 49 +++++++++++++++++-- 6 files changed, 121 insertions(+), 4 deletions(-) create mode 100644 tests/task_comments/test_database_model.py diff --git a/app/database/models/mentorship_relation.py b/app/database/models/mentorship_relation.py index d3d4be6cc..47bbddb94 100644 --- a/app/database/models/mentorship_relation.py +++ b/app/database/models/mentorship_relation.py @@ -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 @@ -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 diff --git a/app/database/models/task_comment.py b/app/database/models/task_comment.py index 3dfac23c7..6846ea6cd 100644 --- a/app/database/models/task_comment.py +++ b/app/database/models/task_comment.py @@ -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): @@ -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 diff --git a/app/database/models/user.py b/app/database/models/user.py index 075b7eb07..c1aadef1c 100644 --- a/app/database/models/user.py +++ b/app/database/models/user.py @@ -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): @@ -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 diff --git a/tests/mentorship_relation/test_database_model.py b/tests/mentorship_relation/test_database_model.py index 654d874ed..9d16b7b7e 100644 --- a/tests/mentorship_relation/test_database_model.py +++ b/tests/mentorship_relation/test_database_model.py @@ -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() diff --git a/tests/task_comments/test_database_model.py b/tests/task_comments/test_database_model.py new file mode 100644 index 000000000..63fd1046a --- /dev/null +++ b/tests/task_comments/test_database_model.py @@ -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") diff --git a/tests/users/test_database_model.py b/tests/users/test_database_model.py index c183d5aef..7ab4be85c 100644 --- a/tests/users/test_database_model.py +++ b/tests/users/test_database_model.py @@ -7,6 +7,7 @@ from tests.test_data import test_admin_user + # Testing User database model # # TODO tests: @@ -14,9 +15,8 @@ # - 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) @@ -31,7 +31,6 @@ def test_is_first_user_admin(self): ) def test_second_user_cannot_be_admin(self): - user = UserModel( name="User1", email="user1@email.com", @@ -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="user1@email.com", + 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="user1@email.com", + username="user_not$$_admin", + password="user1_password", + terms_and_conditions_checked=True, + ) + + def test_user_validations(self): + self.assertRaises( + AssertionError, + UserModel, + name="User", + email="user1@email.com", + username="user_not_admin", + password="user1_password", + terms_and_conditions_checked=False, + ) + if __name__ == "__main__": unittest.main()