From 64257cfb2f2cf236559dd01eb771929ff73ae2d9 Mon Sep 17 00:00:00 2001 From: connormaglynn Date: Tue, 17 Oct 2023 08:09:42 +0100 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Add=20GET=20`/api/user/delete-all`?= =?UTF-8?q?=20to=20allow=20deleting=20of=20all=20users=20for=20ease=20of?= =?UTF-8?q?=20testing?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/clients/db_client.py | 5 +++++ app/routes/api_route.py | 14 +++++++++----- app/services/user_service.py | 3 +++ app/tests/routes/test_api_route.py | 10 ++++++++++ app/tests/services/test_user_service.py | 14 ++++++++++++++ 5 files changed, 41 insertions(+), 5 deletions(-) diff --git a/app/clients/db_client.py b/app/clients/db_client.py index f4b6afd..48072a7 100644 --- a/app/clients/db_client.py +++ b/app/clients/db_client.py @@ -72,3 +72,8 @@ def __add_user(self, email: str, slack_username: str, github_username: str) -> N self.__session.add(UserModel(email=email, slack_username=slack_username, github_username=github_username)) + + def delete_all_users(self) -> int: + rows_deleted = self.__session.query(UserModel).delete() + self.__session.commit() + return rows_deleted diff --git a/app/routes/api_route.py b/app/routes/api_route.py index 4b1af0d..ada2836 100644 --- a/app/routes/api_route.py +++ b/app/routes/api_route.py @@ -1,4 +1,4 @@ -from flask import Blueprint, jsonify, request +from flask import Blueprint, jsonify, request, Response from app.services.user_service import UserService @@ -7,21 +7,25 @@ def create_api_route(user_service: UserService): api_route = Blueprint("api", __name__) @api_route.route("/api/user/email/", methods=["GET"]) - def get_user_by_email(email: str): + def get_user_by_email(email: str) -> Response: return jsonify(user_service.get_user_by_email(email)) @api_route.route("/api/user/slack-username/", methods=["GET"]) - def get_user_by_slack_username(slack_username: str): + def get_user_by_slack_username(slack_username: str) -> Response: return jsonify(user_service.get_user_by_slack_username(slack_username)) @api_route.route("/api/user/github-username/", methods=["GET"]) - def get_user_by_github_username(github_username: str): + def get_user_by_github_username(github_username: str) -> Response: return jsonify(user_service.get_user_by_github_username(github_username)) @api_route.route("/api/user/add", methods=["POST"]) - def add_users(): + def add_users() -> Response: request_data = request.get_json(True) user_service.add_users(request_data['users']) return jsonify(request_data) + @api_route.route("/api/user/delete-all", methods=["GET"]) + def delete_all_users() -> str: + return user_service.delete_all_users() + return api_route diff --git a/app/services/user_service.py b/app/services/user_service.py index e162704..0599c52 100644 --- a/app/services/user_service.py +++ b/app/services/user_service.py @@ -23,3 +23,6 @@ def get_user_by_github_username(self, github_username: str): def add_users(self, users: list[dict]): self.__db_client.add_users(users, self.__allowed_users) + + def delete_all_users(self) -> str: + return str(self.__db_client.delete_all_users()) diff --git a/app/tests/routes/test_api_route.py b/app/tests/routes/test_api_route.py index a06aa2e..f37b83f 100644 --- a/app/tests/routes/test_api_route.py +++ b/app/tests/routes/test_api_route.py @@ -47,5 +47,15 @@ def test_adds_users(self): users_post_data['users'], ['connormaglynn', 'PepperMoJ']) +class TestDeleteAllUsers(unittest.TestCase): + def test_delete_all_users(self): + mock_db_client = MagicMock(DBClient) + mock_db_client.delete_all_users.return_value = 3 + response = app.create_app(mock_db_client).test_client().get( + '/api/user/delete-all').data + self.assertEqual(b"3", response) + mock_db_client.delete_all_users.assert_called_once_with() + + if __name__ == "__main__": unittest.main() diff --git a/app/tests/services/test_user_service.py b/app/tests/services/test_user_service.py index 188b57d..6c7f0fa 100644 --- a/app/tests/services/test_user_service.py +++ b/app/tests/services/test_user_service.py @@ -67,5 +67,19 @@ def test_calls_downstream_services(self): users_post_data, ['connormaglynn', 'PepperMoJ']) +class TestDeleteAllUsers(unittest.TestCase): + + def test_returns_number_of_rows_deleted_as_string(self): + mock_db_client = MagicMock(DBClient) + mock_db_client.delete_all_users.return_value = 3 + response = UserService(mock_db_client).delete_all_users() + self.assertEqual("3", response) + + def test_calls_downstream_services(self): + mock_db_client = MagicMock(DBClient) + UserService(mock_db_client).delete_all_users() + mock_db_client.delete_all_users.assert_called_once_with() + + if __name__ == "__main__": unittest.main()