From 8924616fa20d5d37026f48ef1481e00932622b2a Mon Sep 17 00:00:00 2001 From: "Kushal Beniwal (she/her)" Date: Sat, 31 Jul 2021 22:49:10 +0530 Subject: [PATCH] test: add tests for user file (#1121) --- app/api/dao/user.py | 9 +- tests/users/test_dao.py | 140 +++++++++++++++++++++++----- tests/users/test_dao_dashboard.py | 8 ++ tests/users/test_dao_update_user.py | 88 +++++++++++++++-- 4 files changed, 207 insertions(+), 38 deletions(-) diff --git a/app/api/dao/user.py b/app/api/dao/user.py index f06e93555..15d6f4e0d 100644 --- a/app/api/dao/user.py +++ b/app/api/dao/user.py @@ -94,11 +94,8 @@ def delete_user(user_id: int): if admins_list_count <= UserDAO.MIN_NUMBER_OF_ADMINS: return messages.USER_CANT_DELETE, HTTPStatus.BAD_REQUEST - if user: - user.delete_from_db() - return messages.USER_SUCCESSFULLY_DELETED, HTTPStatus.OK - - return messages.USER_DOES_NOT_EXIST, HTTPStatus.NOT_FOUND + user.delete_from_db() + return messages.USER_SUCCESSFULLY_DELETED, HTTPStatus.OK @staticmethod @email_verification_required @@ -223,8 +220,6 @@ def update_user_profile(user_id: int, data: Dict[str, str]): """ user = UserModel.find_by_id(user_id) - if not user: - return messages.USER_DOES_NOT_EXIST, HTTPStatus.NOT_FOUND username = data.get("username", None) if username: diff --git a/tests/users/test_dao.py b/tests/users/test_dao.py index 182628cb8..968ddb9aa 100644 --- a/tests/users/test_dao.py +++ b/tests/users/test_dao.py @@ -17,6 +17,11 @@ class TestUserDao(BaseTestCase): + def setUp(self): + super().setUp() + + self.user = UserModel(**user2) + def test_dao_create_user(self): dao = UserDAO() data = dict( @@ -45,14 +50,7 @@ def test_dao_create_user(self): def test_dao_confirm_registration_good_token(self): dao = UserDAO() - user = UserModel( - name=user2["name"], - email=user2["email"], - username=user2["username"], - password=user2["password"], - terms_and_conditions_checked=user2["terms_and_conditions_checked"], - ) - db.session.add(user) + db.session.add(self.user) db.session.commit() # Verify that user was inserted in database through DAO @@ -75,14 +73,7 @@ def test_dao_confirm_registration_good_token(self): def test_dao_confirm_registration_bad_token(self): dao = UserDAO() - user = UserModel( - name=user2["name"], - email=user2["email"], - username=user2["username"], - password=user2["password"], - terms_and_conditions_checked=user2["terms_and_conditions_checked"], - ) - db.session.add(user) + db.session.add(self.user) db.session.commit() # Verify that user was inserted in database through DAO @@ -106,14 +97,7 @@ def test_dao_confirm_registration_bad_token(self): def test_dao_confirm_registration_of_already_verified_user(self): dao = UserDAO() - user = UserModel( - name=user2["name"], - email=user2["email"], - username=user2["username"], - password=user2["password"], - terms_and_conditions_checked=user2["terms_and_conditions_checked"], - ) - db.session.add(user) + db.session.add(self.user) db.session.commit() # Verify that user was inserted in database through DAO @@ -150,6 +134,114 @@ def test_dao_delete_only_user_admin(self): (messages.USER_CANT_DELETE, HTTPStatus.BAD_REQUEST), dao_result ) + def test_dao_delete_user(self): + dao = UserDAO() + + db.session.add(self.user) + db.session.commit() + + # Verify that user was inserted in database through DAO + user = UserModel.query.filter_by(id=2).first() + self.assertIsNotNone(user) + + # Verify email + token = generate_confirmation_token(user2["email"]) + result = dao.confirm_registration(token) + self.assertTrue(user.is_email_verified) + + dao_result = dao.delete_user(2) + + user = UserModel.query.filter_by(id=2).first() + self.assertIsNone(user) + self.assertEqual( + (messages.USER_SUCCESSFULLY_DELETED, HTTPStatus.OK), dao_result + ) + + def test_dao_delete_user_that_does_not_exist(self): + dao = UserDAO() + + # Verify that user does not exist + before_delete_user = UserModel.query.filter_by(id=2).first() + self.assertIsNone(before_delete_user) + + dao_result = dao.delete_user(2) + + self.assertEqual( + (messages.USER_DOES_NOT_EXIST, HTTPStatus.NOT_FOUND), dao_result + ) + + def test_dao_get_user_by_email(self): + dao = UserDAO() + + db.session.add(self.user) + db.session.commit() + + # Verify that user was inserted in database through DAO + user = UserModel.query.filter_by(email=user2["email"]).first() + self.assertIsNotNone(user) + + user_with_given_email = dao.get_user_by_email(user2["email"]) + self.assertIsNotNone(user_with_given_email) + self.assertEqual(user_with_given_email, user) + + def test_dao_get_user_by_username(self): + dao = UserDAO() + + db.session.add(self.user) + db.session.commit() + + # Verify that user was inserted in database through DAO + user = UserModel.query.filter_by(email=user2["email"]).first() + self.assertIsNotNone(user) + + user_with_given_username = dao.get_user_by_username(user2["username"]) + self.assertIsNotNone(user_with_given_username) + self.assertEqual(user_with_given_username, user) + + def test_authenticate_user_by_email(self): + dao = UserDAO() + + db.session.add(self.user) + db.session.commit() + + # Verify that user was inserted in database through DAO + before_delete_user = UserModel.query.filter_by(id=2).first() + self.assertIsNotNone(before_delete_user) + + # Verify email + token = generate_confirmation_token(user2["email"]) + result = dao.confirm_registration(token) + self.assertTrue(self.user.is_email_verified) + + dao_result = dao.authenticate(user2["email"], user2["password"]) + + self.assertIsNotNone(dao_result) + self.assertEqual(dao_result, self.user) + + def test_change_password_with_incorrect_password(self): + dao = UserDAO() + + db.session.add(self.user) + db.session.commit() + + # Verify that user was inserted in database through DAO + before_delete_user = UserModel.query.filter_by(id=2).first() + self.assertIsNotNone(before_delete_user) + + # Verify email + token = generate_confirmation_token(user2["email"]) + result = dao.confirm_registration(token) + self.assertTrue(self.user.is_email_verified) + + data = dict(current_password="wrong password", new_password="new password") + dao_result = dao.change_password(user_id=2, data=data) + + self.assertIsNotNone(dao_result) + self.assertEqual( + (messages.USER_ENTERED_INCORRECT_PASSWORD, HTTPStatus.BAD_REQUEST), + dao_result, + ) + def test_get_achievements(self): dao = UserDAO() diff --git a/tests/users/test_dao_dashboard.py b/tests/users/test_dao_dashboard.py index 058969325..ab5b748ce 100644 --- a/tests/users/test_dao_dashboard.py +++ b/tests/users/test_dao_dashboard.py @@ -176,6 +176,14 @@ def test_dao_get_user_dashboard(self): actual_response = UserDAO.get_user_dashboard(self.first_user.id) self.assertEqual(actual_response, expected_response) + def test_user_does_not_exist(self): + id_with_no_user = 4 + user = UserModel.query.filter_by(id=id_with_no_user).first() + self.assertIsNone(user) + + response = UserDAO.get_user_dashboard(id_with_no_user) + self.assertIsNone(response) + if __name__ == "__main__": unittest.main() diff --git a/tests/users/test_dao_update_user.py b/tests/users/test_dao_update_user.py index c9fd422e3..7a0352104 100644 --- a/tests/users/test_dao_update_user.py +++ b/tests/users/test_dao_update_user.py @@ -1,37 +1,111 @@ import unittest +from http import HTTPStatus +from app import messages from app.api.dao.user import UserDAO +from app.database.models.user import UserModel from tests.base_test_case import BaseTestCase class TestUpdateUserDao(BaseTestCase): def test_dao_update_user(self): + self.assertIsNotNone(self.admin_user.name) + self.assertIsNone(self.admin_user.bio) + self.assertIsNone(self.admin_user.location) self.assertIsNone(self.admin_user.occupation) self.assertIsNone(self.admin_user.organization) + self.assertIsNone(self.admin_user.slack_username) + self.assertIsNone(self.admin_user.social_media_links) + self.assertIsNone(self.admin_user.skills) + self.assertIsNone(self.admin_user.interests) + self.assertIsNone(self.admin_user.resume_url) + self.assertIsNone(self.admin_user.photo_url) - data = dict(occupation="good_developer", organization="good_org") + data = dict( + name="good_name", + bio="good_bio", + location="good_location", + occupation="good_developer", + organization="good_org", + slack_username="good_slack_username", + social_media_links="good_social_media_links", + skills="good_skills", + interests="good_interests", + resume_url="good_resume_url", + photo_url="good_photo_url", + ) UserDAO.update_user_profile(self.admin_user.id, data) + self.assertEqual("good_name", self.admin_user.name) + self.assertEqual("good_bio", self.admin_user.bio) + self.assertEqual("good_location", self.admin_user.location) self.assertEqual("good_developer", self.admin_user.occupation) self.assertEqual("good_org", self.admin_user.organization) + self.assertEqual("good_slack_username", self.admin_user.slack_username) + self.assertEqual("good_social_media_links", self.admin_user.social_media_links) + self.assertEqual("good_skills", self.admin_user.skills) + self.assertEqual("good_interests", self.admin_user.interests) + self.assertEqual("good_resume_url", self.admin_user.resume_url) + self.assertEqual("good_photo_url", self.admin_user.photo_url) def test_update_fields_with_empty_data(self): + name_before_update = self.admin_user.name + self.assertIsNotNone(self.admin_user.name) + + self.assertIsNone(self.admin_user.bio) + self.assertIsNone(self.admin_user.location) self.assertIsNone(self.admin_user.occupation) self.assertIsNone(self.admin_user.organization) + self.assertIsNone(self.admin_user.slack_username) + self.assertIsNone(self.admin_user.social_media_links) + self.assertIsNone(self.admin_user.skills) + self.assertIsNone(self.admin_user.interests) + self.assertIsNone(self.admin_user.resume_url) + self.assertIsNone(self.admin_user.photo_url) - data = dict(occupation="good_developer", organization="good_org") + data = dict( + name="", + bio="", + location="", + occupation="", + organization="", + slack_username="", + social_media_links="", + skills="", + interests="", + resume_url="", + photo_url="", + ) UserDAO.update_user_profile(self.admin_user.id, data) - self.assertEqual("good_developer", self.admin_user.occupation) - self.assertEqual("good_org", self.admin_user.organization) - - data = dict(occupation="", organization="") - UserDAO.update_user_profile(self.admin_user.id, data) + name_after_update = self.admin_user.name + self.assertIsNotNone(self.admin_user.name) + self.assertEqual(name_before_update, name_after_update) + self.assertIsNone(self.admin_user.bio) + self.assertIsNone(self.admin_user.location) self.assertIsNone(self.admin_user.occupation) self.assertIsNone(self.admin_user.organization) + self.assertIsNone(self.admin_user.slack_username) + self.assertIsNone(self.admin_user.social_media_links) + self.assertIsNone(self.admin_user.skills) + self.assertIsNone(self.admin_user.interests) + self.assertIsNone(self.admin_user.resume_url) + self.assertIsNone(self.admin_user.photo_url) + + def test_update_user_that_does_not_exist(self): + + user = UserModel.query.filter_by(id=2).first() + self.assertIsNone(user) + + data = dict(occupation="good_developer", organization="good_org") + dao_result = UserDAO.update_user_profile(user, data) + + self.assertEqual( + (messages.USER_DOES_NOT_EXIST, HTTPStatus.NOT_FOUND), dao_result + ) if __name__ == "__main__":