| 1 |
import json |
| 2 |
|
| 3 |
import pytest |
| 4 |
|
| 5 |
try: |
| 6 |
import coreschema |
| 7 |
except ImportError: |
| 8 |
coreschema = None |
| 9 |
|
| 10 |
from drf_yasg.codecs import yaml_load |
| 11 |
|
| 12 |
|
| 13 |
def _validate_text_schema_view(client, validate_schema, path, loader): |
| 14 |
response = client.get(path) |
| 15 |
assert response.status_code == 200 |
| 16 |
validate_schema(loader(response.content)) |
| 17 |
|
| 18 |
|
| 19 |
def _validate_ui_schema_view(client, path, string): |
| 20 |
response = client.get(path) |
| 21 |
assert response.status_code == 200 |
| 22 |
assert string in response.content.decode("utf-8") |
| 23 |
|
| 24 |
|
| 25 |
def test_swagger_json(client, validate_schema): |
| 26 |
_validate_text_schema_view(client, validate_schema, "/swagger.json", json.loads) |
| 27 |
|
| 28 |
|
| 29 |
def test_swagger_yaml(client, validate_schema): |
| 30 |
_validate_text_schema_view(client, validate_schema, "/swagger.yaml", yaml_load) |
| 31 |
|
| 32 |
|
| 33 |
def test_exception_middleware(client, swagger_settings, db): |
| 34 |
swagger_settings["SECURITY_DEFINITIONS"] = { |
| 35 |
"bad": {"bad_attribute": "should not be accepted"} |
| 36 |
} |
| 37 |
|
| 38 |
response = client.get("/swagger.json") |
| 39 |
assert response.status_code == 500 |
| 40 |
assert "errors" in json.loads(response.content) |
| 41 |
|
| 42 |
|
| 43 |
def test_swagger_ui(client, validate_schema): |
| 44 |
_validate_ui_schema_view( |
| 45 |
client, "/swagger/", "swagger-ui-dist/swagger-ui-bundle.js" |
| 46 |
) |
| 47 |
_validate_text_schema_view( |
| 48 |
client, validate_schema, "/swagger/?format=openapi", json.loads |
| 49 |
) |
| 50 |
|
| 51 |
|
| 52 |
def test_redoc(client, validate_schema): |
| 53 |
_validate_ui_schema_view(client, "/redoc/", "redoc/redoc.min.js") |
| 54 |
_validate_text_schema_view( |
| 55 |
client, validate_schema, "/redoc/?format=openapi", json.loads |
| 56 |
) |
| 57 |
|
| 58 |
|
| 59 |
@pytest.mark.urls("urlconfs.legacy_renderer") |
| 60 |
@pytest.mark.parametrize("format", (".json", ".yaml")) |
| 61 |
def test_swagger_ui_legacy_renderer(settings, client, validate_schema, format): |
| 62 |
settings.SWAGGER_SETTINGS = { |
| 63 |
**settings.SWAGGER_SETTINGS, |
| 64 |
"SPEC_URL": ("schema-json", {"format": format}), |
| 65 |
} |
| 66 |
|
| 67 |
_validate_ui_schema_view( |
| 68 |
client, "/swagger/", "swagger-ui-dist/swagger-ui-bundle.js" |
| 69 |
) |
| 70 |
_validate_text_schema_view( |
| 71 |
client, validate_schema, "/swagger/?format=openapi", json.loads |
| 72 |
) |
| 73 |
|
| 74 |
|
| 75 |
@pytest.mark.urls("urlconfs.legacy_renderer") |
| 76 |
@pytest.mark.parametrize("format", (".json", ".yaml")) |
| 77 |
def test_redoc_legacy_renderer(settings, client, validate_schema, format): |
| 78 |
settings.REDOC_SETTINGS = { |
| 79 |
**settings.REDOC_SETTINGS, |
| 80 |
"SPEC_URL": ("schema-json", {"format": format}), |
| 81 |
} |
| 82 |
|
| 83 |
_validate_ui_schema_view(client, "/redoc/", "redoc/redoc.min.js") |
| 84 |
_validate_text_schema_view( |
| 85 |
client, validate_schema, "/redoc/?format=openapi", json.loads |
| 86 |
) |
| 87 |
|
| 88 |
|
| 89 |
def test_caching(client, validate_schema): |
| 90 |
prev_schema = None |
| 91 |
|
| 92 |
for i in range(3): |
| 93 |
_validate_text_schema_view( |
| 94 |
client, validate_schema, "/cached/swagger.yaml", yaml_load |
| 95 |
) |
| 96 |
|
| 97 |
json_schema = client.get("/cached/swagger.json") |
| 98 |
assert json_schema.status_code == 200 |
| 99 |
json_schema = json.loads(json_schema.content) |
| 100 |
if prev_schema is None: |
| 101 |
validate_schema(json_schema) |
| 102 |
prev_schema = json_schema |
| 103 |
else: |
| 104 |
from datadiff.tools import assert_equal |
| 105 |
|
| 106 |
assert_equal(prev_schema, json_schema) |
| 107 |
|
| 108 |
|
| 109 |
@pytest.mark.urls("urlconfs.non_public_urls") |
| 110 |
def test_non_public(client): |
| 111 |
response = client.get("/private/swagger.yaml") |
| 112 |
swagger = yaml_load(response.content) |
| 113 |
assert len(swagger["paths"]) == 0 |
| 114 |
|
| 115 |
|
| 116 |
@pytest.mark.skipif(coreschema is None, reason="Do not test without coreschema.") |
| 117 |
@pytest.mark.urls("urlconfs.coreschema") |
| 118 |
def test_paginator_schema(client, swagger_settings): |
| 119 |
swagger_settings["DEFAULT_FILTER_INSPECTORS"] = [ |
| 120 |
"drf_yasg.inspectors.CoreAPICompatInspector", |
| 121 |
"drf_yasg.inspectors.DrfAPICompatInspector", |
| 122 |
] |
| 123 |
swagger_settings["DEFAULT_PAGINATOR_INSPECTORS"] = [ |
| 124 |
"drf_yasg.inspectors.CoreAPICompatInspector", |
| 125 |
"drf_yasg.inspectors.DrfAPICompatInspector", |
| 126 |
] |
| 127 |
|
| 128 |
response = client.get("/versioned/url/v1.0/swagger.yaml") |
| 129 |
swagger = yaml_load(response.content) |
| 130 |
|
| 131 |
assert ( |
| 132 |
swagger["paths"]["/snippets/"]["get"]["responses"]["200"]["schema"]["type"] |
| 133 |
== "object" |
| 134 |
) |
| 135 |
assert swagger["paths"]["/snippets/"]["get"]["responses"]["200"]["schema"][ |
| 136 |
"required" |
| 137 |
] == ["results"] |
| 138 |
assert ( |
| 139 |
swagger["paths"]["/snippets/"]["get"]["parameters"][0]["name"] == "test_param" |
| 140 |
) |
| 141 |
assert swagger["paths"]["/snippets/"]["get"]["parameters"][0]["type"] == "string" |
| 142 |
assert swagger["paths"]["/snippets/"]["get"]["parameters"][1]["name"] == "limit" |
| 143 |
assert swagger["paths"]["/snippets/"]["get"]["parameters"][1]["in"] == "query" |
| 144 |
assert swagger["paths"]["/snippets/"]["get"]["parameters"][1]["type"] == "integer" |
| 145 |
|
| 146 |
assert ( |
| 147 |
swagger["paths"]["/other_snippets/"]["get"]["responses"]["200"]["schema"][ |
| 148 |
"type" |
| 149 |
] |
| 150 |
== "array" |
| 151 |
) |
| 152 |
assert ( |
| 153 |
swagger["paths"]["/other_snippets/"]["get"]["parameters"][0]["name"] == "limit" |
| 154 |
) |
| 155 |
assert swagger["paths"]["/other_snippets/"]["get"]["parameters"][0]["in"] == "query" |
| 156 |
assert ( |
| 157 |
swagger["paths"]["/other_snippets/"]["get"]["parameters"][0]["type"] |
| 158 |
== "integer" |
| 159 |
) |
| 160 |
|
| 161 |
|
| 162 |
@pytest.mark.urls("urlconfs.additional_fields_checks") |
| 163 |
def test_extra_field_inspections(client, swagger_settings): |
| 164 |
# swagger_settings[] |
| 165 |
response = client.get("/versioned/url/v1.0/swagger.json") |
| 166 |
swagger = json.loads(response.content) |
| 167 |
|
| 168 |
assert swagger["definitions"]["Snippets"]["properties"]["url"]["type"] == "string" |
| 169 |
assert swagger["definitions"]["Snippets"]["properties"]["url"]["format"] == "uri" |
| 170 |
assert swagger["definitions"]["Snippets"]["properties"]["ipv4"]["type"] == "string" |
| 171 |
assert swagger["definitions"]["Snippets"]["properties"]["uri"]["type"] == "string" |
| 172 |
assert swagger["definitions"]["Snippets"]["properties"]["uri"]["format"] == "uri" |
| 173 |
assert swagger["definitions"]["Snippets"]["properties"]["tracks"]["type"] == "array" |
| 174 |
assert ( |
| 175 |
swagger["definitions"]["Snippets"]["properties"]["tracks"]["items"]["type"] |
| 176 |
== "string" |
| 177 |
) |
| 178 |
|
| 179 |
assert swagger["definitions"]["SnippetsV2"]["properties"]["url"]["type"] == "string" |
| 180 |
assert swagger["definitions"]["SnippetsV2"]["properties"]["url"]["format"] == "uri" |
| 181 |
|
| 182 |
assert ( |
| 183 |
swagger["definitions"]["SnippetsV2"]["properties"]["other_owner_snippets"][ |
| 184 |
"type" |
| 185 |
] |
| 186 |
== "array" |
| 187 |
) |
| 188 |
assert ( |
| 189 |
swagger["definitions"]["SnippetsV2"]["properties"]["other_owner_snippets"][ |
| 190 |
"items" |
| 191 |
]["type"] |
| 192 |
== "integer" |
| 193 |
) |
| 194 |
|
| 195 |
# Cannt check type of queryset in property descriptor. |
| 196 |
assert ( |
| 197 |
swagger["definitions"]["SnippetsV2"]["properties"]["owner_snippets"]["type"] |
| 198 |
== "array" |
| 199 |
) |
| 200 |
assert ( |
| 201 |
swagger["definitions"]["SnippetsV2"]["properties"]["owner_snippets"]["items"][ |
| 202 |
"type" |
| 203 |
] |
| 204 |
== "string" |
| 205 |
) |