Skip to content

Commit be5907a

Browse files
[v3-0-test] Connection Extra additional test case for validation (#54239) (#54244)
(cherry picked from commit adc4b8b) Co-authored-by: Pierre Jeambrun <[email protected]>
1 parent c846511 commit be5907a

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

airflow-core/tests/unit/api_fastapi/core_api/routes/public/test_connections.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1257,3 +1257,36 @@ def test_post_should_accept_empty_string_as_extra(self, test_client, session):
12571257
connection = session.query(Connection).filter_by(conn_id=TEST_CONN_ID).first()
12581258
assert connection is not None
12591259
assert connection.extra == "{}" # Backward compatibility: treat "" as empty JSON object
1260+
1261+
@pytest.mark.parametrize(
1262+
"extra, expected_error_message",
1263+
[
1264+
("[1,2,3]", "Expected JSON object in `extra` field, got non-dict JSON"),
1265+
("some_string", "Encountered non-JSON in `extra` field"),
1266+
],
1267+
)
1268+
def test_post_should_fail_with_non_json_object_as_extra(
1269+
self, test_client, extra, expected_error_message, session
1270+
):
1271+
"""JSON primitives are a valid JSON and should raise 422 validation error."""
1272+
body = {"connection_id": TEST_CONN_ID, "conn_type": TEST_CONN_TYPE, "extra": extra}
1273+
1274+
response = test_client.post("/connections", json=body)
1275+
assert response.status_code == 422
1276+
assert (
1277+
"Value error, The `extra` field must be a valid JSON object (e.g., {'key': 'value'})"
1278+
in response.json()["detail"][0]["msg"]
1279+
)
1280+
1281+
_check_last_log(
1282+
session,
1283+
dag_id=None,
1284+
event="post_connection",
1285+
logical_date=None,
1286+
expected_extra={
1287+
"connection_id": "test_connection_id",
1288+
"conn_type": "test_type",
1289+
"extra": expected_error_message,
1290+
"method": "POST",
1291+
},
1292+
)

0 commit comments

Comments
 (0)