def test_crypt_file_ok(self): crypt = CryptFernet() crypt.generate_key("mypassword", True) message = "My crypt message" encrypt_message = crypt.encrypt(message) assert message == crypt.decrypt(str(encrypt_message, encoding="utf-8")) crypt.delete_key()
def test_crypt_file_error(self): crypt = CryptFernet() with pytest.raises(FileDoesNotExistException) as excinfo: crypt.read_key() assert ("Decrypt key None not exists. You must set a correct env var PYMS_KEY_FILE or run " "`pyms crypt create-key` command") \ in str(excinfo.value)
def test_encrypt_conf_deprecated_env(self): os.environ[CRYPT_FILE_KEY_ENVIRONMENT_LEGACY] = os.getenv(CRYPT_FILE_KEY_ENVIRONMENT) del os.environ[CRYPT_FILE_KEY_ENVIRONMENT] crypt = CryptFernet(path=self.BASE_DIR) crypt._loader.put_file(b"9IXx2F5d5Ob-h5xdCnFSUXhuFKLGRibvLfSbixpcfCw=", "wb") config = get_conf(service=CONFIG_BASE, uppercase=True, crypt=CryptFernet) crypt.delete_key() assert config.database_url == "http://database-url"
def test_encrypt_conf(self): crypt = CryptFernet(path=self.BASE_DIR) crypt._loader.put_file(b"9IXx2F5d5Ob-h5xdCnFSUXhuFKLGRibvLfSbixpcfCw=", "wb") config = get_conf(service=CONFIG_BASE, uppercase=True, crypt=CryptFernet) crypt.delete_key() assert config.database_url == "http://database-url"
def setUp(self): os.environ[CONFIGMAP_FILE_ENVIRONMENT] = os.path.join( os.path.dirname(os.path.abspath(__file__)), "config-tests-flask-encrypted-fernet.yml" ) os.environ[CRYPT_FILE_KEY_ENVIRONMENT] = os.path.join(self.BASE_DIR, "key.key") self.crypt = CryptFernet(path=self.BASE_DIR) self.crypt._loader.put_file(b"9IXx2F5d5Ob-h5xdCnFSUXhuFKLGRibvLfSbixpcfCw=", "wb") ms = MyMicroserviceNoSingleton(path=__file__) ms.reload_conf() self.app = ms.create_app() self.client = self.app.test_client() self.assertEqual("Python Microservice With Flask encrypted", self.app.config["APP_NAME"])