-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #17 from jonykalavera/dokli-1
dokli-1: add api_key_cmd. add tests for config.
- Loading branch information
Showing
7 changed files
with
129 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
test: | ||
pytest -vv --cov dokli | ||
pytest -vv --cov dokli --blockage | ||
|
||
format: | ||
ruff format dokli/ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,19 @@ | ||
"""Dokli CLI tests.""" | ||
|
||
import pytest | ||
from dokli.cli import app, main | ||
|
||
from dokli.cli import app, state | ||
|
||
|
||
@pytest.mark.skip | ||
def test_loads_api_commands(mocker): | ||
"""We expect the CLI to load API commands.""" | ||
register_connections = mocker.patch("dokli.cli.register_connections", return_value=None) | ||
|
||
register_connections.assert_called_once_with(app, state["config"]) | ||
assert app.registered_groups[0].typer_instance.info.name == "api" | ||
|
||
|
||
def test_loads_tui_command(): | ||
"""We expect the CLI to load TUI command.""" | ||
assert app.registered_commands[-1].name == "tui" | ||
assert "tui" in [cmd.name for cmd in app.registered_commands] | ||
|
||
|
||
def test_app_has_main_callback(): | ||
"""We expect the CLI to have a main callback.""" | ||
assert app.registered_callback.callback is main | ||
assert app.registered_callback.no_args_is_help |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
"""Config tests.""" | ||
|
||
import pytest | ||
from polyfactory.factories.pydantic_factory import ModelFactory | ||
from pydantic import ValidationError | ||
|
||
from dokli.config import Config, ConnectionConfig | ||
|
||
|
||
class ConnectionConfigFactory(ModelFactory[ConnectionConfig]): | ||
"""Connection factory.""" | ||
|
||
api_key = None | ||
api_key_cmd = "echo 'my api key from cmd'" | ||
|
||
|
||
class ConfigFactory(ModelFactory[Config]): | ||
"""Config factory.""" | ||
|
||
connections = [ConnectionConfigFactory.build()] | ||
|
||
|
||
class TestConfig: | ||
"""Config model tests.""" | ||
|
||
def test_config(self): | ||
"""We expect to be able to declare an api with connections.""" | ||
config = ConfigFactory.build() | ||
assert config.connections | ||
|
||
def test_get_connection(self): | ||
"""We expect to be able to get a connection by name.""" | ||
connection = ConnectionConfigFactory.build(name="dokploy") | ||
config = ConfigFactory.build(connections=[connection]) | ||
assert config.get_connection("dokploy") | ||
|
||
|
||
class TestConnectionConfig: | ||
"""Connection config model tests.""" | ||
|
||
def test_must_provide_api_key_or_cmd(self): | ||
"""We expect to raise an error if no api key or cmd is provided.""" | ||
with pytest.raises(ValidationError): | ||
ConnectionConfig(name="dokli", url="https://dokli.example.com") | ||
|
||
def test_connection_with_api_key(self): | ||
"""We expect to be able to declare a connection with an API key.""" | ||
config = ConnectionConfigFactory.build(api_key="*" * 40) | ||
assert config.get_api_key() == config.api_key.get_secret_value() | ||
|
||
def test_connection_with_api_key_cmd(self, mocker): | ||
"""We expect to be able to declare a connection with an API key command.""" | ||
config = ConnectionConfigFactory.build() | ||
assert config.api_key is None | ||
check_output = mocker.patch("dokli.config.subprocess.check_output", return_value=b"my api key from cmd") | ||
assert config.get_api_key() == "my api key from cmd" | ||
check_output.assert_called_once_with(config.api_key_cmd.split()) | ||
|
||
def test_model_dump_clear_prints_clear_secrets(self): | ||
"""We expect to be able to dump the config with clear secrets.""" | ||
config = ConnectionConfigFactory.build(api_key="*" * 40) | ||
result = config.model_dump_clear() | ||
assert result["api_key"] == "*" * 40 |