def test_connecter(self, mock_parse, mock_chown, mock_getpwnam, test_data): is_newer, data, secret, expect, ex_secret, rewrite = test_data mock_chown.return_value = None mock_parse.return_value = mock_etree_parse(data) mock_getpwnam.return_value = mock_pkiuser_entity() dogtag = MyDogtagInstance(is_newer) with patch('ipaserver.install.dogtaginstance.open', mock_open()) \ as mocked_file: dogtag.secure_ajp_connector() if rewrite: newdata = mocked_file().write.call_args.args f = BytesIO(newdata[0]) server_xml = myparse(f) doc = server_xml.getroot() connectors = doc.xpath('//Connector[@protocol="AJP/1.3"]') assert len(connectors) == len(secret) i = 0 for connector in connectors: if expect[i] == 'RANDOM': assert connector.attrib[secret[i]] else: assert connector.attrib[secret[i]] == expect[i] assert connector.attrib.get(ex_secret[i]) is None i += 1 else: assert mocked_file().write.call_args is None
def mock_etree_parse(data): """Convert a string into a file-like object to pass in the XML""" f = BytesIO(data.strip().encode('utf-8')) return myparse(f)