def test_standard_workflow_302_redirect(self): text = uuid.uuid4().hex self.requests_mock.get(self.TEST_SP_URL, response_list=[ dict(headers=PAOS_HEADER, content=utils.make_oneline(saml2_fixtures.SP_SOAP_RESPONSE)), dict(text=text) ]) authm = self.requests_mock.post(self.TEST_IDP_URL, content=saml2_fixtures.SAML2_ASSERTION) self.requests_mock.post( self.TEST_CONSUMER_URL, status_code=302, headers={'Location': self.TEST_SP_URL}) resp = requests.get(self.TEST_SP_URL, auth=self.get_plugin()) self.assertEqual(200, resp.status_code) self.assertEqual(text, resp.text) self.assertEqual(self.calls, [self.TEST_SP_URL, self.TEST_IDP_URL, self.TEST_CONSUMER_URL, self.TEST_SP_URL]) self.assertEqual(self.basic_header(), authm.last_request.headers['Authorization']) authn_request = self.requests_mock.request_history[1].text self.assertThat(saml2_fixtures.AUTHN_REQUEST, matchers.XMLEquals(authn_request))
def test_workflow(self): token_id = uuid.uuid4().hex token = ksa_fixtures.V3Token() self.requests_mock.get(self.default_sp_url, response_list=[ dict(headers=PAOS_HEADER, content=utils.make_oneline(saml2_fixtures.SP_SOAP_RESPONSE)), dict(headers={'X-Subject-Token': token_id}, json=token) ]) authm = self.requests_mock.post(self.TEST_IDP_URL, content=saml2_fixtures.SAML2_ASSERTION) self.requests_mock.post( self.TEST_CONSUMER_URL, status_code=302, headers={'Location': self.sp_url()}) auth_ref = self.get_plugin().get_auth_ref(self.session) self.assertEqual(token_id, auth_ref.auth_token) self.assertEqual(self.calls, [self.default_sp_url, self.TEST_IDP_URL, self.TEST_CONSUMER_URL, self.default_sp_url]) self.assertEqual(self.basic_header(), authm.last_request.headers['Authorization']) authn_request = self.requests_mock.request_history[1].text self.assertThat(saml2_fixtures.AUTHN_REQUEST, matchers.XMLEquals(authn_request))
def test_get_adfs_security_token(self): """Test ADFSPassword._get_adfs_security_token().""" self.requests_mock.post(self.IDENTITY_PROVIDER_URL, content=utils.make_oneline( self.ADFS_SECURITY_TOKEN_RESPONSE), status_code=200) self.adfsplugin._prepare_adfs_request() self.adfsplugin._get_adfs_security_token(self.session) adfs_response = etree.tostring(self.adfsplugin.adfs_token) fixture_response = self.ADFS_SECURITY_TOKEN_RESPONSE self.assertThat(fixture_response, matchers.XMLEquals(adfs_response))
def test_send_authn_req_to_idp(self): self.requests_mock.post(self.IDENTITY_PROVIDER_URL, content=saml2_fixtures.SAML2_ASSERTION) self.saml2plugin.sp_response_consumer_url = self.SHIB_CONSUMER_URL self.saml2plugin.saml2_authn_request = etree.XML( saml2_fixtures.SP_SOAP_RESPONSE) self.saml2plugin._send_idp_saml2_authn_request(self.session) idp_response = etree.tostring( self.saml2plugin.saml2_idp_authn_response) self.assertThat(idp_response, matchers.XMLEquals(saml2_fixtures.SAML2_ASSERTION))
def test_initial_sp_call(self): """Test initial call, expect SOAP message.""" self.requests_mock.get(self.FEDERATION_AUTH_URL, content=utils.make_oneline( saml2_fixtures.SP_SOAP_RESPONSE)) a = self.saml2plugin._send_service_provider_request(self.session) self.assertFalse(a) sp_soap_response = etree.tostring(self.saml2plugin.saml2_authn_request) self.assertThat(saml2_fixtures.SP_SOAP_RESPONSE, matchers.XMLEquals(sp_soap_response)) self.assertEqual( self.saml2plugin.sp_response_consumer_url, self.SHIB_CONSUMER_URL, "Expected consumer_url set to %s instead of %s" % (self.SHIB_CONSUMER_URL, str(self.saml2plugin.sp_response_consumer_url)))
class TestXMLEquals(testtools.TestCase, 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 = """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(testtools.TestCase): 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 = """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 = ks_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)] def test_matches_match(self): matcher = self.matches_matcher matches = self.matches_matches mismatches = self.matches_mismatches for candidate in matches: self.assertIsNone(matcher.match(candidate)) for candidate in mismatches: mismatch = matcher.match(candidate) self.assertIsNotNone(mismatch) self.assertIsNotNone(getattr(mismatch, 'describe', None)) def test__str__(self): # [(expected, object to __str__)]. examples = self.str_examples for expected, matcher in examples: self.assertThat(matcher, tt_matchers.DocTestMatches(expected)) def test_describe_difference(self): # [(expected, matchee, matcher), ...] examples = self.describe_examples for difference, matchee, matcher in examples: mismatch = matcher.match(matchee) self.assertEqual(difference, mismatch.describe()) def test_mismatch_details(self): # The mismatch object must provide get_details, which must return a # dictionary mapping names to Content objects. examples = self.describe_examples for difference, matchee, matcher in examples: mismatch = matcher.match(matchee) details = mismatch.get_details() self.assertEqual(dict(details), details)