|
| 1 | +import pytest |
| 2 | + |
| 3 | +pytest.importorskip("trytond") |
| 4 | + |
| 5 | +import json |
| 6 | +import unittest.mock |
| 7 | + |
| 8 | +import trytond |
| 9 | +from trytond.exceptions import TrytonException as TrytondBaseException |
| 10 | +from trytond.exceptions import UserError as TrytondUserError |
| 11 | +from trytond.exceptions import UserWarning as TrytondUserWarning |
| 12 | +from trytond.exceptions import LoginException |
| 13 | +from trytond.wsgi import app as trytond_app |
| 14 | + |
| 15 | +from werkzeug.test import Client |
| 16 | +from sentry_sdk import last_event_id |
| 17 | +from sentry_sdk.integrations.trytond import TrytondWSGIIntegration |
| 18 | + |
| 19 | + |
| 20 | +@pytest.fixture(scope="function") |
| 21 | +def app(sentry_init): |
| 22 | + yield trytond_app |
| 23 | + |
| 24 | + |
| 25 | +@pytest.fixture |
| 26 | +def get_client(app): |
| 27 | + def inner(): |
| 28 | + return Client(app) |
| 29 | + |
| 30 | + return inner |
| 31 | + |
| 32 | + |
| 33 | +@pytest.mark.parametrize( |
| 34 | + "exception", [Exception("foo"), type("FooException", (Exception,), {})("bar")] |
| 35 | +) |
| 36 | +def test_exceptions_captured( |
| 37 | + sentry_init, app, capture_exceptions, get_client, exception |
| 38 | +): |
| 39 | + sentry_init(integrations=[TrytondWSGIIntegration()]) |
| 40 | + exceptions = capture_exceptions() |
| 41 | + |
| 42 | + unittest.mock.sentinel.exception = exception |
| 43 | + |
| 44 | + @app.route("/exception") |
| 45 | + def _(request): |
| 46 | + raise unittest.mock.sentinel.exception |
| 47 | + |
| 48 | + client = get_client() |
| 49 | + _ = client.get("/exception") |
| 50 | + |
| 51 | + (e,) = exceptions |
| 52 | + assert e is exception |
| 53 | + |
| 54 | + |
| 55 | +@pytest.mark.parametrize( |
| 56 | + "exception", |
| 57 | + [ |
| 58 | + TrytondUserError("title"), |
| 59 | + TrytondUserWarning("title", "details"), |
| 60 | + LoginException("title", "details"), |
| 61 | + ], |
| 62 | +) |
| 63 | +def test_trytonderrors_not_captured( |
| 64 | + sentry_init, app, capture_exceptions, get_client, exception |
| 65 | +): |
| 66 | + sentry_init(integrations=[TrytondWSGIIntegration()]) |
| 67 | + exceptions = capture_exceptions() |
| 68 | + |
| 69 | + unittest.mock.sentinel.exception = exception |
| 70 | + |
| 71 | + @app.route("/usererror") |
| 72 | + def _(request): |
| 73 | + raise unittest.mock.sentinel.exception |
| 74 | + |
| 75 | + client = get_client() |
| 76 | + _ = client.get("/usererror") |
| 77 | + |
| 78 | + assert not exceptions |
| 79 | + |
| 80 | + |
| 81 | +@pytest.mark.skipif( |
| 82 | + trytond.__version__.split(".") < ["5", "4"], reason="At least Trytond-5.4 required" |
| 83 | +) |
| 84 | +def test_rpc_error_page(sentry_init, app, capture_events, get_client): |
| 85 | + """Test that, after initializing the Trytond-SentrySDK integration |
| 86 | + a custom error handler can be registered to the Trytond WSGI app so as to |
| 87 | + inform the event identifiers to the Tryton RPC client""" |
| 88 | + |
| 89 | + sentry_init(integrations=[TrytondWSGIIntegration()]) |
| 90 | + events = capture_events() |
| 91 | + |
| 92 | + @app.route("/rpcerror", methods=["POST"]) |
| 93 | + def _(request): |
| 94 | + raise Exception("foo") |
| 95 | + |
| 96 | + @app.error_handler |
| 97 | + def _(app, request, e): |
| 98 | + if isinstance(e, TrytondBaseException): |
| 99 | + return |
| 100 | + else: |
| 101 | + event_id = last_event_id() |
| 102 | + data = TrytondUserError(str(event_id), str(e)) |
| 103 | + return app.make_response(request, data) |
| 104 | + |
| 105 | + client = get_client() |
| 106 | + |
| 107 | + # This would look like a natural Tryton RPC call |
| 108 | + _data = dict( |
| 109 | + id=42, # request sequence |
| 110 | + method="class.method", # rpc call |
| 111 | + params=[ |
| 112 | + [1234], # ids |
| 113 | + ["bar", "baz"], # values |
| 114 | + dict( # context |
| 115 | + client="12345678-9abc-def0-1234-56789abc", |
| 116 | + groups=[1], |
| 117 | + language="ca", |
| 118 | + language_direction="ltr", |
| 119 | + ), |
| 120 | + ], |
| 121 | + ) |
| 122 | + response = client.post( |
| 123 | + "/rpcerror", content_type="application/json", data=json.dumps(_data) |
| 124 | + ) |
| 125 | + |
| 126 | + (event,) = events |
| 127 | + (content, status, headers) = response |
| 128 | + data = json.loads(next(content)) |
| 129 | + assert status == "200 OK" |
| 130 | + assert headers.get("Content-Type") == "application/json" |
| 131 | + assert data == dict(id=42, error=["UserError", [event["event_id"], "foo", None]]) |
0 commit comments