Beispiel #1
0
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"
Beispiel #2
0
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."
    }
Beispiel #3
0
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."}
Beispiel #4
0
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"
Beispiel #5
0
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."
    }
Beispiel #6
0
def test_should_delete_the_goal_if_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", "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: "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"
    }
    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-id"
    goal_interactor.delete_task_by_id("test-id")
    tasks = goal_interactor.get_all_tasks_by_goal_id("test-goal-id-1")
    assert tasks == []
def test_should_get_the_progress_of_the_user_in_each_goal_category_if_there_is_data_and_the_admin_is_logged_in(database):
    database.executescript(
        """
        INSERT INTO admin_users VALUES
        ("admin-1", "user-1"),
        ("admin-1", "user-2"),
        ("admin-1", "user-3"),
        ("admin-1", "user-4");

        INSERT INTO goals VALUES
        ("test-goal-id-1", "2020-03-21", "test-title", "test-category-1", 1, "user-1"),
        ("test-goal-id-2", "2020-03-21", "test-title", "test-category-2", 0, "user-1"),
        ("test-goal-id-3", "2020-03-22", "test-title", "test-category-2", 1, "user-1"),
        ("test-goal-id-4", "2020-03-22", "test-title", "test-category-1", 1, "user-2");
        """
    )
    goal_repository = GoalRepository(None, database)
    user_repository = UserRepository(
        None,
        database,
        get_current_user_id=lambda: "admin-1",
    )
    user_interactor = UserInteractor(None, user_repository, goal_repository)
    progress = user_interactor.get_progress_by_user_id("user-1")
    assert progress["test-category-1"]["completed"] == 1
    assert progress["test-category-1"]["total"] == 1
    assert progress["test-category-2"]["completed"] == 1
    assert progress["test-category-2"]["total"] == 2
Beispiel #8
0
def test_should_delete_the_goal_if_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"
    goal_interactor.delete_goal_by_id("test-id")
    goals = goal_interactor.get_goals_by_date_and_assigned_user_id(
        "2020-03-15", "user-1")
    assert goals == []
