def test_get_saml_response_relay_state_optional(self): with cast(BinaryIO, (DATA_DIR / 'saml_response.xml').open('rb')) as f: saml_response_xml = f.read() view = IdentityProviderResponseView() view.request = self.factory.post(self.url, {'SAMLResponse': b64encode(saml_response_xml).decode('ascii')}) saml_response = view.get_saml_response(None, None) self.assertIsNone(saml_response.relay_state)
def test_get_saml_response_invalid_signature(self): with cast(TextIO, (DATA_DIR / 'signed_response_and_assertion.xml').open('r')) as f: tree = parse_xml(f.read()) remove_extra_xml_whitespace(tree) saml_response_encoded = b64encode(dump_xml(tree, pretty_print=False)).decode('ascii') view = IdentityProviderResponseView() view.request = self.factory.post(self.url, {'SAMLResponse': saml_response_encoded}) self.assertRaises(SecurityError, view.get_saml_response, None, WRONG_CERT_FILE)
def test_create_light_response_correct_id_and_issuer(self): self.maxDiff = None view = IdentityProviderResponseView() view.request = self.factory.post(self.url) with cast(TextIO, (DATA_DIR / 'saml_response.xml').open('r')) as f: view.saml_response = SAMLResponse(parse_xml(f.read()), 'relay123') light_response = view.create_light_response('test-light-response-issuer') self.assertEqual(light_response.id, 'test-saml-response-id') # Preserved self.assertEqual(light_response.in_response_to_id, 'test-saml-request-id') # Preserved self.assertEqual(light_response.issuer, 'test-light-response-issuer') # Replaced
def test_create_light_token(self, uuid_mock: MagicMock): view = IdentityProviderResponseView() view.request = self.factory.post(self.url) light_response_data = LIGHT_RESPONSE_DICT.copy() light_response_data['status'] = Status(**light_response_data['status']) view.light_response = LightResponse(**light_response_data) token, encoded_token = view.create_light_token('test-token-issuer', 'sha256', 'test-secret') self.assertEqual(token.id, 'T0uuid4') self.assertEqual(token.issuer, 'test-token-issuer') self.assertEqual(token.created, datetime(2017, 12, 11, 16, 12, 5)) self.assertEqual(token.encode('sha256', 'test-secret').decode('ascii'), encoded_token) self.assertEqual(uuid_mock.mock_calls, [call()])
def test_get_saml_response_signed_and_encrypted(self): with cast(TextIO, (DATA_DIR / 'nia_test_response.xml').open('r')) as f: tree = parse_xml(f.read()) remove_extra_xml_whitespace(tree) saml_response_encoded = b64encode(dump_xml(tree, pretty_print=False)).decode('ascii') view = IdentityProviderResponseView() view.request = self.factory.post(self.url, {'SAMLResponse': saml_response_encoded, 'RelayState': 'relay123'}) saml_response = view.get_saml_response(KEY_FILE, NIA_CERT_FILE) self.assertEqual(saml_response.relay_state, 'relay123') with cast(TextIO, (DATA_DIR / 'nia_test_response_decrypted_verified.xml').open('r')) as f: decrypted_verified_xml = f.read() self.assertXMLEqual(dump_xml(saml_response.document).decode('utf-8'), decrypted_verified_xml)
def test_get_saml_response_encrypted(self): with cast(BinaryIO, (DATA_DIR / 'saml_response_encrypted.xml').open('rb')) as f: saml_response_xml = f.read() with cast(TextIO, (DATA_DIR / 'saml_response_decrypted.xml').open('r')) as f2: decrypted_saml_response_xml = f2.read() view = IdentityProviderResponseView() view.request = self.factory.post(self.url, {'SAMLResponse': b64encode(saml_response_xml).decode('ascii'), 'RelayState': 'relay123'}) saml_response = view.get_saml_response(KEY_FILE, None) self.assertEqual(saml_response.relay_state, 'relay123') self.assertXMLEqual(dump_xml(saml_response.document).decode('utf-8'), decrypted_saml_response_xml)
def test_get_saml_response_signed(self): with cast(TextIO, (DATA_DIR / 'signed_response_and_assertion.xml').open('r')) as f: tree = parse_xml(f.read()) remove_extra_xml_whitespace(tree) saml_response_encoded = b64encode(dump_xml(tree, pretty_print=False)).decode('ascii') view = IdentityProviderResponseView() view.request = self.factory.post(self.url, {'SAMLResponse': saml_response_encoded, 'RelayState': 'relay123'}) saml_response = view.get_saml_response(None, CERT_FILE) self.assertEqual(saml_response.relay_state, 'relay123') root = Element(Q_NAMES['saml2p:Response'], {'ID': 'id-response'}, nsmap={'saml2': EIDAS_NAMESPACES['saml2'], 'saml2p': EIDAS_NAMESPACES['saml2p']}) assertion = SubElement(root, Q_NAMES['saml2:Assertion'], {'ID': 'id-0uuid4'}) SubElement(assertion, Q_NAMES['saml2:Issuer']).text = 'Test Issuer' self.assertXMLEqual(dump_xml(saml_response.document).decode('utf-8'), dump_xml(root).decode('utf-8'))
def test_get_saml_response_no_data(self): view = IdentityProviderResponseView() view.request = self.factory.post(self.url) self.assertRaises(ParseError, view.get_saml_response, None, None)