def test_imap_config_values_should_be_stored(): am = AccountManager() option_spec = get_mailbox_parameter_specs('imap') options = { 'user': '******', 'password': '', 'server': 'imap.example.org', 'port': '', 'ssl': True, 'imap': True, 'idle': True, 'folders': ['a', 'b'], } config = RawConfigParser() config.add_section('account1') am._set_cfg_options(config, 'account1', options, option_spec) expected_config_items = [ ('user', 'you'), ('password', ''), ('server', 'imap.example.org'), ('port', ''), ('ssl', '1'), ('imap', '1'), ('idle', '1'), ('folder', '["a", "b"]'), ] assert set(expected_config_items) == set(config.items('account1'))
def test_imap_config_values_should_be_stored(): am = AccountManager() option_spec = get_mailbox_parameter_specs('imap') options = { 'user': '******', 'password': '', 'server': 'imap.example.org', 'port': '', 'ssl': True, 'imap': True, 'idle': True, 'folders': ['a', 'b'], } config = RawConfigParser() config.add_section('account1') am._set_cfg_options(config, 'account1', options, option_spec) expected_config_items = [ ('user', 'you'), ('password', ''), ('server', 'imap.example.org'), ('port', ''), ('ssl', '1'), ('imap', '1'), ('idle', '1'), ('folder', '["a", "b"]'), ] assert set(expected_config_items) == set(config.items('account1'))
def save_to_cfg(self, cfg): # Remove all accounts from cfg i = 1 section_name = "account" + str(i) while cfg.has_section(section_name): cfg.remove_section(section_name) i = i + 1 section_name = "account" + str(i) # Delete secrets of removed accounts from the credential store # (it's important to do this before adding accounts, # in case multiple accounts with the same credential key exist). if self._credentialstore != None: for acc in self._removed: protocol = 'imap' if acc.imap else 'pop' # Note: CredentialStore implementations must check if the key acutally exists! self._credentialstore.remove(CREDENTIAL_KEY % (protocol, acc.user, acc.server)) del self._removed[:] # Add accounts i = 1 for acc in self._accounts: if acc.oauth2string != '': logging.warning( "Saving of OAuth2 based accounts is not supported. Account '%s' skipped." % acc.name) continue section_name = "account" + str(i) cfg.add_section(section_name) cfg.set(section_name, 'enabled', int(acc.enabled)) cfg.set(section_name, 'type', acc.mailbox_type) cfg.set(section_name, 'name', acc.name) config = acc.get_config() option_spec = get_mailbox_parameter_specs(acc.mailbox_type) # TODO: Setting password to credentials is mailbox specific. # Every backend do not have or need password. if self._credentialstore != None: protocol = 'imap' if acc.imap else 'pop' self._credentialstore.set( CREDENTIAL_KEY % (protocol, acc.user, acc.server), acc.password) config['password'] = '' self._set_cfg_options(cfg, section_name, config, option_spec) i = i + 1
def save_to_cfg(self, cfg): # Remove all accounts from cfg i = 1 section_name = "account" + str(i) while cfg.has_section(section_name): cfg.remove_section(section_name) i = i + 1 section_name = "account" + str(i) # Delete secrets of removed accounts from the secretstore # (it's important to do this before adding accounts, # in case multiple accounts with the same id exist). if self._secretstore != None: for acc in self._removed: self._secretstore.remove( self._get_account_id(acc.user, acc.server, acc.imap)) del self._removed[:] # Add accounts i = 1 for acc in self._accounts: if acc.oauth2string != '': logging.warning( "Saving of OAuth2 based accounts is not supported. Account '%s' skipped." % acc.name) continue section_name = "account" + str(i) cfg.add_section(section_name) cfg.set(section_name, 'enabled', int(acc.enabled)) cfg.set(section_name, 'type', acc.mailbox_type) cfg.set(section_name, 'name', acc.name) config = acc.get_config() option_spec = get_mailbox_parameter_specs(acc.mailbox_type) # TODO: Storing a password is mailbox specific. # Not every backend requires a password. if self._secretstore != None: self._secretstore.set( self._get_account_id(acc.user, acc.server, acc.imap), acc.password, f'{PACKAGE_NAME.capitalize()} password for account {acc.user}@{acc.server}' ) config['password'] = '' self._set_cfg_options(cfg, section_name, config, option_spec) i = i + 1
def test_pop3_config_defaults(config): am = AccountManager() option_spec = get_mailbox_parameter_specs('pop3') options = am._get_cfg_options(config, 'account3', option_spec) expected_options = { 'user': '', 'password': '', 'server': '', 'port': '', 'ssl': True, 'imap': False, 'idle': False, } assert expected_options == options
def test_pop3_config_defaults(config): am = AccountManager() option_spec = get_mailbox_parameter_specs('pop3') options = am._get_cfg_options(config, 'account3', option_spec) expected_options = { 'user': '', 'password': '', 'server': '', 'port': '', 'ssl': True, 'imap': False, 'idle': False, } assert expected_options == options
def test_imap_config_options(config): am = AccountManager() option_spec = get_mailbox_parameter_specs('imap') options = am._get_cfg_options(config, 'account1', option_spec) expected_options = { 'user': '******', 'password': '', 'server': 'imap.example.org', 'port': '', 'ssl': True, 'imap': True, 'idle': True, 'folders': [], } assert expected_options == options
def test_imap_config_options(config): am = AccountManager() option_spec = get_mailbox_parameter_specs('imap') options = am._get_cfg_options(config, 'account1', option_spec) expected_options = { 'user': '******', 'password': '******', 'server': 'imap.example.org', 'port': '', 'ssl': True, 'imap': True, 'idle': True, 'folders': [], } assert expected_options == options
def load_from_cfg(self, cfg, enabled_only=False): del self._accounts[:] del self._removed[:] i = 1 section_name = "account" + str(i) while cfg.has_section(section_name): enabled = bool( int(self._get_account_cfg(cfg, section_name, 'enabled'))) if (not enabled_only) or (enabled_only and enabled): if cfg.has_option(section_name, 'type'): mailbox_type = self._get_account_cfg( cfg, section_name, 'type') imap = (mailbox_type == 'imap') else: imap = bool( int(self._get_account_cfg(cfg, section_name, 'imap'))) mailbox_type = 'imap' if imap else 'pop3' name = self._get_account_cfg(cfg, section_name, 'name') option_spec = get_mailbox_parameter_specs(mailbox_type) options = self._get_cfg_options(cfg, section_name, option_spec) # TODO: Getting a password from the secretstore is mailbox specific. # Not every backend requires a password. user = options.get('user') server = options.get('server') # TODO: get rid of the fake server and user names if mailbox_type == "gmail_rss": user = name server = "gmail_rss" imap = True if self._secretstore != None and user and server: password = self._secretstore.get( self._get_account_id(user, server, imap)) if not password: password = '' options['password'] = password acc = Account(enabled=enabled, name=name, mailbox_type=mailbox_type, **options) self._accounts.append(acc) i = i + 1 section_name = "account" + str(i)
def save_to_cfg(self, cfg): # Remove all accounts from cfg i = 1 section_name = "account" + str(i) while cfg.has_section(section_name): cfg.remove_section(section_name) i = i + 1 section_name = "account" + str(i) # Delete secrets of removed accounts from the credential store # (it's important to do this before adding accounts, # in case multiple accounts with the same credential key exist). if self._credentialstore != None: for acc in self._removed: protocol = 'imap' if acc.imap else 'pop' # Note: CredentialStore implementations must check if the key acutally exists! self._credentialstore.remove(CREDENTIAL_KEY % (protocol, acc.user, acc.server)) del self._removed[:] # Add accounts i = 1 for acc in self._accounts: if acc.oauth2string != '': logging.warning("Saving of OAuth2 based accounts is not supported. Account '%s' skipped." % acc.name) continue section_name = "account" + str(i) cfg.add_section(section_name) cfg.set(section_name, 'enabled', int(acc.enabled)) cfg.set(section_name, 'type', acc.mailbox_type) cfg.set(section_name, 'name', acc.name) config = acc.get_config() option_spec = get_mailbox_parameter_specs(acc.mailbox_type) # TODO: Setting password to credentials is mailbox specific. # Every backend do not have or need password. if self._credentialstore != None: protocol = 'imap' if acc.imap else 'pop' self._credentialstore.set(CREDENTIAL_KEY % (protocol, acc.user, acc.server), acc.password) config['password'] = '' self._set_cfg_options(cfg, section_name, config, option_spec) i = i + 1
def load_from_cfg(self, cfg, enabled_only=False): del self._accounts[:] del self._removed[:] i = 1 section_name = "account" + str(i) while cfg.has_section(section_name): enabled = bool( int(self._get_account_cfg(cfg, section_name, 'enabled'))) if (not enabled_only) or (enabled_only and enabled): if cfg.has_option(section_name, 'type'): mailbox_type = self._get_account_cfg( cfg, section_name, 'type') imap = (mailbox_type == 'imap') else: imap = bool( int(self._get_account_cfg(cfg, section_name, 'imap'))) mailbox_type = 'imap' if imap else 'pop3' name = self._get_account_cfg(cfg, section_name, 'name') option_spec = get_mailbox_parameter_specs(mailbox_type) options = self._get_cfg_options(cfg, section_name, option_spec) # TODO: Getting password from credentials is mailbox specific. # Every backend do not have or need password. user = options.get('user') server = options.get('server') if self._credentialstore != None and user and server: protocol = 'imap' if imap else 'pop' password = self._credentialstore.get( CREDENTIAL_KEY % (protocol, user, server)) options['password'] = password acc = Account(enabled=enabled, name=name, mailbox_type=mailbox_type, **options) self._accounts.append(acc) i = i + 1 section_name = "account" + str(i)
def load_from_cfg(self, cfg, enabled_only = False): del self._accounts[:] del self._removed[:] i = 1 section_name = "account" + str(i) while cfg.has_section(section_name): enabled = bool(int( self._get_account_cfg(cfg, section_name, 'enabled') )) if (not enabled_only) or (enabled_only and enabled): if cfg.has_option(section_name, 'type'): mailbox_type = self._get_account_cfg(cfg, section_name, 'type') imap = (mailbox_type == 'imap') else: imap = bool(int(self._get_account_cfg(cfg, section_name, 'imap'))) mailbox_type = 'imap' if imap else 'pop3' name = self._get_account_cfg(cfg, section_name, 'name') option_spec = get_mailbox_parameter_specs(mailbox_type) options = self._get_cfg_options(cfg, section_name, option_spec) # TODO: Getting password from credentials is mailbox specific. # Every backend do not have or need password. user = options.get('user') server = options.get('server') if self._credentialstore != None and user and server: protocol = 'imap' if imap else 'pop' password = self._credentialstore.get(CREDENTIAL_KEY % (protocol, user, server)) options['password'] = password acc = Account(enabled=enabled, name=name, mailbox_type=mailbox_type, **options) self._accounts.append(acc) i = i + 1 section_name = "account" + str(i)
def test_imap_new_folder_option(config): am = AccountManager() option_spec = get_mailbox_parameter_specs('imap') options = am._get_cfg_options(config, 'account6', option_spec) assert options['folders'] == ['folderA', 'folderB', 'folderC']
def test_imap_new_folder_option(config): am = AccountManager() option_spec = get_mailbox_parameter_specs('imap') options = am._get_cfg_options(config, 'account6', option_spec) assert options['folders'] == ['folderA', 'folderB', 'folderC']
def test_pop3_backend_parameter_names(): specs = get_mailbox_parameter_specs('pop3') names = [spec.param_name for spec in specs] assert set(['user', 'password', 'server', 'port', 'ssl', 'imap', 'idle']) == set(names)
def test_pop3_backend_parameter_names(): specs = get_mailbox_parameter_specs('pop3') names = [spec.param_name for spec in specs] assert set(['user', 'password', 'server', 'port', 'ssl', 'imap', 'idle']) == set(names)