def post_request( message_constructor: Callable[[dict[str, str]], dict[str, str]], url: Optional[str] = None, headers: Optional[dict[str, str]] = None, ) -> requests.Response: context = collect_context() if not url: url = retrieve_from_passwordstore(context["PARAMETER_WEBHOOK_URL"]) if url is None: sys.stderr.write("No URL was retrieved from passwordstore") sys.exit(2) serialized_proxy_config = context.get("PARAMETER_PROXY_URL") verify: bool = True if "PARAMETER_IGNORE_SSL" in context: verify = False try: response = requests.post( url=url, json=message_constructor(context), proxies=typeshed_issue_7724( deserialize_http_proxy_config( serialized_proxy_config).to_requests_proxies()), headers=headers, verify=verify, ) except requests.exceptions.ProxyError: sys.stderr.write("Cannot connect to proxy: %s\n" % serialized_proxy_config) sys.exit(2) return response
def post(self, url: str, data: str, headers: Mapping[str, str]) -> requests.Response: return self._session.post( f"{self._urls[self._urlidx]}{url}", data=data, headers=typeshed_issue_7724(headers), timeout=self._timeout, )
def test_registration_status_uuid_mismtach( client: TestClient, uuid: UUID, registration_status_headers: Mapping[str, str], ) -> None: response = client.get( "/registration_status/123", headers=typeshed_issue_7724(registration_status_headers), ) assert response.status_code == 422
def get_request_json_decoded( self, api_endpoint: str, params: Mapping[str, Any], ) -> Any: return requests.get( f"{self._api_url}/{api_endpoint}", headers=self._query_heads, params=params, proxies=typeshed_issue_7724(self._proxy.to_requests_proxies()), ).json()
def test_registration_status_host_not_registered( client: TestClient, uuid: UUID, registration_status_headers: Mapping[str, str], ) -> None: response = client.get( f"/registration_status/{uuid}", headers=typeshed_issue_7724(registration_status_headers), ) assert response.status_code == 404 assert response.json() == {"detail": "Host is not registered"}
def test_agent_data_no_host( client: TestClient, uuid: UUID, agent_data_headers: Mapping[str, str], compressed_agent_data: io.BytesIO, ) -> None: response = client.post( f"/agent_data/{uuid}", headers=typeshed_issue_7724(agent_data_headers), files={"monitoring_data": ("filename", compressed_agent_data)}, ) assert response.status_code == 403 assert response.json() == {"detail": "Host is not registered"}
def test_agent_data_invalid_data( client: TestClient, uuid: UUID, agent_data_headers: Mapping[str, str], ) -> None: response = client.post( f"/agent_data/{uuid}", headers=typeshed_issue_7724(agent_data_headers), files={ "monitoring_data": ("filename", io.BytesIO(b"certainly invalid")) }, ) assert response.status_code == 400 assert response.json() == {"detail": "Decompression of agent data failed"}
def test_agent_data_move_ready( client: TestClient, uuid: UUID, agent_data_headers: Mapping[str, str], compressed_agent_data: io.BytesIO, ) -> None: (path_ready := site_context.r4r_dir() / "READY").mkdir() (path_ready / f"{uuid}.json").touch() client.post( f"/agent_data/{uuid}", headers=typeshed_issue_7724(agent_data_headers), files={"monitoring_data": ("filename", compressed_agent_data)}, ) assert (site_context.r4r_dir() / "DISCOVERABLE" / f"{uuid}.json").exists()
def test_agent_data_uuid_mismatch( client: TestClient, uuid: UUID, agent_data_headers: Mapping[str, str], compressed_agent_data: io.BytesIO, ) -> None: response = client.post( "/agent_data/123", headers=typeshed_issue_7724(agent_data_headers), files={"monitoring_data": ("filename", compressed_agent_data)}, ) assert response.status_code == 400 assert response.json() == { "detail": f"Verified client UUID ({uuid}) does not match UUID in URL (123)" }
def test_agent_data_move_error( caplog, client: TestClient, uuid: UUID, agent_data_headers: Mapping[str, str], compressed_agent_data: io.BytesIO, ) -> None: with mock.patch("agent_receiver.endpoints.Path.rename") as move_mock: move_mock.side_effect = FileNotFoundError() response = client.post( f"/agent_data/{uuid}", headers=typeshed_issue_7724(agent_data_headers), files={"monitoring_data": ("filename", compressed_agent_data)}, ) assert response.status_code == 204 assert caplog.records[0].message == f"uuid={uuid} Agent data saved"
def test_agent_data_success( tmp_path: Path, client: TestClient, uuid: UUID, agent_data_headers: Mapping[str, str], compressed_agent_data: io.BytesIO, ) -> None: response = client.post( f"/agent_data/{uuid}", headers=typeshed_issue_7724(agent_data_headers), files={"monitoring_data": ("filename", compressed_agent_data)}, ) file_path = tmp_path / "hostname" / "agent_output" assert file_path.read_text() == "mock file" assert response.status_code == 204
def test_registration_status_push_host( client: TestClient, uuid: UUID, registration_status_headers: Mapping[str, str], ) -> None: (path_discoverable := site_context.r4r_dir() / "DISCOVERABLE").mkdir() (path_discoverable / f"{uuid}.json").touch() response = client.get( f"/registration_status/{uuid}", headers=typeshed_issue_7724(registration_status_headers), ) assert response.status_code == 200 assert response.json() == { "hostname": "hostname", "status": "discoverable", "type": HostTypeEnum.PUSH.value, "message": "Host registered", }
def test_agent_data_pull_host( tmp_path: Path, client: TestClient, uuid: UUID, agent_data_headers: Mapping[str, str], compressed_agent_data: io.BytesIO, ) -> None: source = site_context.agent_output_dir() / str(uuid) source.symlink_to(tmp_path / "hostname") response = client.post( f"/agent_data/{uuid}", headers=typeshed_issue_7724(agent_data_headers), files={"monitoring_data": ( "filename", compressed_agent_data, )}, ) assert response.status_code == 403 assert response.json() == {"detail": "Host is not a push host"}
def test_registration_status_declined( client: TestClient, uuid: UUID, registration_status_headers: Mapping[str, str], ) -> None: (path_declined := site_context.r4r_dir() / "DECLINED").mkdir() (path_declined / f"{uuid}.json").write_text( json.dumps({"message": "Registration request declined"})) response = client.get( f"/registration_status/{uuid}", headers=typeshed_issue_7724(registration_status_headers), ) assert response.status_code == 200 assert response.json() == { "hostname": None, "status": "declined", "type": None, "message": "Registration request declined", }
def test_registration_status_pull_host( tmp_path: Path, client: TestClient, uuid: UUID, registration_status_headers: Mapping[str, str], ) -> None: source = site_context.agent_output_dir() / str(uuid) target_dir = tmp_path / "hostname" source.symlink_to(target_dir) response = client.get( f"/registration_status/{uuid}", headers=typeshed_issue_7724(registration_status_headers), ) assert response.status_code == 200 assert response.json() == { "hostname": "hostname", "status": None, "type": HostTypeEnum.PULL.value, "message": "Host registered", }