def test_index(client: RequestHandler, ssm) -> None: ssm.put_parameter(Name="/snyker/VALUE", Value="Gareth", Type="SecureString") response = client.get("/") assert response.status_code == HTTPStatus.OK assert response.json == {"hello": "Gareth"}
def test_exception_boom(app, client: RequestHandler) -> None: response = client.get("/boom") assert response.status_code == 500 assert response.json == dict( [ ("Code", "InternalServerError"), ("Message", "An internal server error occurred."), ] )
def test_has_request(app, capture_events, client: RequestHandler): events = capture_events() response = client.get("/context") assert response.status_code == 500 (event,) = events assert event["level"] == "error" (exception,) = event["exception"]["values"] assert exception["type"] == "Exception"
def test_download_latest_dne(modules_api: str, client: RequestHandler, monkeypatch) -> None: from chalicelib.models import ModuleModel fqmn = "namespace/name/provider" monkeypatch.setattr(ModuleModel, "query", lambda *args, **kwargs: []) response = client.get(f"{modules_api}/{fqmn}/download") assert response.status_code == HTTPStatus.NOT_FOUND
def test_bad_reques(client: RequestHandler) -> None: response = client.get("/badrequest") assert response.status_code == 400 assert response.json == dict( [ ("Code", "BadRequestError"), ("Message", "BadRequestError: bad-request"), ] )
def test_catch_exception_auth(client: RequestHandler) -> None: with mock.patch( 'chalicelib.resources.arcus_read_only.Client') as ClientTesting: ClientTesting.return_value.get.side_effect = InvalidAuth() headers: dict = {'X-ARCUS-SANDBOX': 'true'} response = client.get('/test?page=1', headers=headers) assert response.status_code == 401 assert response.json["message"] == "Invalid Authentication Token"
def test_with_parameters(client: RequestHandler) -> None: with mock.patch( 'chalicelib.resources.arcus_read_only.Client') as ClientTesting: ClientTesting.return_value.get.return_value = dict(resp="Success!!!") headers: dict = {'X-ARCUS-SANDBOX': 'true'} response = client.get('/test?page=1', headers=headers) assert response.status_code == 200 assert response.json["resp"] == "Success!!!"
def test_read_only_accounts(client: RequestHandler) -> None: with mock.patch( 'chalicelib.resources.arcus_read_only.Client') as ClientTesting: ClientTesting.return_value.accounts = dict( primary=Account(), topup=Account(), ) headers: dict = {'X-ARCUS-SANDBOX': 'true'} response = client.get('/account', headers=headers) assert response.status_code == 200 assert response.json["primary"] is not None assert response.json["topup"] is not None
def test_list_versions(modules_api: str, client: RequestHandler, monkeypatch) -> None: from chalicelib.models import ModuleModel fqmn = "namespace/name/provider" versions = ["0.1.0", "0.2.0", "0.3.0"] def mock_query(hash_key, **kwargs): return [ModuleModel(hash_key, version=version) for version in versions] monkeypatch.setattr(ModuleModel, "query", mock_query) response = client.get(f"{modules_api}/{fqmn}/versions") response_versions = [ v["version"] for v in response.json["modules"][0]["versions"] ] assert response_versions == versions
def test_list_versions_response_schema(modules_api: str, client: RequestHandler) -> None: fqmn = "namespace/name/provider" response = client.get(f"{modules_api}/{fqmn}/versions") assert response.status_code == HTTPStatus.OK assert "modules" in response.json modules = response.json["modules"] assert type(modules) == list assert len(modules) == 1 first_module = modules[0] assert "source" in first_module assert first_module["source"] == fqmn assert "versions" in first_module assert type(first_module["versions"]) == list
def test_download_latest(modules_api: str, client: RequestHandler, monkeypatch) -> None: from chalicelib.models import ModuleModel fqmn = "namespace/name/provider" versions = ["0.0.0", "0.9.0", "0.10.0"] fqvmn = f"{fqmn}/{versions[-1]}" def mock_query(hash_key, **kwargs): from random import shuffle shuffle(versions) return [ModuleModel(hash_key, version=version) for version in versions] monkeypatch.setattr(ModuleModel, "query", mock_query) response = client.get(f"{modules_api}/{fqmn}/download") assert response.status_code == HTTPStatus.FOUND assert "Location" in response.headers assert response.headers["Location"] == f"{modules_api}/{fqvmn}/download"
def test_download_success(modules_api: str, client: RequestHandler, monkeypatch) -> None: from chalicelib.models import ModuleModel fqvmn = "namespace/name/provider/0.1.0" def mock_get(hash_key, **kwargs): rv = ModuleModel(hash_key, version=kwargs["range_key"]) rv.getter_url = "./name" return rv def mock_save(*args, **kwargs): pass monkeypatch.setattr(ModuleModel, "get", mock_get) monkeypatch.setattr(ModuleModel, "save", mock_save) response = client.get(f"{modules_api}/{fqvmn}/download") assert response.status_code == HTTPStatus.NO_CONTENT assert response.headers["X-Terraform-Get"] == "./name"
def test_index(self, client: RequestHandler): response: ResponseHandler = client.get("/") assert response.status_code == HTTPStatus.OK assert response.json == {"hello": "world"}
def fixture_modules_api(client: RequestHandler) -> str: response = client.get("/.well-known/terraform.json") return response.json["modules.v1"][:-1]
def test_download_failure(modules_api: str, client: RequestHandler) -> None: fqvmn = "namespace/name/provider/0.0.0" response = client.get(f"{modules_api}/{fqvmn}/download") assert response.status_code == HTTPStatus.NOT_FOUND assert type(response.json["errors"]) == list
def test_discovery(client: RequestHandler) -> None: response = client.get("/.well-known/terraform.json") assert response.status_code == HTTPStatus.OK assert response.json == {"modules.v1": "/modules/"}
def test_index(client: RequestHandler) -> None: response = client.get('/') assert response.status_code == HTTPStatus.OK assert response.json == {'hello': 'world'}
def test_healthcheck(client: RequestHandler) -> None: response = client.get('/') assert response.status_code == 200 assert response.json == dict(greeting="I'm healthy")