• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

SAML-Toolkits / python3-saml / 9636737938

23 Jun 2024 10:06PM UTC coverage: 95.19%. Remained the same
9636737938

push

github

pitbulk
Adjust format to black

1193 of 1364 branches covered (87.46%)

Branch coverage included in aggregate %.

2181 of 2242 new or added lines in 24 files covered. (97.28%)

2 existing lines in 2 files now uncovered.

6902 of 7140 relevant lines covered (96.67%)

0.97 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

85.47
/tests/src/OneLogin/saml2_tests/xml_utils_test.py
1
# -*- coding: utf-8 -*-
2

3

4
import json
1✔
5
import unittest
1✔
6
import xmlsec
1✔
7

8
from base64 import b64decode
1✔
9
from lxml import etree
1✔
10
from os.path import dirname, join, exists
1✔
11
from onelogin.saml2.utils import OneLogin_Saml2_XML
1✔
12

13

14
class TestOneLoginSaml2Xml(unittest.TestCase):
1✔
15
    data_path = join(dirname(__file__), "..", "..", "..", "data")
1✔
16

17
    def loadSettingsJSON(self, filename=None):
1✔
18
        if filename:
×
NEW
19
            filename = join(dirname(__file__), "..", "..", "..", "settings", filename)
×
20
        else:
NEW
21
            filename = join(dirname(__file__), "..", "..", "..", "settings", "settings1.json")
×
22
        if exists(filename):
×
NEW
23
            stream = open(filename, "r")
×
24
            settings = json.load(stream)
×
25
            stream.close()
×
26
            return settings
×
27
        else:
NEW
28
            raise Exception("Settings json file does not exist")
×
29

30
    def file_contents(self, filename):
1✔
31
        f = open(filename, "r")
1✔
32
        content = f.read()
1✔
33
        f.close()
1✔
34
        return content
1✔
35

36
    def testLibxml2(self):
1✔
37
        """
38
        Tests that libxml2 versions used by xmlsec and lxml are compatible
39

40
        If this test fails, reinstall lxml without using binary to ensure it is
41
        linked to same version of libxml2 as xmlsec:
42
        pip install --force-reinstall --no-binary lxml lxml
43

44
        See https://bugs.launchpad.net/lxml/+bug/1960668
45
        """
46
        env = etree.fromstring("<xml></xml>")
1✔
47
        sig = xmlsec.template.create(env, xmlsec.Transform.EXCL_C14N, xmlsec.Transform.RSA_SHA256, ns="ds")
1✔
48

49
        ds = etree.QName(sig).namespace
1✔
50
        cm = sig.find(".//{%s}CanonicalizationMethod" % ds)
1✔
51

52
        self.assertIsNotNone(cm)
1✔
53

54
    def testValidateXML(self):
1✔
55
        """
56
        Tests the validate_xml method of the OneLogin_Saml2_XML
57
        """
58
        metadata_unloaded = "<xml><EntityDescriptor>"
1✔
59
        res = OneLogin_Saml2_XML.validate_xml(metadata_unloaded, "saml-schema-metadata-2.0.xsd")
1✔
60
        self.assertIsInstance(res, str)
1✔
61
        self.assertIn("unloaded_xml", res)
1✔
62

63
        metadata_invalid = self.file_contents(join(self.data_path, "metadata", "noentity_metadata_settings1.xml"))
1✔
64

65
        res = OneLogin_Saml2_XML.validate_xml(metadata_invalid, "saml-schema-metadata-2.0.xsd")
1✔
66
        self.assertIsInstance(res, str)
1✔
67
        self.assertIn("invalid_xml", res)
1✔
68

69
        metadata_expired = self.file_contents(join(self.data_path, "metadata", "expired_metadata_settings1.xml"))
1✔
70
        res = OneLogin_Saml2_XML.validate_xml(metadata_expired, "saml-schema-metadata-2.0.xsd")
1✔
71
        self.assertIsInstance(res, OneLogin_Saml2_XML._element_class)
1✔
72

73
        metadata_ok = self.file_contents(join(self.data_path, "metadata", "metadata_settings1.xml"))
1✔
74
        res = OneLogin_Saml2_XML.validate_xml(metadata_ok, "saml-schema-metadata-2.0.xsd")
1✔
75
        self.assertIsInstance(res, OneLogin_Saml2_XML._element_class)
1✔
76

77
    def testToString(self):
1✔
78
        """
79
        Tests the to_string method of the OneLogin_Saml2_XML
80
        """
81
        xml = "<test>test1</test>"
1✔
82
        elem = etree.fromstring(xml)
1✔
83
        bxml = xml.encode("utf8")
1✔
84

85
        self.assertIs(xml, OneLogin_Saml2_XML.to_string(xml))
1✔
86
        self.assertIs(bxml, OneLogin_Saml2_XML.to_string(bxml))
