def assertSerializeDeserialize(self, d, xml, xmlns=None): self.assertThat(serializer.to_xml(copy.deepcopy(d), xmlns), ksmatchers.XMLEquals(xml)) self.assertEqual(serializer.from_xml(xml), d) # operations should be invertible self.assertEqual( serializer.from_xml(serializer.to_xml(copy.deepcopy(d), xmlns)), d) self.assertThat(serializer.to_xml(serializer.from_xml(xml), xmlns), ksmatchers.XMLEquals(xml))
def test_admin_version_v3(self): client = self.client(self.public_app) resp = client.get('/v3/', headers=self.REQUEST_HEADERS) self.assertEqual(resp.status_int, 200) data = resp.body expected = self.v3_VERSION_RESPONSE % dict(port=CONF.admin_port) self.assertThat(data, matchers.XMLEquals(expected))
def test_use_site_url_if_endpoint_unset(self): client = self.client(self.public_app) resp = client.get('/', headers=self.REQUEST_HEADERS) self.assertEqual(resp.status_int, 300) data = resp.body expected = self.VERSIONS_RESPONSE % dict(port=CONF.public_port) self.assertThat(data, matchers.XMLEquals(expected))
def test_v2_links_special_case(self): # There's special-case code (for backward compatibility) where if the # data is the v2 version data, the link elements are also added to the # main element. d = { "object": { "id": "v2.0", "status": "deprecated", "updated": "2014-04-17T00:00:00Z", "links": [{ "href": "http://localhost:5000/v2.0/", "rel": "self" }, { "href": "http://docs.openstack.org/api/openstack-" "identity-service/2.0/content/", "type": "text/html", "rel": "describedby" }, { "href": "http://docs.openstack.org/api/openstack-" "identity-service/2.0/" "identity-dev-guide-2.0.pdf", "type": "application/pdf", "rel": "describedby" }] } } xml = """ <?xml version="1.0" encoding="UTF-8"?> <object xmlns="http://docs.openstack.org/identity/api/v2.0" id="v2.0" status="deprecated" updated="2014-04-17T00:00:00Z"> <links> <link rel="self" href="http://localhost:5000/v2.0/"/> <link rel="describedby" href="http://docs.openstack.org/api/openstack-\ identity-service/2.0/content/" type="text/html"/> <link rel="describedby" href="http://docs.openstack.org/api/openstack-\ identity-service/2.0/identity-dev-guide-2.0.pdf" type="application/pdf"/> </links> <link rel="self" href="http://localhost:5000/v2.0/"/> <link rel="describedby" href="http://docs.openstack.org/api/openstack-\ identity-service/2.0/content/" type="text/html"/> <link rel="describedby" href="http://docs.openstack.org/api/openstack-\ identity-service/2.0/identity-dev-guide-2.0.pdf" type="application/pdf"/> </object> """ self.assertThat(serializer.to_xml(d), ksmatchers.XMLEquals(xml))
def test_policy_list(self): d = {"policies": [{"id": "ab12cd"}]} xml = """ <?xml version="1.0" encoding="UTF-8"?> <policies xmlns="http://docs.openstack.org/identity/api/v2.0"> <policy id="ab12cd"/> </policies> """ self.assertThat(serializer.to_xml(d), ksmatchers.XMLEquals(xml))
def test_v3_disabled(self): client = self.client(self.public_app) # request to /v2.0 should pass resp = client.get('/v2.0/', headers=self.REQUEST_HEADERS) self.assertEqual(resp.status_int, 200) data = resp.body expected = self.v2_VERSION_RESPONSE % dict(port=CONF.public_port) self.assertThat(data, matchers.XMLEquals(expected)) # only v2 information should be displayed by requests to / v2_only_response = ( (self.DOC_INTRO + '<versions %(namespace)s>' + self.v2_VERSION_DATA + '</versions>') % dict(namespace=self.XML_NAMESPACE_ATTR, v2_namespace='') % dict(port=CONF.public_port)) resp = client.get('/', headers=self.REQUEST_HEADERS) self.assertEqual(resp.status_int, 300) data = resp.body self.assertThat(data, matchers.XMLEquals(v2_only_response))
def test_values_list(self): d = { "objects": { "values": [{ "attribute": "value1", }, { "attribute": "value2", }] } } xml = """ <?xml version="1.0" encoding="UTF-8"?> <objects xmlns="http://docs.openstack.org/identity/api/v2.0"> <object attribute="value1"/> <object attribute="value2"/> </objects> """ self.assertThat(serializer.to_xml(d), ksmatchers.XMLEquals(xml))
class TestXMLEquals(tests.BaseTestCase, helpers.TestMatchersInterface): matches_xml = b"""<?xml version="1.0" encoding="UTF-8"?> <test xmlns="http://docs.openstack.org/identity/api/v2.0"> <first z="0" y="1" x="2"/> <second a="a" b="b"></second> </test> """ equivalent_xml = b"""<?xml version="1.0" encoding="UTF-8"?> <test xmlns="http://docs.openstack.org/identity/api/v2.0"> <second a="a" b="b"/> <first z="0" y="1" x="2"></first> </test> """ mismatches_xml = b"""<?xml version="1.0" encoding="UTF-8"?> <test xmlns="http://docs.openstack.org/identity/api/v2.0"> <nope_it_fails/> </test> """ mismatches_description = b"""expected = <test xmlns="http://docs.openstack.org/identity/api/v2.0"> <first z="0" y="1" x="2"/> <second a="a" b="b"/> </test> actual = <test xmlns="http://docs.openstack.org/identity/api/v2.0"> <nope_it_fails/> </test> """ matches_matcher = matchers.XMLEquals(matches_xml) matches_matches = [matches_xml, equivalent_xml] matches_mismatches = [mismatches_xml] describe_examples = [ (mismatches_description, mismatches_xml, matches_matcher), ] str_examples = [('XMLEquals(%r)' % matches_xml, matches_matcher)]
class TestXMLEquals(tests.BaseTestCase, helpers.TestMatchersInterface): matches_xml = b""" <?xml version="1.0" encoding="UTF-8"?> <test xmlns="http://docs.openstack.org/identity/api/v2.0"> <success a="a" b="b"/> </test> """ equivalent_xml = b""" <?xml version="1.0" encoding="UTF-8"?> <test xmlns="http://docs.openstack.org/identity/api/v2.0"> <success b="b" a="a"></success> </test> """ mismatches_xml = b""" <?xml version="1.0" encoding="UTF-8"?> <test xmlns="http://docs.openstack.org/identity/api/v2.0"> <nope_it_fails/> </test> """ mismatches_description = textwrap.dedent("""\ expected = <test xmlns="http://docs.openstack.org/identity/api/v2.0"> <success a="a" b="b"/> </test> actual = <test xmlns="http://docs.openstack.org/identity/api/v2.0"> <nope_it_fails/> </test> """).lstrip() matches_matcher = matchers.XMLEquals(matches_xml) matches_matches = [matches_xml, equivalent_xml] matches_mismatches = [mismatches_xml] describe_examples = [ (mismatches_description, mismatches_xml, matches_matcher), ] str_examples = [('XMLEquals(%r)' % matches_xml, matches_matcher)]