Skip to content

Commit

Permalink
use mock http for integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
magland committed Oct 25, 2023
1 parent 1a5134e commit 7dafccb
Show file tree
Hide file tree
Showing 3 changed files with 180 additions and 52 deletions.
4 changes: 2 additions & 2 deletions python/protocaas/api_helpers/routers/gui/project_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
router = APIRouter()

# get project
class GetProjectReponse(BaseModel):
class GetProjectResponse(BaseModel):
project: ProtocaasProject
success: bool

Expand All @@ -23,7 +23,7 @@ async def get_project(project_id):
try:
project = await fetch_project(project_id)
assert project is not None, f"No project with ID {project_id}"
return GetProjectReponse(project=project, success=True)
return GetProjectResponse(project=project, success=True)
except Exception as e:
traceback.print_exc()
raise HTTPException(status_code=500, detail=str(e)) from e
Expand Down
97 changes: 97 additions & 0 deletions python/protocaas/common/_api_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,3 +168,100 @@ def _client_get_api_request(*,
print(f'Error in client get api request for {url}')
raise
return resp.json()

####################################################################################################
# The GUI API requests below are only here for use with pytest since the real GUI requests come from the browser using typescript

def _gui_get_api_request(*,
url_path: str,
github_access_token: str
):
headers = {
'github-access-token': github_access_token
}
test_client = _globals['test_client']
if test_client is None:
url = f'{protocaas_url}{url_path}'
client = requests

Check warning on line 185 in python/protocaas/common/_api_request.py

View check run for this annotation

Codecov / codecov/patch

python/protocaas/common/_api_request.py#L184-L185

Added lines #L184 - L185 were not covered by tests
else:
assert url_path.startswith('/api')
url = url_path
client = test_client
try:
resp = client.get(url, headers=headers, timeout=60)
resp.raise_for_status()
except: # noqa E722
print(f'Error in gui get api request for {url}')
raise

Check warning on line 195 in python/protocaas/common/_api_request.py

View check run for this annotation

Codecov / codecov/patch

python/protocaas/common/_api_request.py#L193-L195

Added lines #L193 - L195 were not covered by tests
return resp.json()

def _gui_post_api_request(*,
url_path: str,
github_access_token: str,
data: dict
):
headers = {
'github-access-token': github_access_token
}
test_client = _globals['test_client']
if test_client is None:
url = f'{protocaas_url}{url_path}'
client = requests

Check warning on line 209 in python/protocaas/common/_api_request.py

View check run for this annotation

Codecov / codecov/patch

python/protocaas/common/_api_request.py#L208-L209

Added lines #L208 - L209 were not covered by tests
else:
assert url_path.startswith('/api')
url = url_path
client = test_client
try:
resp = client.post(url, headers=headers, json=data, timeout=60)
resp.raise_for_status()
except: # noqa E722
print(f'Error in gui post api request for {url}')
raise

Check warning on line 219 in python/protocaas/common/_api_request.py

View check run for this annotation

Codecov / codecov/patch

python/protocaas/common/_api_request.py#L217-L219

Added lines #L217 - L219 were not covered by tests
return resp.json()

def _gui_put_api_request(*,
url_path: str,
github_access_token: str,
data: dict
):
headers = {
'github-access-token': github_access_token
}
test_client = _globals['test_client']
if test_client is None:
url = f'{protocaas_url}{url_path}'
client = requests

Check warning on line 233 in python/protocaas/common/_api_request.py

View check run for this annotation

Codecov / codecov/patch

python/protocaas/common/_api_request.py#L232-L233

Added lines #L232 - L233 were not covered by tests
else:
assert url_path.startswith('/api')
url = url_path
client = test_client
try:
resp = client.put(url, headers=headers, json=data, timeout=60)
resp.raise_for_status()
except: # noqa E722
print(f'Error in gui put api request for {url}')
raise

Check warning on line 243 in python/protocaas/common/_api_request.py

View check run for this annotation

Codecov / codecov/patch

python/protocaas/common/_api_request.py#L241-L243

Added lines #L241 - L243 were not covered by tests
return resp.json()

def _gui_delete_api_request(*,
url_path: str,
github_access_token: str
):
headers = {
'github-access-token': github_access_token
}
test_client = _globals['test_client']
if test_client is None:
url = f'{protocaas_url}{url_path}'
client = requests

Check warning on line 256 in python/protocaas/common/_api_request.py

View check run for this annotation

Codecov / codecov/patch

python/protocaas/common/_api_request.py#L255-L256

Added lines #L255 - L256 were not covered by tests
else:
assert url_path.startswith('/api')
url = url_path
client = test_client
try:
resp = client.delete(url, headers=headers, timeout=60)
resp.raise_for_status()
except: # noqa E722
print(f'Error in gui delete api request for {url}')
raise

Check warning on line 266 in python/protocaas/common/_api_request.py

View check run for this annotation

Codecov / codecov/patch

python/protocaas/common/_api_request.py#L264-L266

Added lines #L264 - L266 were not covered by tests
return resp.json()
Loading

0 comments on commit 7dafccb

Please sign in to comment.