Test -t auth token option

This commit is contained in:
Simon Willison 2023-07-17 16:17:44 -07:00
commit a424a0e27b

View file

@ -2,6 +2,7 @@ from click.testing import CliRunner
from dclient.cli import cli
import json
import pathlib
import pytest
def test_query_error(httpx_mock):
@ -23,7 +24,8 @@ def test_query_error(httpx_mock):
)
def test_query(httpx_mock):
@pytest.mark.parametrize("with_token", (False, True))
def test_query(httpx_mock, with_token):
httpx_mock.add_response(
json={
"ok": True,
@ -40,10 +42,22 @@ def test_query(httpx_mock):
status_code=200,
)
runner = CliRunner()
result = runner.invoke(cli, ["query", "https://example.com", "hello"])
args = ["query", "https://example.com", "hello"]
if with_token:
args.append("--token")
args.append("xyz")
result = runner.invoke(cli, args)
assert result.exit_code == 0
assert json.loads(result.output) == [{"5 * 2": 10}]
# Check the request
request = httpx_mock.get_request()
assert str(request.url) == "https://example.com.json?sql=hello&_shape=objects"
if with_token:
assert request.headers["authorization"] == "Bearer xyz"
else:
assert "authorization" not in request.headers
def test_aliases(mocker, tmpdir, httpx_mock):
mocker.patch("dclient.cli.get_config_dir", return_value=pathlib.Path(tmpdir))