Example #1
0
def test_write_csv_with_enrichments(mocker):
    """Test CSV export with enrichments."""
    open_patch = mocker.patch("builtins.open", mock_open())
    output_buffer = StringIO()
    open_patch.return_value.__enter__.return_value = output_buffer
    sentry2csv.write_csv(
        "outfile.csv",
        [
            {
                "metadata": {
                    "type": "warning",
                    "value": "explanation of warning"
                },
                "type": "error",
                "culprit": "culprit body",
                "count": 123,
                "userCount": 3,
                "permalink": "https://sentry.io/warning/warning_details",
                "_enrichments": {
                    "Extra Field": 12,
                    "Another Field": "ANOTHER FIELD"
                },
            },
            {
                "metadata": {
                    "type": "error",
                    "value": "explanation of error"
                },
                "type": "error",
                "culprit": "culprit body",
                "count": 12,
                "userCount": 10,
                "permalink": "https://sentry.io/error/error_details",
                "_enrichments": {
                    "Extra Field": "Mixed Content",
                    "Another Field": "yup"
                },
            },
        ],
    )
    open_patch.assert_called_once()
    assert output_buffer.getvalue().split("\n") == [
        "Error,Location,Details,Events,Users,Notes,Link,Extra Field,Another Field\r",
        "warning,culprit body,explanation of warning,123,3,,https://sentry.io/warning/warning_details,12,ANOTHER FIELD\r",  # pylint: disable=line-too-long
        "error,culprit body,explanation of error,12,10,,https://sentry.io/error/error_details,Mixed Content,yup\r",
        "",
    ]
Example #2
0
def test_write_csv_with_errors(mocker):
    """Test CSV export with enrichments."""
    open_patch = mocker.patch("builtins.open", mock_open())
    output_buffer = StringIO()
    open_patch.return_value.__enter__.return_value = output_buffer
    with pytest.raises(sentry2csv.Sentry2CSVException) as excinfo:
        sentry2csv.write_csv(
            "outfile.csv",
            [
                {
                    "metadata": {
                        "value": "explanation of warning"
                    },
                    "culprit": "culprit body",
                    "count": 123,
                    "userCount": 3,
                    "permalink": "https://sentry.io/warning/warning_details",
                    "_enrichments": {
                        "Extra Field": 12,
                        "Another Field": "ANOTHER FIELD"
                    },
                },
                {
                    "metadata": {
                        "type": "error",
                        "value": "explanation of error"
                    },
                    "culprit": "culprit body",
                    "count": 12,
                    "userCount": 10,
                    "permalink": "https://sentry.io/error/error_details",
                    "_enrichments": {
                        "Extra Field": "Mixed Content",
                        "Another Field": "yup"
                    },
                },
            ],
        )
    assert "Run with -vv to debug" in str(excinfo.value)
Example #3
0
def test_write_csv(mocker):
    """Test CSV export."""
    open_patch = mocker.patch("builtins.open", mock_open())
    output_buffer = StringIO()
    open_patch.return_value.__enter__.return_value = output_buffer
    sentry2csv.write_csv(
        "outfile.csv",
        [
            {
                "metadata": {
                    "type": "warning",
                    "value": "explanation of warning"
                },
                "culprit": "culprit body",
                "count": 123,
                "userCount": 3,
                "permalink": "https://sentry.io/warning/warning_details",
                "type": "error",
            },
            {
                "metadata": {
                    "type": "error",
                    "value": "explanation of error"
                },
                "culprit": "culprit body",
                "count": 12,
                "userCount": 10,
                "permalink": "https://sentry.io/error/error_details",
                "type": "error",
            },
            {
                "metadata": {
                    "value": "explanation of error"
                },
                "culprit": "culprit body",
                "count": 12,
                "userCount": 10,
                "permalink": "https://sentry.io/error/error_details",
                "type": "error",
            },
            {
                "metadata": {
                    "message": "a CSP error"
                },
                "culprit": "culprit body",
                "count": 12,
                "userCount": 10,
                "permalink": "https://sentry.io/error/error_details",
                "type": "csp",
            },
            {
                "metadata": {},
                "culprit": "culprit body",
                "count": 12,
                "userCount": 10,
                "permalink": "https://sentry.io/error/error_details",
                "type": "hpkp",
            },
            {
                "metadata": {
                    "title": "successful JS title"
                },
                "culprit": "https://www.example.com/culprit/path",
                "count": 12,
                "userCount": 10,
                "permalink": "https://sentry.io/error/error_details",
                "type": "default",
                "platform": "javascript",
            },
        ],
    )
    open_patch.assert_called_once()
    assert output_buffer.getvalue().split("\n") == [
        "Error,Location,Details,Events,Users,Notes,Link\r",
        "warning,culprit body,explanation of warning,123,3,,https://sentry.io/warning/warning_details\r",
        "error,culprit body,explanation of error,12,10,,https://sentry.io/error/error_details\r",
        "error,culprit body,explanation of error,12,10,,https://sentry.io/error/error_details\r",
        "csp,culprit body,a CSP error,12,10,,https://sentry.io/error/error_details\r",
        "hpkp,culprit body,,12,10,,https://sentry.io/error/error_details\r",
        "default,https://www.example.com/culprit/path,successful JS title,12,10,,https://sentry.io/error/error_details\r",
        "",
    ]