Skip to content

Commit

Permalink
#700 #703 Improve local testing + alt schema url
Browse files Browse the repository at this point in the history
  • Loading branch information
filippomc committed Aug 23, 2023
1 parent 8a6d67c commit ac7c646
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ def get_user_password(main_user: ApplicationUser):
def get_app_environment(app_config: ApplicationHarnessConfig, app_domain, use_local_env=True):
my_env = os.environ.copy() if use_local_env else {}
my_env["APP_URL"] = app_domain

schema_file = f"applications/{app_config.name}/api/openapi.yaml"
if os.path.exists(schema_file):
my_env["APP_SCHEMA_FILE"] = schema_file

if app_config.accounts and app_config.accounts.users:
main_user: ApplicationUser = app_config.accounts.users[0]
Expand Down
54 changes: 36 additions & 18 deletions tools/cloudharness-test/cloudharness_test/apitest_init.py
Original file line number Diff line number Diff line change
@@ -1,44 +1,62 @@
import os
import logging
import requests

import schemathesis as st

from cloudharness.auth import get_token

if "APP_URL" in os.environ:

app_url = os.environ["APP_URL"]

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
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:
openapi_uri = app_schema
schema = st.from_file(openapi_uri)
else:
try:
openapi_uri = openapi_uri = app_url + "/openapi.json"
schema = st.from_file(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

except Exception as e:
raise Exception(
f"Cannot setup api tests: {openapi_uri}: {e}") 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}"

0 comments on commit ac7c646

Please sign in to comment.