Skip to content

Commit

Permalink
Merge pull request #4329 from opsmill/lgu-clean-error-on-missing-sche…
Browse files Browse the repository at this point in the history
…ma-menu-placement

Raise 422 SchemaNotFoundError while uploading a schema referring an unexisting schemanode
  • Loading branch information
LucasG0 authored Sep 12, 2024
2 parents 9ecc426 + 21d298b commit 8795c14
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 5 deletions.
9 changes: 6 additions & 3 deletions backend/infrahub/core/schema_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -903,9 +903,12 @@ def validate_menu_placements(self) -> None:
if node.menu_placement:
try:
placement_node = self.get(name=node.menu_placement, duplicate=False)
except SchemaNotFoundError:
raise ValueError(f"{node.kind}: {node.menu_placement} is not a valid menu placement") from None

except SchemaNotFoundError as exc:
raise SchemaNotFoundError(
branch_name=self.name,
identifier=node.menu_placement,
message=f"{node.kind} refers to an invalid menu placement node: {node.menu_placement}.",
) from exc
if node == placement_node:
raise ValueError(f"{node.kind}: cannot be placed under itself in the menu") from None

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
from infrahub_sdk import InfrahubClient

from .shared import (
TestSchemaLifecycleBase,
)


class TestSchemaMissingMenuPlacement(TestSchemaLifecycleBase):
async def test_schema_missing_menu_placement(self, client: InfrahubClient):
schema = {
"version": "1.0",
"nodes": [
{
"name": "BNode",
"namespace": "Infra",
"menu_placement": "UnexistingNode",
"label": "BNode",
"display_labels": ["name__value"],
"attributes": [{"name": "name", "kind": "Text", "unique": True}],
}
],
}

response = await client.schema.load(schemas=[schema], branch="main")
assert response.schema_updated is False
assert response.errors["errors"][0]["extensions"]["code"] == 422
assert (
response.errors["errors"][0]["message"]
== "InfraBNode refers to an invalid menu placement node: UnexistingNode."
)
4 changes: 2 additions & 2 deletions backend/tests/unit/core/schema_manager/test_manager_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -835,10 +835,10 @@ async def test_schema_branch_validate_menu_placement():
schema = SchemaBranch(cache={})
schema.load_schema(schema=SchemaRoot(**FULL_SCHEMA))

with pytest.raises(ValueError) as exc:
with pytest.raises(SchemaNotFoundError) as exc:
schema.validate_menu_placements()

assert str(exc.value) == "TestSubObject: NoSuchObject is not a valid menu placement"
assert exc.value.message == "TestSubObject refers to an invalid menu placement node: NoSuchObject."


async def test_schema_branch_validate_same_node_menu_placement():
Expand Down
1 change: 1 addition & 0 deletions changelog/4089.fixed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Loading a schema with a schemanode referencing an incorrect menu placement now returns a proper HTTP 422 error

0 comments on commit 8795c14

Please sign in to comment.