11import os
22import logging
3+ import requests
34
45import schemathesis as st
56
67from 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