Skip to content

Commit

Permalink
Feat: Test cases for POST and GET /user/personal_background api endpo…
Browse files Browse the repository at this point in the history
…ints

Refactored Background enum logic

Refactor test to include other fields for physical, mental ability and socio economic

Remove POST api endpoint for create user personal background

Remove Create personal background dao
  • Loading branch information
mtreacy002 committed Aug 10, 2020
1 parent 35bafbb commit c0c12d0
Show file tree
Hide file tree
Showing 3 changed files with 196 additions and 4 deletions.
5 changes: 2 additions & 3 deletions tests/users/test_api_create_user_additional_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ def setUp(self, mock_login):
AUTH_COOKIE["user_id"] = self.test_user_data.id

self.correct_payload_additional_info = {
"user_id": self.test_user_data.id,
"is_organization_rep": True,
"timezone": "UTC-01:00/Cape Verde Time",
"phone": "123-456-789",
Expand Down Expand Up @@ -91,7 +90,7 @@ def test_api_dao_create_user_additional_info_successfully(self, mock_create_addi
)

test_user_additional_info_data = UserExtensionModel.query.filter_by(user_id=self.test_user_data.id).first()
self.assertEqual(test_user_additional_info_data.user_id, self.correct_payload_additional_info["user_id"])
self.assertEqual(test_user_additional_info_data.user_id, int(AUTH_COOKIE["user_id"].value))
self.assertEqual(test_user_additional_info_data.is_organization_rep, self.correct_payload_additional_info["is_organization_rep"])
self.assertEqual(test_user_additional_info_data.timezone.value, self.correct_payload_additional_info["timezone"])
self.assertEqual(test_user_additional_info_data.additional_info["phone"], self.correct_payload_additional_info["phone"])
Expand All @@ -117,7 +116,7 @@ def test_api_dao_create_user_additional_info_invalid_payload(self, mock_create_a
mock_create_additional_info.side_effect = requests.exceptions.HTTPError(response=mock_error)

test_user_additional_info = {
"user_id": self.test_user_data.id,
# "user_id": self.test_user_data.id,
"is_organization_rep": True,
"timezone": "UTC-01:00/Cape Verde Time",
"phone": "128abc",
Expand Down
193 changes: 193 additions & 0 deletions tests/users/test_api_get_personal_background.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,193 @@
import unittest
from http import HTTPStatus, cookies
from unittest.mock import patch, Mock
import requests
from requests.exceptions import HTTPError
from flask import json
from flask_restx import marshal
from app import messages
from tests.base_test_case import BaseTestCase
from app.api.request_api_utils import post_request, get_request, BASE_MS_API_URL, AUTH_COOKIE
from app.api.models.user import full_user_api_model, get_user_personal_background_response_model
from tests.test_data import user1
from app.database.models.ms_schema.user import UserModel
from app.database.models.bit_schema.personal_background import PersonalBackgroundModel


class TestGetUserPersonalBackgroundApi(BaseTestCase):

@patch("requests.post")
def setUp(self, mock_login):
super(TestGetUserPersonalBackgroundApi, self).setUp()

success_message = {"access_token": "this is fake token", "access_expiry": 1601478236}
success_code = HTTPStatus.OK

mock_login_response = Mock()
mock_login_response.json.return_value = success_message
mock_login_response.status_code = success_code
mock_login.return_value = mock_login_response
mock_login.raise_for_status = json.dumps(success_code)

user_login_success = {
"username": user1.get("username"),
"password": user1.get("password")
}

with self.client:
login_response = self.client.post(
"/login",
data=json.dumps(user_login_success),
follow_redirects=True,
content_type="application/json",
)

test_user = UserModel(
name=user1["name"],
username=user1["username"],
password=user1["password"],
email=user1["email"],
terms_and_conditions_checked=user1["terms_and_conditions_checked"]
)
test_user.need_mentoring = user1["need_mentoring"]
test_user.available_to_mentor = user1["available_to_mentor"]

test_user.save_to_db()

self.test_user_data = UserModel.find_by_email(test_user.email)

AUTH_COOKIE["user_id"] = self.test_user_data.id

self.response_personal_background = {
"user_id": int(AUTH_COOKIE["user_id"].value),
"gender": "Female",
"age": "Between 55 to 64 yo",
"ethnicity": "Middle Eastern/North African (MENA)",
"sexual_orientation": "Prefer not to say",
"religion": "Islam",
"physical_ability": "With/had limited physical ability (or with/had some type of physical disability/ies)",
"mental_ability": "With/previously had some type of mental disorders",
"socio_economic": "Lower Middle class (e.g. blue collars in skilled trades/Paralegals/Bank tellers/Sales/Clerical-Admin/other support workers)",
"highest_education": "Prefer not to say",
"years_of_experience": "Prefer not to say",
"gender_other": "They",
"ethnicity_other": "",
"sexual_orientation_other": "",
"religion_other": "Daoism",
"physical_ability_other": "",
"mental_ability_other": "",
"socio_economic_other": "",
"highest_education_other": "",
"is_public": False
}


@patch("requests.get")
def test_api_dao_get_user_personal_background_successfully(self, mock_get_personal_background):
success_message = self.response_personal_background
success_code = HTTPStatus.OK

mock_get_response = Mock()
mock_get_response.json.return_value = success_message
mock_get_response.status_code = success_code

mock_get_personal_background.return_value = mock_get_response
mock_get_personal_background.raise_for_status = json.dumps(success_code)

# prepare existing personal background
others = {
"gender_other": self.response_personal_background["gender_other"],
"ethnicity_other": self.response_personal_background["ethnicity_other"],
"sexual_orientation_other": self.response_personal_background["sexual_orientation_other"],
"religion_other": self.response_personal_background["religion_other"],
"physical_ability_other": self.response_personal_background["physical_ability_other"],
"mental_ability_other": self.response_personal_background["mental_ability_other"],
"socio_economic_other": self.response_personal_background["socio_economic_other"],
"highest_education_other": self.response_personal_background["highest_education_other"],
}

personal_background = PersonalBackgroundModel(
int(AUTH_COOKIE["user_id"].value),
"FEMALE",
"AGE_55_TO_64",
"MIDDLE_EASTERN",
"DECLINED",
"ISLAM",
"WITH_DISABILITY",
"WITH_DISORDER",
"LOWER_MIDDLE",
"DECLINED",
"DECLINED"
)
personal_background.others = others
personal_background.is_public = self.response_personal_background["is_public"]

personal_background.save_to_db()

with self.client:
response = self.client.get(
"/user/personal_background",
headers={"Authorization": AUTH_COOKIE["Authorization"].value},
follow_redirects=True,
content_type="application/json",
)

test_user_personal_background_data = PersonalBackgroundModel.query.filter_by(user_id=self.test_user_data.id).first()
self.assertEqual(test_user_personal_background_data.user_id, response.json["user_id"])
self.assertEqual(test_user_personal_background_data.gender.value, response.json["gender"])
self.assertEqual(test_user_personal_background_data.age.value, response.json["age"])
self.assertEqual(test_user_personal_background_data.ethnicity.value, response.json["ethnicity"])
self.assertEqual(test_user_personal_background_data.sexual_orientation.value, response.json["sexual_orientation"])
self.assertEqual(test_user_personal_background_data.religion.value, response.json["religion"])
self.assertEqual(test_user_personal_background_data.physical_ability.value, response.json["physical_ability"])
self.assertEqual(test_user_personal_background_data.mental_ability.value, response.json["mental_ability"])
self.assertEqual(test_user_personal_background_data.socio_economic.value, response.json["socio_economic"])
self.assertEqual(test_user_personal_background_data.highest_education.value, response.json["highest_education"])
self.assertEqual(test_user_personal_background_data.years_of_experience.value, response.json["years_of_experience"])
self.assertEqual(test_user_personal_background_data.others["gender_other"], response.json["gender_other"])
self.assertEqual(test_user_personal_background_data.others["ethnicity_other"], response.json["ethnicity_other"])
self.assertEqual(test_user_personal_background_data.others["sexual_orientation_other"], response.json["sexual_orientation_other"])
self.assertEqual(test_user_personal_background_data.others["religion_other"], response.json["religion_other"])
self.assertEqual(test_user_personal_background_data.others["physical_ability_other"], response.json["physical_ability_other"])
self.assertEqual(test_user_personal_background_data.others["mental_ability_other"], response.json["mental_ability_other"])
self.assertEqual(test_user_personal_background_data.others["socio_economic_other"], response.json["socio_economic_other"])
self.assertEqual(test_user_personal_background_data.others["highest_education_other"], response.json["highest_education_other"])
self.assertEqual(test_user_personal_background_data.is_public, response.json["is_public"])
self.assertEqual(response.json, success_message)
self.assertEqual(response.status_code, success_code)




@patch("requests.get")
def test_api_dao_get_non_existence_additional_info(self, mock_get_personal_background):
error_message = messages.PERSONAL_BACKGROUND_DOES_NOT_EXIST
error_code = HTTPStatus.NOT_FOUND

mock_response = Mock()
mock_error = Mock()
http_error = requests.exceptions.HTTPError()
mock_response.raise_for_status.side_effect = http_error
mock_get_personal_background.return_value = mock_response
mock_error.json.return_value = error_message
mock_error.status_code = error_code
mock_get_personal_background.side_effect = requests.exceptions.HTTPError(response=mock_error)

with self.client:
response = self.client.get(
"/user/personal_background",
headers={"Authorization": AUTH_COOKIE["Authorization"].value},
follow_redirects=True,
content_type="application/json",
)

test_user_personal_background_data = PersonalBackgroundModel.query.filter_by(user_id=self.test_user_data.id).first()
self.assertEqual(test_user_personal_background_data, None)
self.assertEqual(response.json, error_message)
self.assertEqual(response.status_code, error_code)


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


2 changes: 1 addition & 1 deletion tests/users/test_api_get_user_additional_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def setUp(self, mock_login):
AUTH_COOKIE["user_id"] = self.test_user_data.id

self.response_additional_info = {
"user_id": self.test_user_data.id,
"user_id": int(AUTH_COOKIE["user_id"].value),
"is_organization_rep": True,
"timezone": "UTC-01:00/Cape Verde Time",
"phone": "123-456-789",
Expand Down

0 comments on commit c0c12d0

Please sign in to comment.