Skip to content

Commit

Permalink
Add HTTP Digest Auth support
Browse files Browse the repository at this point in the history
  • Loading branch information
Stanislav Tkachenko authored and Stranger6667 committed Oct 10, 2019
1 parent b55463b commit d8e4b39
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 0 deletions.
6 changes: 6 additions & 0 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ Changelog
`Unreleased`_
-------------

Added
~~~~~

- HTTP Digest Auth support. `#106`_

`0.9.0`_ - 2019-10-09
---------------------

Expand Down Expand Up @@ -212,6 +217,7 @@ Fixed
.. _#130: https://github.com/kiwicom/schemathesis/issues/130
.. _#121: https://github.com/kiwicom/schemathesis/issues/121
.. _#118: https://github.com/kiwicom/schemathesis/issues/118
.. _#106: https://github.com/kiwicom/schemathesis/issues/106
.. _#99: https://github.com/kiwicom/schemathesis/issues/99
.. _#98: https://github.com/kiwicom/schemathesis/issues/98
.. _#92: https://github.com/kiwicom/schemathesis/issues/92
Expand Down
12 changes: 12 additions & 0 deletions src/schemathesis/cli/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from typing import Dict, Iterable, Optional, Tuple

import click
from requests.auth import HTTPDigestAuth

from .. import runner, utils
from ..types import Filter
Expand Down Expand Up @@ -35,6 +36,13 @@ def main() -> None:
type=str,
callback=validators.validate_auth, # type: ignore
)
@click.option( # type: ignore
"--auth-type",
"-A",
type=click.Choice(["basic", "digest"], case_sensitive=False),
default="basic",
help="The authentication mechanism to be used. Defaults to 'basic'.",
)
@click.option( # type: ignore
"--header",
"-H",
Expand All @@ -57,6 +65,7 @@ def main() -> None:
def run( # pylint: disable=too-many-arguments
schema: str,
auth: Optional[Tuple[str, str]],
auth_type: str,
headers: Dict[str, str],
checks: Iterable[str] = DEFAULT_CHECKS_NAMES,
endpoints: Optional[Filter] = None,
Expand All @@ -71,6 +80,9 @@ def run( # pylint: disable=too-many-arguments

click.echo("Running schemathesis test cases ...")

if auth and auth_type == "digest":
auth = HTTPDigestAuth(*auth) # type: ignore

options = dict_true_values(
api_options=dict_true_values(base_url=base_url, auth=auth, headers=headers),
loader_options=dict_true_values(endpoint=endpoints, method=methods),
Expand Down
11 changes: 11 additions & 0 deletions test/cli/test_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import pytest
from click.testing import CliRunner
from requests.auth import HTTPDigestAuth

from schemathesis import cli, runner

Expand Down Expand Up @@ -45,6 +46,10 @@ def test_commands_version(schemathesis_cmd):
("run", "http://127.0.0.1", "--auth=123"),
'Error: Invalid value for "--auth" / "-a": Should be in KEY:VALUE format. Got: 123',
),
(
("run", "http://127.0.0.1", "--auth-type=random"),
'Error: Invalid value for "--auth-type" / "-A": invalid choice: random. (choose from basic, digest)',
),
(
("run", "http://127.0.0.1", "--header=123"),
'Error: Invalid value for "--header" / "-H": Should be in KEY:VALUE format. Got: 123',
Expand Down Expand Up @@ -79,6 +84,8 @@ def test_commands_run_help(schemathesis_cmd):
" List of checks to run.",
" -a, --auth TEXT Server user and password. Example:",
" USER:PASSWORD",
" -A, --auth-type [basic|digest] The authentication mechanism to be used.",
" Defaults to 'basic'.",
" -H, --header TEXT Custom header in a that will be used in all",
r" requests to the server. Example:",
r" Authorization: Bearer\ 123",
Expand All @@ -101,6 +108,10 @@ def test_commands_run_help(schemathesis_cmd):
[SCHEMA_URI, "--auth=test:test"],
{"checks": runner.DEFAULT_CHECKS, "api_options": {"auth": ("test", "test")}},
),
(
[SCHEMA_URI, "--auth=test:test", "--auth-type=digest"],
{"checks": runner.DEFAULT_CHECKS, "api_options": {"auth": HTTPDigestAuth("test", "test")}},
),
(
[SCHEMA_URI, "--header=Authorization:Bearer 123"],
{"checks": runner.DEFAULT_CHECKS, "api_options": {"headers": {"Authorization": "Bearer 123"}}},
Expand Down

0 comments on commit d8e4b39

Please sign in to comment.