Skip to content

Commit

Permalink
Return HTTP 400 when attempting to post an event with an unregistered…
Browse files Browse the repository at this point in the history
… schema
  • Loading branch information
afshin committed Oct 18, 2024
1 parent 74655ce commit f5bb5a8
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
4 changes: 4 additions & 0 deletions jupyter_server/services/events/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from typing import TYPE_CHECKING, Any, Dict, Optional, cast

from jupyter_core.utils import ensure_async
from jupyter_events.logger import SchemaNotRegistered
from tornado import web, websocket

from jupyter_server.auth.decorator import authorized, ws_authenticated
Expand Down Expand Up @@ -121,6 +122,9 @@ async def post(self):
self.finish()
except web.HTTPError:
raise
except SchemaNotRegistered as e:
message = f"Unregistered event schema: ${str(e)}"
raise web.HTTPError(400, message) from e
except Exception as e:
raise web.HTTPError(500, str(e)) from e

Expand Down
18 changes: 14 additions & 4 deletions tests/services/events/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,16 +117,26 @@ async def test_post_event(jp_fetch, event_logger_sink, payload):
}
"""

payload_7 = """\
{
"schema_id": "http://event.mock.jupyter.org/UNREGISTERED-SCHEMA",
"version": 1,
"data": {
"event_message": "Hello, world!"
}
}
"""


@pytest.mark.parametrize("payload", [payload_3, payload_4, payload_5, payload_6])
@pytest.mark.parametrize("payload", [payload_3, payload_4, payload_5, payload_6, payload_7])
async def test_post_event_400(jp_fetch, event_logger, payload):
with pytest.raises(tornado.httpclient.HTTPClientError) as e:
await jp_fetch("api", "events", method="POST", body=payload)

assert expected_http_error(e, 400)


payload_7 = """\
payload_8 = """\
{
"schema_id": "http://event.mock.jupyter.org/message",
"version": 1,
Expand All @@ -136,7 +146,7 @@ async def test_post_event_400(jp_fetch, event_logger, payload):
}
"""

payload_8 = """\
payload_9 = """\
{
"schema_id": "http://event.mock.jupyter.org/message",
"version": 2,
Expand All @@ -147,7 +157,7 @@ async def test_post_event_400(jp_fetch, event_logger, payload):
"""


@pytest.mark.parametrize("payload", [payload_7, payload_8])
@pytest.mark.parametrize("payload", [payload_8, payload_9])
async def test_post_event_500(jp_fetch, event_logger, payload):
with pytest.raises(tornado.httpclient.HTTPClientError) as e:
await jp_fetch("api", "events", method="POST", body=payload)
Expand Down

0 comments on commit f5bb5a8

Please sign in to comment.