Skip to content

Commit 9e516eb

Browse files
committed
feat: add support for auth in Call keywords
1 parent 734e2f2 commit 9e516eb

File tree

8 files changed

+96
-4
lines changed

8 files changed

+96
-4
lines changed

atest/.DS_Store

-6 KB
Binary file not shown.
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
*** Settings ***
2+
Library SchemathesisLibrary url=http://127.0.0.1/openapi.json
3+
Variables authentication.py
4+
5+
Test Template Wrapper
6+
7+
8+
*** Test Cases ***
9+
All Tests
10+
Wrapper test_case_1
11+
12+
13+
*** Keywords ***
14+
Wrapper
15+
[Arguments] ${case}
16+
IF ${{'${case.path}'.startswith('/user')}}
17+
VAR &{headers} = key1=value1 key2=value2
18+
ELSE
19+
VAR &{headers} =
20+
END
21+
${r} = Call And Validate ${case} auth=${BASIC_AUTH_TUPLE}
22+
Log ${r.json()}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
*** Settings ***
2+
Library SchemathesisLibrary url=http://127.0.0.1/openapi.json
3+
Variables authentication.py
4+
5+
Test Template Wrapper
6+
7+
8+
*** Test Cases ***
9+
All Tests
10+
Wrapper test_case_1
11+
12+
13+
*** Keywords ***
14+
Wrapper
15+
[Arguments] ${case}
16+
${r} = Call ${case} auth=${BASIC_AUTH_TUPLE}
17+
Validate Response ${case} ${r} auth=${BASIC_AUTH_TUPLE}
18+
Log ${r.json()}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
*** Settings ***
2+
Resource runner.resource
3+
Resource all_cases.resource
4+
5+
Suite Setup Run Suite
6+
7+
8+
*** Test Cases ***
9+
Check All Cases
10+
${test_description} = Get All Cases Test Description
11+
Check Suite ${LIBRARY_OUTPUT_XML} ${test_description}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
*** Settings ***
2+
Resource runner.resource
3+
Resource all_cases.resource
4+
5+
Suite Setup Run Suite
6+
7+
8+
*** Test Cases ***
9+
Check All Cases
10+
${test_description} = Get All Cases Test Description
11+
Check Suite ${LIBRARY_OUTPUT_XML} ${test_description}

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ dev = [
2525
"requests>=2.32.5",
2626
"robotframework-robocop>=6.13.0",
2727
"ruff==0.14.8",
28+
"types-requests>=2.32.4.20250913",
2829
]
2930

3031
[tool.semantic_release.settings]

src/SchemathesisLibrary/__init__.py

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
from typing import TYPE_CHECKING, Any
1515

1616
from DataDriver import DataDriver # type: ignore
17+
from requests.auth import HTTPDigestAuth
1718
from robot.api import logger
1819
from robot.api.deco import keyword
1920
from robot.result.model import TestCase as ResultTestCase # type: ignore
@@ -125,16 +126,18 @@ def call_and_validate(
125126
*,
126127
base_url: str | None = None,
127128
headers: dict[str, Any] | None = None,
129+
auth: tuple[str, str] | HTTPDigestAuth | None = None,
128130
) -> Response:
129131
"""Call and validate a Schemathesis case.
130132
131133
Example:
132134
| ${response} = Call And Validate Case ${case}
133135
"""
136+
logger.info(f"auth: {auth} {type(auth)}")
134137
headers = self._dot_dict_to_dict(headers) if headers else None
135138
self.info(f"Case: {case.path} | {case.method} | {case.path_parameters}")
136139
self._log_case(case, headers)
137-
response = case.call_and_validate(base_url=base_url, headers=headers)
140+
response = case.call_and_validate(base_url=base_url, headers=headers, auth=auth)
138141
self._log_request(response)
139142
self.debug(f"Response: {response.headers} | {response.status_code} | {response.text}")
140143
return response
@@ -146,6 +149,7 @@ def call(
146149
*,
147150
base_url: str | None = None,
148151
headers: dict[str, Any] | None = None,
152+
auth: tuple[str, str] | HTTPDigestAuth | None = None,
149153
) -> Response:
150154
"""Call a Schemathesis case without validation.
151155
@@ -159,12 +163,18 @@ def call(
159163
headers = self._dot_dict_to_dict(headers) if headers else None
160164
self.info(f"Calling case: {case.path} | {case.method} | {case.path_parameters}")
161165
self._log_case(case)
162-
response = case.call(base_url=base_url, headers=headers)
166+
response = case.call(base_url=base_url, headers=headers, auth=auth)
163167
self._log_request(response)
164168
return response
165169

166170
@keyword
167-
def validate_response(self, case: Case, response: Response) -> None:
171+
def validate_response(
172+
self,
173+
case: Case,
174+
response: Response,
175+
headers: dict[str, Any] | None = None,
176+
auth: tuple[str, str] | HTTPDigestAuth | None = None,
177+
) -> None:
168178
"""Validate a Schemathesis response.
169179
170180
The response is validated against the case's expectations.
@@ -173,7 +183,12 @@ def validate_response(self, case: Case, response: Response) -> None:
173183
`Call` keyword documentation.
174184
"""
175185
self.info(f"Validating response: {response.status_code} | {response.headers}")
176-
case.validate_response(response)
186+
transport_kwargs: dict[str, Any] = {}
187+
if auth:
188+
transport_kwargs["auth"] = auth
189+
if headers:
190+
transport_kwargs["headers"] = headers
191+
case.validate_response(response=response, transport_kwargs=transport_kwargs)
177192
self.info("Response validation passed.")
178193

179194
def info(self, message: str) -> None:

uv.lock

Lines changed: 14 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)