def write_ovpn(ovpn: Ovpn, private_key: str, certificate: str, target: PathLike): """ Write the OVPN configuration file to target. """ logger.info(f"Writing configuration to {target}") with open(target, mode='w+t') as f: ovpn.write(f) f.writelines(f"\n<key>\n{private_key}\n</key>\n") f.writelines(f"\n<cert>\n{certificate}\n</cert>\n")
def test_parse_and_write(self): ovpn_file = ('# hello world\n' 'set a 1\n' '\n' '<ca>\n' 'the\n' 'certificate\n' 'content\n' '</ca>\n') ovpn = Ovpn.parse(ovpn_file) self.assertEqual(ovpn.content, [ Comment(' hello world'), Field('set', ['a', '1']), Empty(), Section('ca', ['the', 'certificate', 'content']), ]) self.assertEqual(ovpn.to_string(), ovpn_file)
def test_force_tcp(self): ovpn = Ovpn.parse('a\n' 'remote vpn.example.com 1234 udp\n' 'remote vpn.example.com 5678 udp\r\n' 'remote vpn.example.com 1234 tcp\r' 'remote vpn.example.com 5678 tcp\r\n' 'b\r\n' 'c\n') expected_content = ( 'a \n' '# omitted to force tcp: remote vpn.example.com 1234 udp\n' '# omitted to force tcp: remote vpn.example.com 5678 udp\n' 'remote vpn.example.com 1234 tcp\n' 'remote vpn.example.com 5678 tcp\n' 'b \n' 'c \n') ovpn.force_tcp() self.assertEqual(ovpn.to_string(), expected_content)
def write_config(config: str, private_key: str, certificate: str, target: PathLike): """ Write the configuration to target. """ ovpn = Ovpn.parse(config) write_ovpn(ovpn, private_key, certificate, target)
def test_get_add_connection(self): client = get_client() ovpn = Ovpn.parse(mock_config) simple_connection = import_ovpn(ovpn) add_connection(client, simple_connection)
def test_import_ovpn(self): ovpn = Ovpn.parse(mock_config) import_ovpn(ovpn)
def test_force_tcp_fail(self): ovpn = Ovpn.parse('remote vpn.example.com 1234 udp\n') with self.assertRaises(InvalidOVPN): ovpn.force_tcp()