diff options
author | Dylan Baker <baker.dylan.c@gmail.com> | 2017-08-30 10:53:19 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-08-30 10:53:19 -0700 |
commit | 0a7bf658fdcf906f75bf29c44ef05da65fd1c053 (patch) | |
tree | e65eb1bcd9ce33e114272c47992870b1e33ff21c /alot | |
parent | 9bd32e5b0b253aa80c44e7d54f15e66ceb84a9e8 (diff) | |
parent | e16e846db354e5775c69891c2b58b12e110f79b8 (diff) |
Merge pull request #1136 from dcbaker/submit/more-db-utils-tests
more tests + cleanups
Diffstat (limited to 'alot')
-rw-r--r-- | alot/crypto.py | 41 | ||||
-rw-r--r-- | alot/db/utils.py | 29 |
2 files changed, 33 insertions, 37 deletions
diff --git a/alot/crypto.py b/alot/crypto.py index 52eb8c58..f3dd6d11 100644 --- a/alot/crypto.py +++ b/alot/crypto.py @@ -81,31 +81,22 @@ def get_key(keyid, validate=False, encrypt=False, sign=False, valid_key = None - # Catching exceptions for list_keys - try: - for k in list_keys(hint=keyid): - try: - validate_key(k, encrypt=encrypt, sign=sign) - except GPGProblem: - # if the key is invalid for given action skip it - continue - - if valid_key: - # we have already found one valid key and now we find - # another? We really received an ambiguous keyid - raise GPGProblem( - "More than one key found matching this filter. " - "Please be more specific " - "(use a key ID like 4AC8EE1D).", - code=GPGCode.AMBIGUOUS_NAME) - valid_key = k - except gpg.errors.GPGMEError as e: - # This if will be triggered if there is no key matching at all. - if e.getcode() == gpg.errors.AMBIGUOUS_NAME: + for k in list_keys(hint=keyid): + try: + validate_key(k, encrypt=encrypt, sign=sign) + except GPGProblem: + # if the key is invalid for given action skip it + continue + + if valid_key: + # we have already found one valid key and now we find + # another? We really received an ambiguous keyid raise GPGProblem( - 'Can not find any key for "{}".'.format(keyid), - code=GPGCode.NOT_FOUND) - raise + "More than one key found matching this filter. " + "Please be more specific " + "(use a key ID like 4AC8EE1D).", + code=GPGCode.AMBIGUOUS_NAME) + valid_key = k if not valid_key: # there were multiple keys found but none of them are valid for @@ -120,7 +111,7 @@ def get_key(keyid, validate=False, encrypt=False, sign=False, 'Can not find usable key for "{}".'.format(keyid), code=GPGCode.NOT_FOUND) else: - raise e + raise e # pragma: nocover if signed_only and not check_uid_validity(key, keyid): raise GPGProblem('Cannot find a trusworthy key for "{}".'.format(keyid), code=GPGCode.NOT_FOUND) diff --git a/alot/db/utils.py b/alot/db/utils.py index 488e0ed7..34f86991 100644 --- a/alot/db/utils.py +++ b/alot/db/utils.py @@ -45,6 +45,8 @@ def add_signature_headers(mail, sigs, error_msg): string indicating no error ''' sig_from = u'' + sig_known = True + uid_trusted = False if isinstance(error_msg, str): error_msg = error_msg.decode('utf-8') @@ -60,13 +62,11 @@ def add_signature_headers(mail, sigs, error_msg): uid_trusted = True break else: - # No trusted uid found, we did not break but drop from the - # for loop. - uid_trusted = False + # No trusted uid found, since we did not break from the loop. sig_from = key.uids[0].uid.decode('utf-8') - except: + except GPGProblem: sig_from = sigs[0].fpr.decode('utf-8') - uid_trusted = False + sig_known = False if error_msg: msg = u'Invalid: {}'.format(error_msg) @@ -75,7 +75,8 @@ def add_signature_headers(mail, sigs, error_msg): else: msg = u'Untrusted: {}'.format(sig_from) - mail.add_header(X_SIGNATURE_VALID_HEADER, 'False' if error_msg else 'True') + mail.add_header(X_SIGNATURE_VALID_HEADER, + 'False' if (error_msg or not sig_known) else 'True') mail.add_header(X_SIGNATURE_MESSAGE_HEADER, msg) @@ -297,16 +298,20 @@ def extract_headers(mail, headers=None): def extract_body(mail, types=None, field_key='copiousoutput'): - """ - returns a body text string for given mail. - If types is `None`, `text/*` is used: - The exact preferred type is specified by the prefer_plaintext config option - which defaults to text/html. + """Returns a string view of a Message. + + If the `types` argument is set then any encoding types there will be used + as the prefered encoding to extract. If `types` is None then + :ref:`prefer_plaintext <prefer-plaintext>` will be consulted; if it is True + then text/plain parts will be returned, if it is false then text/html will + be returned if present or text/plain if there are no text/html parts. :param mail: the mail to use :type mail: :class:`email.Message` :param types: mime content types to use for body string - :type types: list of str + :type types: list[str] + :returns: The combined text of any parts to be used + :rtype: str """ preferred = 'text/plain' if settings.get( |