1✔
87
        self.assertEqual(etree.tostring(elem), OneLogin_Saml2_XML.to_string(elem))
1✔
88
        with self.assertRaises(ValueError) as context:
1✔
89
            OneLogin_Saml2_XML.to_string(1)
1✔
90
            exception = context.exception
×
91
            self.assertIn("unsupported type", str(exception))
×
92

93
    def testToElement(self):
1✔
94
        """
95
        Tests the to_etree method of the OneLogin_Saml2_XML
96
        """
97
        xml = "<test>test1</test>"
1✔
98
        elem = etree.fromstring(xml)
1✔
99
        xml_expected = etree.tostring(elem)
1✔
100

101
        res = OneLogin_Saml2_XML.to_etree(xml)
1✔
102
        self.assertIsInstance(res, etree._Element)
1✔
103
        self.assertEqual(xml_expected, etree.tostring(res))
1✔
104

105
        res = OneLogin_Saml2_XML.to_etree(xml.encode("utf8"))
1✔
106
        self.assertIsInstance(res, etree._Element)
1✔
107
        self.assertEqual(xml_expected, etree.tostring(res))
1✔
108

109
        self.assertIsInstance(res, etree._Element)
1✔
110
        self.assertEqual(xml_expected, etree.tostring(res))
1✔
111

112
        res = OneLogin_Saml2_XML.to_etree(elem)
1✔
113
        self.assertIs(res, elem)
1✔
114

115
        with self.assertRaises(ValueError) as context:
1✔
116
            OneLogin_Saml2_XML.to_etree(1)
1✔
117
            exception = context.exception
×
118
            self.assertIn("unsupported type", str(exception))
×
119

120
    def testQuery(self):
1✔
121
        """
122
        Tests the query method of the OneLogin_Saml2_Utils
123
        """
124
        xml = self.file_contents(join(self.data_path, "responses", "valid_response.xml.base64"))
1✔
125
        xml = b64decode(xml)
1✔
126
        dom = etree.fromstring(xml)
1✔
127

128
        assertion_nodes = OneLogin_Saml2_XML.query(dom, "/samlp:Response/saml:Assertion")
1✔
129
        self.assertEqual(1, len(assertion_nodes))
1✔
130
        assertion = assertion_nodes[0]
1✔
131
        self.assertIn("Assertion", assertion.tag)
1✔
132

133
        attribute_statement_nodes = OneLogin_Saml2_XML.query(dom, "/samlp:Response/saml:Assertion/saml:AttributeStatement")
1✔
134
        self.assertEqual(1, len(assertion_nodes))
1✔
135
        attribute_statement = attribute_statement_nodes[0]
1✔
136
        self.assertIn("AttributeStatement", attribute_statement.tag)
1✔
137

138
        attribute_statement_nodes_2 = OneLogin_Saml2_XML.query(dom, "./saml:AttributeStatement", assertion)
1✔
139
        self.assertEqual(1, len(attribute_statement_nodes_2))
1✔
140
        attribute_statement_2 = attribute_statement_nodes_2[0]
1✔
141
        self.assertEqual(attribute_statement, attribute_statement_2)
1✔
142

143
        signature_res_nodes = OneLogin_Saml2_XML.query(dom, "/samlp:Response/ds:Signature")
1✔
144
        self.assertEqual(1, len(signature_res_nodes))
1✔
145
        signature_res = signature_res_nodes[0]
1✔
146
        self.assertIn("Signature", signature_res.tag)
1✔
147

148
        signature_nodes = OneLogin_Saml2_XML.query(dom, "/samlp:Response/saml:Assertion/ds:Signature")
1✔
149
        self.assertEqual(1, len(signature_nodes))
1✔
150
        signature = signature_nodes[0]
1✔
151
        self.assertIn("Signature", signature.tag)
1✔
152

153
        signature_nodes_2 = OneLogin_Saml2_XML.query(dom, "./ds:Signature", assertion)
1✔
154
        self.assertEqual(1, len(signature_nodes_2))
1✔
155
        signature2 = signature_nodes_2[0]
1✔
156
        self.assertNotEqual(signature_res, signature2)
1✔
157
        self.assertEqual(signature, signature2)
1✔
158

159
        signature_nodes_3 = OneLogin_Saml2_XML.query(dom, "./ds:SignatureValue", assertion)
1✔
160
        self.assertEqual(0, len(signature_nodes_3))
1✔
161

162
        signature_nodes_4 = OneLogin_Saml2_XML.query(dom, "./ds:Signature/ds:SignatureValue", assertion)
1✔
163
        self.assertEqual(1, len(signature_nodes_4))
1✔
164

165
        signature_nodes_5 = OneLogin_Saml2_XML.query(dom, ".//ds:SignatureValue", assertion)
1✔
166
        self.assertEqual(1, len(signature_nodes_5))
1✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc