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 GET /api/user/delete-all to allow deleting of all users for ease of testing #6

Merged
merged 1 commit into from
Oct 17, 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
5 changes: 5 additions & 0 deletions app/clients/db_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
14 changes: 9 additions & 5 deletions app/routes/api_route.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from flask import Blueprint, jsonify, request
from flask import Blueprint, jsonify, request, Response

from app.services.user_service import UserService

Expand All @@ -7,21 +7,25 @@ def create_api_route(user_service: UserService):
api_route = Blueprint("api", __name__)

@api_route.route("/api/user/email/<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/<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/<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
3 changes: 3 additions & 0 deletions app/services/user_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -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())
10 changes: 10 additions & 0 deletions app/tests/routes/test_api_route.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
14 changes: 14 additions & 0 deletions app/tests/services/test_user_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()