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

update : Refactored code to use dict.get() method #1112

Closed
wants to merge 2 commits into from
Closed
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
61 changes: 15 additions & 46 deletions app/api/dao/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -240,71 +240,40 @@ def update_user_profile(user_id: int, data: Dict[str, str]):
messages.USER_USES_A_USERNAME_THAT_ALREADY_EXISTS,
HTTPStatus.BAD_REQUEST,
)

user.username = username

if "name" in data and data["name"]:
user.name = data["name"]

if "bio" in data:
if data["bio"]:
user.bio = data["bio"]
else:
user.bio = None

user.bio = data.get("bio",None)

if "location" in data:
if data["location"]:
user.location = data["location"]
else:
user.location = None

user.location = data.get("location",None)

if "occupation" in data:
if data["occupation"]:
user.occupation = data["occupation"]
else:
user.occupation = None

user.occupation = data.get("occupation",None)

if "organization" in data:
if data["organization"]:
user.organization = data["organization"]
else:
user.organization = None
user.organization = data.get("organization",None)

if "slack_username" in data:
if data["slack_username"]:
user.slack_username = data["slack_username"]
else:
user.slack_username = None
user.slack_username = data.get("slack_username",None)

if "social_media_links" in data:
if data["social_media_links"]:
user.social_media_links = data["social_media_links"]
else:
user.social_media_links = None
user.social_media_links = data.get("social_media_links",None)

if "skills" in data:
if data["skills"]:
user.skills = data["skills"]
else:
user.skills = None

user.skills = data.get("skills",None)

if "interests" in data:
if data["interests"]:
user.interests = data["interests"]
else:
user.interests = None

user.interests = data("interests",None)

if "resume_url" in data:
if data["resume_url"]:
user.resume_url = data["resume_url"]
else:
user.resume_url = None
user.resume_url = data.get("resume_url",None)

if "photo_url" in data:
if data["photo_url"]:
user.photo_url = data["photo_url"]
else:
user.photo_url = None
user.photo_url = data.get("photo_url",None)

if "need_mentoring" in data:
user.need_mentoring = data["need_mentoring"]
Expand Down
10 changes: 5 additions & 5 deletions tests/users/test_api_authentication.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import unittest
from datetime import timedelta

from http import HTTPStatus
from flask import json
from flask_restx import marshal

Expand Down Expand Up @@ -38,14 +38,14 @@ def test_user_profile_with_header_api(self):
"/user", follow_redirects=True, headers=auth_header
)

self.assertEqual(200, actual_response.status_code)
self.assertEqual(HTTPStatus.OK, actual_response.status_code)
self.assertEqual(expected_response, json.loads(actual_response.data))

def test_user_profile_without_header_api(self):
expected_response = messages.AUTHORISATION_TOKEN_IS_MISSING
actual_response = self.client.get("/user", follow_redirects=True)

self.assertEqual(401, actual_response.status_code)
self.assertEqual(HTTPStatus.UNAUTHORIZED, actual_response.status_code)
self.assertDictEqual(expected_response, json.loads(actual_response.data))

def test_user_profile_incomplete_token_api(self):
Expand All @@ -56,7 +56,7 @@ def test_user_profile_incomplete_token_api(self):
"/user", follow_redirects=True, headers=auth_header
)

self.assertEqual(401, actual_response.status_code)
self.assertEqual(HTTPStatus.UNAUTHORIZED, actual_response.status_code)
self.assertDictEqual(expected_response, json.loads(actual_response.data))

def test_user_profile_with_token_expired_api(self):
Expand All @@ -68,7 +68,7 @@ def test_user_profile_with_token_expired_api(self):
"/user", follow_redirects=True, headers=auth_header
)

self.assertEqual(401, actual_response.status_code)
self.assertEqual(HTTPStatus.UNAUTHORIZED, actual_response.status_code)
self.assertDictEqual(expected_response, json.loads(actual_response.data))


Expand Down
12 changes: 6 additions & 6 deletions tests/users/test_api_change_password.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import datetime
import json
import unittest

from http import HTTPStatus
from app.api.validations.user import PASSWORD_MIN_LENGTH, PASSWORD_MAX_LENGTH
from app.database.models.user import UserModel
from app.database.sqlalchemy_extension import db
Expand Down Expand Up @@ -52,7 +52,7 @@ def test_change_password_to_correct_new_one(self):
follow_redirects=True,
headers=self.auth_header,
)
self.assertEqual(201, response.status_code)
self.assertEqual(HTTPStatus.CREATED, response.status_code)
self.assertEqual(expected_response, json.loads(response.data))

def test_change_password_with_authentication_token_missing(self):
Expand All @@ -67,7 +67,7 @@ def test_change_password_with_authentication_token_missing(self):
},
follow_redirects=True,
)
self.assertEqual(401, response.status_code)
self.assertEqual(HTTPStatus.UNAUTHORIZED, response.status_code)
self.assertEqual(expected_response, json.loads(response.data))

