diff --git a/app/clients/db_client.py b/app/clients/db_client.py index 48072a7..b934ead 100644 --- a/app/clients/db_client.py +++ b/app/clients/db_client.py @@ -1,5 +1,5 @@ from dataclasses import dataclass -from typing import Type +from typing import Type, List from flask import Flask from flask_sqlalchemy import SQLAlchemy @@ -49,14 +49,14 @@ def __init__(self, app: Flask, session: Session = db.session): github_username='test_user_3_github_username')) self.__session.commit() - def get_user_by_email(self, email: str) -> Type[UserModel] | None: - return self.__session.query(UserModel).filter_by(email=email).first() + def get_user_by_email(self, email: str) -> List[Type[UserModel]] | None: + return self.__session.query(UserModel).filter(UserModel.email.ilike(f"%{email}%")).all() - def get_user_by_slack_username(self, slack_username: str) -> Type[UserModel] | None: - return self.__session.query(UserModel).filter_by(slack_username=slack_username).first() + def get_user_by_slack_username(self, slack_username: str) -> List[Type[UserModel]] | None: + return self.__session.query(UserModel).filter(UserModel.slack_username.ilike(f"%{slack_username}%")).all() - def get_user_by_github_username(self, github_username: str) -> Type[UserModel] | None: - return self.__session.query(UserModel).filter_by(github_username=github_username).first() + def get_user_by_github_username(self, github_username: str) -> List[Type[UserModel]] | None: + return self.__session.query(UserModel).filter(UserModel.github_username.ilike(f"%{github_username}%")).all() def add_users(self, users: list[dict], allowed_users: list[str]): for user in users: diff --git a/app/routes/views.py b/app/routes/views.py index ef30371..518bc1e 100644 --- a/app/routes/views.py +++ b/app/routes/views.py @@ -20,17 +20,17 @@ def user_search(): email_results = user_service.get_user_by_email(user_query) - if email_results is not None: + if email_results: return render_template('search-results.html', results=email_results, user_query=user_query) slack_results = user_service.get_user_by_slack_username(user_query) - if slack_results is not None: + if slack_results: return render_template('search-results.html', results=slack_results, user_query=user_query) github_results = user_service.get_user_by_github_username(user_query) - if github_results is not None: + if github_results: return render_template('search-results.html', results=github_results, user_query=user_query) return render_template('search-results.html', results=None, user_query=user_query) diff --git a/app/templates/home.html b/app/templates/home.html index d2f0c78..cef6224 100644 --- a/app/templates/home.html +++ b/app/templates/home.html @@ -9,7 +9,7 @@

Ministry of Justice User Search

Overview

- This website enabled users to cross reference email, Slack usernames and GitHub usernames to identify user information from one source to another. + This website enables users to cross reference email, Slack usernames and GitHub usernames to identify user information from one source to another.

diff --git a/app/templates/search-results.html b/app/templates/search-results.html index 16fac80..11ff3e6 100644 --- a/app/templates/search-results.html +++ b/app/templates/search-results.html @@ -7,35 +7,52 @@

Search Results

-{% if results %} -

for {{user_query}}

- - - - - - - - - +{% if results and results|length > 0 %} +

{{ results|length }} results returned for "{{ user_query }}"

+ +{% for result in results %} +
TypeValue
+ + + + + + + + - + + - + + - + + - -
TypeValueAction
Email{{ results.email }}{{ result.email }} + Email +
GitHub Username{{ results.github_username }}{{ result.github_username }} + View Profile +
Slack Username{{ results.slack_username }}{{ result.slack_username }} + +
+ + +
+{% endfor %} + {% else %} -

No results found for {{user_query}}

+

No results found for {{ user_query }}

