| 1 | import json |
| 2 | |
| 3 | from flasgger.base import Swagger |
| 4 | from flasgger.commands import generate_api_schema |
| 5 | |
| 6 | |
| 7 | def test_default_specs(app, cli_runner): |
| 8 | swagger = Swagger(app) |
| 9 | |
| 10 | result = cli_runner.invoke(generate_api_schema) |
| 11 | |
| 12 | assert result.exit_code == 0 |
| 13 | spec = json.loads(result.output) |
| 14 | assert spec == swagger.get_apispecs(Swagger.DEFAULT_ENDPOINT) |
| 15 | |
| 16 | |
| 17 | def test_custom_specs(app, cli_runner): |
| 18 | endpoint = "custom_endpoint" |
| 19 | config = dict(Swagger.DEFAULT_CONFIG) |
| 20 | config["specs"][0]["endpoint"] = endpoint |
| 21 | swagger = Swagger(app, config=config) |
| 22 | |
| 23 | result = cli_runner.invoke(generate_api_schema, ["-e", endpoint]) |
| 24 | |
| 25 | assert result.exit_code == 0 |
| 26 | spec = json.loads(result.output) |
| 27 | assert spec == swagger.get_apispecs(endpoint) |
| 28 | |
| 29 | |
| 30 | def test_invalid_endpoint(app, cli_runner): |
| 31 | endpoint = "custom_endpoint" |
| 32 | config = dict(Swagger.DEFAULT_CONFIG) |
| 33 | config["specs"][0]["endpoint"] = endpoint |
| 34 | Swagger(app, config=config) |
| 35 | |
| 36 | result = cli_runner.invoke(generate_api_schema, ["-e", "other_endpoint"]) |
| 37 | |
| 38 | assert result.exit_code == 1 |
| 39 | assert "other_endpoint" in result.stderr |
| 40 | |
| 41 | |
| 42 | def test_definitions_is_removed_for_openapi_3(app, cli_runner): |
| 43 | app.config["SWAGGER"] = { |
| 44 | "openapi": "3.0.3", |
| 45 | } |
| 46 | |
| 47 | Swagger(app) |
| 48 | |
| 49 | result = cli_runner.invoke(generate_api_schema) |
| 50 | assert result.exit_code == 0 |
| 51 | assert "definitions" not in json.loads(result.output) |