def test_change_password_to_empty_one(self):
Expand All @@ -87,7 +87,7 @@ def test_change_password_to_empty_one(self):
follow_redirects=True,
headers=self.auth_header,
)
self.assertEqual(400, response.status_code)
self.assertEqual(HTTPStatus.BAD_REQUEST, response.status_code)
self.assertEqual(expected_response, json.loads(response.data))

def test_change_password_to_one_with_empty_spaces(self):
Expand All @@ -103,7 +103,7 @@ def test_change_password_to_one_with_empty_spaces(self):
follow_redirects=True,
headers=self.auth_header,
)
self.assertEqual(400, response.status_code)
self.assertEqual(HTTPStatus.BAD_REQUEST, response.status_code)
self.assertEqual(expected_response, json.loads(response.data))

def test_change_password_with_authentication_token_expired(self):
Expand All @@ -122,7 +122,7 @@ def test_change_password_with_authentication_token_expired(self):
follow_redirects=True,
headers=auth_header,
)
self.assertEqual(401, response.status_code)
self.assertEqual(HTTPStatus.UNAUTHORIZED, response.status_code)
self.assertEqual(expected_response, json.loads(response.data))


Expand Down
18 changes: 9 additions & 9 deletions tests/users/test_api_home_statistics.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from datetime import datetime, timedelta
from flask import json

from http import HTTPStatus
from app import messages
from app.database.models.mentorship_relation import MentorshipRelationModel
from app.database.models.tasks_list import TasksListModel
Expand Down Expand Up @@ -29,7 +29,7 @@ def setUp(self):
def test_relations_non_auth(self):
expected_response = messages.AUTHORISATION_TOKEN_IS_MISSING
actual_response = self.client.get("/home", follow_redirects=True)
self.assertEqual(401, actual_response.status_code)
self.assertEqual(HTTPStatus.UNAUTHORIZED, actual_response.status_code)
self.assertDictEqual(expected_response, json.loads(actual_response.data))

def test_relations_invalid_id(self):
Expand All @@ -39,7 +39,7 @@ def test_relations_invalid_id(self):
actual_response = self.client.get(
"/home", follow_redirects=True, headers=auth_header
)
self.assertEqual(404, actual_response.status_code)
self.assertEqual(HTTPStatus.NOT_FOUND, actual_response.status_code)
self.assertEqual(messages.USER_NOT_FOUND, json.loads(actual_response.data))

def test_pending_requests_auth(self):
Expand Down Expand Up @@ -75,7 +75,7 @@ def test_pending_requests_auth(self):
actual_response = self.client.get(
"/home", follow_redirects=True, headers=auth_header
)
self.assertEqual(200, actual_response.status_code)
self.assertEqual(HTTPStatus.OK, actual_response.status_code)
self.assertEqual(expected_response, json.loads(actual_response.data))

def test_accepted_requests_auth(self):
Expand Down Expand Up @@ -111,7 +111,7 @@ def test_accepted_requests_auth(self):
actual_response = self.client.get(
"/home", follow_redirects=True, headers=auth_header
)
self.assertEqual(200, actual_response.status_code)
self.assertEqual(HTTPStatus.OK, actual_response.status_code)
self.assertEqual(expected_response, json.loads(actual_response.data))

def test_rejected_requests(self):
Expand Down Expand Up @@ -147,7 +147,7 @@ def test_rejected_requests(self):
actual_response = self.client.get(
"/home", follow_redirects=True, headers=auth_header
)
self.assertEqual(200, actual_response.status_code)
self.assertEqual(HTTPStatus.OK, actual_response.status_code)
self.assertEqual(expected_response, json.loads(actual_response.data))

def test_completed_relations(self):
Expand Down Expand Up @@ -183,7 +183,7 @@ def test_completed_relations(self):
actual_response = self.client.get(
"/home", follow_redirects=True, headers=auth_header
)
self.assertEqual(200, actual_response.status_code)
self.assertEqual(HTTPStatus.OK, actual_response.status_code)
self.assertEqual(expected_response, json.loads(actual_response.data))

def test_cancelled_relations(self):
Expand Down Expand Up @@ -218,7 +218,7 @@ def test_cancelled_relations(self):
actual_response = self.client.get(
"/home", follow_redirects=True, headers=auth_header
)
self.assertEqual(200, actual_response.status_code)
self.assertEqual(HTTPStatus.OK, actual_response.status_code)
self.assertEqual(expected_response, json.loads(actual_response.data))

def test_achievements(self):
Expand Down Expand Up @@ -281,5 +281,5 @@ def test_achievements(self):
actual_response = self.client.get(
"/home", follow_redirects=True, headers=auth_header
)
self.assertEqual(200, actual_response.status_code)
self.assertEqual(HTTPStatus.OK, actual_response.status_code)
self.assertEqual(expected_response, json.loads(actual_response.data))
Loading