Skip to content

Commit

Permalink
issue 760 fix
Browse files Browse the repository at this point in the history
  • Loading branch information
ashokrayal committed Sep 8, 2020
1 parent 84cad7e commit 00e7ad6
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 8 deletions.
5 changes: 4 additions & 1 deletion app/api/dao/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ def get_user_by_username(username: str):
return UserModel.find_by_username(username)

@staticmethod
def list_users(user_id: int, search_query: str = "", page: int = DEFAULT_PAGE, per_page: int = DEFAULT_USERS_PER_PAGE, is_verified = None):
def list_users(user_id: int, search_query: str = "", page: int = DEFAULT_PAGE, per_page: int = DEFAULT_USERS_PER_PAGE, is_verified = None, location = None):
""" Retrieves a list of verified users with the specified ID.
Arguments:
Expand All @@ -157,15 +157,18 @@ def list_users(user_id: int, search_query: str = "", page: int = DEFAULT_PAGE, p
is_verified: Status of the user's verification; None when provided as an argument.
page: The page of users to be returned
per_page: The number of users to return per page
location: The location of users to be listed
Returns:
A list of users matching conditions and the HTTP response code.
"""

print('location', location)
users_list = UserModel.query.filter(
UserModel.id != user_id,
not is_verified or UserModel.is_email_verified,
not location or (UserModel.location and func.lower(UserModel.location).contains(location.lower())) ,
func.lower(UserModel.name).contains(search_query.lower())
).order_by(UserModel.id).paginate(page=page,
per_page=per_page,
Expand Down
10 changes: 6 additions & 4 deletions app/api/resources/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
class UserList(Resource):
@classmethod
@jwt_required
@users_ns.doc("list_users", params={"search": "Search query", "page": "specify page of users", "per_page": "specify number of users per page"})
@users_ns.doc("list_users", params={"search": "Search query", "page": "specify page of users", "per_page": "specify number of users per page", "location":"location of the user"})
@users_ns.doc(
responses={
HTTPStatus.UNAUTHORIZED: f"{messages.TOKEN_HAS_EXPIRED['message']}<br>"
Expand All @@ -60,9 +60,10 @@ def get(cls):

page = request.args.get("page", default=UserDAO.DEFAULT_PAGE, type=int)
per_page = request.args.get("per_page", default=UserDAO.DEFAULT_USERS_PER_PAGE, type=int)
location = request.args.get("location", None)

user_id = get_jwt_identity()
return DAO.list_users(user_id, request.args.get("search", ""), page, per_page)
return DAO.list_users(user_id, request.args.get("search", ""), page, per_page, location=location)


@users_ns.route("users/<int:user_id>")
Expand Down Expand Up @@ -216,7 +217,7 @@ def put(cls):
class VerifiedUser(Resource):
@classmethod
@jwt_required
@users_ns.doc("get_verified_users", params={"search": "Search query", "page": "specify page of users", "per_page": "specify number of users per page"})
@users_ns.doc("get_verified_users", params={"search": "Search query", "page": "specify page of users", "per_page": "specify number of users per page", "location":"location of the user"})
@users_ns.doc(
responses={
HTTPStatus.UNAUTHORIZED: f"{messages.TOKEN_HAS_EXPIRED['message']}<br>"
Expand All @@ -239,9 +240,10 @@ def get(cls):

page = request.args.get("page", default=UserDAO.DEFAULT_PAGE, type=int)
per_page = request.args.get("per_page", default=UserDAO.DEFAULT_USERS_PER_PAGE, type=int)
location = request.args.get("location", None)

user_id = get_jwt_identity()
return DAO.list_users(user_id, request.args.get("search", ""), page, per_page, is_verified=True)
return DAO.list_users(user_id, request.args.get("search", ""), page, per_page, is_verified=True, location=location)


@users_ns.route("register")
Expand Down
11 changes: 11 additions & 0 deletions tests/test_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,15 @@
"username": "user4",
"password": "user4_pwd",
"terms_and_conditions_checked": True,
}

user5 = {
"name": "usernumfive",
"email": "[email protected]",
"username": "usernum5",
"password": "user5_pwd",
"terms_and_conditions_checked": True,
"available_to_mentor": True,
"need_mentoring": True,
"location":"jaipur"
}
34 changes: 31 additions & 3 deletions tests/users/test_api_list_users.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from app.utils.enum_utils import MentorshipRelationState
from tests.base_test_case import BaseTestCase
from tests.test_utils import get_test_request_header
from tests.test_data import user1, user2, user3
from tests.test_data import user1, user2, user3, user5


class TestListUsersApi(BaseTestCase):
Expand Down Expand Up @@ -42,6 +42,15 @@ def setUp(self):
terms_and_conditions_checked=user3["terms_and_conditions_checked"],
)

self.third_user = UserModel(
name=user5["name"],
email=user5["email"],
username=user5["username"],
password=user5["password"],
terms_and_conditions_checked=user5["terms_and_conditions_checked"],
)
self.third_user.location = user5["location"]

self.verified_user.is_email_verified = True
self.verified_user.need_mentoring = True

Expand All @@ -50,10 +59,12 @@ def setUp(self):
self.verified_user.is_available = True
self.other_user.is_available = False
self.second_user.is_available = False
self.third_user.is_available = False

db.session.add(self.verified_user)
db.session.add(self.other_user)
db.session.add(self.second_user)
db.session.add(self.third_user)
db.session.commit()

def create_relationship(self):
Expand Down Expand Up @@ -84,6 +95,7 @@ def test_list_users_api_without_search_query_resource_auth(self):
marshal(self.verified_user, public_user_api_model),
marshal(self.other_user, public_user_api_model),
marshal(self.second_user, public_user_api_model),
marshal(self.third_user, public_user_api_model),
]
actual_response = self.client.get(
"/users", follow_redirects=True, headers=auth_header
Expand Down Expand Up @@ -124,6 +136,19 @@ def test_list_users_api_with_a_search_query_with_spaces_resource_auth(self):
self.assertEqual(200, actual_response.status_code)
self.assertEqual(expected_response, json.loads(actual_response.data))

def test_list_users_api_with_location_resource_auth(self):
auth_header = get_test_request_header(self.admin_user.id)
expected_response = [marshal(self.third_user, public_user_api_model)]
print(f"/users?location={self.third_user.location}")
actual_response = self.client.get(
f"/users?location={self.third_user.location}",
follow_redirects=True,
headers=auth_header,
)

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

def test_list_users_api_with_search_with_special_characters_resource_auth(self):
auth_header = get_test_request_header(self.admin_user.id)
expected_response = [marshal(self.second_user, public_user_api_model)]
Expand All @@ -141,7 +166,8 @@ def test_list_users_api_with_a_page_query_resource_auth(self):
expected_response = [
marshal(self.verified_user, public_user_api_model),
marshal(self.other_user, public_user_api_model),
marshal(self.second_user, public_user_api_model)]
marshal(self.second_user, public_user_api_model),
marshal(self.third_user, public_user_api_model)]
actual_response = self.client.get(
"/users?page=1", follow_redirects=True, headers=auth_header
)
Expand Down Expand Up @@ -171,7 +197,8 @@ def test_list_users_api_with_a_page_and_per_page_query_resource_auth(self):

def test_list_users_api_with_a_partial_page_and_per_page_query_resource_auth(self):
auth_header = get_test_request_header(self.admin_user.id)
expected_response = [marshal(self.second_user, public_user_api_model)]
expected_response = [marshal(self.second_user, public_user_api_model),
marshal(self.third_user, public_user_api_model)]
actual_response = self.client.get(
"/users?page=2&per_page=2", follow_redirects=True, headers=auth_header
)
Expand Down Expand Up @@ -241,6 +268,7 @@ def test_list_users_api_relation(self):
marshal(self.verified_user, public_user_api_model),
marshal(self.other_user, public_user_api_model),
marshal(self.second_user, public_user_api_model),
marshal(self.third_user, public_user_api_model),
]
actual_response = self.client.get(
"/users", follow_redirects=True, headers=auth_header
Expand Down

0 comments on commit 00e7ad6

Please sign in to comment.