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
  • Loading branch information
mtreacy002 committed Aug 3, 2020
1 parent a54f550 commit 453f1a9
Show file tree
Hide file tree
Showing 5 changed files with 249 additions and 102 deletions.
96 changes: 48 additions & 48 deletions app/api/dao/personal_background.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,60 +11,60 @@ class PersonalBackgroundDAO:

"""Data Access Object for Personal_Background functionalities"""

@staticmethod
def create_user_personal_background(data):
"""Creates a personal_background instance for a new registered user.
# @staticmethod
# def create_user_personal_background(data):
# """Creates a personal_background instance for a new registered user.

Arguments:
data: A list containing user's id, and user's background details (gender,
age, ethnicity, sexual_orientation, religion, physical_ability, mental_ability,
socio_economic, highest_education, years_of_experience, others) as well as
whether or not user agrees to make their personal background information
public to other members of BridgeInTech.
# Arguments:
# data: A list containing user's id, and user's background details (gender,
# age, ethnicity, sexual_orientation, religion, physical_ability, mental_ability,
# socio_economic, highest_education, years_of_experience, others) as well as
# whether or not user agrees to make their personal background information
# public to other members of BridgeInTech.

Returns:
A dictionary containing "message" which indicates whether or not the user_exension
was created successfully and "code" for the HTTP response code.
"""
# Returns:
# A dictionary containing "message" which indicates whether or not the user_exension
# was created successfully and "code" for the HTTP response code.
# """

user_id = int(AUTH_COOKIE["user_id"].value)
if user_id == 0:
return messages.USER_ID_IS_NOT_RETRIEVED, HTTPStatus.FORBIDDEN
existing_personal_background = PersonalBackgroundModel.find_by_user_id(user_id)
if existing_personal_background:
return messages.PERSONAL_BACKGROUND_OF_USER_ALREADY_EXIST, HTTPStatus.CONFLICT
# user_id = int(AUTH_COOKIE["user_id"].value)
# if user_id == 0:
# return messages.USER_ID_IS_NOT_RETRIEVED, HTTPStatus.FORBIDDEN
# existing_personal_background = PersonalBackgroundModel.find_by_user_id(user_id)
# if existing_personal_background:
# return messages.PERSONAL_BACKGROUND_OF_USER_ALREADY_EXIST, HTTPStatus.CONFLICT

try:
personal_background = PersonalBackgroundModel(
user_id=user_id,
gender=Gender(data["gender"]).name,
age=Age(data["age"]).name,
ethnicity=Ethnicity(data["ethnicity"]).name,
sexual_orientation=SexualOrientation(data["sexual_orientation"]).name,
religion=Religion(data["religion"]).name,
physical_ability=PhysicalAbility(data["physical_ability"]).name,
mental_ability=MentalAbility(data["mental_ability"]).name,
socio_economic=SocioEconomic(data["socio_economic"]).name,
highest_education=HighestEducation(data["highest_education"]).name,
years_of_experience=YearsOfExperience(data["years_of_experience"]).name,
)
personal_background.others = {
"gender_other": data["gender_other"],
"ethnicity_other": data["ethnicity_other"],
"sexual_orientation_other": data["sexual_orientation_other"],
"religion_other": data["religion_other"],
"physical_ability_other": data["physical_ability_other"],
"mental_ability_other": data["mental_ability_other"],
"socio_economic_other": data["socio_economic_other"],
"highest_education_other": data["highest_education_other"],
}
personal_background.is_public = data["is_public"]
except KeyError as e:
return e, HTTPStatus.BAD_REQUEST
# try:
# personal_background = PersonalBackgroundModel(
# user_id=user_id,
# gender=Gender(data["gender"]).name,
# age=Age(data["age"]).name,
# ethnicity=Ethnicity(data["ethnicity"]).name,
# sexual_orientation=SexualOrientation(data["sexual_orientation"]).name,
# religion=Religion(data["religion"]).name,
# physical_ability=PhysicalAbility(data["physical_ability"]).name,
# mental_ability=MentalAbility(data["mental_ability"]).name,
# socio_economic=SocioEconomic(data["socio_economic"]).name,
# highest_education=HighestEducation(data["highest_education"]).name,
# years_of_experience=YearsOfExperience(data["years_of_experience"]).name,
# )
# personal_background.others = {
# "gender_other": data["gender_other"],
# "ethnicity_other": data["ethnicity_other"],
# "sexual_orientation_other": data["sexual_orientation_other"],
# "religion_other": data["religion_other"],
# "physical_ability_other": data["physical_ability_other"],
# "mental_ability_other": data["mental_ability_other"],
# "socio_economic_other": data["socio_economic_other"],
# "highest_education_other": data["highest_education_other"],
# }
# personal_background.is_public = data["is_public"]
# except KeyError as e:
# return e, HTTPStatus.BAD_REQUEST

