From 99ca0ac78df2ecc084d6da9d78ee02a284e4c2a3 Mon Sep 17 00:00:00 2001 From: Mathias Petermann Date: Tue, 10 Nov 2020 15:32:04 +0100 Subject: [PATCH] Add context fix to tests, Add regex, Flake8 --- lemur/plugins/lemur_acme/acme_handlers.py | 4 +--- lemur/plugins/lemur_acme/challenge_types.py | 4 +--- lemur/plugins/lemur_acme/plugin.py | 4 ++-- lemur/plugins/lemur_acme/tests/test_acme_handler.py | 11 +++++++++++ lemur/plugins/lemur_acme/tests/test_acme_http.py | 11 +++++++++++ 5 files changed, 26 insertions(+), 8 deletions(-) diff --git a/lemur/plugins/lemur_acme/acme_handlers.py b/lemur/plugins/lemur_acme/acme_handlers.py index 40de0f77..ebc2b2b4 100644 --- a/lemur/plugins/lemur_acme/acme_handlers.py +++ b/lemur/plugins/lemur_acme/acme_handlers.py @@ -123,9 +123,7 @@ class AcmeHandler(object): current_app.config.get("IDENTRUST_CROSS_SIGNED_LE_ICA_EXPIRATION_DATE", "17/03/21"), '%d/%m/%y'): pem_certificate_chain = current_app.config.get("IDENTRUST_CROSS_SIGNED_LE_ICA") else: - pem_certificate_chain = orderr.fullchain_pem[ - len(pem_certificate): # noqa - ].lstrip() + pem_certificate_chain = orderr.fullchain_pem[len(pem_certificate):].lstrip() current_app.logger.debug( "{0} {1}".format(type(pem_certificate), type(pem_certificate_chain)) diff --git a/lemur/plugins/lemur_acme/challenge_types.py b/lemur/plugins/lemur_acme/challenge_types.py index f87acc41..fd779742 100644 --- a/lemur/plugins/lemur_acme/challenge_types.py +++ b/lemur/plugins/lemur_acme/challenge_types.py @@ -121,9 +121,7 @@ class AcmeHttpChallenge(AcmeChallenge): current_app.config.get("IDENTRUST_CROSS_SIGNED_LE_ICA_EXPIRATION_DATE", "17/03/21"), '%d/%m/%y'): pem_certificate_chain = current_app.config.get("IDENTRUST_CROSS_SIGNED_LE_ICA") else: - pem_certificate_chain = finalized_orderr.fullchain_pem[ - len(pem_certificate): # noqa - ].lstrip() + pem_certificate_chain = finalized_orderr.fullchain_pem[len(pem_certificate):].lstrip() # validation is a random string, we use it as external id, to make it possible to implement revoke_certificate return pem_certificate, pem_certificate_chain, None diff --git a/lemur/plugins/lemur_acme/plugin.py b/lemur/plugins/lemur_acme/plugin.py index 51f6aa3a..7dc7af25 100644 --- a/lemur/plugins/lemur_acme/plugin.py +++ b/lemur/plugins/lemur_acme/plugin.py @@ -309,7 +309,7 @@ class ACMEHttpIssuerPlugin(IssuerPlugin): "name": "acme_url", "type": "str", "required": True, - "validation": "/^http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+$/", + "validation": r"/^http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+$/", "helpMessage": "Must be a valid web url starting with http[s]://", }, { @@ -322,7 +322,7 @@ class ACMEHttpIssuerPlugin(IssuerPlugin): "name": "email", "type": "str", "default": "", - "validation": "/^?([-a-zA-Z0-9.`?{}]+@\w+\.\w+)$/", + "validation": r"/^?([-a-zA-Z0-9.`?{}]+@\w+\.\w+)$/", "helpMessage": "Email to use", }, { diff --git a/lemur/plugins/lemur_acme/tests/test_acme_handler.py b/lemur/plugins/lemur_acme/tests/test_acme_handler.py index d9f4313d..650bf513 100644 --- a/lemur/plugins/lemur_acme/tests/test_acme_handler.py +++ b/lemur/plugins/lemur_acme/tests/test_acme_handler.py @@ -1,6 +1,7 @@ import unittest from unittest.mock import patch, Mock +from flask import Flask from cryptography.x509 import DNSName from lemur.plugins.lemur_acme import acme_handlers @@ -9,6 +10,16 @@ class TestAcmeHandler(unittest.TestCase): def setUp(self): self.acme = acme_handlers.AcmeHandler() + # Creates a new Flask application for a test duration. In python 3.8, manual push of application context is + # needed to run tests in dev environment without getting error 'Working outside of application context'. + _app = Flask('lemur_test_acme') + self.ctx = _app.app_context() + assert self.ctx + self.ctx.push() + + def tearDown(self): + self.ctx.pop() + def test_strip_wildcard(self): expected = ("example.com", False) result = self.acme.strip_wildcard("example.com") diff --git a/lemur/plugins/lemur_acme/tests/test_acme_http.py b/lemur/plugins/lemur_acme/tests/test_acme_http.py index 365bcb5c..786e0eba 100644 --- a/lemur/plugins/lemur_acme/tests/test_acme_http.py +++ b/lemur/plugins/lemur_acme/tests/test_acme_http.py @@ -1,6 +1,7 @@ import unittest from unittest.mock import patch, Mock +from flask import Flask from acme import challenges from lemur.plugins.lemur_acme import plugin @@ -11,6 +12,16 @@ class TestAcmeHttp(unittest.TestCase): self.ACMEHttpIssuerPlugin = plugin.ACMEHttpIssuerPlugin() self.acme = plugin.AcmeHandler() + # Creates a new Flask application for a test duration. In python 3.8, manual push of application context is + # needed to run tests in dev environment without getting error 'Working outside of application context'. + _app = Flask('lemur_test_acme') + self.ctx = _app.app_context() + assert self.ctx + self.ctx.push() + + def tearDown(self): + self.ctx.pop() + @patch("lemur.plugins.lemur_acme.plugin.current_app") def test_create_authority(self, mock_current_app): mock_current_app.config = Mock()