コード例 #1
0
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
コード例 #2
0
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
コード例 #3
0
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"
コード例 #4
0
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"
コード例 #5
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
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 == []
コード例 #7
0
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 == []
コード例 #8
0
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
コード例 #9
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."}
コード例 #10
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,
    )
    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."
    }
コード例 #11
0
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"
コード例 #12
0
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."
    }
コード例 #13
0
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."
    }
コード例 #14
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
コード例 #15
0
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",
    }
コード例 #16
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"
    }
コード例 #17
0
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.",
    }
コード例 #18
0
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"
コード例 #19
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
コード例 #20
0
def test_should_get_NotAuthorizedError_if_the_user_is_not_logged_in(database):
    user_repository = UserRepository(
        None,
        database,
        get_current_user_id=lambda: None,
    )
    user_interactor = UserInteractor(None, user_repository)
    data = {
        "timestamp": "2021-03-04T08:47:19Z"
    }
    with pytest.raises(NotAuthorizedError) as exception:
        user_interactor.update_current_users_log(data)
    assert exception.value.data == {
        "msg": "This operation is not authorized. Please, log in."
    }
コード例 #21
0
def test_should_raise_BadRequestError_if_the_field_timestamp_isnt_in_iso8601_format_in_the_request(database):
    user_repository = UserRepository(
        None,
        database,
        get_current_user_id=lambda: "user-1",
    )
    user_interactor = UserInteractor(None, user_repository)
    data = {
        "timestamp": "timestamp-in-wrong-format"
    }
    with pytest.raises(BadRequestError) as exception:
        user_interactor.update_current_users_log(data)
    assert exception.value.data == {
        "timestamp": "BAD FORMAT"
    }
コード例 #22
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)
    data = {
        "username": "******",
        "name": "test-name",
        "password": "******",
    }
    with pytest.raises(NotAuthorizedError) as exception:
        user_interactor.update_user_profile("test-id", data)
    assert exception.value.data == {"msg": "This operation is not authorized."}
コード例 #23
0
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)

    all_fields_missing = {}
    with pytest.raises(BadRequestError) as exception:
        user_interactor.update_user_profile("test-id", all_fields_missing)
    assert exception.value.data == {
        "username": "******",
        "name": "REQUIRED",
        "password": "******",
    }

    username_missing = {
        "name": "test-name",
        "password": "******",
    }
    with pytest.raises(BadRequestError) as exception:
        user_interactor.update_user_profile("test-id", username_missing)
    assert exception.value.data == {
        "username": "******",
    }

    name_missing = {
        "username": "******",
        "password": "******",
    }
    with pytest.raises(BadRequestError) as exception:
        user_interactor.update_user_profile("test-id", name_missing)
    assert exception.value.data == {
        "name": "REQUIRED",
    }

    password_missing = {
        "username": "******",
        "name": "test-name",
    }
    with pytest.raises(BadRequestError) as exception:
        user_interactor.update_user_profile("test-id", password_missing)
    assert exception.value.data == {
        "password": "******",
    }
コード例 #24
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,
    )
    user_interactor = UserInteractor(None, user_repository)
    data = {
        "id": "id",
        "username": "******",
        "name": "test-name",
        "password": "******",
    }
    with pytest.raises(NotAuthorizedError) as exception:
        user_interactor.register_user(data)
    assert exception.value.data == {
        "msg": "This operation is not authorized. Please, log in."
    }
コード例 #25
0
def test_should_get_an_empty_dict_if_there_is_no_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");
        """
    )
    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 == {}
コード例 #26
0
def test_should_register_the_new_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 = {
        "id": "test-id",
        "username": "******",
        "name": "test-name",
        "password": "******",
    }
    user_interactor.register_user(data)
    user = user_interactor.get_user_by_id("test-id")
    assert user.id == "test-id"
    assert user.username == "test-username"
    assert user.name == "test-name"
    assert user.password == hash_password("test-password")
def test_should_get_a_list_of_unique_days_in_which_the_user_has_logged_in_if_there_is_data_and_the_user_is_logged_in(
        database):
    database.executescript("""
        INSERT INTO logs VALUES
        ("2021-03-04T08:47:19Z", "user-1"),
        ("2021-03-04T08:50:23Z", "user-1"),
        ("2021-03-05T08:50:23Z", "user-1"),
        ("2021-03-06T08:50:23Z", "user-1"),
        ("2021-03-06T08:50:23Z", "user-2");
        """)
    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 len(log) == 3
    assert log[0] == "2021-03-04"
コード例 #28
0
def test_should_get_all_the_assigned_users_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");
        """)
    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 len(assigned_users) == 4
    assert assigned_users[0].id == "user-1"
    assert assigned_users[0].username == "*****@*****.**"
コード例 #29
0
def test_should_get_the_log_if_there_is_data_and_the_user_is_logged_in(
        database):
    database.executescript("""
        INSERT INTO logs VALUES
        ("2021-03-04T08:47:19Z", "user-1"),
        ("2021-03-04T08:50:23Z", "user-1"),
        ("2021-03-05T08:50:23Z", "user-1"),
        ("2021-03-06T08:50:23Z", "user-1"),
        ("2021-03-06T08:50:23Z", "user-2");
        """)
    user_repository = UserRepository(
        None,
        database,
        get_current_user_id=lambda: "user-1",
    )
    user_interactor = UserInteractor(None, user_repository)
    log = user_interactor.get_current_users_log()
    assert len(log) == 4
    assert log[0].user_id == "user-1"
    assert log[0].timestamp == "2021-03-04T08:47:19Z"
コード例 #30
0
def test_auth_user_should_raises_unauthorized_if_password_not_ok(database):
    user_repository = UserRepository(None, database, lambda: "user-1")
    interactor = UserInteractor(None, user_repository)

    with pytest.raises(NotAuthorizedError):
        interactor.auth_user("*****@*****.**", "user-1-bad-password")