-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #704 from MetaCell/feature/700
Fix and improve api tests
- Loading branch information
Showing
5 changed files
with
109 additions
and
27 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,27 @@ | ||
import os | ||
from pprint import pprint | ||
import schemathesis as st | ||
from schemathesis.checks import response_schema_conformance, not_a_server_error | ||
|
||
from cloudharness_test import apitest_init # include to perform default authorization | ||
|
||
app_url = os.environ.get("APP_URL", "http://samples.ch.local/api") | ||
|
||
try: | ||
schema = st.from_uri(app_url + "/openapi.json") | ||
except: | ||
# support alternative schema location | ||
schema = st.from_uri(app_url.replace("/api", "") + "/openapi.json") | ||
|
||
|
||
@schema.parametrize(endpoint="/ping") | ||
def test_ping(case): | ||
response = case.call() | ||
pprint(response.__dict__) | ||
assert response.status_code == 200, "this api errors on purpose" | ||
|
||
def test_state_machine(): | ||
schema.as_state_machine().run() | ||
# APIWorkflow = schema.as_state_machine() | ||
# APIWorkflow.run() | ||
# TestAPI = APIWorkflow.TestCase |
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
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
52 changes: 36 additions & 16 deletions
52
tools/cloudharness-test/cloudharness_test/apitest_init.py
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 |
---|---|---|
@@ -1,44 +1,64 @@ | ||
import os | ||
import logging | ||
import requests | ||
|
||
import schemathesis as st | ||
|
||
from cloudharness.auth import get_token | ||
|
||
if "APP_URL" in os.environ: | ||
if "APP_URL" or "APP_SCHEMA_FILE" in os.environ: | ||
app_schema = os.environ.get("APP_SCHEMA_FILE", None) | ||
app_url = os.environ.get("APP_URL", "http://samples.ch.local/api") | ||
logging.info("Start schemathesis tests on %s", app_url) | ||
if app_schema: | ||
# Test locally with harness-test -- use local schema for convenience during test development | ||
openapi_uri = app_schema | ||
schema = st.from_file(openapi_uri) | ||
else: | ||
# remote testing: might be /api/openapi.json or /openapi.json | ||
try: | ||
openapi_uri = openapi_uri = app_url + "/openapi.json" | ||
schema = st.from_uri(openapi_uri) | ||
except st.exceptions.SchemaLoadingError as e: | ||
# Use alternative configuration | ||
try: | ||
openapi_uri = app_url.replace("/api", "") + "/openapi.json" | ||
print(requests.get(openapi_uri)) | ||
schema = st.from_uri(openapi_uri) | ||
except st.exceptions.SchemaLoadingError as e: | ||
raise Exception( | ||
f"Cannot setup api tests: {openapi_uri} not valid. Check your deployment is up and configuration") from e | ||
|
||
app_url = os.environ["APP_URL"] | ||
except Exception as e: | ||
raise Exception( | ||
f"Cannot setup api tests: {openapi_uri}: {e}") from e | ||
|
||
try: | ||
openapi_uri = app_url + "/openapi.json" | ||
logging.info("Using openapi spec at %s", openapi_uri) | ||
schema = st.from_uri(openapi_uri) | ||
except Exception as e: | ||
raise Exception(f"Cannot setup api tests: {openapi_uri} not reachable. Check your deployment is up and configuration") from e | ||
logging.info("Using openapi spec at %s", openapi_uri) | ||
|
||
if "USERNAME" in os.environ and "PASSWORD" in os.environ: | ||
logging.info("Setting token from username and password") | ||
|
||
@st.auth.register() | ||
class TokenAuth: | ||
def get(self, context): | ||
|
||
username = os.environ["USERNAME"] | ||
password = os.environ["PASSWORD"] | ||
password = os.environ["PASSWORD"] | ||
|
||
return get_token(username, password) | ||
|
||
def set(self, case, data, context): | ||
case.headers = case.headers or {} | ||
case.headers["Authorization"] = f"Bearer {data}" | ||
case.headers["Cookie"] = f"kc-access={data}" | ||
case.headers["Authorization"] = f"Bearer {data}" | ||
case.headers["Cookie"] = f"kc-access={data}" | ||
else: | ||
@st.auth.register() | ||
class TokenAuth: | ||
def get(self, context): | ||
|
||
return "" | ||
|
||
def set(self, case, data, context): | ||
case.headers = case.headers or {} | ||
case.headers["Authorization"] = f"Bearer {data}" | ||
case.headers["Cookie"] = f"kc-access={data}" | ||
case.headers["Authorization"] = f"Bearer {data}" | ||
case.headers["Cookie"] = f"kc-access={data}" |