certbot/acme/tests/messages_test.py

499 lines
17 KiB
Python
Raw Permalink Normal View History

2015-06-11 11:00:18 -04:00
"""Tests for acme.messages."""
from typing import Dict
2015-03-27 05:47:03 -04:00
import unittest
from unittest import mock
2015-03-27 05:47:03 -04:00
import josepy as jose
2015-03-27 05:47:03 -04:00
2015-05-10 07:26:21 -04:00
from acme import challenges
import test_util
2015-03-27 05:47:03 -04:00
2015-12-23 13:48:52 -05:00
CERT = test_util.load_comparable_cert('cert.der')
CSR = test_util.load_comparable_csr('csr.der')
2015-07-10 08:26:51 -04:00
KEY = test_util.load_rsa_private_key('rsa512_key.pem')
2015-03-27 05:47:03 -04:00
class ErrorTest(unittest.TestCase):
2015-06-11 11:00:18 -04:00
"""Tests for acme.messages.Error."""
2015-03-27 05:47:03 -04:00
def setUp(self):
from acme.messages import Error, ERROR_PREFIX
Lint certbot code on Python 3, and update Pylint to the latest version (#7551) Part of #7550 This PR makes appropriate corrections to run pylint on Python 3. Why not keeping the dependencies unchanged and just run pylint on Python 3? Because the old version of pylint breaks horribly on Python 3 because of unsupported version of astroid. Why updating pylint + astroid to the latest version ? Because this version only fixes some internal errors occuring during the lint of Certbot code, and is also ready to run gracefully on Python 3.8. Why upgrading mypy ? Because the old version does not support the new version of astroid required to run pylint correctly. Why not upgrading mypy to its latest version ? Because this latest version includes a new typshed version, that adds a lot of new type definitions, and brings dozens of new errors on the Certbot codebase. I would like to fix that in a future PR. That said so, the work has been to find the correct set of new dependency versions, then configure pylint for sane configuration errors in our situation, disable irrelevant lintings errors, then fixing (or ignoring for good reason) the remaining mypy errors. I also made PyLint and MyPy checks run correctly on Windows. * Start configuration * Reconfigure travis * Suspend a check specific to python 3. Start fixing code. * Repair call_args * Fix return + elif lints * Reconfigure development to run mainly on python3 * Remove incompatible Python 3.4 jobs * Suspend pylint in some assertions * Remove pylint in dev * Take first mypy that supports typed-ast>=1.4.0 to limit the migration path * Various return + else lint errors * Find a set of deps that is working with current mypy version * Update local oldest requirements * Remove all current pylint errors * Rebuild letsencrypt-auto * Update mypy to fix pylint with new astroid version, and fix mypy issues * Explain type: ignore * Reconfigure tox, fix none path * Simplify pinning * Remove useless directive * Remove debugging code * Remove continue * Update requirements * Disable unsubscriptable-object check * Disable one check, enabling two more * Plug certbot dev version for oldest requirements * Remove useless disable directives * Remove useless no-member disable * Remove no-else-* checks. Use elif in symetric branches. * Add back assertion * Add new line * Remove unused pylint disable * Remove other pylint disable
2019-12-10 17:12:50 -05:00
self.error = Error.with_code('malformed', detail='foo', title='title')
self.jobj = {
'detail': 'foo',
'title': 'some title',
'type': ERROR_PREFIX + 'malformed',
}
self.error_custom = Error(typ='custom', detail='bar')
self.empty_error = Error()
2015-03-27 05:47:03 -04:00
def test_default_typ(self):
from acme.messages import Error
self.assertEqual(Error().typ, 'about:blank')
def test_from_json_empty(self):
from acme.messages import Error
self.assertEqual(Error(), Error.from_json('{}'))
def test_from_json_hashable(self):
2015-06-11 11:00:18 -04:00
from acme.messages import Error
hash(Error.from_json(self.error.to_json()))
2015-03-27 05:47:03 -04:00
def test_description(self):
Lint certbot code on Python 3, and update Pylint to the latest version (#7551) Part of #7550 This PR makes appropriate corrections to run pylint on Python 3. Why not keeping the dependencies unchanged and just run pylint on Python 3? Because the old version of pylint breaks horribly on Python 3 because of unsupported version of astroid. Why updating pylint + astroid to the latest version ? Because this version only fixes some internal errors occuring during the lint of Certbot code, and is also ready to run gracefully on Python 3.8. Why upgrading mypy ? Because the old version does not support the new version of astroid required to run pylint correctly. Why not upgrading mypy to its latest version ? Because this latest version includes a new typshed version, that adds a lot of new type definitions, and brings dozens of new errors on the Certbot codebase. I would like to fix that in a future PR. That said so, the work has been to find the correct set of new dependency versions, then configure pylint for sane configuration errors in our situation, disable irrelevant lintings errors, then fixing (or ignoring for good reason) the remaining mypy errors. I also made PyLint and MyPy checks run correctly on Windows. * Start configuration * Reconfigure travis * Suspend a check specific to python 3. Start fixing code. * Repair call_args * Fix return + elif lints * Reconfigure development to run mainly on python3 * Remove incompatible Python 3.4 jobs * Suspend pylint in some assertions * Remove pylint in dev * Take first mypy that supports typed-ast>=1.4.0 to limit the migration path * Various return + else lint errors * Find a set of deps that is working with current mypy version * Update local oldest requirements * Remove all current pylint errors * Rebuild letsencrypt-auto * Update mypy to fix pylint with new astroid version, and fix mypy issues * Explain type: ignore * Reconfigure tox, fix none path * Simplify pinning * Remove useless directive * Remove debugging code * Remove continue * Update requirements * Disable unsubscriptable-object check * Disable one check, enabling two more * Plug certbot dev version for oldest requirements * Remove useless disable directives * Remove useless no-member disable * Remove no-else-* checks. Use elif in symetric branches. * Add back assertion * Add new line * Remove unused pylint disable * Remove other pylint disable
2019-12-10 17:12:50 -05:00
self.assertEqual('The request message was malformed', self.error.description)
self.assertTrue(self.error_custom.description is None)
2015-04-18 01:48:32 -04:00
def test_code(self):
from acme.messages import Error
self.assertEqual('malformed', self.error.code)
self.assertEqual(None, self.error_custom.code)
self.assertEqual(None, Error().code)
def test_is_acme_error(self):
Lint certbot code on Python 3, and update Pylint to the latest version (#7551) Part of #7550 This PR makes appropriate corrections to run pylint on Python 3. Why not keeping the dependencies unchanged and just run pylint on Python 3? Because the old version of pylint breaks horribly on Python 3 because of unsupported version of astroid. Why updating pylint + astroid to the latest version ? Because this version only fixes some internal errors occuring during the lint of Certbot code, and is also ready to run gracefully on Python 3.8. Why upgrading mypy ? Because the old version does not support the new version of astroid required to run pylint correctly. Why not upgrading mypy to its latest version ? Because this latest version includes a new typshed version, that adds a lot of new type definitions, and brings dozens of new errors on the Certbot codebase. I would like to fix that in a future PR. That said so, the work has been to find the correct set of new dependency versions, then configure pylint for sane configuration errors in our situation, disable irrelevant lintings errors, then fixing (or ignoring for good reason) the remaining mypy errors. I also made PyLint and MyPy checks run correctly on Windows. * Start configuration * Reconfigure travis * Suspend a check specific to python 3. Start fixing code. * Repair call_args * Fix return + elif lints * Reconfigure development to run mainly on python3 * Remove incompatible Python 3.4 jobs * Suspend pylint in some assertions * Remove pylint in dev * Take first mypy that supports typed-ast>=1.4.0 to limit the migration path * Various return + else lint errors * Find a set of deps that is working with current mypy version * Update local oldest requirements * Remove all current pylint errors * Rebuild letsencrypt-auto * Update mypy to fix pylint with new astroid version, and fix mypy issues * Explain type: ignore * Reconfigure tox, fix none path * Simplify pinning * Remove useless directive * Remove debugging code * Remove continue * Update requirements * Disable unsubscriptable-object check * Disable one check, enabling two more * Plug certbot dev version for oldest requirements * Remove useless disable directives * Remove useless no-member disable * Remove no-else-* checks. Use elif in symetric branches. * Add back assertion * Add new line * Remove unused pylint disable * Remove other pylint disable
2019-12-10 17:12:50 -05:00
from acme.messages import is_acme_error, Error
self.assertTrue(is_acme_error(self.error))
self.assertFalse(is_acme_error(self.error_custom))
Lint certbot code on Python 3, and update Pylint to the latest version (#7551) Part of #7550 This PR makes appropriate corrections to run pylint on Python 3. Why not keeping the dependencies unchanged and just run pylint on Python 3? Because the old version of pylint breaks horribly on Python 3 because of unsupported version of astroid. Why updating pylint + astroid to the latest version ? Because this version only fixes some internal errors occuring during the lint of Certbot code, and is also ready to run gracefully on Python 3.8. Why upgrading mypy ? Because the old version does not support the new version of astroid required to run pylint correctly. Why not upgrading mypy to its latest version ? Because this latest version includes a new typshed version, that adds a lot of new type definitions, and brings dozens of new errors on the Certbot codebase. I would like to fix that in a future PR. That said so, the work has been to find the correct set of new dependency versions, then configure pylint for sane configuration errors in our situation, disable irrelevant lintings errors, then fixing (or ignoring for good reason) the remaining mypy errors. I also made PyLint and MyPy checks run correctly on Windows. * Start configuration * Reconfigure travis * Suspend a check specific to python 3. Start fixing code. * Repair call_args * Fix return + elif lints * Reconfigure development to run mainly on python3 * Remove incompatible Python 3.4 jobs * Suspend pylint in some assertions * Remove pylint in dev * Take first mypy that supports typed-ast>=1.4.0 to limit the migration path * Various return + else lint errors * Find a set of deps that is working with current mypy version * Update local oldest requirements * Remove all current pylint errors * Rebuild letsencrypt-auto * Update mypy to fix pylint with new astroid version, and fix mypy issues * Explain type: ignore * Reconfigure tox, fix none path * Simplify pinning * Remove useless directive * Remove debugging code * Remove continue * Update requirements * Disable unsubscriptable-object check * Disable one check, enabling two more * Plug certbot dev version for oldest requirements * Remove useless disable directives * Remove useless no-member disable * Remove no-else-* checks. Use elif in symetric branches. * Add back assertion * Add new line * Remove unused pylint disable * Remove other pylint disable
2019-12-10 17:12:50 -05:00
self.assertFalse(is_acme_error(Error()))
self.assertFalse(is_acme_error(self.empty_error))
self.assertFalse(is_acme_error("must pet all the {dogs|rabbits}"))
def test_unicode_error(self):
Lint certbot code on Python 3, and update Pylint to the latest version (#7551) Part of #7550 This PR makes appropriate corrections to run pylint on Python 3. Why not keeping the dependencies unchanged and just run pylint on Python 3? Because the old version of pylint breaks horribly on Python 3 because of unsupported version of astroid. Why updating pylint + astroid to the latest version ? Because this version only fixes some internal errors occuring during the lint of Certbot code, and is also ready to run gracefully on Python 3.8. Why upgrading mypy ? Because the old version does not support the new version of astroid required to run pylint correctly. Why not upgrading mypy to its latest version ? Because this latest version includes a new typshed version, that adds a lot of new type definitions, and brings dozens of new errors on the Certbot codebase. I would like to fix that in a future PR. That said so, the work has been to find the correct set of new dependency versions, then configure pylint for sane configuration errors in our situation, disable irrelevant lintings errors, then fixing (or ignoring for good reason) the remaining mypy errors. I also made PyLint and MyPy checks run correctly on Windows. * Start configuration * Reconfigure travis * Suspend a check specific to python 3. Start fixing code. * Repair call_args * Fix return + elif lints * Reconfigure development to run mainly on python3 * Remove incompatible Python 3.4 jobs * Suspend pylint in some assertions * Remove pylint in dev * Take first mypy that supports typed-ast>=1.4.0 to limit the migration path * Various return + else lint errors * Find a set of deps that is working with current mypy version * Update local oldest requirements * Remove all current pylint errors * Rebuild letsencrypt-auto * Update mypy to fix pylint with new astroid version, and fix mypy issues * Explain type: ignore * Reconfigure tox, fix none path * Simplify pinning * Remove useless directive * Remove debugging code * Remove continue * Update requirements * Disable unsubscriptable-object check * Disable one check, enabling two more * Plug certbot dev version for oldest requirements * Remove useless disable directives * Remove useless no-member disable * Remove no-else-* checks. Use elif in symetric branches. * Add back assertion * Add new line * Remove unused pylint disable * Remove other pylint disable
2019-12-10 17:12:50 -05:00
from acme.messages import Error, is_acme_error
arabic_error = Error.with_code(
'malformed', detail=u'\u0639\u062f\u0627\u0644\u0629', title='title')
self.assertTrue(is_acme_error(arabic_error))
def test_with_code(self):
from acme.messages import Error, is_acme_error
self.assertTrue(is_acme_error(Error.with_code('badCSR')))
self.assertRaises(ValueError, Error.with_code, 'not an ACME error code')
def test_str(self):
self.assertEqual(
str(self.error),
u"{0.typ} :: {0.description} :: {0.detail} :: {0.title}"
.format(self.error))
2015-03-27 05:47:03 -04:00
class ConstantTest(unittest.TestCase):
2015-06-11 11:00:18 -04:00
"""Tests for acme.messages._Constant."""
2015-03-27 05:47:03 -04:00
def setUp(self):
2015-06-11 11:00:18 -04:00
from acme.messages import _Constant
2015-09-06 05:19:26 -04:00
2015-03-27 05:47:03 -04:00
class MockConstant(_Constant): # pylint: disable=missing-docstring
POSSIBLE_NAMES: Dict = {}
2015-03-27 05:47:03 -04:00
self.MockConstant = MockConstant # pylint: disable=invalid-name
self.const_a = MockConstant('a')
self.const_b = MockConstant('b')
def test_to_partial_json(self):
self.assertEqual('a', self.const_a.to_partial_json())
self.assertEqual('b', self.const_b.to_partial_json())
2015-03-27 05:47:03 -04:00
def test_from_json(self):
self.assertEqual(self.const_a, self.MockConstant.from_json('a'))
self.assertRaises(
jose.DeserializationError, self.MockConstant.from_json, 'c')
2015-04-18 01:48:32 -04:00
def test_from_json_hashable(self):
hash(self.MockConstant.from_json('a'))
2015-03-27 05:47:03 -04:00
def test_repr(self):
self.assertEqual('MockConstant(a)', repr(self.const_a))
self.assertEqual('MockConstant(b)', repr(self.const_b))
2015-04-27 15:42:50 -04:00
def test_equality(self):
2015-04-27 17:59:44 -04:00
const_a_prime = self.MockConstant('a')
self.assertNotEqual(self.const_a, self.const_b)
self.assertEqual(self.const_a, const_a_prime)
2015-04-27 15:42:50 -04:00
self.assertNotEqual(self.const_a, self.const_b)
self.assertEqual(self.const_a, const_a_prime)
2015-03-27 05:47:03 -04:00
2015-09-08 16:37:09 -04:00
class DirectoryTest(unittest.TestCase):
"""Tests for acme.messages.Directory."""
def setUp(self):
from acme.messages import Directory
self.dir = Directory({
'new-reg': 'reg',
mock.MagicMock(resource_type='new-cert'): 'cert',
2016-04-06 02:14:31 -04:00
'meta': Directory.Meta(
terms_of_service='https://example.com/acme/terms',
website='https://www.example.com/',
caa_identities=['example.com'],
),
2015-09-08 16:37:09 -04:00
})
def test_init_wrong_key_value_success(self): # pylint: disable=no-self-use
2015-09-08 16:37:09 -04:00
from acme.messages import Directory
Directory({'foo': 'bar'})
2015-09-08 16:37:09 -04:00
def test_getitem(self):
self.assertEqual('reg', self.dir['new-reg'])
from acme.messages import NewRegistration
self.assertEqual('reg', self.dir[NewRegistration])
self.assertEqual('reg', self.dir[NewRegistration()])
def test_getitem_fails_with_key_error(self):
self.assertRaises(KeyError, self.dir.__getitem__, 'foo')
def test_getattr(self):
self.assertEqual('reg', self.dir.new_reg)
def test_getattr_fails_with_attribute_error(self):
self.assertRaises(AttributeError, self.dir.__getattr__, 'foo')
2016-04-06 02:14:31 -04:00
def test_to_json(self):
self.assertEqual(self.dir.to_json(), {
'new-reg': 'reg',
'new-cert': 'cert',
'meta': {
'terms-of-service': 'https://example.com/acme/terms',
'website': 'https://www.example.com/',
'caaIdentities': ['example.com'],
2016-04-06 02:14:31 -04:00
},
})
2015-09-08 16:37:09 -04:00
def test_from_json_deserialization_unknown_key_success(self): # pylint: disable=no-self-use
2015-09-08 16:37:09 -04:00
from acme.messages import Directory
Directory.from_json({'foo': 'bar'})
2015-09-08 16:37:09 -04:00
def test_iter_meta(self):
result = False
for k in self.dir.meta:
if k == 'terms_of_service':
result = self.dir.meta[k] == 'https://example.com/acme/terms'
self.assertTrue(result)
2015-09-08 16:37:09 -04:00
WIP External Account Binding (#6059) * Rough draft of External Account Binding. * Remove parameter --eab and namespace kid and hmac. Also add parameters to "register" subcommand. * Refactor as much as possible of the EAB functionality into ExternalAccountBinding class. * Remove debug line. * Added external account binding to Directory.Meta. * Rename to account_public_key, hmac_key and make some non-optional. Rename command line argument to --eab-hmac-key. * Error out when the server requires External Account Binding and the user has not supplied kid and hmac key. * Remove whitespace. * Refactor a bit to make it possible to set the url argument. * Move from_data method into client. * Revert "Move from_data method into client." This reverts commit 8963fae * Refactored to use json field on Registration. * Inherit from object according to Google Python Style Guide. * Move to two separate ifs. * Get tests to pass after External Account Binding additions. * messages.py back to 100% test coverage with some EAB tests. * .encode() this JSON key. * Set eab parameter default values to None. * * Remove unnecessary public key mock on most of the test. * Restructure the directory mock to be able to mock both True and False for externalAccountRequired easily. * Add EAB client tests. * Move external_account_required check into BackwardsCompatibleClientV2 to be able to mock it. * Update versions. * Try 0.29.0. * Revert "Try 0.29.0." This reverts commit 5779509 * Try 0.29.0 again. * Try this. * Fix pylint failures. * Add tests for external_account_required method. * Test not needed, avoid: ************* Module acme.client_test C: 1, 0: Too many lines in module (1258/1250) (too-many-lines) * Move real external_account_required method into ClientV2 and pass through to it in BackwardsCompatibleClientV2. * Handle missing meta key in server ACME directory. * Add docstring for BackwardsCompatibleClientV2.external_account_required(). * Add tests for BackwardsCompatibleClientV2.external_account_required(). * Fix coverage for ACMEv1 code in BackwardsCompatibleClientV2. * Disable pylint too-many-lines check for client_test.py. * Fix versions. * Remove whitespace that accidently snuck into an earlier commit. * Remove these two stray whitespaces also. * And the last couple of whitespaces. * Add External Account Binding to changelog. * Add dev0 suffix to setup.py. Co-Authored-By: robaman <robert@kastel.se> * Set to "-e acme[dev]" again. Co-Authored-By: robaman <robert@kastel.se>
2018-12-03 18:27:35 -05:00
class ExternalAccountBindingTest(unittest.TestCase):
def setUp(self):
from acme.messages import Directory
self.key = jose.jwk.JWKRSA(key=KEY.public_key())
self.kid = "kid-for-testing"
self.hmac_key = "hmac-key-for-testing"
self.dir = Directory({
'newAccount': 'http://url/acme/new-account',
})
def test_from_data(self):
from acme.messages import ExternalAccountBinding
eab = ExternalAccountBinding.from_data(self.key, self.kid, self.hmac_key, self.dir)
self.assertEqual(len(eab), 3)
self.assertEqual(sorted(eab.keys()), sorted(['protected', 'payload', 'signature']))
class RegistrationTest(unittest.TestCase):
2015-06-11 11:00:18 -04:00
"""Tests for acme.messages.Registration."""
def setUp(self):
key = jose.jwk.JWKRSA(key=KEY.public_key())
contact = (
'mailto:admin@foo.com',
'tel:1234',
)
agreement = 'https://letsencrypt.org/terms'
2015-06-11 11:00:18 -04:00
from acme.messages import Registration
2015-07-25 07:53:37 -04:00
self.reg = Registration(key=key, contact=contact, agreement=agreement)
self.reg_none = Registration()
self.jobj_to = {
'contact': contact,
'agreement': agreement,
'key': key,
}
self.jobj_from = self.jobj_to.copy()
self.jobj_from['key'] = key.to_json()
def test_from_data(self):
from acme.messages import Registration
reg = Registration.from_data(phone='1234', email='admin@foo.com')
self.assertEqual(reg.contact, (
'tel:1234',
'mailto:admin@foo.com',
))
WIP External Account Binding (#6059) * Rough draft of External Account Binding. * Remove parameter --eab and namespace kid and hmac. Also add parameters to "register" subcommand. * Refactor as much as possible of the EAB functionality into ExternalAccountBinding class. * Remove debug line. * Added external account binding to Directory.Meta. * Rename to account_public_key, hmac_key and make some non-optional. Rename command line argument to --eab-hmac-key. * Error out when the server requires External Account Binding and the user has not supplied kid and hmac key. * Remove whitespace. * Refactor a bit to make it possible to set the url argument. * Move from_data method into client. * Revert "Move from_data method into client." This reverts commit 8963fae * Refactored to use json field on Registration. * Inherit from object according to Google Python Style Guide. * Move to two separate ifs. * Get tests to pass after External Account Binding additions. * messages.py back to 100% test coverage with some EAB tests. * .encode() this JSON key. * Set eab parameter default values to None. * * Remove unnecessary public key mock on most of the test. * Restructure the directory mock to be able to mock both True and False for externalAccountRequired easily. * Add EAB client tests. * Move external_account_required check into BackwardsCompatibleClientV2 to be able to mock it. * Update versions. * Try 0.29.0. * Revert "Try 0.29.0." This reverts commit 5779509 * Try 0.29.0 again. * Try this. * Fix pylint failures. * Add tests for external_account_required method. * Test not needed, avoid: ************* Module acme.client_test C: 1, 0: Too many lines in module (1258/1250) (too-many-lines) * Move real external_account_required method into ClientV2 and pass through to it in BackwardsCompatibleClientV2. * Handle missing meta key in server ACME directory. * Add docstring for BackwardsCompatibleClientV2.external_account_required(). * Add tests for BackwardsCompatibleClientV2.external_account_required(). * Fix coverage for ACMEv1 code in BackwardsCompatibleClientV2. * Disable pylint too-many-lines check for client_test.py. * Fix versions. * Remove whitespace that accidently snuck into an earlier commit. * Remove these two stray whitespaces also. * And the last couple of whitespaces. * Add External Account Binding to changelog. * Add dev0 suffix to setup.py. Co-Authored-By: robaman <robert@kastel.se> * Set to "-e acme[dev]" again. Co-Authored-By: robaman <robert@kastel.se>
2018-12-03 18:27:35 -05:00
def test_new_registration_from_data_with_eab(self):
from acme.messages import NewRegistration, ExternalAccountBinding, Directory
key = jose.jwk.JWKRSA(key=KEY.public_key())
kid = "kid-for-testing"
hmac_key = "hmac-key-for-testing"
directory = Directory({
'newAccount': 'http://url/acme/new-account',
})
eab = ExternalAccountBinding.from_data(key, kid, hmac_key, directory)
reg = NewRegistration.from_data(email='admin@foo.com', external_account_binding=eab)
self.assertEqual(reg.contact, (
'mailto:admin@foo.com',
))
self.assertEqual(sorted(reg.external_account_binding.keys()),
sorted(['protected', 'payload', 'signature']))
def test_phones(self):
self.assertEqual(('1234',), self.reg.phones)
def test_emails(self):
self.assertEqual(('admin@foo.com',), self.reg.emails)
def test_to_partial_json(self):
self.assertEqual(self.jobj_to, self.reg.to_partial_json())
def test_from_json(self):
2015-06-11 11:00:18 -04:00
from acme.messages import Registration
self.assertEqual(self.reg, Registration.from_json(self.jobj_from))
def test_from_json_hashable(self):
2015-06-11 11:00:18 -04:00
from acme.messages import Registration
hash(Registration.from_json(self.jobj_from))
Support Register Unsafely in Update (#8212) * Allow user to remove email using update command Fixes #3162. Slight change to control flow to replace current email addresses with an empty list. Also add appropriate result message when an email is removed. * Update ACME to allow update to remove fields - New field type "UnFalseyField" that treats all non-None fields as non-empty - Contact changed to new field type to allow sending of empty contact field - Certbot update adjusted to use tuple instead of None when empty - Test updated to check more logic - Unrelated type hint added to keep pycharm gods happy * Moved some mocks into decorators * Restore default to `contact` but do not serialize - Add `to_partial_json` and `fields_to_partial_json` to Registration - Store private variable noting if the value of the `contact` field was provided by the user. - Change message when updating without email to reflect removal of all contact info. - Add note in changelog that `update_account` with the `--register-unsafely-without-email` flag will remove contact from an account. * Reverse logic for field handling on serialization Now forcably add contact when serilizing, but go back to base `jose` field type. * Responding to Review - change out of date name - update several comments - update `from_data` function of `Registration` - Update test to remove superfluous mock * Responding to review - Change comments to make from_data more clear - Remove code worried about None (omitempty has got my back) - Update test to be more reliable - Add typing import with comment to avoid pylint bug
2020-08-26 18:22:51 -04:00
def test_default_not_transmitted(self):
from acme.messages import NewRegistration
empty_new_reg = NewRegistration()
new_reg_with_contact = NewRegistration(contact=())
self.assertEqual(empty_new_reg.contact, ())
self.assertEqual(new_reg_with_contact.contact, ())
self.assertTrue('contact' not in empty_new_reg.to_partial_json())
self.assertTrue('contact' not in empty_new_reg.fields_to_partial_json())
self.assertTrue('contact' in new_reg_with_contact.to_partial_json())
self.assertTrue('contact' in new_reg_with_contact.fields_to_partial_json())
class UpdateRegistrationTest(unittest.TestCase):
"""Tests for acme.messages.UpdateRegistration."""
def test_empty(self):
from acme.messages import UpdateRegistration
jstring = '{"resource": "reg"}'
self.assertEqual(jstring, UpdateRegistration().json_dumps())
self.assertEqual(
UpdateRegistration(), UpdateRegistration.json_loads(jstring))
class RegistrationResourceTest(unittest.TestCase):
"""Tests for acme.messages.RegistrationResource."""
def setUp(self):
from acme.messages import RegistrationResource
self.regr = RegistrationResource(
body=mock.sentinel.body, uri=mock.sentinel.uri,
terms_of_service=mock.sentinel.terms_of_service)
def test_to_partial_json(self):
self.assertEqual(self.regr.to_json(), {
'body': mock.sentinel.body,
'uri': mock.sentinel.uri,
'terms_of_service': mock.sentinel.terms_of_service,
})
2015-03-27 05:47:03 -04:00
class ChallengeResourceTest(unittest.TestCase):
2015-06-11 11:00:18 -04:00
"""Tests for acme.messages.ChallengeResource."""
2015-03-27 05:47:03 -04:00
def test_uri(self):
2015-06-11 11:00:18 -04:00
from acme.messages import ChallengeResource
2015-03-27 05:47:03 -04:00
self.assertEqual('http://challb', ChallengeResource(body=mock.MagicMock(
2015-03-27 06:33:07 -04:00
uri='http://challb'), authzr_uri='http://authz').uri)
2015-03-27 05:47:03 -04:00
class ChallengeBodyTest(unittest.TestCase):
2015-06-11 11:00:18 -04:00
"""Tests for acme.messages.ChallengeBody."""
2015-03-27 05:47:03 -04:00
def setUp(self):
2015-08-22 11:41:37 -04:00
self.chall = challenges.DNS(token=jose.b64decode(
'evaGxfADs6pSRb2LAv9IZf17Dt3juxGJ-PCt92wr-oA'))
2015-03-27 05:47:03 -04:00
2015-06-11 11:00:18 -04:00
from acme.messages import ChallengeBody
from acme.messages import Error
from acme.messages import STATUS_INVALID
self.status = STATUS_INVALID
Lint certbot code on Python 3, and update Pylint to the latest version (#7551) Part of #7550 This PR makes appropriate corrections to run pylint on Python 3. Why not keeping the dependencies unchanged and just run pylint on Python 3? Because the old version of pylint breaks horribly on Python 3 because of unsupported version of astroid. Why updating pylint + astroid to the latest version ? Because this version only fixes some internal errors occuring during the lint of Certbot code, and is also ready to run gracefully on Python 3.8. Why upgrading mypy ? Because the old version does not support the new version of astroid required to run pylint correctly. Why not upgrading mypy to its latest version ? Because this latest version includes a new typshed version, that adds a lot of new type definitions, and brings dozens of new errors on the Certbot codebase. I would like to fix that in a future PR. That said so, the work has been to find the correct set of new dependency versions, then configure pylint for sane configuration errors in our situation, disable irrelevant lintings errors, then fixing (or ignoring for good reason) the remaining mypy errors. I also made PyLint and MyPy checks run correctly on Windows. * Start configuration * Reconfigure travis * Suspend a check specific to python 3. Start fixing code. * Repair call_args * Fix return + elif lints * Reconfigure development to run mainly on python3 * Remove incompatible Python 3.4 jobs * Suspend pylint in some assertions * Remove pylint in dev * Take first mypy that supports typed-ast>=1.4.0 to limit the migration path * Various return + else lint errors * Find a set of deps that is working with current mypy version * Update local oldest requirements * Remove all current pylint errors * Rebuild letsencrypt-auto * Update mypy to fix pylint with new astroid version, and fix mypy issues * Explain type: ignore * Reconfigure tox, fix none path * Simplify pinning * Remove useless directive * Remove debugging code * Remove continue * Update requirements * Disable unsubscriptable-object check * Disable one check, enabling two more * Plug certbot dev version for oldest requirements * Remove useless disable directives * Remove useless no-member disable * Remove no-else-* checks. Use elif in symetric branches. * Add back assertion * Add new line * Remove unused pylint disable * Remove other pylint disable
2019-12-10 17:12:50 -05:00
error = Error.with_code('serverInternal', detail='Unable to communicate with DNS server')
2015-03-27 05:47:03 -04:00
self.challb = ChallengeBody(
uri='http://challb', chall=self.chall, status=self.status,
error=error)
2015-03-27 05:47:03 -04:00
self.jobj_to = {
'uri': 'http://challb',
'status': self.status,
'type': 'dns',
2015-08-22 11:41:37 -04:00
'token': 'evaGxfADs6pSRb2LAv9IZf17Dt3juxGJ-PCt92wr-oA',
'error': error,
2015-03-27 05:47:03 -04:00
}
self.jobj_from = self.jobj_to.copy()
self.jobj_from['status'] = 'invalid'
self.jobj_from['error'] = {
'type': 'urn:ietf:params:acme:error:serverInternal',
'detail': 'Unable to communicate with DNS server',
}
def test_encode(self):
self.assertEqual(self.challb.encode('uri'), self.challb.uri)
def test_to_partial_json(self):
self.assertEqual(self.jobj_to, self.challb.to_partial_json())
2015-03-27 05:47:03 -04:00
2015-04-18 01:48:32 -04:00
def test_from_json(self):
2015-06-11 11:00:18 -04:00
from acme.messages import ChallengeBody
2015-03-27 05:47:03 -04:00
self.assertEqual(self.challb, ChallengeBody.from_json(self.jobj_from))
2015-04-18 01:48:32 -04:00
def test_from_json_hashable(self):
2015-06-11 11:00:18 -04:00
from acme.messages import ChallengeBody
2015-04-18 01:48:32 -04:00
hash(ChallengeBody.from_json(self.jobj_from))
def test_proxy(self):
2015-08-22 11:41:37 -04:00
self.assertEqual(jose.b64decode(
'evaGxfADs6pSRb2LAv9IZf17Dt3juxGJ-PCt92wr-oA'), self.challb.token)
2015-03-27 05:47:03 -04:00
class AuthorizationTest(unittest.TestCase):
2015-06-11 11:00:18 -04:00
"""Tests for acme.messages.Authorization."""
2015-03-27 05:47:03 -04:00
def setUp(self):
2015-06-11 11:00:18 -04:00
from acme.messages import ChallengeBody
from acme.messages import STATUS_VALID
2015-09-28 18:24:51 -04:00
2015-03-27 05:47:03 -04:00
self.challbs = (
ChallengeBody(
uri='http://challb1', status=STATUS_VALID,
2015-10-28 16:41:15 -04:00
chall=challenges.HTTP01(token=b'IlirfxKKXAsHtmzK29Pj8A')),
2015-03-27 05:47:03 -04:00
ChallengeBody(uri='http://challb2', status=STATUS_VALID,
2015-07-31 18:45:48 -04:00
chall=challenges.DNS(
token=b'DGyRejmCefe7v4NfDGDKfA')),
2015-03-27 05:47:03 -04:00
)
2016-03-07 21:42:44 -05:00
combinations = ((0,), (1,))
2015-03-27 05:47:03 -04:00
2015-06-11 11:00:18 -04:00
from acme.messages import Authorization
from acme.messages import Identifier
from acme.messages import IDENTIFIER_FQDN
2015-03-27 05:47:03 -04:00
identifier = Identifier(typ=IDENTIFIER_FQDN, value='example.com')
self.authz = Authorization(
identifier=identifier, combinations=combinations,
challenges=self.challbs)
self.jobj_from = {
'identifier': identifier.to_json(),
'challenges': [challb.to_json() for challb in self.challbs],
2015-03-27 05:47:03 -04:00
'combinations': combinations,
}
def test_from_json(self):
2015-06-11 11:00:18 -04:00
from acme.messages import Authorization
2015-03-27 05:47:03 -04:00
Authorization.from_json(self.jobj_from)
2015-04-18 01:48:32 -04:00
def test_from_json_hashable(self):
2015-06-11 11:00:18 -04:00
from acme.messages import Authorization
2015-04-18 01:48:32 -04:00
hash(Authorization.from_json(self.jobj_from))
2015-03-27 05:47:03 -04:00
def test_resolved_combinations(self):
self.assertEqual(self.authz.resolved_combinations, (
2016-03-07 21:42:44 -05:00
(self.challbs[0],),
(self.challbs[1],),
2015-03-27 05:47:03 -04:00
))
class AuthorizationResourceTest(unittest.TestCase):
"""Tests for acme.messages.AuthorizationResource."""
def test_json_de_serializable(self):
from acme.messages import AuthorizationResource
authzr = AuthorizationResource(
uri=mock.sentinel.uri,
body=mock.sentinel.body)
self.assertTrue(isinstance(authzr, jose.JSONDeSerializable))
class CertificateRequestTest(unittest.TestCase):
"""Tests for acme.messages.CertificateRequest."""
def setUp(self):
from acme.messages import CertificateRequest
self.req = CertificateRequest(csr=CSR)
def test_json_de_serializable(self):
self.assertTrue(isinstance(self.req, jose.JSONDeSerializable))
from acme.messages import CertificateRequest
self.assertEqual(
self.req, CertificateRequest.from_json(self.req.to_json()))
class CertificateResourceTest(unittest.TestCase):
"""Tests for acme.messages.CertificateResourceTest."""
def setUp(self):
from acme.messages import CertificateResource
self.certr = CertificateResource(
body=CERT, uri=mock.sentinel.uri, authzrs=(),
cert_chain_uri=mock.sentinel.cert_chain_uri)
def test_json_de_serializable(self):
self.assertTrue(isinstance(self.certr, jose.JSONDeSerializable))
from acme.messages import CertificateResource
self.assertEqual(
self.certr, CertificateResource.from_json(self.certr.to_json()))
2015-03-27 05:47:03 -04:00
class RevocationTest(unittest.TestCase):
2015-06-11 11:00:18 -04:00
"""Tests for acme.messages.RevocationTest."""
2015-03-27 05:47:03 -04:00
2015-06-17 12:13:26 -04:00
def setUp(self):
from acme.messages import Revocation
self.rev = Revocation(certificate=CERT)
2015-03-27 05:47:03 -04:00
2015-04-18 01:48:32 -04:00
def test_from_json_hashable(self):
2015-06-11 11:00:18 -04:00
from acme.messages import Revocation
2015-06-17 12:13:26 -04:00
hash(Revocation.from_json(self.rev.to_json()))
2015-04-18 01:48:32 -04:00
2015-03-27 05:47:03 -04:00
class OrderResourceTest(unittest.TestCase):
"""Tests for acme.messages.OrderResource."""
def setUp(self):
from acme.messages import OrderResource
self.regr = OrderResource(
body=mock.sentinel.body, uri=mock.sentinel.uri)
def test_to_partial_json(self):
self.assertEqual(self.regr.to_json(), {
'body': mock.sentinel.body,
'uri': mock.sentinel.uri,
'authorizations': None,
})
class NewOrderTest(unittest.TestCase):
"""Tests for acme.messages.NewOrder."""
def setUp(self):
from acme.messages import NewOrder
self.reg = NewOrder(
identifiers=mock.sentinel.identifiers)
def test_to_partial_json(self):
self.assertEqual(self.reg.to_json(), {
'identifiers': mock.sentinel.identifiers,
})
class JWSPayloadRFC8555Compliant(unittest.TestCase):
"""Test for RFC8555 compliance of JWS generated from resources/challenges"""
def test_message_payload(self):
from acme.messages import NewAuthorization
new_order = NewAuthorization()
new_order.le_acme_version = 2
jobj = new_order.json_dumps(indent=2).encode()
# RFC8555 states that JWS bodies must not have a resource field.
self.assertEqual(jobj, b'{}')
2015-03-27 05:47:03 -04:00
if __name__ == '__main__':
unittest.main() # pragma: no cover