Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -247,4 +247,8 @@ reporting-cli \
--states \
--bounds
```

If neither `--auth_key` nor `--sign_secret` is given, both are read from
`FREQUENZ_API_KEY` and `FREQUENZ_API_SECRET` as a pair.

In addition to the default CSV format, individual samples can also be output using the `--format iter` option.
1 change: 1 addition & 0 deletions RELEASE_NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
## New Features

- The CLI now rejects `--end` before `--start`.
- `reporting-cli` falls back to `FREQUENZ_API_KEY` and `FREQUENZ_API_SECRET` when neither `--auth_key` nor `--sign_secret` is given.

## Bug Fixes

Expand Down
16 changes: 15 additions & 1 deletion src/frequenz/client/reporting/cli/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import argparse
import asyncio
import csv
import os
import sys
from datetime import datetime, timedelta
from typing import AsyncIterator
Expand Down Expand Up @@ -97,7 +98,10 @@ def main() -> None:
parser.add_argument(
"--auth_key",
type=str,
help="API key",
help=(
"API key. If neither key nor secret is given, both are read from "
"FREQUENZ_API_KEY and FREQUENZ_API_SECRET."
),
default=None,
)
parser.add_argument(
Expand All @@ -109,6 +113,16 @@ def main() -> None:
args = parser.parse_args()
if args.start and args.end and args.end < args.start:
parser.error("--end must not be before --start")

# Take the fallback as a pair so a key and secret from different sources
# are never combined.
if not args.auth_key and not args.sign_secret:
args.auth_key = os.environ.get("FREQUENZ_API_KEY")
args.sign_secret = os.environ.get("FREQUENZ_API_SECRET")

if args.sign_secret and not args.auth_key:
parser.error("no auth key: --sign_secret was given without --auth_key")

asyncio.run(
run(
microgrid_id=args.mid,
Expand Down
76 changes: 76 additions & 0 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
# License: MIT
# Copyright © 2026 Frequenz Energy-as-a-Service GmbH

"""Tests for the reporting CLI."""

from unittest.mock import AsyncMock

import pytest

from frequenz.client.reporting.cli import __main__ as cli

_ARGV = ["reporting-cli", "--mid", "1", "--cid", "1"]


def _run_main(
monkeypatch: pytest.MonkeyPatch, args: list[str], env: dict[str, str]
) -> AsyncMock:
"""Run `main` with `args` and `env`, returning the mocked `run`."""
run = AsyncMock()
monkeypatch.setattr(cli, "run", run)
monkeypatch.setattr("sys.argv", _ARGV + args)
for name in ("FREQUENZ_API_KEY", "FREQUENZ_API_SECRET"):
monkeypatch.delenv(name, raising=False)
for name, value in env.items():
monkeypatch.setenv(name, value)
cli.main()
return run


_FALLBACK_ENV = {
"FREQUENZ_API_KEY": "fallback-key",
"FREQUENZ_API_SECRET": "fallback-secret",
}


def test_credentials_fall_back_to_frequenz_env_vars(
monkeypatch: pytest.MonkeyPatch,
) -> None:
"""`FREQUENZ_API_*` are used when no credentials are given."""
run = _run_main(monkeypatch, [], _FALLBACK_ENV)

assert run.call_args.kwargs["auth_key"] == "fallback-key"
assert run.call_args.kwargs["sign_secret"] == "fallback-secret"


def test_flags_take_precedence(monkeypatch: pytest.MonkeyPatch) -> None:
"""`--auth_key` and `--sign_secret` win over `FREQUENZ_API_*`."""
run = _run_main(
monkeypatch,
["--auth_key", "own-key", "--sign_secret", "own-secret"],
_FALLBACK_ENV,
)

assert run.call_args.kwargs["auth_key"] == "own-key"
assert run.call_args.kwargs["sign_secret"] == "own-secret"


def test_credentials_are_not_mixed_across_sources(
monkeypatch: pytest.MonkeyPatch,
) -> None:
"""A `--auth_key` is never paired with a `FREQUENZ_API_*` secret."""
run = _run_main(monkeypatch, ["--auth_key", "own-key"], _FALLBACK_ENV)

assert run.call_args.kwargs["auth_key"] == "own-key"
assert run.call_args.kwargs["sign_secret"] is None


def test_secret_without_key_does_not_fall_back(
monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str]
) -> None:
"""A `--sign_secret` alone is an error, not a mixed pair."""
with pytest.raises(SystemExit) as exc_info:
_run_main(monkeypatch, ["--sign_secret", "own-secret"], _FALLBACK_ENV)

assert exc_info.value.code != 0
assert "auth key" in capsys.readouterr().err