Skip to content

Commit

Permalink
feat: Handle cases when the remote schema is not publicly accessible
Browse files Browse the repository at this point in the history
  • Loading branch information
Stanislav Tkachenko authored and Stranger6667 committed Feb 7, 2020
1 parent cb2874d commit 4160583
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 12 deletions.
2 changes: 2 additions & 0 deletions docs/changelog.rst
Expand Up @@ -11,6 +11,7 @@ Added

- Support for testing of examples in Parameter & Media Type objects in Open API 3.0. `#394`_
- ``--show-error-tracebacks`` CLI option to display errors' tracebacks in the output. `#391`_
- Support for schema behind auth. `#115`_

Changed
~~~~~~~
Expand Down Expand Up @@ -759,6 +760,7 @@ Fixed
.. _#121: https://github.com/kiwicom/schemathesis/issues/121
.. _#119: https://github.com/kiwicom/schemathesis/issues/119
.. _#118: https://github.com/kiwicom/schemathesis/issues/118
.. _#115: https://github.com/kiwicom/schemathesis/issues/115
.. _#110: https://github.com/kiwicom/schemathesis/issues/110
.. _#109: https://github.com/kiwicom/schemathesis/issues/109
.. _#107: https://github.com/kiwicom/schemathesis/issues/107
Expand Down
9 changes: 8 additions & 1 deletion src/schemathesis/cli/__init__.py
Expand Up @@ -13,7 +13,7 @@
from .. import checks as checks_module
from .. import models, runner, utils
from ..exceptions import HTTPError
from ..loaders import from_path, get_loader_for_app
from ..loaders import from_path, from_uri, get_loader_for_app
from ..runner import events
from ..types import Filter
from ..utils import WSGIResponse, dict_not_none_values, dict_true_values
Expand Down Expand Up @@ -199,6 +199,13 @@ def run( # pylint: disable=too-many-arguments
# If `schema` is not an existing filesystem path or an URL then it is considered as an endpoint with
# the given app
options["loader"] = get_loader_for_app(app)
else:
options["loader"] = from_uri
loader_options = dict_true_values(headers=headers, auth=auth, auth_type=auth_type)
if options.get("loader_options") and loader_options:
options["loader_options"].update(loader_options)
elif loader_options:
options["loader_options"] = loader_options
prepared_runner = runner.prepare(schema, **options)
execute(prepared_runner, workers_num, show_errors_tracebacks)

Expand Down
42 changes: 31 additions & 11 deletions test/cli/test_commands.py
Expand Up @@ -11,7 +11,7 @@
from schemathesis import Case
from schemathesis._compat import metadata
from schemathesis.checks import ALL_CHECKS
from schemathesis.loaders import from_path
from schemathesis.loaders import from_path, from_uri
from schemathesis.models import Endpoint
from schemathesis.runner import DEFAULT_CHECKS

Expand Down Expand Up @@ -163,9 +163,9 @@ def test_commands_run_help(cli):
@pytest.mark.parametrize(
"args, expected",
(
([SCHEMA_URI], {"checks": DEFAULT_CHECKS, "workers_num": 1}),
([SCHEMA_URI, "--checks=all"], {"checks": ALL_CHECKS, "workers_num": 1}),
([SCHEMA_URI, "--exitfirst"], {"checks": DEFAULT_CHECKS, "exit_first": True, "workers_num": 1}),
([SCHEMA_URI], {"checks": DEFAULT_CHECKS, "loader": from_uri, "workers_num": 1}),
([SCHEMA_URI, "--checks=all"], {"checks": ALL_CHECKS, "loader": from_uri, "workers_num": 1}),
([SCHEMA_URI, "--exitfirst"], {"checks": DEFAULT_CHECKS, "exit_first": True, "loader": from_uri, "workers_num": 1}),
(
[SIMPLE_PATH, "--base-url=http://127.0.0.1"],
{
Expand All @@ -180,6 +180,8 @@ def test_commands_run_help(cli):
{
"checks": DEFAULT_CHECKS,
"api_options": {"auth": ("test", "test"), "auth_type": "basic"},
"loader": from_uri,
"loader_options": {"auth": ("test", "test"), "auth_type": "basic"},
"workers_num": 1,
},
),
Expand All @@ -188,6 +190,8 @@ def test_commands_run_help(cli):
{
"checks": DEFAULT_CHECKS,
"api_options": {"auth": ("test", "test"), "auth_type": "digest"},
"loader": from_uri,
"loader_options": {"auth": ("test", "test"), "auth_type": "digest"},
"workers_num": 1,
},
),
Expand All @@ -196,35 +200,50 @@ def test_commands_run_help(cli):
{
"checks": DEFAULT_CHECKS,
"api_options": {"auth": ("test", "test"), "auth_type": "digest"},
"loader": from_uri,
"loader_options": {"auth": ("test", "test"), "auth_type": "digest"},
"workers_num": 1,
},
),
(
[SCHEMA_URI, "--header=Authorization:Bearer 123"],
{"checks": DEFAULT_CHECKS, "api_options": {"headers": {"Authorization": "Bearer 123"}}, "workers_num": 1},
{
"checks": DEFAULT_CHECKS,
"api_options": {"headers": {"Authorization": "Bearer 123"}},
"loader": from_uri,
"loader_options": {"headers": {"Authorization": "Bearer 123"}},
"workers_num": 1,
},
),
(
[SCHEMA_URI, "--header=Authorization: Bearer 123 "],
{"checks": DEFAULT_CHECKS, "api_options": {"headers": {"Authorization": "Bearer 123 "}}, "workers_num": 1},
{
"checks": DEFAULT_CHECKS,
"api_options": {"headers": {"Authorization": "Bearer 123 "}},
"loader": from_uri,
"loader_options": {"headers": {"Authorization": "Bearer 123 "}},
"workers_num": 1,
},
),
(
[SCHEMA_URI, "--method=POST", "--method", "GET"],
{"checks": DEFAULT_CHECKS, "loader_options": {"method": ("POST", "GET")}, "workers_num": 1},
{"checks": DEFAULT_CHECKS, "loader": from_uri, "loader_options": {"method": ("POST", "GET")}, "workers_num": 1},
),
(
[SCHEMA_URI, "--endpoint=users"],
{"checks": DEFAULT_CHECKS, "loader_options": {"endpoint": ("users",)}, "workers_num": 1},
{"checks": DEFAULT_CHECKS, "loader": from_uri, "loader_options": {"endpoint": ("users",)}, "workers_num": 1},
),
([SCHEMA_URI, "--tag=foo"], {"checks": DEFAULT_CHECKS, "loader_options": {"tag": ("foo",)}, "workers_num": 1}),
([SCHEMA_URI, "--tag=foo"], {"checks": DEFAULT_CHECKS, "loader": from_uri, "loader_options": {"tag": ("foo",)}, "workers_num": 1}),
(
[SCHEMA_URI, "--base-url=https://example.com/api/v1test"],
{
"checks": DEFAULT_CHECKS,
"loader": from_uri,
"loader_options": {"base_url": "https://example.com/api/v1test"},
"workers_num": 1,
},
),
([SCHEMA_URI, "--hypothesis-seed=123"], {"checks": DEFAULT_CHECKS, "seed": 123, "workers_num": 1}),
([SCHEMA_URI, "--hypothesis-seed=123"], {"checks": DEFAULT_CHECKS, "loader": from_uri, "seed": 123, "workers_num": 1}),
(
[
SCHEMA_URI,
Expand All @@ -247,12 +266,13 @@ def test_commands_run_help(cli):
"suppress_health_check": [HealthCheck.too_slow, HealthCheck.filter_too_much],
"verbosity": Verbosity.normal,
},
"loader": from_uri,
"workers_num": 1,
},
),
(
[SCHEMA_URI, "--hypothesis-deadline=None"],
{"checks": DEFAULT_CHECKS, "hypothesis_options": {"deadline": None}, "workers_num": 1},
{"checks": DEFAULT_CHECKS, "hypothesis_options": {"deadline": None}, "loader": from_uri, "workers_num": 1},
),
),
)
Expand Down

0 comments on commit 4160583

Please sign in to comment.