{% endif %} + + + {% endblock %} diff --git a/app/tests/routes/test_api_route.py b/app/tests/routes/test_api_route.py index 6c13903..eaa79b4 100644 --- a/app/tests/routes/test_api_route.py +++ b/app/tests/routes/test_api_route.py @@ -16,6 +16,19 @@ def test_returns_user_if_present(self): f'/api/user/email/{user_1.email}').data) self.assertEqual(user_1, UserModel(**response)) + def test_returns_mutliple_users(self): + search_query = "email" + mock_db_client = MagicMock(DBClient) + mock_db_client.get_user_by_email.return_value = [ + user_1, user_2, user_3] + response = json.loads(app.create_app(mock_db_client).test_client().get( + f'/api/user/email/{search_query}').data) + + expected_users = [user_1, user_2, user_3] + found_users = [UserModel(**user_dict) for user_dict in response] + + self.assertListEqual(expected_users, found_users) + class TestGetUserBySlackUsername(unittest.TestCase): @@ -26,6 +39,19 @@ def test_returns_user_if_present(self): f'/api/user/slack-username/{user_2.slack_username}').data) self.assertEqual(user_2, UserModel(**response)) + def test_returns_mutliple_users(self): + search_query = "slack" + mock_db_client = MagicMock(DBClient) + mock_db_client.get_user_by_slack_username.return_value = [ + user_1, user_2, user_3] + response = json.loads(app.create_app(mock_db_client).test_client().get( + f'/api/user/slack-username/{search_query}').data) + + expected_users = [user_1, user_2, user_3] + found_users = [UserModel(**user_dict) for user_dict in response] + + self.assertListEqual(expected_users, found_users) + class TestGetUserByGithubUsername(unittest.TestCase): @@ -36,6 +62,19 @@ def test_returns_user_if_present(self): f'/api/user/github-username/{user_3.github_username}').data) self.assertEqual(user_3, UserModel(**response)) + def test_returns_mutliple_users(self): + search_query = "github" + mock_db_client = MagicMock(DBClient) + mock_db_client.get_user_by_github_username.return_value = [ + user_1, user_2, user_3] + response = json.loads(app.create_app(mock_db_client).test_client().get( + f'/api/user/github-username/{search_query}').data) + + expected_users = [user_1, user_2, user_3] + found_users = [UserModel(**user_dict) for user_dict in response] + + self.assertListEqual(expected_users, found_users) + class TestCreateUser(unittest.TestCase): def test_adds_users(self): diff --git a/app/tests/services/test_user_service.py b/app/tests/services/test_user_service.py index 41a34d3..c1b68bc 100644 --- a/app/tests/services/test_user_service.py +++ b/app/tests/services/test_user_service.py @@ -22,6 +22,17 @@ def test_returns_nothing_if_not_found(self): mock_db_client).get_user_by_email("unknown_email") self.assertIsNone(found_user) + def test_returns_all_matching_users(self): + search_query = "email" + mock_db_client = MagicMock(DBClient) + mock_db_client.get_user_by_email.return_value = [ + user_1, user_2, user_3] + found_users = UserService( + mock_db_client).get_user_by_email(search_query) + + expected_users = [user_1, user_2, user_3] + self.assertListEqual(expected_users, found_users) + class TestGetUserBySlackUsername(unittest.TestCase): @@ -39,6 +50,17 @@ def test_returns_nothing_if_not_found(self): "unknown_slack_username") self.assertIsNone(found_user) + def test_returns_all_matching_users(self): + search_query = "slack" + mock_db_client = MagicMock(DBClient) + mock_db_client.get_user_by_slack_username.return_value = [ + user_1, user_2, user_3] + found_users = UserService( + mock_db_client).get_user_by_slack_username(search_query) + + expected_users = [user_1, user_2, user_3] + self.assertListEqual(expected_users, found_users) + class TestGetUserByGithubUsername(unittest.TestCase): @@ -56,6 +78,17 @@ def test_returns_nothing_if_not_found(self): "unknown_github_username") self.assertIsNone(found_user) + def test_returns_all_matching_users(self): + search_query = "github" + mock_db_client = MagicMock(DBClient) + mock_db_client.get_user_by_github_username.return_value = [ + user_1, user_2, user_3] + found_users = UserService( + mock_db_client).get_user_by_github_username(search_query) + + expected_users = [user_1, user_2, user_3] + self.assertListEqual(expected_users, found_users) + class TestAddUsers(unittest.TestCase):