def test_add_posts_expected_data_and_to_expected_url(
        self,
        mock_connection,
        user_context,
        mock_get_all_cases_response,
        mock_detection_list_user_client,
        departing_date,
    ):
        client = DepartingEmployeeService(
            mock_connection, user_context, mock_detection_list_user_client
        )
        user_context.get_current_tenant_id.return_value = _TENANT_ID_PARAM
        # Return value should have been set based on the arguments passed
        # in add, here however as we are mocking it, it doesn't matter. Can be refactored
        mock_connection.post.return_value = mock_get_all_cases_response
        client.add(_USER_ID, departing_date)

        # Have to convert the request data to a dict because
        # older versions of Python don't have deterministic order.
        posted_data = mock_connection.post.call_args[1]["json"]
        assert (
            posted_data["userId"] == _USER_ID
            and posted_data["tenantId"] == _TENANT_ID_PARAM
            and posted_data["departureDate"] == "2022-12-20"
        )
        assert (
            mock_connection.post.call_args[0][0] == "/svc/api/v2/departingemployee/add"
        )
        assert mock_connection.post.call_count == 2
    def test_add_when_user_already_on_list_raises_user_already_added_error(
            self, mocker, mock_connection, user_context,
            mock_detection_list_user_client):
        def side_effect(url, json):
            if "add" in url:
                raise create_mock_error(Py42BadRequestError, mocker,
                                        "User already on list")

        mock_connection.post.side_effect = side_effect
        client = DepartingEmployeeService(mock_connection, user_context,
                                          mock_detection_list_user_client)
        with pytest.raises(Py42UserAlreadyAddedError) as err:
            client.add("user_id")

        expected = "User with ID user_id is already on the departing-employee list."
        assert str(err.value) == expected
    def test_add_when_user_already_on_list_raises_user_already_added_error(
            self, mocker, mock_connection, user_context,
            mock_detection_list_user_client):
        def side_effect(url, json):
            if "add" in url:
                base_err = mocker.MagicMock(spec=HTTPError)
                base_err.response = mocker.MagicMock(spec=Response)
                base_err.response.text = "User already on list"
                raise Py42BadRequestError(base_err)

        mock_connection.post.side_effect = side_effect
        client = DepartingEmployeeService(mock_connection, user_context,
                                          mock_detection_list_user_client)
        with pytest.raises(Py42UserAlreadyAddedError) as err:
            client.add("user_id")

        expected = "User with ID user_id is already on the departing-employee list."
        assert str(err.value) == expected