Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat : add validations #1096

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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":
if 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()
20 changes: 20 additions & 0 deletions tests/task_comments/test_database_model.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
from app.database.models.task_comment import TaskCommentModel
from tests.base_test_case import BaseTestCase


class TestTaskCommentModel(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")
10 changes: 5 additions & 5 deletions tests/test_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@
}

test_admin_user_2 = {
"name": "Admin_2",
"name": "Adminb",
"email": "[email protected]",
"username": "admin2_username",
"password": "admin2_pwd",
"terms_and_conditions_checked": True,
}

test_admin_user_3 = {
"name": "Admin_3",
"name": "Adminc",
"email": "[email protected]",
"username": "admin3_username",
"password": "admin3_pwd",
Expand All @@ -42,23 +42,23 @@
}

user3 = {
"name": "s_t-r$a/n'ge name",
"name": "strange name",
"email": "[email protected]",
"username": "user3",
"password": "user3_pwd",
"terms_and_conditions_checked": True,
}

user4 = {
"name": "[email protected]",
"name": "userc",
"email": "[email protected]",
"username": "user4",
"password": "user4_pwd",
"terms_and_conditions_checked": True,
}

user5 = {
"name": "[email protected]",
"name": "userd",
"email": "[email protected]",
"username": "user5",
"password": "user5_pwd",
Expand Down
2 changes: 1 addition & 1 deletion tests/users/test_api_change_password.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def setUp(self):
super().setUp()
self.first_user = UserModel(
password=user1["password"],
name="User1",
name="User",
email="[email protected]",
username="user_not_admin",
terms_and_conditions_checked=True,
Expand Down
28 changes: 14 additions & 14 deletions tests/users/test_api_home_statistics.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ class TestHomeStatisticsApi(BaseTestCase):
def setUp(self):
super().setUp()

self.user1 = UserModel("User1", "user1", "__test__", "[email protected]", True)
self.user2 = UserModel("User2", "user2", "__test__", "[email protected]", True)
self.user1 = UserModel("UserA", "user1", "__test__", "[email protected]", True)
self.user2 = UserModel("UserB", "user2", "__test__", "[email protected]", True)
self.user1.available_to_mentor = True
self.user1.is_email_verified = True
self.user2.need_mentoring = True
Expand Down Expand Up @@ -56,14 +56,14 @@ def test_pending_requests_auth(self):
creation_date=start_date,
end_date=end_date,
state=MentorshipRelationState.PENDING,
notes="",
notes=None,
tasks_list=tasks_list,
)

db.session.add(mentorship_relation)
db.session.commit()
expected_response = {
"name": "User1",
"name": "UserA",
"pending_requests": 1,
"accepted_requests": 0,
"rejected_requests": 0,
Expand Down Expand Up @@ -92,14 +92,14 @@ def test_accepted_requests_auth(self):
creation_date=start_date,
end_date=end_date,
state=MentorshipRelationState.ACCEPTED,
notes="",
notes=None,
tasks_list=tasks_list,
)

db.session.add(mentorship_relation)
db.session.commit()
expected_response = {
"name": "User1",
"name": "UserA",
"pending_requests": 0,
"accepted_requests": 1,
"rejected_requests": 0,
Expand Down Expand Up @@ -128,14 +128,14 @@ def test_rejected_requests(self):
creation_date=start_date,
end_date=end_date,
state=MentorshipRelationState.REJECTED,
notes="",
notes=None,
tasks_list=tasks_list,
)

db.session.add(mentorship_relation)
db.session.commit()
expected_response = {
"name": "User1",
"name": "UserA",
"pending_requests": 0,
"accepted_requests": 0,
"rejected_requests": 1,
Expand Down Expand Up @@ -164,14 +164,14 @@ def test_completed_relations(self):
creation_date=start_date,
end_date=end_date,
state=MentorshipRelationState.COMPLETED,
notes="",
notes=None,
tasks_list=tasks_list,
)

db.session.add(mentorship_relation)
db.session.commit()
expected_response = {
"name": "User1",
"name": "UserA",
"pending_requests": 0,
"accepted_requests": 0,
"rejected_requests": 0,
Expand Down Expand Up @@ -199,14 +199,14 @@ def test_cancelled_relations(self):
creation_date=start_date,
end_date=end_date,
state=MentorshipRelationState.CANCELLED,
notes="",
notes=None,
tasks_list=tasks_list,
)

db.session.add(mentorship_relation)
db.session.commit()
expected_response = {
"name": "User1",
"name": "UserA",
"pending_requests": 0,
"accepted_requests": 0,
"rejected_requests": 0,
Expand Down Expand Up @@ -253,15 +253,15 @@ def test_achievements(self):
creation_date=start_date,
end_date=end_date,
state=MentorshipRelationState.ACCEPTED,
notes="",
notes=None,
tasks_list=tasks_list,
)

db.session.add(mentorship_relation)
db.session.commit()

expected_response = {
"name": "User1",
"name": "UserA",
"pending_requests": 0,
"accepted_requests": 1,
"rejected_requests": 0,
Expand Down
3 changes: 2 additions & 1 deletion tests/users/test_api_list_users.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,11 +124,12 @@ def test_list_users_api_with_a_search_query_with_spaces_resource_auth(self):
self.assertEqual(HTTPStatus.OK, actual_response.status_code)
self.assertEqual(expected_response, json.loads(actual_response.data))

# invalid test case
def test_list_users_api_with_search_with_special_characters_resource_auth(self):
auth_header = get_test_request_header(self.admin_user.id)
expected_response = [marshal(self.second_user, public_user_api_model)]
actual_response = self.client.get(
f"/users?search=s_t-r%24a%2Fn'ge",
f"/users?search=strange%20%20name",
follow_redirects=True,
headers=auth_header,
)
Expand Down
4 changes: 2 additions & 2 deletions tests/users/test_dao.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class TestUserDao(BaseTestCase):
def test_dao_create_user(self):
dao = UserDAO()
data = dict(
name="User2",
name="UserB",
username="user2",
email="[email protected]",
password="test_password",
Expand All @@ -30,7 +30,7 @@ def test_dao_create_user(self):
user = UserModel.query.filter_by(email="[email protected]").first()
self.assertTrue(user is not None)
self.assertTrue(user.id is not None)
self.assertTrue(user.name == "User2")
self.assertTrue(user.name == "UserB")
self.assertTrue(user.username == "user2")
self.assertTrue(user.email == "[email protected]")
self.assertFalse(user.is_admin)
Expand Down
Loading