async def test_get_team_by_bane__success(): team_data = { "name": "team1", } db_team = Team(**team_data) with dal.get_session_ctx() as session: session.add(db_team) session.commit() session.refresh(db_team) team_details = team_schemas.Details.from_orm(db_team) async with AsyncClient(app=app, base_url=BASE_URL) as client: response = await client.get(f"/teams/name/{db_team.name}") assert response.status_code == 200 assert response.json() == team_details with dal.get_session_ctx() as session: session.delete(db_team) session.commit()
async def test_async_auth_history() -> None: """ Test that intermediate requests sent as part of an authentication flow are recorded in the response history. """ url = "https://example.org/" auth = RepeatAuth(repeat=2) client = AsyncClient(transport=AsyncMockTransport(auth_header=b"abc")) response = await client.get(url, auth=auth) assert response.status_code == 200 assert response.json() == {"auth": "Repeat abc.abc"} assert len(response.history) == 2 resp1, resp2 = response.history assert resp1.json() == {"auth": "Repeat 0"} assert resp2.json() == {"auth": "Repeat 1"} assert len(resp2.history) == 1 assert resp2.history == [resp1] assert len(resp1.history) == 0
async def embedd_batch( self, data: List[Any], proc_callback: Callable[[bool], None] = None ) -> List[Optional[List[float]]]: """ Function perform embedding of a batch of data items. Parameters ---------- data A list of data that must be embedded. proc_callback A function that is called after each item is fully processed by either getting a successful response from the server, getting the result from cache or skipping the item. Returns ------- List of float list (embeddings) for successfully embedded items and Nones for skipped items. Raises ------ EmbeddingCancelledException: If cancelled attribute is set to True (default=False). """ requests = [] async with AsyncClient( timeout=self.timeout, base_url=self.server_url, proxies=get_proxies() ) as client: for p in data: if self._cancelled: raise EmbeddingCancelledException() requests.append(self._send_to_server(p, client, proc_callback)) embeddings = await asyncio.gather(*requests) self._cache.persist_cache() assert self.num_parallel_requests == 0 return embeddings
async def test_ignoring_invalid_cookie( signer: typing.Type[Fernet], secret: str, session_id: str, app: FastAPI, settings: SessionSettings, ): """ Check that a session middleware ignores a request with an invalid cookie """ async def index(response: Response) -> Response: response.status_code = status.HTTP_200_OK return response session = SessionManager( secret=secret, signer=signer, settings=settings, on_load_cookie=AsyncMock(return_value=session_id), ) app.add_middleware( SessionMiddleware, manager=session, ) app.add_api_route("/", index) async with AsyncClient(app=app, base_url="http://testserver") as client: response = await client.get( "/", cookies={ # Try to temper a user session id settings.SESSION_COOKIE_NAME: sha256( session_id.encode("utf-8") ).hexdigest(), }, ) assert response.status_code == status.HTTP_200_OK
async def init() -> None: """ Set properties for the app object :return: None """ app.data_queue = Queue() app.environment = os.environ.get('ENVIRONMENT', 'LOCAL') app.parquet_path = Path().cwd().joinpath('parquet') app.pickle_path = Path().cwd().joinpath('offset.pickle') app.logger = set_logging() app.api_session = AsyncClient( headers={ "Content-Type": "application/json", }, transport=AsyncHTTPTransport(retries=5), timeout=Timeout(connect=300, read=300, write=300, pool=5) ) app.current_offset = await get_current_offset() app.api_pagination_limit = 500 app.buffer = Buffer(mb_max_size=1)
async def get_wishlish(steamid: int, settings: dict, proxy: dict = None) -> dict: ''' 异步读取Steam愿望单 参数: steamid: 64位SteamID settings: 配置字典 返回: dict: {appid:{游戏详情}} ''' global PIC_URL steam = settings.get('steam', {}) lang = steam.get('lang', 'schinese') sm_pic = steam.get('small_game_pic', True) PIC_URL = URLs.Steam_Game_Pic_SM if sm_pic else URLs.Steam_Game_Pic_MD async with AsyncClient(proxies=proxy) as client: client.cookies = { 'Cookie': f'wants_mature_content=1;Steam_Language={lang}' } try: count = await _get_page_count(client=client, steamid=steamid) except ValueError as e: logger.warning(f'{e}') return ({}) tasks = { asyncio.create_task( _get_single_page(client=client, steamid=steamid, page=p)) for p in range(0, count) } await asyncio.wait(tasks) wishlist = {} for task in tasks: wishlist.update(task.result()) return (wishlist)
async def test_host_with_auth_and_port_in_url(): """ The Host header should only include the hostname, or hostname:port (for non-default ports only). Any userinfo or default port should not be present. """ url = "http://*****:*****@example.org:80/echo_headers" client = AsyncClient(transport=MockTransport()) response = await client.get(url) assert response.status_code == 200 assert response.json() == { "headers": { "accept": "*/*", "accept-encoding": "gzip, deflate, br", "connection": "keep-alive", "host": "example.org", "user-agent": f"python-httpx/{__version__}", "authorization": "Basic dXNlcm5hbWU6cGFzc3dvcmQ=", } }
async def test_start(self, nursery): """ HTTP01Server.start starts both the HTTP server and the challenge receiver. """ http01 = HTTP01Server.build() chal_tx, chal_rx = trio.open_memory_channel(0) with trio.fail_after(2): await http01.start(nursery, chal_rx) async with AsyncClient(base_url="http://localhost:8000") as client: # Challenge we've never seen before. await assert_challenge_404(client, "abc123") # Active challenge. await chal_tx.send(Challenge("abc123", "accepted")) await assert_challenge_content(client, "abc123", b"accepted") # Deleted challenge. await chal_tx.send(Challenge("abc123", None)) await assert_challenge_404(client, "abc123")
async def validate_input(hass: core.HomeAssistant, data): """Validate the user input allows us to connect. Data has the keys from STEP_USER_DATA_SCHEMA with values provided by the user. """ session = AsyncClient() iotawatt = Iotawatt( data["name"], data["ip_address"], session, data["username"], data["password"] ) is_connected = await iotawatt.connect() _LOGGER.debug("isConnected: %s", is_connected) if not is_connected: raise CannotConnect # If you cannot connect: # throw CannotConnect # If the authentication is wrong: # InvalidAuth # Return info that you want to store in the config entry. return {"title": data["name"]}
async def password_reset( response:Response, email:Optional[str] = Form(...)) -> RedirectResponse: if email: try: async with AsyncClient() as client: data = await client.post(f"{API_WEBSITE_URL}/passwordResetting", json={"email": email, "passwordReset_url": f"{WEBSITE_URL}/password/change"}) if data.status_code == 200: response = RedirectResponse('/password/reset', status_code=status.HTTP_302_FOUND) response.set_cookie(key="message", value=json.dumps({'message': 'A password reset link has been sent to your email'}), max_age=1) return response response = RedirectResponse('/password/reset', status_code=status.HTTP_302_FOUND) response.set_cookie(key="error", value=data.text , max_age=1) return response except: response = RedirectResponse('/password/reset', status_code=status.HTTP_302_FOUND) response.set_cookie(key="error", value=json.dumps({'error':'Error resetting password'}),max_age=1) return response response = RedirectResponse('/password/reset', status_code=status.HTTP_302_FOUND) response.set_cookie(key="error", value=json.dumps({'error':'Your account email is required'}), max_age=1) return response
async def test_client_header(): """ Set a header in the Client. """ url = "http://example.org/echo_headers" headers = {"Example-Header": "example-value"} client = AsyncClient(dispatch=MockDispatch(), headers=headers) response = await client.get(url) assert response.status_code == 200 assert response.json() == { "headers": { "accept": "*/*", "accept-encoding": "gzip, deflate, br", "connection": "keep-alive", "content-length": "0", "example-header": "example-value", "host": "example.org", "user-agent": f"python-httpx/{__version__}", } }
async def _workflow(self) -> int: """Create base workflow if one does not exist.""" workflow_exists = False workflow_id = 0 workflow_payload = { "organization": { "organization/id": self.config["organization"]["id"] }, "title": self.config["workflow"]["title"], "type": "workflow/default", "handlers": [self.rems_user], } async with AsyncClient() as client: response = await client.get( f"{self.rems_api}/api/workflows", headers=self.headers, ) if response.status_code == 200: workflow_resp = response.json() for wkf in workflow_resp: if (wkf["organization"]["organization/id"] == self.config["organization"]["id"] and wkf["title"] == self.config["workflow"]["title"]): workflow_exists = True workflow_id = wkf["id"] LOG.info( f"Workflow {self.config['workflow']['title']} with id {workflow_id} exists." ) else: LOG.error( f"Retrieving workflows failed with HTTP status: {response.status_code}" ) if not workflow_exists: workflow_id = await self._process_create("workflows", workflow_payload) return workflow_id
async def test_host_with_non_default_port_in_url(): """ If the URL includes a non-default port, then it should be included in the Host header. """ url = "http://*****:*****@example.org:123/echo_headers" client = AsyncClient(dispatch=MockDispatch()) response = await client.get(url) assert response.status_code == 200 assert response.json() == { "headers": { "accept": "*/*", "accept-encoding": "gzip, deflate, br", "connection": "keep-alive", "content-length": "0", "host": "example.org:123", "user-agent": f"python-httpx/{__version__}", "authorization": "Basic dXNlcm5hbWU6cGFzc3dvcmQ=", } }
async def create_default_asset( dataset_name, version, dataset_metadata: Dict[str, Any] = generic_dataset_metadata, version_metadata: Dict[str, Any] = generic_version_metadata, ) -> Dict[str, Any]: # Create dataset, version, and default asset records. # The default asset is created automatically when the version is created. _ = await create_dataset(dataset_name, dataset_metadata) _ = await create_version(dataset_name, version, version_metadata) # Verify the default asset was created async with AsyncClient( app=app, base_url="http://test", trust_env=False ) as test_client: resp = await test_client.get(f"/meta/{dataset_name}/{version}/assets") assert len(resp.json()["data"]) == 1 assert resp.json()["status"] == "success" return resp.json()["data"][0]
async def standalone(cls) -> AsyncIterator[ComponentFactory]: """Build Gafaelfawr components outside of a request. Intended for background jobs. Uses the non-request default values for the dependencies of `ComponentFactory`. Do not use this factory inside the web application or anywhere that may use the default `ComponentFactory`, since they will interfere with each other's Redis pools. Yields ------ factory : `ComponentFactory` The factory. Must be used as a context manager. """ config = await config_dependency() token_cache = TokenCache() logger = structlog.get_logger(config.safir.logger_name) assert logger logger.debug("Connecting to Redis") redis = await redis_dependency(config) logger.debug("Connecting to PostgreSQL") engine = create_async_engine(config.database_url, future=True) try: session_factory = sessionmaker( engine, expire_on_commit=False, class_=AsyncSession ) async with session_factory() as session: async with AsyncClient() as client: yield cls( config=config, redis=redis, session=session, http_client=client, token_cache=token_cache, logger=logger, ) finally: await redis_dependency.aclose() await engine.dispose()
async def test_fastapi_custom_status_code_overridden(trace_transport, fastapi_app): """custom status code test - status code overridden by returned Response """ path = OVERRIDDEN_CUSTOM_STATUS_CODE_PATH request_path = f'{path}?x=testval' async with AsyncClient(app=fastapi_app, base_url="http://test") as ac: response = await ac.get(request_path) response_data = response.json() runner = trace_transport.last_trace.events[0] assert isinstance(runner, FastapiRunner) assert runner.resource['name'].startswith('127.0.0.1') assert runner.resource['metadata']['Path'] == path assert runner.resource['metadata'][ 'status_code'] == DEFAULT_SUCCESS_STATUS_CODE assert runner.resource['metadata']['Query Params'] == {'x': 'testval'} expected_response_data = _get_response_data(RETURN_VALUE) assert runner.resource['metadata']['Response Data'] == ( expected_response_data) assert runner.resource['metadata']['Query Params'] == {'x': 'testval'} assert response_data == expected_response_data # validating no `zombie` traces exist assert not trace_factory.traces
async def test_fastapi_handled_custom_exception( trace_transport, fastapi_app_with_exception_handlers): """ Test when the handler raises a custom exception and there's a matching exception handler. """ app = fastapi_app_with_exception_handlers request_path = f'{HANDLED_EXCEPTION_PATH}?x=testval' async with AsyncClient(app=app, base_url="http://test") as ac: response = await ac.get(request_path) response_data = response.json() runner = trace_transport.last_trace.events[0] assert isinstance(runner, FastapiRunner) assert runner.resource['name'].startswith('127.0.0.1') assert runner.resource['metadata']['Path'] == HANDLED_EXCEPTION_PATH assert runner.resource['metadata']['status_code'] == CUSTOM_STATUS_CODE assert runner.resource['metadata']['Response Data'] == ( CUSTON_EXCEPTION_HANDLER_RESPONSE) assert runner.resource['metadata']['Query Params'] == {'x': 'testval'} assert response_data == CUSTON_EXCEPTION_HANDLER_RESPONSE # validating no `zombie` traces exist assert not trace_factory.traces
async def query_intents( intent_names: List[str], query: str = "" ) -> List[CoCoIntentResult]: """ A thin wrapper to call the coco query intents endpoint. Arguments: intent_names: (List[str]) List of strings when each string is an existing and valid intent name. query: (string) The query that will be examined by each intent. Response: List of CoCo intent responses. """ payload = {"query": query, "intent_names": intent_names} async with AsyncClient() as http_client: http_resp = await http_client.post( f"{COCOHUB_URL}/v2/intent/query", json=payload, ) coco_resp = http_resp.json() return [CoCoIntentResult(**r) for r in coco_resp]
async def test_add_player_to_team__wrong_user_type(): user_data = { "username": "******", "password": "******", } team_data = { "name": "team 1", } db_team = Team(**team_data) db_user = User(**user_data) db_user.user_type = enums.UserType.ADMIN with dal.get_session_ctx() as session: session.add(db_team) session.add(db_user) session.commit() session.refresh(db_team) session.refresh(db_user) async with AsyncClient(app=app, base_url=BASE_URL) as client: response = await client.patch( f"/teams/{db_team.id}/add-player/{db_user.id}") assert response.status_code == 403 assert response.json() == { "detail": f"Only players can be part of teams. { db_user } has user_type: { db_user.user_type }" } with dal.get_session_ctx() as session: session.delete(db_team) session.delete(db_user) session.commit()
async def async_client_no_admin(db, init_db) -> AsyncGenerator[AsyncClient, None]: """Async Test Client.""" from app.main import app # mock authentication function to avoid having to reach out to RW API during tests app.dependency_overrides[is_admin] = bool_function_closure(False, with_args=False) app.dependency_overrides[is_service_account] = bool_function_closure( False, with_args=False) app.dependency_overrides[get_user] = get_user_mocked async with AsyncClient( app=app, base_url="http://test", trust_env=False, headers={"Origin": "https://www.globalforestwatch.org"}, ) as client: yield client # Clean up app.dependency_overrides = {}
async def get_lowest_price(plains: list, token: str, region: str, country: str, proxy: dict = None) -> dict: ''' 获取Steam商店史低价格 参数: plains: plain列表,例如['counterstrikeglobaloffensive'] token: ITAD的API token region: 地区 country: 国家 返回: dict: 价格字典,以plain为键名,每个键是(史低,史低折扣,史低时间) ''' params = { 'key': token, 'plains': '', 'region': region, 'country': country, 'shops': 'steam' } pricedict = {} if plains: async with AsyncClient(proxies=proxy) as client: max_ = len(plains) tasks = set() for i in range(0, max_, 5): part = plains[i:i + 5] tasks.add( asyncio.create_task(_get_lowest_price( client, params, part))) await asyncio.wait(tasks) for task in tasks: dic = task.result() pricedict.update(dic) return (pricedict)
async def signup(name: str, email: str) -> SignUp: """Obtain a token form RW API using given user name and password.""" headers = {"Content-Type": "application/json"} payload = {"name": name, "email": email, "apps": ["gfw"]} logger.debug(f"Create user account for user {name} with email {email}") url = f"{RW_API_URL}/auth/sign-up" try: async with AsyncClient() as client: response: HTTPXResponse = await client.post( url, json=payload, headers=headers ) except ReadTimeout: raise HTTPException( status_code=500, detail="Call to authorization server timed-out. Please try again.", ) if response.status_code == 422: raise HTTPException( status_code=422, detail="An account already exists for the provided email address.", ) elif response.status_code != 200: logger.error( "An error occurred while trying to create a new user account", response.json(), ) raise HTTPException( status_code=500, detail="An error occurred while trying to create a new user account. Please try again.", ) return SignUp(**response.json()["data"])
async def test_remove_uploaded_file_challenge__success(): challenge_data = { "name": "challenge old", "description": None, "flag": "secret flag", "file_name": "test_file.txt", } db_challenge = Challenge(**challenge_data) with dal.get_session_ctx() as session: session.add(db_challenge) session.commit() session.refresh(db_challenge) filepath = os.path.join(constants.UPLOAD_FILE_LOCATION, db_challenge.file_name) open(filepath, "w").close() async with AsyncClient(app=app, base_url=BASE_URL) as client: response = await client.post( f"/challenges/{db_challenge.id}/remove-file") with dal.get_session_ctx() as session: session.add(db_challenge) session.refresh(db_challenge) challenge_update = challenge_schemas.Details.from_orm(db_challenge) assert response.status_code == 200 assert response.json() == challenge_update assert not os.path.isfile(filepath) with dal.get_session_ctx() as session: session.delete(db_challenge) session.commit()
async def main( get_raw: bool = False, get_nice: bool = False, get_basic: bool = False, test: str | None = None, ) -> None: print("Saving test data ...") if not any([get_raw, get_nice, get_basic]): get_raw = get_nice = get_basic = True async with LifespanManager(app), AsyncClient(app=app, base_url="http://test") as ac: for to_download, query_data, endpoint, folder in ( (get_raw, test_raw_data, "raw", "test_data_raw"), (get_nice, test_nice_data, "nice", "test_data_nice"), (get_basic, test_basic_data, "basic", "test_data_basic"), ): if to_download: if test and test in query_data: query, file_name = query_data[test] await save_test_data(ac, endpoint, query, folder, file_name) else: for query, file_name in query_data.values(): await save_test_data(ac, endpoint, query, folder, file_name)
async def test_create_user__success(): app.dependency_overrides[validators.validate_admin] = lambda: None user_data = { "username": "******", "password": "******", } async with AsyncClient(app=app, base_url=BASE_URL) as client: response = await client.post("/users/", json=user_data) assert response.status_code == 200 assert "id" in response.json().keys() assert ("username", user_data["username"]) in response.json().items() with dal.get_session_ctx() as session: id = response.json()["id"] db_user = session.query(User).filter(User.id == id).first() session.delete(db_user) session.commit() app.dependency_overrides = {}
class AsyncSingletonSender(AsyncSender): """ Use one client for all instances and requests. Parameters ---------- httpx_kwargs keyword arguments for :meth:`httpx.AsyncClient.request` """ client = AsyncClient() def __init__(self, **httpx_kwargs): self.httpx_kwargs = httpx_kwargs or default_httpx_kwargs async def send(self, request: Request) -> Response: return await AsyncSingletonSender.client.request( request.method, request.url, data=request.data or None, params=request.params or None, headers=request.headers, **self.httpx_kwargs, )
async def test_event_dispatch(slack_event_manager): async def handler(data): assert json.dumps(data) == pytest.reaction_event_fixture, 'Wrong body' slack_event_manager.on_event('reaction_added', handler) timestamp = int(time()) data = pytest.reaction_event_fixture signature = pytest.create_signature(slack_event_manager.singing_secret, timestamp, data) async with AsyncClient(app=slack_event_manager.app, base_url='http://test') as client: response = await client.post('/', content=data, headers={ 'Content-Type': 'application/json', 'X-Slack-Request-Timestamp': str(timestamp), 'X-Slack-Signature': signature }) assert response.status_code == 200, 'Not valid response code'
async def async_client() -> AsyncClient: # Async用のengineとsessionを作成 async_engine = create_async_engine(ASYNC_DB_URL, echo=True) async_session = sessionmaker(autocommit=False, autoflush=False, bind=async_engine, class_=AsyncSession) # テスト用にオンメモリのSQLiteテーブルを初期化(関数ごとにリセット) async with async_engine.begin() as conn: await conn.run_sync(Base.metadata.drop_all) await conn.run_sync(Base.metadata.create_all) # DIを使ってFastAPIのDBの向き先をテスト用DBに変更 async def get_test_db(): async with async_session() as session: yield session app.dependency_overrides[get_db] = get_test_db # テスト用に非同期HTTPクライアントを返却 async with AsyncClient(app=app, base_url="http://test") as client: yield client
async def test_client_credentials_flow(self, authorized_client): # Generate a set of client credentials credentials = (await authorized_client.post(f"{v1}/credentials")).json() # Create a separate client and attempt to authenticate using the generated credentials async with AsyncClient(app=app, base_url="http://testserver") as client2: form_data = { "grant_type": "client_credentials", "client_id": credentials["client_id"], "client_secret": credentials["client_secret"], } response = await client2.post(f"{v1}/login/access-token", data=form_data) assert response.status_code == status.HTTP_200_OK token_data = response.json() assert "access_token" in token_data.keys() assert token_data["access_token"]
async def test_create_team__already_exists(): team_data = { "name": "team1", } db_team = Team(**team_data) with dal.get_session_ctx() as session: session.add(db_team) session.commit() session.refresh(db_team) async with AsyncClient(app=app, base_url=BASE_URL) as client: response = await client.post("/teams/", json=team_data) assert response.status_code == 409 assert response.json() == { "detail": f"The name: { team_data['name'] } is already taken" } with dal.get_session_ctx() as session: session.delete(db_team) session.commit()