Files
zulip/zerver/tests/test_legacy_subject.py
Anders Kaseorg bd9a1dc971 tests: Consistently JSON-encode ‘to’ parameter
Although our POST /messages handler accepts the ‘to’ parameter with or
without JSON encoding, there are two problems with passing it as an
unencoded string.

Firstly, you’d fail to send a message to a stream named ‘true’ or
‘false’ or ‘null’ or ‘2022’, as the JSON interpretation is prioritized
over the plain string interpretation.

Secondly, and more importantly for our tests, it violates our OpenAPI
schema, which requires the parameter to be JSON-encoded.  This is
because OpenAPI has no concept of a parameter that’s “optionally
JSON-encoded”, nor should it: such a parameter cannot be unambiguously
decoded for the reason above.

Our version of openapi-core doesn’t currently detect this schema
violation, but after the next upgrade it will.

Signed-off-by: Anders Kaseorg <anders@zulip.com>
2022-09-13 11:05:37 -07:00

24 lines
711 B
Python

import orjson
from zerver.lib.test_classes import ZulipTestCase
class LegacySubjectTest(ZulipTestCase):
def test_legacy_subject(self) -> None:
self.login("hamlet")
payload = dict(
type="stream",
to=orjson.dumps("Verona").decode(),
content="Test message",
)
payload["subject"] = "whatever"
result = self.client_post("/json/messages", payload)
self.assert_json_success(result)
# You can't use both subject and topic.
payload["topic"] = "whatever"
result = self.client_post("/json/messages", payload)
self.assert_json_error(result, "Can't decide between 'topic' and 'subject' arguments")