Skip to content

Commit ac7c646

Browse files
committed
#700 #703 Improve local testing + alt schema url
1 parent 8a6d67c commit ac7c646

2 files changed

Lines changed: 39 additions & 19 deletions

File tree

  • libraries/cloudharness-utils/cloudharness_utils/testing
  • tools/cloudharness-test/cloudharness_test

libraries/cloudharness-utils/cloudharness_utils/testing/util.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ def get_user_password(main_user: ApplicationUser):
1111
def get_app_environment(app_config: ApplicationHarnessConfig, app_domain, use_local_env=True):
1212
my_env = os.environ.copy() if use_local_env else {}
1313
my_env["APP_URL"] = app_domain
14-
14+
schema_file = f"applications/{app_config.name}/api/openapi.yaml"
15+
if os.path.exists(schema_file):
16+
my_env["APP_SCHEMA_FILE"] = schema_file
1517

1618
if app_config.accounts and app_config.accounts.users:
1719
main_user: ApplicationUser = app_config.accounts.users[0]
Lines changed: 36 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,62 @@
11
import os
22
import logging
3+
import requests
34

45
import schemathesis as st
56

67
from cloudharness.auth import get_token
78

8-
if "APP_URL" in os.environ:
9-
10-
app_url = os.environ["APP_URL"]
11-
12-
try:
13-
openapi_uri = app_url + "/openapi.json"
14-
logging.info("Using openapi spec at %s", openapi_uri)
15-
schema = st.from_uri(openapi_uri)
16-
except Exception as e:
17-
raise Exception(f"Cannot setup api tests: {openapi_uri} not reachable. Check your deployment is up and configuration") from e
9+
if "APP_URL" or "APP_SCHEMA_FILE" in os.environ:
10+
app_schema = os.environ.get("APP_SCHEMA_FILE", None)
11+
app_url = os.environ.get("APP_URL", "http://samples.ch.local/api")
12+
logging.info("Start schemathesis tests on %s", app_url)
13+
if app_schema:
14+
openapi_uri = app_schema
15+
schema = st.from_file(openapi_uri)
16+
else:
17+
try:
18+
openapi_uri = openapi_uri = app_url + "/openapi.json"
19+
schema = st.from_file(openapi_uri)
20+
except st.exceptions.SchemaLoadingError as e:
21+
# Use alternative configuration
22+
try:
23+
openapi_uri = app_url.replace("/api", "") + "/openapi.json"
24+
print(requests.get(openapi_uri))
25+
schema = st.from_uri(openapi_uri)
26+
except st.exceptions.SchemaLoadingError as e:
27+
raise Exception(
28+
f"Cannot setup api tests: {openapi_uri} not valid. Check your deployment is up and configuration") from e
29+
30+
except Exception as e:
31+
raise Exception(
32+
f"Cannot setup api tests: {openapi_uri}: {e}") from e
33+
34+
logging.info("Using openapi spec at %s", openapi_uri)
1835

1936
if "USERNAME" in os.environ and "PASSWORD" in os.environ:
2037
logging.info("Setting token from username and password")
38+
2139
@st.auth.register()
2240
class TokenAuth:
2341
def get(self, context):
24-
42+
2543
username = os.environ["USERNAME"]
26-
password = os.environ["PASSWORD"]
27-
44+
password = os.environ["PASSWORD"]
45+
2846
return get_token(username, password)
2947

3048
def set(self, case, data, context):
3149
case.headers = case.headers or {}
32-
case.headers["Authorization"] = f"Bearer {data}"
33-
case.headers["Cookie"] = f"kc-access={data}"
50+
case.headers["Authorization"] = f"Bearer {data}"
51+
case.headers["Cookie"] = f"kc-access={data}"
3452
else:
3553
@st.auth.register()
3654
class TokenAuth:
3755
def get(self, context):
38-
56+
3957
return ""
4058

4159
def set(self, case, data, context):
4260
case.headers = case.headers or {}
43-
case.headers["Authorization"] = f"Bearer {data}"
44-
case.headers["Cookie"] = f"kc-access={data}"
61+
case.headers["Authorization"] = f"Bearer {data}"
62+
case.headers["Cookie"] = f"kc-access={data}"

0 commit comments

Comments
 (0)