personal_background.save_to_db()
# personal_background.save_to_db()

return messages.PERSONAL_BACKGROUND_SUCCESSFULLY_CREATED, HTTPStatus.CREATED
# return messages.PERSONAL_BACKGROUND_SUCCESSFULLY_CREATED, HTTPStatus.CREATED


@staticmethod
Expand Down
55 changes: 5 additions & 50 deletions app/api/resources/users.py
Original file line number Diff line number Diff line change
Expand Up @@ -324,60 +324,15 @@ def get(cls):
is_wrong_token = validate_token(token)

if not is_wrong_token:
user_id = int(AUTH_COOKIE["user_id"].value)
if user_id == 0:
return messages.USER_ID_IS_NOT_RETRIEVED, HTTPStatus.FORBIDDEN
try:
user_id = int(AUTH_COOKIE["user_id"].value)
except ValueError:
return messages.USER_ID_IS_NOT_RETRIEVED, HTTPStatus.FORBIDDEN
result = PersonalBackgroundDAO.get_user_personal_background_info(user_id)
if not result:
return messages.PERSONAL_BACKGROUND_DOES_NOT_EXIST, HTTPStatus.NOT_FOUND
return result
return is_wrong_token


@classmethod
@users_ns.doc("create_user_personal_background")
@users_ns.response(
HTTPStatus.CREATED, f"{messages.PERSONAL_BACKGROUND_SUCCESSFULLY_CREATED}"
)
@users_ns.response(
HTTPStatus.BAD_REQUEST,
f"{messages.USER_ID_IS_NOT_VALID}\n"
f"{messages.PERSONAL_BACKGROUND_DATA_HAS_MISSING_FIELD}\n"
f"{messages.UNEXPECTED_INPUT}"
)
@users_ns.response(
HTTPStatus.INTERNAL_SERVER_ERROR, f"{messages.INTERNAL_SERVER_ERROR}"
)
@users_ns.expect(auth_header_parser, user_personal_background_request_body_model)
def post(cls):
"""
Creates user personal background.
A user with valid access token can use this endpoint to add personal background information to their own data.
The endpoint takes any of the given parameters (gender, age, ethnicity, sexusl_orientation, religion, physical_ability,
mental_ability, socio_economic, highest_education, years_of_experience enums) using each enum VALUE as input, others (dictionary of any additional
information the user added from the background fields) and is_public (boolean value true or false which
indicates whether or not the user agrees to make their personal background information public to other BridgeInTech members).
The response contains a success or error message. This request only accessible once user retrieves their user_id
by sending GET /user/personal_details.
"""

token = request.headers.environ["HTTP_AUTHORIZATION"]
is_wrong_token = validate_token(token)

if not is_wrong_token:
data = request.json
if not data:
return messages.NO_DATA_FOR_UPDATING_PROFILE_WAS_SENT, HTTPStatus.BAD_REQUEST

is_field_valid = expected_fields_validator(data, user_personal_background_request_body_model)
if not is_field_valid.get("is_field_valid"):
return is_field_valid.get("message"), HTTPStatus.BAD_REQUEST

is_not_valid = validate_update_personal_background_info_request(data)
if is_not_valid:
return is_not_valid, HTTPStatus.BAD_REQUEST

return PersonalBackgroundDAO.create_user_personal_background(data)

return is_wrong_token

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
Loading

0 comments on commit 453f1a9

Please sign in to comment.