Skip to content

Commit 2e0ddcb

Browse files
committed
fix: added example for dynamic token auth
1 parent 74eca2b commit 2e0ddcb

File tree

2 files changed

+67
-0
lines changed

2 files changed

+67
-0
lines changed

README.md

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,3 +56,54 @@ Wrapper
5656
Call And Validate ${case}
5757
5858
```
59+
60+
# Authentication
61+
62+
## Dynamic token authentication
63+
Library currently supports Schemathesis
64+
[dynamic token](https://schemathesis.readthedocs.io/en/stable/guides/auth/#dynamic-token-authentication)
65+
authentication by the library import `auth` argument. The dynamic token generation
66+
class should follow the Schemathesis documentation. The only addition is the import.
67+
Importing the class must follow the Robot Framework library
68+
[import rules](https://robotframework.org/robotframework/latest/RobotFrameworkUserGuide.html#specifying-library-to-import)
69+
, example if importing with filename, filename much match to the class name. Example
70+
if test case looks like:
71+
72+
```robotframework
73+
*** Settings ***
74+
Library SchemathesisLibrary url=http://127.0.0.1/openapi.json auth=${CURDIR}/AuthExtension.py
75+
Test Template Wrapper
76+
77+
*** Test Cases ***
78+
All Tests
79+
Wrapper test_case_1
80+
81+
*** Keywords ***
82+
Wrapper
83+
[Arguments] ${case}
84+
Call And Validate ${case}
85+
```
86+
And `AuthExtension.py` looks like
87+
```python
88+
from base64 import b64encode
89+
90+
import schemathesis
91+
from robot.api import logger
92+
93+
94+
@schemathesis.auth()
95+
class AuthExtension:
96+
def get(self, case, ctx):
97+
# Instead of hard coding secrets to class, it is better to get them dynamically.
98+
# Jenkins or GitHub secrets, Azure keyvault, or from somewhere which is appropriate
99+
# for your needs.
100+
return b64encode("joulu:pukki".encode("utf-8")).decode("ascii")
101+
102+
def set(self, case, data, ctx):
103+
case.headers = case.headers or {}
104+
case.headers["Authorization"] = f"Basic {data}"
105+
logger.debug(f"Updated headers for case: {case.operation.method} {case.operation.path}")
106+
```
107+
Then with all API calls, will have
108+
[basic auth](https://en.wikipedia.org/wiki/Basic_access_authentication) set in the
109+
headers for all calls made to your API endpoint.

src/SchemathesisLibrary/__init__.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,22 @@ def __init__(
8888
| `path` | Path to the OpenAPI schema file. Using either `path` or `url` is mandatory. |
8989
| `url` | URL where the OpenAPI schema can be downloaded. |
9090
| `auth` | Optional authentication class to be used passed for Schemathesis authentication when test cases are executed. |
91+
92+
The `headers` argument is only needed when the schema is downloaded from a URL and there is need to pass example
93+
authentication headers to the endpoint. `headers` is not used the API calls are made during test execution.
94+
95+
`path` and `url` are mutually exclusive, only one of them should be used to specify the OpenAPI schema location.
96+
97+
`auth` can be used create Schemathesis
98+
[https://schemathesis.readthedocs.io/en/stable/guides/auth/#dynamic-token-authentication|dynamic token]
99+
authentication for the test cases. The dynamic token generation class should
100+
follow the Schemathesis documentation. The only addition is the import. Importing
101+
the class must follow the Robot Framework library
102+
[https://robotframework.org/robotframework/latest/RobotFrameworkUserGuide.html#specifying-library-to-import|import rules]
103+
, example if importing with filename, filename much match to the class name.
104+
See example from
105+
[https://github.com/aaltat/robotframework-schemathesis?tab=readme-ov-file##dynamic-token-authentication|README.md]
106+
file
91107
"""
92108
self.ROBOT_LIBRARY_LISTENER = self
93109
SchemathesisReader.options = Options(

0 commit comments

Comments
 (0)