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
  • Loading branch information
mtreacy002 committed Jul 18, 2020
1 parent 02a6ad3 commit c96d6f2
Show file tree
Hide file tree
Showing 4 changed files with 468 additions and 12 deletions.
258 changes: 258 additions & 0 deletions tests/users/test_api_create_personal_background.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,258 @@
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 TestCreatePersonalBackgroundApi(BaseTestCase):

@patch("requests.post")
def setUp(self, mock_login):
super(TestCreatePersonalBackgroundApi, 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.correct_payload_personal_background = {
"gender": "FEMALE",
"age": "AGE_55_TO_64",
"ethnicity": "MIDDLE_EASTERN",
"sexual_orientation": "DECLINED",
"religion": "ISLAM",
"physical_ability": "WITH_DISABILITY",
"mental_ability": "WITH_DISORDER",
"socio_economic": "LOWER_MIDDLE",
"highest_education": "DECLINED",
"years_of_experience": "DECLINED",
"gender_other": "They",
"age_other": "",
"ethnicity_other": "",
"sexual_orientation_other": "",
"religion_other": "Daoism",
"physical_ability_other": "",
"mental_ability_other": "",
"socio_economic_other": "",
"highest_education_other": "",
"years_of_experience_other": "",
"is_public": False
}


@patch("requests.post")
def test_api_dao_create_user_personal_background_successfully(self, mock_create_personal_background):
success_message = messages.PERSONAL_BACKGROUND_SUCCESSFULLY_CREATED
success_code = HTTPStatus.CREATED

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

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

with self.client:
response = self.client.post(
"/user/personal_background",
headers={"Authorization": AUTH_COOKIE["Authorization"].value},
data=json.dumps(
dict(self.correct_payload_personal_background)
),
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(str(test_user_personal_background_data.user_id), AUTH_COOKIE["user_id"].value)
self.assertEqual(test_user_personal_background_data.gender.name, self.correct_payload_personal_background["gender"])
self.assertEqual(test_user_personal_background_data.age.name, self.correct_payload_personal_background["age"])
self.assertEqual(test_user_personal_background_data.ethnicity.name, self.correct_payload_personal_background["ethnicity"])
self.assertEqual(test_user_personal_background_data.sexual_orientation.name, self.correct_payload_personal_background["sexual_orientation"])
self.assertEqual(test_user_personal_background_data.religion.name, self.correct_payload_personal_background["religion"])
self.assertEqual(test_user_personal_background_data.physical_ability.name, self.correct_payload_personal_background["physical_ability"])
self.assertEqual(test_user_personal_background_data.mental_ability.name, self.correct_payload_personal_background["mental_ability"])
self.assertEqual(test_user_personal_background_data.socio_economic.name, self.correct_payload_personal_background["socio_economic"])
self.assertEqual(test_user_personal_background_data.highest_education.name, self.correct_payload_personal_background["highest_education"])
self.assertEqual(test_user_personal_background_data.years_of_experience.name, self.correct_payload_personal_background["years_of_experience"])
self.assertEqual(test_user_personal_background_data.others["gender_other"], self.correct_payload_personal_background["gender_other"])
self.assertEqual(test_user_personal_background_data.others["age_other"], self.correct_payload_personal_background["age_other"])
self.assertEqual(test_user_personal_background_data.others["ethnicity_other"], self.correct_payload_personal_background["ethnicity_other"])
self.assertEqual(test_user_personal_background_data.others["sexual_orientation_other"], self.correct_payload_personal_background["sexual_orientation_other"])
self.assertEqual(test_user_personal_background_data.others["religion_other"], self.correct_payload_personal_background["religion_other"])
self.assertEqual(test_user_personal_background_data.others["physical_ability_other"], self.correct_payload_personal_background["physical_ability_other"])
self.assertEqual(test_user_personal_background_data.others["mental_ability_other"], self.correct_payload_personal_background["mental_ability_other"])
self.assertEqual(test_user_personal_background_data.others["socio_economic_other"], self.correct_payload_personal_background["socio_economic_other"])
self.assertEqual(test_user_personal_background_data.others["highest_education_other"], self.correct_payload_personal_background["highest_education_other"])
self.assertEqual(test_user_personal_background_data.others["years_of_experience_other"], self.correct_payload_personal_background["years_of_experience_other"])
self.assertEqual(test_user_personal_background_data.is_public, self.correct_payload_personal_background["is_public"])
self.assertEqual(response.json, success_message)
self.assertEqual(response.status_code, success_code)


@patch("requests.post")
def test_api_dao_create_user_personal_background_invalid_payload(self, mock_create_personal_background):
error_message = messages.PERSONAL_BACKGROUND_IS_INVALID
error_code = HTTPStatus.BAD_REQUEST

mock_response = Mock()
http_error = requests.exceptions.HTTPError()
mock_response.raise_for_status.side_effect = http_error
mock_create_personal_background.return_value = mock_response

mock_error = Mock()
mock_error.json.return_value = error_message
mock_error.status_code = error_code
mock_create_personal_background.side_effect = requests.exceptions.HTTPError(response=mock_error)

test_user_additional_info = {
"gender": "Some random value",
"age": "AGE_55_TO_64",
"ethnicity": "MIDDLE_EASTERN",
"sexual_orientation": "DECLINED",
"religion": "ISLAM",
"physical_ability": "WITH_DISABILITY",
"mental_ability": "WITH_DISORDER",
"socio_economic": "LOWER_MIDDLE",
"highest_education": "DECLINED",
"years_of_experience": "DECLINED",
"gender_other": "",
"age_other": "",
"ethnicity_other": "",
"sexual_orientation_other": "",
"religion_other": "",
"physical_ability_other": "",
"mental_ability_other": "",
"socio_economic_other": "",
"highest_education_other": "",
"years_of_experience_other": "",
"is_public": False
}

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

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


@patch("requests.post")
def test_api_dao_create_personal_background_with_existing_data(self, mock_create_personal_background):
error_message = messages.PERSONAL_BACKGROUND_OF_USER_ALREADY_EXIST
error_code = HTTPStatus.CONFLICT

mock_response = Mock()
http_error = requests.exceptions.HTTPError()
mock_response.raise_for_status.side_effect = http_error
mock_create_personal_background.return_value = mock_response

mock_error = Mock()
mock_error.json.return_value = error_message
mock_error.status_code = error_code
mock_create_personal_background.side_effect = requests.exceptions.HTTPError(response=mock_error)

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

existing_personal_background = PersonalBackgroundModel(
int(AUTH_COOKIE["user_id"].value),
self.correct_payload_personal_background["gender"],
self.correct_payload_personal_background["age"],
self.correct_payload_personal_background["ethnicity"],
self.correct_payload_personal_background["sexual_orientation"],
self.correct_payload_personal_background["religion"],
self.correct_payload_personal_background["physical_ability"],
self.correct_payload_personal_background["mental_ability"],
self.correct_payload_personal_background["socio_economic"],
self.correct_payload_personal_background["highest_education"],
self.correct_payload_personal_background["years_of_experience"],
)
existing_personal_background.others = others
existing_personal_background.is_public = self.correct_payload_personal_background["is_public"]

existing_personal_background.save_to_db()

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

mock_create_personal_background.assert_not_called()
self.assertEqual(response.json, error_message)
self.assertEqual(response.status_code, error_code)


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


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": "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.name, self.correct_payload_additional_info["timezone"])
self.assertEqual(test_user_additional_info_data.additional_info["phone"], self.correct_payload_additional_info["phone"])
Expand Down Expand Up @@ -165,7 +164,7 @@ def test_api_dao_create_additional_info_with_existing_additional_info(self, mock
}

user_extension = UserExtensionModel(
self.correct_payload_additional_info["user_id"],
int(AUTH_COOKIE["user_id"].value),
self.correct_payload_additional_info["timezone"],
)
user_extension.is_organization_rep = self.correct_payload_additional_info["is_organization_rep"]
Expand Down
Loading

0 comments on commit c96d6f2

Please sign in to comment.