Skip to content

Commit

Permalink
✅ Run XSD validation in eherkenning metadata test
Browse files Browse the repository at this point in the history
  • Loading branch information
sergei-maertens committed Dec 18, 2024
1 parent f6bdf84 commit c554cbd
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
13 changes: 13 additions & 0 deletions tests/test_eherkenning_metadata.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from pathlib import Path

from django.test import TestCase

import pytest
Expand All @@ -12,6 +14,13 @@
)

from .mixins import EherkenningMetadataMixin
from .utils import validate_against_xsd

_repo_root = Path(__file__).parent.parent.resolve()

SAML_METADATA_XSD = (
_repo_root / "digid_eherkenning" / "xsd" / "saml-schema-metadata-2.0.xsd"
)

NAME_SPACES = {
"md": "urn:oasis:names:tc:SAML:2.0:metadata",
Expand Down Expand Up @@ -162,6 +171,10 @@ def test_generate_metadata_all_options_specified(self):
self.eherkenning_config.save()

eherkenning_metadata = generate_eherkenning_metadata()

with self.subTest("passes XSD validation"):
validate_against_xsd(eherkenning_metadata, SAML_METADATA_XSD)

self.assertEqual(eherkenning_metadata[:5], b"<?xml")
entity_descriptor_node = etree.XML(eherkenning_metadata)

Expand Down
22 changes: 22 additions & 0 deletions tests/utils.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
from base64 import b64encode
from functools import lru_cache
from hashlib import sha1
from io import BytesIO
from pathlib import Path

from lxml import etree

SAML_NAMESPACES = {
"samlp": "urn:oasis:names:tc:SAML:2.0:protocol",
Expand All @@ -19,3 +24,20 @@ def create_example_artifact(endpoint_url, endpoint_index=b"\x00\x00"):
message_handle = b"01234567890123456789" # something random

return b64encode(type_code + endpoint_index + source_id + message_handle)


@lru_cache
def _load_schema(path: Path):
with path.open("r") as infile:
return etree.parse(infile)


def validate_against_xsd(xml: bytes, xsd_schema: Path) -> None:
"""
Validate the XML against a schema.
See https://lxml.de/validation.html
"""
xmlschema = etree.XMLSchema(_load_schema(xsd_schema))
doc = etree.parse(BytesIO(xml))
xmlschema.assertValid(doc)

0 comments on commit c554cbd

Please sign in to comment.