-
-
Notifications
You must be signed in to change notification settings - Fork 140
Expand file tree
/
Copy pathtest_security_override.py
More file actions
111 lines (82 loc) · 3.53 KB
/
Copy pathtest_security_override.py
File metadata and controls
111 lines (82 loc) · 3.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
from base64 import b64encode
import pytest
from openapi_core.templating.security.exceptions import SecurityNotFound
from openapi_core.testing import MockRequest
from openapi_core.unmarshalling.request.unmarshallers import (
V30RequestUnmarshaller,
V31RequestUnmarshaller,
)
from openapi_core.validation.request.exceptions import SecurityValidationError
@pytest.fixture(
scope="class",
params=[
("data/v3.0/security_override.yaml", V30RequestUnmarshaller),
("data/v3.1/security_override.yaml", V31RequestUnmarshaller),
],
ids=["openapi-3.0", "openapi-3.1"],
)
def request_unmarshaller(schema_path_factory, request):
schema_file, unmarshaller_cls = request.param
schema_path = schema_path_factory.from_file(schema_file)
return unmarshaller_cls(schema_path)
class TestSecurityOverride:
host_url = "http://petstore.swagger.io"
api_key = "12345"
@property
def api_key_encoded(self):
api_key_bytes = self.api_key.encode("utf8")
api_key_bytes_enc = b64encode(api_key_bytes)
return str(api_key_bytes_enc, "utf8")
def test_default(self, request_unmarshaller):
args = {"api_key": self.api_key}
request = MockRequest(self.host_url, "get", "/resource/one", args=args)
result = request_unmarshaller.unmarshal(request)
assert not result.errors
assert result.security == {
"api_key": self.api_key,
}
def test_default_invalid(self, request_unmarshaller):
request = MockRequest(self.host_url, "get", "/resource/one")
result = request_unmarshaller.unmarshal(request)
assert len(result.errors) == 1
assert type(result.errors[0]) is SecurityValidationError
assert type(result.errors[0].__cause__) is SecurityNotFound
assert result.security is None
def test_override(self, request_unmarshaller):
authorization = "Basic " + self.api_key_encoded
headers = {
"Authorization": authorization,
}
request = MockRequest(
self.host_url, "post", "/resource/one", headers=headers
)
result = request_unmarshaller.unmarshal(request)
assert not result.errors
assert result.security == {
"petstore_auth": self.api_key_encoded,
}
def test_override_invalid(self, request_unmarshaller):
request = MockRequest(self.host_url, "post", "/resource/one")
result = request_unmarshaller.unmarshal(request)
assert len(result.errors) == 1
assert type(result.errors[0]) is SecurityValidationError
assert type(result.errors[0].__cause__) is SecurityNotFound
assert result.security is None
def test_remove(self, request_unmarshaller):
request = MockRequest(self.host_url, "put", "/resource/one")
result = request_unmarshaller.unmarshal(request)
assert not result.errors
assert result.security == {}
def test_optional_without_security(self, request_unmarshaller):
request = MockRequest(self.host_url, "patch", "/resource/one")
result = request_unmarshaller.unmarshal(request)
assert not result.errors
assert result.security == {}
def test_optional_with_security(self, request_unmarshaller):
args = {"api_key": self.api_key}
request = MockRequest(self.host_url, "patch", "/resource/one", args=args)
result = request_unmarshaller.unmarshal(request)
assert not result.errors
assert result.security == {
"api_key": self.api_key,
}