def test_should_get_two_points_for_each_completed_task_if_the_user_is_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"),
        ("test-id-3", "test-title", "test-description", "test-hint", "test-goal-id-2"),
        ("test-id-4", "test-title", "test-description", "test-hint", "test-goal-id-3"),
        ("test-id-5", "test-title", "test-description", "test-hint", "test-goal-id-4");
        """)
    goal_repository = GoalRepository(None, database)
    user_repository = UserRepository(
        None,
        database,
        get_current_user_id=lambda: "user-1",
    )
    user_interactor = UserInteractor(None, user_repository, goal_repository)
    points = user_interactor.get_current_users_points()
    assert points == 4
def test_auth_user_should_return_user_if_password_is_ok(database):
    user_repository = UserRepository(None, database, lambda: "admin-1")
    interactor = UserInteractor(None, user_repository)

    user = interactor.auth_user("admin1", "admin1111")
    assert user.user_id == "admin-1"
    assert user.username == "admin1"
    assert user.user_rol == "superadmin"
def test_current_user_should_return_user_if_logged(database):
    user_repository = UserRepository(None, database, lambda: "admin-1")
    interactor = UserInteractor(None, user_repository)

    user = interactor.get_current_user()
    assert user.user_id == "admin-1"
    assert user.username == "admin1"
    assert user.user_mail == "*****@*****.**"
    assert user.user_rol == "superadmin"
Beispiel #12
0
def test_auth_user_should_return_user_if_password_is_ok(database):
    user_repository = UserRepository(None, database, lambda: "user-1")
    interactor = UserInteractor(None, user_repository)

    user = interactor.auth_user("*****@*****.**", "user-1-password")
    assert user.id == "user-1"
    assert user.username == "*****@*****.**"
    assert user.name == "User 1"
    assert user.is_admin is False
Beispiel #13
0
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_an_empty_list_if_there_is_no_data_and_the_user_is_logged_in(
        database):
    user_repository = UserRepository(
        None,
        database,
        get_current_user_id=lambda: "user-1",
    )
    user_interactor = UserInteractor(None, user_repository)
    log = user_interactor.get_current_users_routine_accomplishments()
    assert log == []
def test_should_get_an_empty_list_if_there_is_no_data_and_the_admin_is_logged_in(
        database):
    user_repository = UserRepository(
        None,
        database,
        get_current_user_id=lambda: "admin-1",
    )
    user_interactor = UserInteractor(None, user_repository)
    assigned_users = user_interactor.get_all_assigned_users()
    assert assigned_users == []
def test_should_get_zero_if_there_are_not_completed_tasks_and_the_user_is_logged_in(
        database):
    goal_repository = GoalRepository(None, database)
    user_repository = UserRepository(
        None,
        database,
        get_current_user_id=lambda: "user-1",
    )
    user_interactor = UserInteractor(None, user_repository, goal_repository)
    points = user_interactor.get_current_users_points()
    assert points == 0
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",
    )
    user_interactor = UserInteractor(None, user_repository)
    with pytest.raises(NotAuthorizedError) as exception:
        user_interactor.get_all_assigned_users()
    assert exception.value.data == {"msg": "This operation is not authorized."}
def test_should_assign_the_user_if_the_request_is_OK_and_the_admin_is_logged_in(database):
    user_repository = UserRepository(
        None,
        database,
        get_current_user_id=lambda: "admin-1",
    )
    user_interactor = UserInteractor(None, user_repository)
    data = {"user_id": "user-1"}
    user_interactor.assign_user(data)
    assigned_users = user_interactor.get_all_assigned_users()
    assert len(assigned_users) == 1
    assert assigned_users[0].id == "user-1"
Beispiel #19
0
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_NotFoundError_if_the_user_doesnt_exist(database):
    user_repository = UserRepository(
        None,
        database,
        get_current_user_id=lambda: "admin-1",
    )
    user_interactor = UserInteractor(None, user_repository)
    with pytest.raises(NotFoundError) as exception:
        user_interactor.get_progress_by_user_id("user-not-exists")
    assert exception.value.data == {
        "msg": "User with id 'user-not-exists' not found."
    }
def test_should_get_NotAuthorizedError_if_the_user_isnt_assigned_to_the_admin(database):
    user_repository = UserRepository(
        None,
        database,
        get_current_user_id=lambda: "admin-1",
    )
    user_interactor = UserInteractor(None, user_repository)
    with pytest.raises(NotAuthorizedError) as exception:
        user_interactor.get_progress_by_user_id("user-1")
    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,
    )
    user_interactor = UserInteractor(None, user_repository)
    with pytest.raises(NotAuthorizedError) as exception:
        user_interactor.get_all_assigned_users()
    assert exception.value.data == {
        "msg": "This operation is not authorized. Please, log in."
    }
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",
    )
    user_interactor = UserInteractor(None, user_repository)
    data = {}
    with pytest.raises(BadRequestError) as exception:
        user_interactor.assign_user(data)
    assert exception.value.data == {
        "user_id": "REQUIRED",
    }
def test_should_raise_NotFoundError_if_the_user_doesnt_exist(database):
    user_repository = UserRepository(
        None,
        database,
        get_current_user_id=lambda: "admin-1",
    )
    user_interactor = UserInteractor(None, user_repository)
    data = {"user_id": "test-user-doesnt-exist"}
    with pytest.raises(NotFoundError) as exception:
        user_interactor.assign_user(data)
    assert exception.value.data == {
        "msg": "User with id 'test-user-doesnt-exist' not found.",
    }
Beispiel #25
0
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."
    }
Beispiel #26
0
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."
    }
Beispiel #27
0
def test_should_add_a_new_log_entry_if_the_user_is_logged_in(database):
    user_repository = UserRepository(
        None,
        database,
        get_current_user_id=lambda: "user-1",
    )
    user_interactor = UserInteractor(None, user_repository)
    data = {
        "timestamp": "2021-03-04T08:47:19Z"
    }
    user_interactor.update_current_users_log(data)
    log = user_interactor.get_current_users_log()
    assert len(log) == 1
Beispiel #28
0
def test_should_raise_BadRequestError_if_there_is_not_the_field_timestamp_in_the_request(database):
    user_repository = UserRepository(
        None,
        database,
        get_current_user_id=lambda: "user-1",
    )
    user_interactor = UserInteractor(None, user_repository)
    data = {}
    with pytest.raises(BadRequestError) as exception:
        user_interactor.update_current_users_log(data)
    assert exception.value.data == {
        "timestamp": "REQUIRED"
    }
Beispiel #29
0
def test_current_user_should_return_user_if_logged(database):
    def fake_get_user_id():
        return "user-1"

    user_repository = UserRepository(None,
                                     database,
                                     get_current_user_id=fake_get_user_id)
    interactor = UserInteractor(None, user_repository)

    user = interactor.get_current_user()
    assert user.id == "user-1"
    assert user.username == "*****@*****.**"
    assert user.name == "User 1"
    assert user.is_admin is False
def test_current_user_should_return_user_if_logged(database):
    def fake_get_user_id():
        return "admin-1"

    user_repository = UserRepository(None,
                                     database,
                                     get_current_user_id=fake_get_user_id)
    interactor = UserInteractor(None, user_repository)

    user = interactor.get_current_user()
    assert user.user_id == "admin-1"
    assert user.username == "admin1"
    assert user.user_mail == "*****@*****.**"
    assert user.user_rol == "superadmin"