def test_loading_ok(self): isps_expected = ['mail.com', 'yandex.ru', 'laposte.net', 'vivaldi.net', 'net-c.com'] isps_expected.sort() conf = Config.get_conf_from_file(config_path) isps = conf.get_isps() isps.sort() self.assertEqual(isps_expected, isps) self.assertEqual('imap.mail.com', conf.get_hostname('mail.com')) self.assertEqual(993, conf.get_port('mail.com')) self.assertEqual('/', conf.get_path_set('mail.com')) self.assertEqual('*****@*****.**', conf.get_user_login('mail.com')) self.assertEqual('Orn1th0r1que', conf.get_user_password('mail.com')) conf = Config.get_conf_from_string(conf.dump()) isps = conf.get_isps() isps.sort() self.assertEqual(isps_expected, isps) self.assertEqual('imap.mail.com', conf.get_hostname('mail.com')) self.assertEqual(993, conf.get_port('mail.com')) self.assertEqual('/', conf.get_path_set('mail.com')) self.assertEqual('*****@*****.**', conf.get_user_login('mail.com')) self.assertEqual('Orn1th0r1que', conf.get_user_password('mail.com'))
def test_cmp_conf(self): conf1 = Config.get_conf_from_file(config_path) conf2 = Config.get_conf_from_file(config_path) m1 = conf1.get_conf() m2 = conf2.get_conf() self.assertTrue(Config.cmp_conf(m1, m2)) m1 = conf1.get_conf() m2 = conf2.get_conf() m2['mail.com']['net']['hostname'] = 'toto' self.assertFalse(Config.cmp_conf(m1, m2)) m2 = conf2.get_conf() m2['mail.com']['net']['port'] = 10 self.assertFalse(Config.cmp_conf(m1, m2)) m2 = conf2.get_conf() m2['mail.com']['user']['login'] = '******' self.assertFalse(Config.cmp_conf(m1, m2)) m2 = conf2.get_conf() m2['mail.com']['user']['password'] = '******' self.assertFalse(Config.cmp_conf(m1, m2))
import sys import pickle from typing import List, Tuple sys.path.insert( 0, os.path.join(os.path.dirname(os.path.abspath(__file__)), os.path.pardir, os.path.pardir)) from dbeurive.imap.config import Config from dbeurive.imap.client import Client data_path: str = os.path.join(os.path.dirname(os.path.abspath(__file__)), os.path.pardir, 'data') config_path_clear: str = os.path.join(data_path, 'isp.yaml') config = Config.get_conf_from_file(config_path_clear) def dump_emails_ids(emails_ids: Tuple[str, List[bytes]], dir_path: str, file_name: str) -> bool: file_path = os.path.join(dir_path, file_name) with open(file_path, mode='wb') as fd: pickle.dump(emails_ids, fd) isp: str for isp in config.get_isps(): hostname = config.get_hostname(isp) port = config.get_port(isp) username = config.get_user_login(isp) password = config.get_user_password(isp)
def test_cypher2(self): clear_conf = Config.get_conf_from_file(config_path) str_cyphered_conf: bytes = Config.cypher(clear_conf.dump()) decrypted_conf = Config.get_conf_from_string(str_cyphered_conf, False) self.assertTrue(Config.cmp_conf(clear_conf.get_conf(), decrypted_conf.get_conf()))
import yaml import os import sys from typing import Mapping, List, Any sys.path.insert( 0, os.path.join(os.path.dirname(os.path.abspath(__file__)), os.path.pardir, os.path.pardir)) from dbeurive.imap.config import Config DIR = os.path.dirname(os.path.abspath(__file__)) DEFAULT_CONF = os.path.join(DIR, os.path.pardir, 'data', 'isp.yaml') config_file = DEFAULT_CONF print(f'Configuration file: {config_file}') text = Config.load_conf(config_file) conf: Mapping[str, Mapping[str, Mapping[str, str]]] = yaml.load(text) if Config.validate_conf(conf): print("The configuration is valid.") else: print("The configuration is _NOT_ valid.")
data_path: str = os.path.join(os.path.dirname(os.path.abspath(__file__)), os.path.pardir, 'data') config_path_clear: str = os.path.join(data_path, 'isp.yaml') config_path_clear_backup: str = find_backup_name(data_path, 'isp.yaml.backup') config_path_cyphered: str = os.path.join(data_path, 'isp.yaml.aes') print(f"Decrypt the configuration file:\n\n" f"\t{config_path_cyphered}\n\n" "and store the result into the file\n\n" f"\t{config_path_clear}\n") path = Path(config_path_clear) if path.is_file(): print(f"Make a backup copy of the clear configuration...") try: copyfile(config_path_clear, config_path_clear_backup) except Exception as e: print(f'Cannot backup the configuration file {config_path_clear}: {e}') sys.exit(1) print(f'Done. Path to the backup: "{config_path_clear_backup}"' + "\n") cyphered_conf: Config = Config.get_conf_from_file(config_path_cyphered, False) with open(config_path_clear, 'w') as fd: fd.write(cyphered_conf.dump()) print('Done. Test that we can load it...')
import sys sys.path.insert( 0, os.path.join(os.path.dirname(os.path.abspath(__file__)), os.path.pardir, os.path.pardir)) from dbeurive.imap.config import Config data_path: str = os.path.join(os.path.dirname(os.path.abspath(__file__)), os.path.pardir, 'data') config_path_clear: str = os.path.join(data_path, 'isp.yaml') config_path_cyphered: str = os.path.join(data_path, 'isp.yaml.aes') print(f"Cypher the configuration file:\n\n" f"\t{config_path_clear}\n\n" "and store the result into the file\n\n" f"\t{config_path_cyphered}\n") conf: Config = Config.get_conf_from_file(config_path_clear) cyphered_conf: bytes = Config.cypher(conf.dump()) with open(config_path_cyphered, 'wb') as fd: fd.write(cyphered_conf) print('Done. Test that we can decrypt...') dconf = Config.get_conf_from_file(config_path_cyphered, clear=False) if Config.cmp_conf(conf.get_conf(), dconf.get_conf()): print("OK")