def test_should_get_NotAuthorizedError_if_the_goal_is_not_accessible_by_the_admin( database): database.executescript(""" INSERT INTO admin_users VALUES ("admin-1", "user-2"); INSERT INTO goals VALUES ("test-goal-id-1", "2020-03-15", "test-title", "test-category", 1, "user-1"), ("test-goal-id-2", "2020-03-15", "test-title", "test-category", 1, "user-1"), ("test-goal-id-3", "2020-03-15", "test-title", "test-category", 1, "user-1"), ("test-goal-id-4", "2020-03-16", "test-title", "test-category", 0, "user-1"), ("test-goal-id-5", "2020-03-17", "test-title", "test-category", 1, "user-2"); """) user_repository = UserRepository( None, database, get_current_user_id=lambda: "admin-1", ) goal_repository = GoalRepository(None, database) goal_interactor = GoalInteractor(None, goal_repository, user_repository) data = { "id": "test-id", "title": "test-title", "description": "test-description", "hint": "test-hint", "goal_id": "test-goal-id-1" } with pytest.raises(NotAuthorizedError) as exception: goal_interactor.save_task(data) assert exception.value.data == {"msg": "This operation is not authorized."}
def test_should_get_all_tasks_if_there_is_data_and_the_goal_exists_and_the_user_or_the_admin_are_logged_in( database): database.executescript(""" INSERT INTO goals VALUES ("test-goal-id-1", "test-date", "test-title", "test-category", 1, "user-1"), ("test-goal-id-2", "test-date", "test-title", "test-category", 1, "user-1"), ("test-goal-id-3", "test-date", "test-title", "test-category", 1, "user-1"), ("test-goal-id-4", "test-date", "test-title", "test-category", 1, "user-2"); INSERT INTO tasks VALUES ("test-id-1", "test-title", "test-description", "test-hint", "test-goal-id-1"), ("test-id-2", "test-title", "test-description", "test-hint", "test-goal-id-1"); """) user_repository = UserRepository( None, database, get_current_user_id=lambda: "user-1", ) goal_repository = GoalRepository(None, database) goal_interactor = GoalInteractor(None, goal_repository, user_repository, get_current_date=lambda: "test-date") all_tasks = goal_interactor.get_all_tasks_by_goal_id("test-goal-id-1") assert len(all_tasks) == 2 assert all_tasks[0].id == "test-id-1" assert all_tasks[0].title == "test-title" assert all_tasks[0].description == "test-description" assert all_tasks[0].hint == "test-hint" assert all_tasks[0].goal_id == "test-goal-id-1"
def test_should_get_goals_of_the_day_if_there_is_data_and_the_user_is_logged(database): database.executescript( """ INSERT INTO goals VALUES ("test-id-1", "test-date-1", "test-title", "test-category", 1, "user-1"), ("test-id-2", "test-date-1", "test-title", "test-category", 1, "user-1"), ("test-id-3", "test-date-1", "test-title", "test-category", 1, "user-1"), ("test-id-4", "test-date-2", "test-title", "test-category", 0, "user-1"), ("test-id-5", "test-date", "test-title", "test-category", 1, "user-2"); """ ) user_repository = UserRepository( None, database, get_current_user_id=lambda: "user-1", ) goal_repository = GoalRepository(None, database) goal_interactor = GoalInteractor( None, goal_repository, user_repository, get_current_date=lambda: "test-date-1") all_goals = goal_interactor.get_current_users_daily_goals() assert len(all_goals) == 3 assert all_goals[0].id == "test-id-1" assert all_goals[0].date == "test-date-1" assert all_goals[0].title == "test-title" assert all_goals[0].category == "test-category" assert all_goals[0].status == 1 assert all_goals[0].user_id == "user-1"
def test_should_get_NotAuthorizedError_if_the_user_or_the_admin_are_not_logged_in( database): database.executescript(""" INSERT INTO goals VALUES ("test-goal-id-1", "test-date", "test-title", "test-category", 1, "user-1"), ("test-goal-id-2", "test-date", "test-title", "test-category", 1, "user-1"), ("test-goal-id-3", "test-date", "test-title", "test-category", 1, "user-1"), ("test-goal-id-4", "test-date", "test-title", "test-category", 1, "user-2"); INSERT INTO tasks VALUES ("test-task-id-1", "test-title", "test-description", "test-hint", "test-goal-id-1"), ("test-task-id-2", "test-title", "test-description", "test-hint", "test-goal-id-1"); """) user_repository = UserRepository( None, database, get_current_user_id=lambda: None, ) goal_repository = GoalRepository(None, database) goal_interactor = GoalInteractor(None, goal_repository, user_repository, get_current_date=lambda: "test-date") with pytest.raises(NotAuthorizedError) as exception: goal_interactor.get_all_tasks_by_goal_id("test-goal-id-1") assert exception.value.data == { "msg": "This operation is not authorized. Please, log in." }
def test_should_raise_NotFoundError_if_the_goal_doesnt_exist_and_the_user_or_the_admin_are_logged_in( database): database.executescript(""" INSERT INTO goals VALUES ("test-goal-id-1", "test-date", "test-title", "test-category", 1, "user-1"), ("test-goal-id-2", "test-date", "test-title", "test-category", 1, "user-1"), ("test-goal-id-3", "test-date", "test-title", "test-category", 1, "user-1"), ("test-goal-id-4", "test-date", "test-title", "test-category", 1, "user-2"); INSERT INTO tasks VALUES ("test-task-id-1", "test-title", "test-description", "test-hint", "test-goal-id-1"), ("test-task-id-2", "test-title", "test-description", "test-hint", "test-goal-id-1"); """) user_repository = UserRepository( None, database, get_current_user_id=lambda: "user-1", ) goal_repository = GoalRepository(None, database) goal_interactor = GoalInteractor(None, goal_repository, user_repository, get_current_date=lambda: "test-date") with pytest.raises(NotFoundError) as exception: goal_interactor.get_all_tasks_by_goal_id("goal-not-exists") assert exception.value.data == { "msg": "Goal with id 'goal-not-exists' not found." }
def test_should_create_the_goal_if_the_goal_doesnt_exist_and_all_data_is_OK_and_the_admin_is_logged_in(database): database.executescript( "INSERT INTO admin_users VALUES('admin-1', 'user-1');") user_repository = UserRepository( None, database, get_current_user_id=lambda: "admin-1", ) goal_repository = GoalRepository(None, database) goal_interactor = GoalInteractor(None, goal_repository, user_repository) data = { "id": "test-id", "date": "2020-03-15", "title": "test-title", "category": "test-category", "status": 0, "user_id": "user-1" } goal_interactor.save_goal(data) goals = goal_interactor.get_goals_by_date_and_assigned_user_id( "2020-03-15", "user-1") assert len(goals) == 1 assert goals[0].id == "test-id" assert goals[0].date == "2020-03-15" assert goals[0].title == "test-title" assert goals[0].category == "test-category" assert goals[0].status == False assert goals[0].user_id == "user-1"
def test_should_create_the_goal_if_the_goal_doesnt_exist_and_all_data_is_OK_and_the_admin_is_logged_in( database): database.executescript(""" INSERT INTO admin_users VALUES ("admin-1", "user-1"); INSERT INTO goals VALUES ("test-goal-id-1", "2020-03-15", "test-title", "test-category", 1, "user-1"), ("test-goal-id-2", "2020-03-15", "test-title", "test-category", 1, "user-1"), ("test-goal-id-3", "2020-03-15", "test-title", "test-category", 1, "user-1"), ("test-goal-id-4", "2020-03-16", "test-title", "test-category", 0, "user-1"), ("test-goal-id-5", "2020-03-17", "test-title", "test-category", 1, "user-2"); """) user_repository = UserRepository( None, database, get_current_user_id=lambda: "admin-1", ) goal_repository = GoalRepository(None, database) goal_interactor = GoalInteractor(None, goal_repository, user_repository) data = { "id": "test-task-id", "title": "test-title", "description": "test-description", "hint": "test-hint", "goal_id": "test-goal-id-1" } goal_interactor.save_task(data) tasks = goal_interactor.get_all_tasks_by_goal_id("test-goal-id-1") assert len(tasks) == 1 assert tasks[0].id == "test-task-id" assert tasks[0].title == "test-title" assert tasks[0].description == "test-description" assert tasks[0].hint == "test-hint" assert tasks[0].goal_id == "test-goal-id-1"
def test_should_get_an_empty_list_if_there_is_no_data_and_the_user_is_logged(database): user_repository = UserRepository( None, database, get_current_user_id=lambda: "user-1", ) goal_repository = GoalRepository(None, database) goal_interactor = GoalInteractor(None, goal_repository, user_repository) all_goals = goal_interactor.get_current_users_daily_goals() assert all_goals == []
def test_should_get_NotAuthorizedError_if_the_user_doesnt_have_an_admin_role( database): user_repository = UserRepository( None, database, get_current_user_id=lambda: "user-1", ) goal_repository = GoalRepository(None, database) goal_interactor = GoalInteractor(None, goal_repository, user_repository) with pytest.raises(NotAuthorizedError) as exception: goal_interactor.delete_goal_by_id("test-goal-id") assert exception.value.data == {"msg": "This operation is not authorized."}
def test_should_get_NotAuthorizedError_if_the_admin_is_not_logged_in(database): user_repository = UserRepository( None, database, get_current_user_id=lambda: None, ) goal_repository = GoalRepository(None, database) goal_interactor = GoalInteractor(None, goal_repository, user_repository) with pytest.raises(NotAuthorizedError) as exception: goal_interactor.delete_task_by_id("test-task-id") assert exception.value.data == { "msg": "This operation is not authorized. Please, log in." }
def test_should_get_NotFoundError_if_the_goal_doesnt_exist(database): user_repository = UserRepository( None, database, get_current_user_id=lambda: "admin-1", ) goal_repository = GoalRepository(None, database) goal_interactor = GoalInteractor(None, goal_repository, user_repository) with pytest.raises(NotFoundError) as exception: goal_interactor.delete_goal_by_id("goal-doesnt-exist") assert exception.value.data == { "msg": "Goal with id 'goal-doesnt-exist' not found." }
def test_should_get_NotAuthorizedError_if_the_user_doesnt_have_an_admin_role( database): user_repository = UserRepository( None, database, get_current_user_id=lambda: "user-1", ) goal_repository = GoalRepository(None, database) goal_interactor = GoalInteractor(None, goal_repository, user_repository) data = { "id": "test-id", "title": "test-title", "description": "test-description", "hint": "test-hint", "goal_id": "test-goal-id-1" } with pytest.raises(NotAuthorizedError) as exception: goal_interactor.save_task(data) assert exception.value.data == {"msg": "This operation is not authorized."}
def test_should_get_NotAuthorizedError_if_the_user_is_not_assigned_to_the_admin( database): database.executescript(""" INSERT INTO goals VALUES ("test-goal-id-1", "2020-03-21", "test-title", "test-category", 1, "user-1"), ("test-goal-id-2", "2020-03-21", "test-title", "test-category", 1, "user-1"), ("test-goal-id-3", "2020-03-22", "test-title", "test-category", 1, "user-1"), ("test-goal-id-4", "2020-03-22", "test-title", "test-category", 1, "user-2"); """) user_repository = UserRepository( None, database, get_current_user_id=lambda: "admin-1", ) goal_repository = GoalRepository(None, database) goal_interactor = GoalInteractor(None, goal_repository, user_repository) with pytest.raises(NotAuthorizedError) as exception: goal_interactor.delete_goal_by_id("test-goal-id-1") assert exception.value.data == {"msg": "This operation is not authorized."}
def test_should_get_NotFoundError_if_the_goal_doesnt_exist(database): user_repository = UserRepository( None, database, get_current_user_id=lambda: "admin-1", ) goal_repository = GoalRepository(None, database) goal_interactor = GoalInteractor(None, goal_repository, user_repository) data = { "id": "test-id", "title": "test-title", "description": "test-description", "hint": "test-hint", "goal_id": "goal-doesnt-exist" } with pytest.raises(NotFoundError) as exception: goal_interactor.save_task(data) assert exception.value.data == { "msg": "Goal with id 'goal-doesnt-exist' not found." }
def test_should_get_an_empty_list_if_there_is_no_data_but_the_goal_exists_and_the_admin_or_the_user_are_logged_in( database): database.executescript(""" INSERT INTO goals VALUES ("test-goal-id-1", "test-date", "test-title", "test-category", 1, "user-1"), ("test-goal-id-2", "test-date", "test-title", "test-category", 1, "user-1"), ("test-goal-id-3", "test-date", "test-title", "test-category", 1, "user-1"), ("test-goal-id-4", "test-date", "test-title", "test-category", 1, "user-2"); """) user_repository = UserRepository( None, database, get_current_user_id=lambda: "user-1", ) goal_repository = GoalRepository(None, database) goal_interactor = GoalInteractor(None, goal_repository, user_repository, get_current_date=lambda: "test-date") all_tasks = goal_interactor.get_all_tasks_by_goal_id("test-goal-id-1") assert all_tasks == []
def test_should_get_BadRequestError_if_the_goal_status_isnt_zero_or_one(database): user_repository = UserRepository( None, database, get_current_user_id=lambda: "admin-1", ) goal_repository = GoalRepository(None, database) goal_interactor = GoalInteractor( None, goal_repository, user_repository) data = { "id": "test-id", "date": "2020-03-15", "title": "test-title", "category": "test-category", "status": "status-with-wrong-format", "user_id": "user-1" } with pytest.raises(BadRequestError) as exception: goal_interactor.save_goal(data) assert exception.value.data == { "status": "BAD VALUE" }
def test_should_get_NotAuthorizedError_if_the_user_doesnt_have_an_admin_role(database): user_repository = UserRepository( None, database, get_current_user_id=lambda: "user-1", ) goal_repository = GoalRepository(None, database) goal_interactor = GoalInteractor( None, goal_repository, user_repository) data = { "id": "test-id", "date": "2020-03-21", "title": "test-title", "category": "test-category", "status": 1, "user_id": "user-1" } with pytest.raises(NotAuthorizedError) as exception: goal_interactor.save_goal(data) assert exception.value.data == { "msg": "This operation is not authorized." }
def test_should_get_NotFoundError_if_the_user_doesnt_exist(database): user_repository = UserRepository( None, database, get_current_user_id=lambda: "admin-1", ) goal_repository = GoalRepository(None, database) goal_interactor = GoalInteractor( None, goal_repository, user_repository) data = { "id": "test-id", "date": "2020-03-21", "title": "test-title", "category": "test-category", "status": 1, "user_id": "user-doesnt-exist" } with pytest.raises(NotFoundError) as exception: goal_interactor.save_goal(data) assert exception.value.data == { "msg": "User with id 'user-doesnt-exist' not found." }
def test_should_raise_BadRequestError_if_there_are_not_the_required_fields_in_the_request(database): user_repository = UserRepository( None, database, get_current_user_id=lambda: "admin-1", ) goal_repository = GoalRepository(None, database) goal_interactor = GoalInteractor(None, goal_repository, user_repository) all_fields_missing = {} with pytest.raises(BadRequestError) as exception: goal_interactor.save_goal(all_fields_missing) assert exception.value.data == { "id": "REQUIRED", "date": "REQUIRED", "title": "REQUIRED", "category": "REQUIRED", "status": "REQUIRED", "user_id": "REQUIRED", } id_missing = { "date": "2020-02-05", "title": "test-title", "category": "test-category", "status": 1, "user_id": "user-1" } with pytest.raises(BadRequestError) as exception: goal_interactor.save_goal(id_missing) assert exception.value.data == { "id": "REQUIRED", } date_missing = { "id": "test-id", "title": "test-title", "category": "test-category", "status": 1, "user_id": "user-1" } with pytest.raises(BadRequestError) as exception: goal_interactor.save_goal(date_missing) assert exception.value.data == { "date": "REQUIRED", } title_missing = { "id": "test-id", "date": "2020-02-05", "category": "test-category", "status": 1, "user_id": "user-1" } with pytest.raises(BadRequestError) as exception: goal_interactor.save_goal(title_missing) assert exception.value.data == { "title": "REQUIRED", } category_missing = { "id": "test-id", "date": "2020-02-05", "title": "test-title", "status": 1, "user_id": "user-1" } with pytest.raises(BadRequestError) as exception: goal_interactor.save_goal(category_missing) assert exception.value.data == { "category": "REQUIRED", } status_missing = { "id": "test-id", "date": "2020-02-05", "title": "test-title", "category": "test-category", "user_id": "user-1" } with pytest.raises(BadRequestError) as exception: goal_interactor.save_goal(status_missing) assert exception.value.data == { "status": "REQUIRED", } user_id_missing = { "id": "test-id", "date": "2020-02-05", "title": "test-title", "category": "test-category", "status": 1 } with pytest.raises(BadRequestError) as exception: goal_interactor.save_goal(user_id_missing) assert exception.value.data == { "user_id": "REQUIRED", }
from src.lib.web import create_app, request, create_access_token, json_response from config import config from src.domain.interactor.goal_interactor import GoalInteractor from src.domain.repository.goal_repository import GoalRepository from src.domain.interactor.user_interactor import UserInteractor from src.domain.repository.user_repository import UserRepository app = create_app(config) goal_repository = GoalRepository(config) user_repository = UserRepository(config, get_current_user_id=app.get_current_user_id) goal_interactor = GoalInteractor(config, goal_repository, user_repository) user_interactor = UserInteractor(config, user_repository, goal_repository) @app.route("/") def home(): return "magic ..." @app.route("/api/auth/login", methods=["POST"]) def auth_login(): username = request.json.get("username", None) password = request.json.get("password", None) user = user_interactor.auth_user(username, password) access_token = create_access_token(identity=user.id) return json_response({"access_token": access_token, "user": user})
def test_should_raise_BadRequestError_if_there_are_not_the_required_fields_in_the_request( database): user_repository = UserRepository( None, database, get_current_user_id=lambda: "admin-1", ) goal_repository = GoalRepository(None, database) goal_interactor = GoalInteractor(None, goal_repository, user_repository) all_fields_missing = {} with pytest.raises(BadRequestError) as exception: goal_interactor.save_task(all_fields_missing) assert exception.value.data == { "id": "REQUIRED", "title": "REQUIRED", "description": "REQUIRED", "hint": "REQUIRED", "goal_id": "REQUIRED", } id_missing = { "title": "test-title", "description": "test-description", "hint": "test-hint", "goal_id": "test-goal-id" } with pytest.raises(BadRequestError) as exception: goal_interactor.save_task(id_missing) assert exception.value.data == { "id": "REQUIRED", } title_missing = { "id": "test-id", "description": "test-description", "hint": "test-hint", "goal_id": "test-goal-id" } with pytest.raises(BadRequestError) as exception: goal_interactor.save_task(title_missing) assert exception.value.data == { "title": "REQUIRED", } description_missing = { "id": "test-id", "title": "test-title", "hint": "test-hint", "goal_id": "test-goal-id" } with pytest.raises(BadRequestError) as exception: goal_interactor.save_task(description_missing) assert exception.value.data == { "description": "REQUIRED", } hint_missing = { "id": "test-id", "title": "test-title", "description": "test-description", "goal_id": "test-goal-id" } with pytest.raises(BadRequestError) as exception: goal_interactor.save_task(hint_missing) assert exception.value.data == { "hint": "REQUIRED", } goal_id_missing = { "id": "test-id", "title": "test-title", "description": "test-description", "hint": "test-hint", } with pytest.raises(BadRequestError) as exception: goal_interactor.save_task(goal_id_missing) assert exception.value.data == { "goal_id": "REQUIRED", }