-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'next' into feat/api-signup
- Loading branch information
Showing
19 changed files
with
2,155 additions
and
311 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
name: OpenAPI Validation & HTTP Test | ||
|
||
on: | ||
push: | ||
branches: | ||
- master | ||
- next | ||
pull_request: | ||
|
||
jobs: | ||
build: | ||
|
||
runs-on: ubuntu-20.04 | ||
strategy: | ||
max-parallel: 4 | ||
matrix: | ||
python-version: ["3.11.9"] | ||
|
||
steps: | ||
- uses: actions/checkout@v2 | ||
- name: Set up Python ${{ matrix.python-version }} | ||
uses: actions/setup-python@v4 | ||
with: | ||
python-version: ${{ matrix.python-version }} | ||
cache: 'pip' | ||
- name: Install Dependencies | ||
run: | | ||
python -m pip install --upgrade pip | ||
pip install -r requirements.txt | ||
pip install -r openapi/requirements.txt | ||
- uses: actions/setup-node@v4 | ||
with: | ||
node-version: 18 | ||
- run: npm i -g dredd | ||
- run: dredd --config openapi/dredd.yml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
color: true | ||
dry-run: null | ||
hookfiles: ["openapi/dredd_hook.py"] | ||
language: python | ||
require: null | ||
server: "python3 manage.py testserver stregsystem/fixtures/openapi-fixture.json" | ||
server-wait: 5 | ||
init: false | ||
custom: {} | ||
names: false | ||
only: [] | ||
reporter: apiary | ||
output: [] | ||
header: [] | ||
sorted: false | ||
user: null | ||
inline-errors: false | ||
details: false | ||
method: [] | ||
loglevel: warn | ||
path: [] | ||
hooks-worker-timeout: 5000 | ||
hooks-worker-connect-timeout: 1500 | ||
hooks-worker-connect-retry: 500 | ||
hooks-worker-after-connect-wait: 100 | ||
hooks-worker-term-timeout: 5000 | ||
hooks-worker-term-retry: 500 | ||
hooks-worker-handler-host: 127.0.0.1 | ||
hooks-worker-handler-port: 61321 | ||
config: ./dredd.yml | ||
blueprint: openapi/stregsystem.yaml | ||
endpoint: 'http://127.0.0.1:8000' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import dredd_hooks as hooks | ||
import json | ||
from utils import update_query_parameter_values, update_dictionary_values | ||
|
||
not_found_parameter_values = { | ||
'room_id': 1, | ||
'member_id': 1, | ||
'username': "404_user", | ||
} | ||
|
||
skipped_endpoints = [ | ||
"GET (400) /api/member/payment/qr?username=kresten" # Skipped: test can't be implemented properly in OpenAPI | ||
] | ||
|
||
@hooks.before_each | ||
def skip_endpoint(transaction): | ||
if transaction['id'] in skipped_endpoints: | ||
print(f"Skipping endpoint: {transaction['id']}") | ||
transaction['skip'] = True | ||
|
||
|
||
# https://dredd.org/en/latest/data-structures.html#transaction-object | ||
@hooks.before_each | ||
def replace_4xx_parameter_values(transaction): | ||
""" | ||
It isn't possible to specify individual parameter example values for each response type in OpenAPI. | ||
To properly test the return value of not-found parameters, replace all parameters. | ||
""" | ||
if transaction['expected']['statusCode'][0] == '4': | ||
new_path = update_query_parameter_values(transaction['fullPath'], not_found_parameter_values) | ||
print(f"Update endpoint path, from '{transaction['fullPath']}' to '{new_path}'") | ||
transaction['fullPath'] = new_path | ||
transaction['request']['uri'] = new_path | ||
|
||
|
||
@hooks.before_each | ||
def replace_body_in_post_requests(transaction): | ||
if transaction['expected']['statusCode'][0] == '4' and transaction['id'].startswith("POST"): | ||
body = json.loads(transaction['request']['body']) | ||
update_dictionary_values(body, not_found_parameter_values) | ||
|
||
transaction['request']['body'] = json.dumps(body) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
dredd_hooks |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
from typing import Any | ||
from urllib.parse import parse_qs, urlencode, urlparse, urlunparse | ||
|
||
|
||
def update_dictionary_values(original: dict[Any, Any], replacement: dict[Any, Any]) -> None: | ||
""" | ||
Same as dict.update(...) but doesn't add new keys. | ||
Mutates 'original' | ||
:param original: The dictionary to mutate. | ||
:param replacement: The dictionary with values to update the original with. | ||
""" | ||
original.update({k: v for k, v in replacement.items() if k in original}) | ||
|
||
|
||
def update_query_parameter_values(url_string: str, new_parameter_values: dict[str, Any]) -> str: | ||
""" | ||
Updates query parameters with new parameter values from new_parameter_values. | ||
:param url_string: The URL path of which to modify query parameters. | ||
:param new_parameter_values: The dictionary with new query parameter values. | ||
:return: The URL with updated query parameter. | ||
""" | ||
parsed_url = urlparse(url_string) | ||
|
||
qs_dict = parse_qs(parsed_url.query, keep_blank_values=True) | ||
qs_dict_flattened = {key: value[0] for key, value in qs_dict.items()} | ||
update_dictionary_values(qs_dict_flattened, new_parameter_values) | ||
updated_qs = urlencode(qs_dict_flattened, doseq=False) | ||
|
||
parsed_url = parsed_url._replace(query=updated_qs) | ||
|
||
return str(urlunparse(parsed_url)) |
Oops, something went wrong.