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

Add qol features #15

Merged
merged 6 commits into from
Nov 2, 2023
Merged
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
14 changes: 7 additions & 7 deletions app/clients/db_client.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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:
Expand Down
6 changes: 3 additions & 3 deletions app/routes/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion app/templates/home.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ <h1 class="govuk-heading-xl">Ministry of Justice User Search</h1>
<h3 class="govuk-heading-l">Overview</h3>

<p class="govuk-body">
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.
</p>

<div class="govuk-grid-row">
Expand Down
51 changes: 34 additions & 17 deletions app/templates/search-results.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,35 +7,52 @@

<h1 class="govuk-heading-xl">Search Results</h1>

{% if results %}
<h3 class="govuk-heading-l"> for {{user_query}}</h3>

<table class="govuk-table">
<thead class="govuk-table__head">
<tr class="govuk-table__row">
<th class="govuk-table__header" scope="col">Type</th>
<th class="govuk-table__header" scope="col">Value</th>
</tr>
</thead>
<tbody class="govuk-table__body">
{% if results and results|length > 0 %}
<h3 class="govuk-heading-l">{{ results|length }} results returned for "{{ user_query }}"</h3>

{% for result in results %}
<table class="govuk-table">
<thead class="govuk-table__head">
<tr class="govuk-table__row">
<th class="govuk-table__header" scope="col">Type</th>
<th class="govuk-table__header" scope="col">Value</th>
<th class="govuk-table__header" scope="col">Action</th>
</tr>
</thead>
<tbody class="govuk-table__body">
<tr class="govuk-table__row">
<td class="govuk-table__cell">Email</td>
<td class="govuk-table__cell">{{ results.email }}</td>
<td class="govuk-table__cell">{{ result.email }}</td>
<td class="govuk-table__cell">
<a href="mailto:{{ result.email }}" class="govuk-button">Email</a>
</td>
</tr>
<tr class="govuk-table__row">
<td class="govuk-table__cell">GitHub Username</td>
<td class="govuk-table__cell">{{ results.github_username }}</td>
<td class="govuk-table__cell">{{ result.github_username }}</td>
<td class="govuk-table__cell">
<a href="https://github.com/{{ result.github_username }}" target="_blank" class="govuk-button">View Profile</a>
</td>
</tr>
<tr class="govuk-table__row">
<td class="govuk-table__cell">Slack Username</td>
<td class="govuk-table__cell">{{ results.slack_username }}</td>
<td class="govuk-table__cell">{{ result.slack_username }}</td>
<td class="govuk-table__cell">
<button class="govuk-button" disabled>View Profile</button>
</td>
</tr>
</tbody>
</table>
</tbody>
</table>
<br>
{% endfor %}

{% else %}
<h3 class="govuk-heading-l">No results found for {{user_query}}</h3>
<h3 class="govuk-heading-l">No results found for {{ user_query }}</h3>
{% endif %}




<button class="govuk-button" onclick="location.href='/home'" style="margin-top: 10px;">Return to Search</button>

{% endblock %}
Expand Down
39 changes: 39 additions & 0 deletions app/tests/routes/test_api_route.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):

Expand All @@ -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):

Expand All @@ -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):
Expand Down
33 changes: 33 additions & 0 deletions app/tests/services/test_user_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):

Expand All @@ -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):

Expand All @@ -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):

Expand Down