22from pathlib import Path
33from tempfile import NamedTemporaryFile
44from typing import Any , Callable , Dict , List
5+ from unittest .mock import AsyncMock , MagicMock
56
67import pytest as pytest
78from aleph_message .models import AggregateMessage , AlephMessage , PostMessage
1011import aleph .sdk .chains .sol as solana
1112import aleph .sdk .chains .substrate as substrate
1213import aleph .sdk .chains .tezos as tezos
14+ from aleph .sdk import AlephHttpClient , AuthenticatedAlephHttpClient
1315from aleph .sdk .chains .common import get_fallback_private_key
16+ from aleph .sdk .types import Account
1417
1518
1619@pytest .fixture
@@ -111,6 +114,12 @@ def aleph_messages() -> List[AlephMessage]:
111114 ]
112115
113116
117+ @pytest .fixture
118+ def json_post () -> dict :
119+ with open (Path (__file__ ).parent / "post.json" , "r" ) as f :
120+ return json .load (f )
121+
122+
114123@pytest .fixture
115124def raw_messages_response (aleph_messages ) -> Callable [[int ], Dict [str , Any ]]:
116125 return lambda page : {
@@ -122,3 +131,85 @@ def raw_messages_response(aleph_messages) -> Callable[[int], Dict[str, Any]]:
122131 "pagination_per_page" : max (len (aleph_messages ), 20 ),
123132 "pagination_total" : len (aleph_messages ) if page == 1 else 0 ,
124133 }
134+
135+
136+ @pytest .fixture
137+ def raw_posts_response (json_post ) -> Callable [[int ], Dict [str , Any ]]:
138+ return lambda page : {
139+ "posts" : [json_post ] if int (page ) == 1 else [],
140+ "pagination_item" : "posts" ,
141+ "pagination_page" : int (page ),
142+ "pagination_per_page" : 1 ,
143+ "pagination_total" : 1 if page == 1 else 0 ,
144+ }
145+
146+
147+ class MockResponse :
148+ def __init__ (self , sync : bool ):
149+ self .sync = sync
150+
151+ async def __aenter__ (self ):
152+ return self
153+
154+ async def __aexit__ (self , exc_type , exc_val , exc_tb ):
155+ ...
156+
157+ async def raise_for_status (self ):
158+ ...
159+
160+ @property
161+ def status (self ):
162+ return 200 if self .sync else 202
163+
164+ async def json (self ):
165+ message_status = "processed" if self .sync else "pending"
166+ return {
167+ "message_status" : message_status ,
168+ "publication_status" : {"status" : "success" , "failed" : []},
169+ }
170+
171+ async def text (self ):
172+ return json .dumps (await self .json ())
173+
174+
175+ @pytest .fixture
176+ def mock_session_with_post_success (
177+ ethereum_account : Account ,
178+ ) -> AuthenticatedAlephHttpClient :
179+ http_session = AsyncMock ()
180+ http_session .post = MagicMock ()
181+ http_session .post .side_effect = lambda * args , ** kwargs : MockResponse (
182+ sync = kwargs .get ("sync" , False )
183+ )
184+
185+ client = AuthenticatedAlephHttpClient (
186+ account = ethereum_account , api_server = "http://localhost"
187+ )
188+ client .http_session = http_session
189+
190+ return client
191+
192+
193+ def make_custom_mock_response (resp_json , status = 200 ) -> MockResponse :
194+ class CustomMockResponse (MockResponse ):
195+ async def json (self ):
196+ return resp_json
197+
198+ @property
199+ def status (self ):
200+ return status
201+
202+ return CustomMockResponse (sync = True )
203+
204+
205+ def make_mock_get_session (get_return_value : Dict [str , Any ]) -> AlephHttpClient :
206+ class MockHttpSession (AsyncMock ):
207+ def get (self , * _args , ** _kwargs ):
208+ return make_custom_mock_response (get_return_value )
209+
210+ http_session = MockHttpSession ()
211+
212+ client = AlephHttpClient (api_server = "http://localhost" )
213+ client .http_session = http_session
214+
215+ return client
0 commit comments