Skip to content

Commit

Permalink
internal improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
amenezes committed Aug 14, 2023
1 parent 60a6ffb commit 53f06df
Show file tree
Hide file tree
Showing 11 changed files with 92 additions and 26 deletions.
1 change: 1 addition & 0 deletions .tool-versions
@@ -0,0 +1 @@
python 3.11.2
4 changes: 4 additions & 0 deletions Makefile
Expand Up @@ -10,6 +10,10 @@ ifeq ($(SKIP_STYLE), )
black config
black tests
endif
@echo "> running bandit"
bandit -r -ll -ii -s B104 config
@echo "> running radon"
radon cc -s -n C config tests
@echo "> running flake8..."
flake8 config
flake8 tests
Expand Down
2 changes: 1 addition & 1 deletion config/__init__.py
Expand Up @@ -3,7 +3,7 @@
from .cfenv import CFenv
from .spring import ConfigClient, config_client, create_config_client

__version__ = "1.3.0"
__version__ = "1.4.0"
__all__ = [
"__version__",
"ConfigClient",
Expand Down
43 changes: 25 additions & 18 deletions config/cli.py
Expand Up @@ -82,16 +82,7 @@ def client(
)

if file:
# get file from server and exit
with Status("Contacting server...", spinner="dots4") as status:
try:
resp = client.get_file(file)
except RequestFailedException:
raise click.ClickException("💥 Failed to contact server!")
Path(file).write_text(resp)
status.update("OK!")
console.print(f"File saved: [cyan]{file}[/cyan]", style="bold")
raise SystemExit
_download_file(file, client)

if verbose:
table = Table.grid(padding=(0, 1))
Expand All @@ -114,14 +105,7 @@ def client(
with Status("Contacting server...", spinner="dots4") as status:
emoji = random.choice(EMOJI_ERRORS)
try:
if auth:
username, password = auth.split(":")
auth = HTTPBasicAuth(username, password)
elif digest:
username, password = digest.split(":")
auth = HTTPDigestAuth(username, password)
else:
auth = None
auth = _configure_auth(auth, digest)
client.get_config(auth=auth)
except ValueError:
raise click.ClickException(
Expand Down Expand Up @@ -160,6 +144,29 @@ def client(
)


def _download_file(file, client):
"""Get file from server and exit."""
with Status("Contacting server...", spinner="dots4") as status:
try:
resp = client.get_file(file)
except RequestFailedException:
raise click.ClickException("💥 Failed to contact server!")
Path(file).write_text(resp)
status.update("OK!")
console.print(f"File saved: [cyan]{file}[/cyan]", style="bold")
raise SystemExit


def _configure_auth(basic_auth, digest_auth):
if basic_auth:
username, password = basic_auth.split(":")
return HTTPBasicAuth(username, password)
elif digest_auth:
username, password = digest_auth.split(":")
return HTTPDigestAuth(username, password)
return None


@cli.command()
@click.argument("text")
@click.option(
Expand Down
2 changes: 2 additions & 0 deletions requirements-dev.txt
Expand Up @@ -14,6 +14,8 @@ build
types-requests
requests-mock
pre-commit
radon
bandit
# flask integration
Flask
# aiohttp integration
Expand Down
6 changes: 3 additions & 3 deletions setup.cfg
Expand Up @@ -4,13 +4,13 @@ universal = 1
[metadata]
name = config-client
version = attr: config.__version__
author = alexandre menezes
author = Alexandre Menezes
author_email = alexandre.fmenezes@gmail.com
description = config client for Spring Cloud Config Server and Cloud Foundry
long_description = file: README.md
long_description_content_type = text/markdown
license = Apache-2.0
license_file = LICENSE
license_files = LICENSE
url = https://github.com/amenezes/config-client
project_urls =
Documentation = https://config-client.amenezes.net
Expand Down Expand Up @@ -84,7 +84,7 @@ warn_unused_ignores = True
warn_unreachable = True

[tox:tox]
envlist = py{37,38,39,310,311},pypy{3.6,3.7,3.8}
envlist = py{37,38,39,310,311},pypy{3.6,3.7,3.8,3.9,3.10}

[testenv]
deps = -rrequirements-dev.txt
Expand Down
7 changes: 7 additions & 0 deletions tests/unit/test_cf.py
Expand Up @@ -30,6 +30,13 @@ def test_configure_custom_client():
assert cf.client.address == "http://localhost:8888/configuration"


@pytest.mark.parametrize(
"attr", ["cfenv", "oauth2", "client", "vcap_services", "vcap_application", "config"]
)
def test_cf_attributes(cf, attr):
assert hasattr(cf, attr)


def test_default_properties(cf):
assert cf is not None

Expand Down
16 changes: 16 additions & 0 deletions tests/unit/test_cfenv.py
Expand Up @@ -64,3 +64,19 @@ def test_custom_vcap_configserver_client_id(custom_cfenv):

def test_custom_vcap_configserver_client_secret(custom_cfenv):
assert custom_cfenv.configserver_client_secret() == "example_client_secret"


@pytest.mark.parametrize(
"attr",
[
"vcap_service_prefix",
"vcap_application",
"vcap_services",
"space_name",
"organization_name",
"application_name",
"uris",
],
)
def test_cfenv_attributes(cfenv, attr):
assert hasattr(cfenv, attr)
14 changes: 10 additions & 4 deletions tests/unit/test_cli.py
Expand Up @@ -101,14 +101,20 @@ class TestDecryptCommand:
def _mock_decrypt(self, *args, **kwargs):
return "123"

def test_decrypt_command(self, cli_runner, monkeypatch):
@pytest.mark.parametrize(
"secret",
[
"dfa76862fe7f367d9c1923de55ba85512eea7a41163ade3059d64fcfbed31017",
"{cipher}dfa76862fe7f367d9c1923de55ba85512eea7a41163ade3059d64fcfbed31017",
],
)
def test_decrypt_command(self, cli_runner, monkeypatch, secret):
monkeypatch.setattr(ConfigClient, "decrypt", self._mock_decrypt)
result = cli_runner.invoke(
decrypt,
["dfa76862fe7f367d9c1923de55ba85512eea7a41163ade3059d64fcfbed31017"],
[secret],
)
assert result.output is not None
assert len(result.output) >= 100
assert "123" in result.output

def test_decrypt_command_error(self, cli_runner, monkeypatch):
monkeypatch.setattr(http, "post", SystemExit())
Expand Down
15 changes: 15 additions & 0 deletions tests/unit/test_oauth2.py
Expand Up @@ -31,3 +31,18 @@ def test_authorization_header(oauth2, monkeypatch):
monkeypatch.setattr(http, "post", conftest.oauth2_mock)
oauth2.configure()
assert isinstance(oauth2.authorization_header, dict)


@pytest.mark.parametrize(
"attr",
[
"access_token_uri",
"client_id",
"client_secret",
"grant_type",
"token",
"authorization_header",
],
)
def test_oauth_attributes(oauth2, attr):
assert hasattr(oauth2, attr)
8 changes: 8 additions & 0 deletions tests/unit/test_spring.py
Expand Up @@ -211,3 +211,11 @@ def test_client_with_auth_and_headers(monkeypatch, mocker, oauth2):
},
verify=False,
)


@pytest.mark.parametrize(
"attr",
["address", "label", "app_name", "profile", "fail_fast", "oauth2", "url", "config"],
)
def test_config_client_attributes(client, attr):
assert hasattr(client, attr)

0 comments on commit 53f06df

Please sign in to comment.