From b8027c221487870d109144d8435b92b207160f64 Mon Sep 17 00:00:00 2001 From: David McKenna Date: Sun, 3 Aug 2025 16:16:22 -0300 Subject: [PATCH 1/5] Migrated GraphAPI, BasicDisplayAPI, ThreadsGraphAPI to Httpx --- .../api/facebook/common_edges/albums.py | 4 +- .../api/facebook/common_edges/comments.py | 4 +- pyfacebook/api/facebook/common_edges/feed.py | 8 +- pyfacebook/api/facebook/common_edges/likes.py | 12 +- .../api/facebook/common_edges/live_videos.py | 4 +- .../api/facebook/common_edges/photos.py | 4 +- .../api/facebook/common_edges/videos.py | 4 +- pyfacebook/api/facebook/resource/album.py | 8 +- .../api/facebook/resource/application.py | 8 +- pyfacebook/api/facebook/resource/business.py | 8 +- pyfacebook/api/facebook/resource/comment.py | 20 +- .../api/facebook/resource/conversation.py | 8 +- pyfacebook/api/facebook/resource/event.py | 8 +- pyfacebook/api/facebook/resource/group.py | 8 +- .../api/facebook/resource/live_video.py | 8 +- pyfacebook/api/facebook/resource/message.py | 8 +- pyfacebook/api/facebook/resource/page.py | 24 +- pyfacebook/api/facebook/resource/photo.py | 8 +- pyfacebook/api/facebook/resource/post.py | 8 +- pyfacebook/api/facebook/resource/user.py | 20 +- pyfacebook/api/facebook/resource/video.py | 8 +- pyfacebook/api/graph.py | 132 +++-- .../api/instagram_basic/resource/media.py | 12 +- .../api/instagram_basic/resource/user.py | 8 +- .../instagram_business/resource/comment.py | 20 +- .../instagram_business/resource/container.py | 8 +- .../instagram_business/resource/hashtag.py | 16 +- .../api/instagram_business/resource/media.py | 24 +- .../api/instagram_business/resource/user.py | 64 +- pyfacebook/utils/facebook_compliance_fix.py | 25 + pyproject.toml | 8 +- tests/facebook/edges/test_likes.py | 69 ++- tests/facebook/test_album.py | 89 ++- tests/facebook/test_application.py | 58 +- tests/facebook/test_business.py | 55 +- tests/facebook/test_comment.py | 159 +++-- tests/facebook/test_conversation.py | 58 +- tests/facebook/test_event.py | 50 +- tests/facebook/test_group.py | 143 +++-- tests/facebook/test_live_video.py | 106 ++-- tests/facebook/test_message.py | 57 +- tests/facebook/test_page.py | 464 ++++++++++----- tests/facebook/test_photo.py | 104 ++-- tests/facebook/test_post.py | 147 +++-- tests/facebook/test_user.py | 170 +++--- tests/facebook/test_video.py | 106 ++-- tests/instagram_basic/test_media.py | 91 ++- tests/instagram_basic/test_user.py | 84 ++- tests/instagram_business/test_comment.py | 151 +++-- tests/instagram_business/test_hashtag.py | 172 ++++-- tests/instagram_business/test_media.py | 222 ++++--- tests/instagram_business/test_publish.py | 62 +- tests/instagram_business/test_user.py | 558 ++++++++++++------ tests/test_basic_display_api.py | 69 ++- tests/test_graph.py | 420 ++++++++----- tests/test_threads_graph_api.py | 70 ++- 56 files changed, 2700 insertions(+), 1543 deletions(-) create mode 100644 pyfacebook/utils/facebook_compliance_fix.py diff --git a/pyfacebook/api/facebook/common_edges/albums.py b/pyfacebook/api/facebook/common_edges/albums.py index d3926f20..2a511f10 100644 --- a/pyfacebook/api/facebook/common_edges/albums.py +++ b/pyfacebook/api/facebook/common_edges/albums.py @@ -12,7 +12,7 @@ class AlbumsEdge: __slots__ = () - def get_albums( + async def get_albums( self, object_id: str, fields: Optional[Union[str, list, dict]] = None, @@ -43,7 +43,7 @@ def get_albums( if fields is None: fields = const.ALBUM_PUBLIC_FIELDS - data = self.client.get_full_connections( + data = await self.client.get_full_connections( object_id=object_id, connection="albums", count=count, diff --git a/pyfacebook/api/facebook/common_edges/comments.py b/pyfacebook/api/facebook/common_edges/comments.py index c7c8c407..f03cbe84 100644 --- a/pyfacebook/api/facebook/common_edges/comments.py +++ b/pyfacebook/api/facebook/common_edges/comments.py @@ -12,7 +12,7 @@ class CommentsEdge: __slots__ = () - def get_comments( + async def get_comments( self, object_id: str, fields: Optional[Union[str, list, dict]] = None, @@ -50,7 +50,7 @@ def get_comments( if fields is None: fields = const.COMMENT_PUBLIC_FIELDS - data = self.client.get_full_connections( + data = await self.client.get_full_connections( object_id=object_id, connection="comments", count=count, diff --git a/pyfacebook/api/facebook/common_edges/feed.py b/pyfacebook/api/facebook/common_edges/feed.py index a9d1ef1d..1bea4cee 100644 --- a/pyfacebook/api/facebook/common_edges/feed.py +++ b/pyfacebook/api/facebook/common_edges/feed.py @@ -16,7 +16,7 @@ class FeedEdge: __slots__ = () - def _get_feed( + async def _get_feed( self, object_id: str, fields: Optional[Union[str, list, dict]] = None, @@ -49,7 +49,7 @@ def _get_feed( if fields is None: fields = const.POST_PUBLIC_FIELDS + const.POST_CONNECTIONS_SUMMERY_FIELDS - data = self.client.get_full_connections( + data = await self.client.get_full_connections( object_id=object_id, connection=source, count=count, @@ -64,7 +64,7 @@ def _get_feed( else: return FeedResponse.new_from_json_dict(data) - def get_feed( + async def get_feed( self, object_id: str, fields: Optional[Union[str, list, dict]] = None, @@ -91,7 +91,7 @@ def get_feed( :param kwargs: Additional parameters for different object. :return: Posts response information """ - return self._get_feed( + return await self._get_feed( object_id=object_id, fields=fields, since=since, diff --git a/pyfacebook/api/facebook/common_edges/likes.py b/pyfacebook/api/facebook/common_edges/likes.py index a6a74d03..4f40d8d5 100644 --- a/pyfacebook/api/facebook/common_edges/likes.py +++ b/pyfacebook/api/facebook/common_edges/likes.py @@ -14,7 +14,7 @@ class LikesEdge: __slots__ = () - def get_likes( + async def get_likes( self, object_id: str, fields: Optional[Union[str, list, dict]] = None, @@ -39,7 +39,7 @@ def get_likes( if fields is None: fields = const.LIKES_FIELDS - data = self.client.get_connection( + data = await self.client.get_connection( object_id=object_id, connection="likes", fields=enf_comma_separated(field="fields", value=fields), @@ -51,27 +51,27 @@ def get_likes( else: return LikesResponse.new_from_json_dict(data) - def creat_like(self, object_id: str) -> dict: + async def creat_like(self, object_id: str) -> dict: """ Like an object. :param object_id: ID the facebook object. :return: status for the operation. """ - data = self.client.post_object( + data = await self.client.post_object( object_id=object_id, connection="likes", ) return data - def delete_like(self, object_id: str) -> dict: + async def delete_like(self, object_id: str) -> dict: """ Delete likes on object using this endpoint. :param object_id: ID the facebook object. :return: status for the operation. """ - data = self.client.delete_object( + data = await self.client.delete_object( object_id=object_id, connection="likes", ) diff --git a/pyfacebook/api/facebook/common_edges/live_videos.py b/pyfacebook/api/facebook/common_edges/live_videos.py index f0743763..cdca5c89 100644 --- a/pyfacebook/api/facebook/common_edges/live_videos.py +++ b/pyfacebook/api/facebook/common_edges/live_videos.py @@ -12,7 +12,7 @@ class LiveVideosEdge: __slots__ = () - def get_live_videos( + async def get_live_videos( self, object_id: str, fields: Optional[Union[str, list, dict]] = None, @@ -43,7 +43,7 @@ def get_live_videos( if fields is None: fields = const.LIVE_VIDEO_PUBLIC_FIELDS - data = self.client.get_full_connections( + data = await self.client.get_full_connections( object_id=object_id, connection="live_videos", count=count, diff --git a/pyfacebook/api/facebook/common_edges/photos.py b/pyfacebook/api/facebook/common_edges/photos.py index eec27a83..de54d190 100644 --- a/pyfacebook/api/facebook/common_edges/photos.py +++ b/pyfacebook/api/facebook/common_edges/photos.py @@ -12,7 +12,7 @@ class PhotosEdge: __slots__ = () - def get_photos( + async def get_photos( self, object_id: str, fields: Optional[Union[str, list, dict]] = None, @@ -43,7 +43,7 @@ def get_photos( if fields is None: fields = const.PHOTO_PUBLIC_FIELDS - data = self.client.get_full_connections( + data = await self.client.get_full_connections( object_id=object_id, connection="photos", count=count, diff --git a/pyfacebook/api/facebook/common_edges/videos.py b/pyfacebook/api/facebook/common_edges/videos.py index a7157e01..6d6fff09 100644 --- a/pyfacebook/api/facebook/common_edges/videos.py +++ b/pyfacebook/api/facebook/common_edges/videos.py @@ -12,7 +12,7 @@ class VideosEdge: __slots__ = () - def get_videos( + async def get_videos( self, object_id: str, fields: Optional[Union[str, list, dict]] = None, @@ -43,7 +43,7 @@ def get_videos( if fields is None: fields = const.VIDEO_PUBLIC_FIELDS - data = self.client.get_full_connections( + data = await self.client.get_full_connections( object_id=object_id, connection="videos", count=count, diff --git a/pyfacebook/api/facebook/resource/album.py b/pyfacebook/api/facebook/resource/album.py index b3817b22..b470f09d 100644 --- a/pyfacebook/api/facebook/resource/album.py +++ b/pyfacebook/api/facebook/resource/album.py @@ -12,7 +12,7 @@ class FacebookAlbum(BaseResource, PhotosEdge, LikesEdge): - def get_info( + async def get_info( self, album_id: Optional[str], fields: Optional[Union[str, list, tuple]] = None, @@ -32,7 +32,7 @@ def get_info( if fields is None: fields = const.ALBUM_PUBLIC_FIELDS - data = self.client.get_object( + data = await self.client.get_object( object_id=album_id, fields=enf_comma_separated(field="fields", value=fields), ) @@ -41,7 +41,7 @@ def get_info( else: return Album.new_from_json_dict(data=data) - def get_batch( + async def get_batch( self, ids: Optional[Union[str, list, tuple]], fields: Optional[Union[str, list, tuple]] = None, @@ -62,7 +62,7 @@ def get_batch( if fields is None: fields = const.PHOTO_PUBLIC_FIELDS - data = self.client.get_objects( + data = await self.client.get_objects( ids=ids, fields=enf_comma_separated(field="fields", value=fields), ) diff --git a/pyfacebook/api/facebook/resource/application.py b/pyfacebook/api/facebook/resource/application.py index dcca5d97..854b1da6 100644 --- a/pyfacebook/api/facebook/resource/application.py +++ b/pyfacebook/api/facebook/resource/application.py @@ -11,7 +11,7 @@ class FacebookApplication(BaseResource): - def get_info( + async def get_info( self, fields: Optional[Union[str, list, tuple]] = None, return_json: bool = False, @@ -28,7 +28,7 @@ def get_info( if fields is None: fields = const.APPLICATION_PUBLIC_FIELDS - data = self.client.get_object( + data = await self.client.get_object( object_id=self.client.app_id, fields=enf_comma_separated(field="fields", value=fields), ) @@ -37,7 +37,7 @@ def get_info( else: return Application.new_from_json_dict(data=data) - def get_accounts( + async def get_accounts( self, fields: Optional[Union[str, list, dict]] = None, count: Optional[int] = 10, @@ -60,7 +60,7 @@ def get_accounts( if fields is None: fields = const.APPLICATION_ACCOUNT_PUBLIC_FIELDS - data = self.client.get_full_connections( + data = await self.client.get_full_connections( object_id=self.client.app_id, connection="accounts", count=count, diff --git a/pyfacebook/api/facebook/resource/business.py b/pyfacebook/api/facebook/resource/business.py index f0755384..84d64386 100644 --- a/pyfacebook/api/facebook/resource/business.py +++ b/pyfacebook/api/facebook/resource/business.py @@ -11,7 +11,7 @@ class FacebookBusiness(BaseResource): - def get_info( + async def get_info( self, business_id: str, fields: Optional[Union[str, list, tuple]] = None, @@ -30,7 +30,7 @@ def get_info( if fields is None: fields = const.BUSINESS_PUBLIC_FIELDS - data = self.client.get_object( + data = await self.client.get_object( object_id=business_id, fields=enf_comma_separated(field="fields", value=fields), ) @@ -39,7 +39,7 @@ def get_info( else: return Business.new_from_json_dict(data=data) - def get_batch( + async def get_batch( self, ids: Union[str, list, tuple], fields: Optional[Union[str, list, tuple]] = None, @@ -60,7 +60,7 @@ def get_batch( if fields is None: fields = const.BUSINESS_PUBLIC_FIELDS - data = self.client.get_objects( + data = await self.client.get_objects( ids=ids, fields=enf_comma_separated(field="fields", value=fields), ) diff --git a/pyfacebook/api/facebook/resource/comment.py b/pyfacebook/api/facebook/resource/comment.py index bab1c632..0e8d93bf 100644 --- a/pyfacebook/api/facebook/resource/comment.py +++ b/pyfacebook/api/facebook/resource/comment.py @@ -12,7 +12,7 @@ class FacebookComment(BaseResource, LikesEdge): - def get_info( + async def get_info( self, comment_id: Optional[str], fields: Optional[Union[str, list, tuple]] = None, @@ -32,7 +32,7 @@ def get_info( if fields is None: fields = const.COMMENT_PUBLIC_FIELDS - data = self.client.get_object( + data = await self.client.get_object( object_id=comment_id, fields=enf_comma_separated(field="fields", value=fields), ) @@ -41,7 +41,7 @@ def get_info( else: return Comment.new_from_json_dict(data=data) - def get_batch( + async def get_batch( self, ids: Optional[Union[str, list, tuple]], fields: Optional[Union[str, list, tuple]] = None, @@ -63,7 +63,7 @@ def get_batch( if fields is None: fields = const.COMMENT_PUBLIC_FIELDS - data = self.client.get_objects( + data = await self.client.get_objects( ids=ids, fields=enf_comma_separated(field="fields", value=fields), ) @@ -75,7 +75,7 @@ def get_batch( for comment_id, item in data.items() } - def create( + async def create( self, object_id: str, attachment_id: Optional[str] = None, @@ -116,7 +116,7 @@ def create( if message is not None: data["message"] = message - data = self.client.post_object( + data = await self.client.post_object( object_id=object_id, connection="comments", params=params, @@ -129,7 +129,7 @@ def create( else: return Comment.new_from_json_dict(data=data) - def update( + async def update( self, comment_id: str, attachment_id: Optional[str] = None, @@ -175,7 +175,7 @@ def update( if is_hidden is not None: data["is_hidden"] = is_hidden - data = self.client.post_object( + data = await self.client.post_object( object_id=comment_id, params=params, data=data, @@ -187,12 +187,12 @@ def update( else: return Comment.new_from_json_dict(data=data) - def delete(self, comment_id: str): + async def delete(self, comment_id: str): """ Delete a comment by using this endpoint :param comment_id: ID for the comment. :return: status for delete comment. """ - data = self.client.delete_object(object_id=comment_id) + data = await self.client.delete_object(object_id=comment_id) return data diff --git a/pyfacebook/api/facebook/resource/conversation.py b/pyfacebook/api/facebook/resource/conversation.py index c4abee85..ee5721e2 100644 --- a/pyfacebook/api/facebook/resource/conversation.py +++ b/pyfacebook/api/facebook/resource/conversation.py @@ -11,7 +11,7 @@ class FacebookConversation(BaseResource): - def get_info( + async def get_info( self, conversation_id: Optional[str], fields: Optional[Union[str, list, tuple]] = None, @@ -31,7 +31,7 @@ def get_info( if fields is None: fields = const.CONVERSATION_FIELDS - data = self.client.get_object( + data = await self.client.get_object( object_id=conversation_id, fields=enf_comma_separated(field="fields", value=fields), ) @@ -40,7 +40,7 @@ def get_info( else: return Conversation.new_from_json_dict(data=data) - def get_batch( + async def get_batch( self, ids: Optional[Union[str, list, tuple]], fields: Optional[Union[str, list, tuple]] = None, @@ -62,7 +62,7 @@ def get_batch( if fields is None: fields = const.CONVERSATION_FIELDS - data = self.client.get_objects( + data = await self.client.get_objects( ids=ids, fields=enf_comma_separated(field="fields", value=fields), ) diff --git a/pyfacebook/api/facebook/resource/event.py b/pyfacebook/api/facebook/resource/event.py index 8e0600d5..4285627b 100644 --- a/pyfacebook/api/facebook/resource/event.py +++ b/pyfacebook/api/facebook/resource/event.py @@ -12,7 +12,7 @@ class FacebookEvent(BaseResource, PhotosEdge, LiveVideosEdge): - def get_info( + async def get_info( self, event_id: Optional[str], fields: Optional[Union[str, list, tuple]] = None, @@ -32,7 +32,7 @@ def get_info( if fields is None: fields = const.EVENT_PUBLIC_FIELDS - data = self.client.get_object( + data = await self.client.get_object( object_id=event_id, fields=enf_comma_separated(field="fields", value=fields), ) @@ -41,7 +41,7 @@ def get_info( else: return Event.new_from_json_dict(data=data) - def get_batch( + async def get_batch( self, ids: Optional[Union[str, list, tuple]], fields: Optional[Union[str, list, tuple]] = None, @@ -63,7 +63,7 @@ def get_batch( if fields is None: fields = const.EVENT_PUBLIC_FIELDS - data = self.client.get_objects( + data = await self.client.get_objects( ids=ids, fields=enf_comma_separated(field="fields", value=fields), ) diff --git a/pyfacebook/api/facebook/resource/group.py b/pyfacebook/api/facebook/resource/group.py index 18a3cadc..05148fb0 100644 --- a/pyfacebook/api/facebook/resource/group.py +++ b/pyfacebook/api/facebook/resource/group.py @@ -24,7 +24,7 @@ class FacebookGroup( Note: Groups Edge will be deprecated after April 22, 2024. """ - def get_info( + async def get_info( self, group_id: str, fields: Optional[Union[str, list, tuple]] = None, @@ -42,7 +42,7 @@ def get_info( """ if fields is None: fields = const.GROUP_PUBLIC_FIELDS - data = self.client.get_object( + data = await self.client.get_object( object_id=group_id, fields=enf_comma_separated(field="fields", value=fields) ) if return_json: @@ -50,7 +50,7 @@ def get_info( else: return Group.new_from_json_dict(data=data) - def get_batch( + async def get_batch( self, ids: Union[str, list, tuple], fields: Optional[Union[str, list, tuple]] = None, @@ -72,7 +72,7 @@ def get_batch( if fields is None: fields = const.GROUP_PUBLIC_FIELDS - data = self.client.get_objects( + data = await self.client.get_objects( ids=ids, fields=enf_comma_separated(field="fields", value=fields) ) if return_json: diff --git a/pyfacebook/api/facebook/resource/live_video.py b/pyfacebook/api/facebook/resource/live_video.py index 0865451b..992c434b 100644 --- a/pyfacebook/api/facebook/resource/live_video.py +++ b/pyfacebook/api/facebook/resource/live_video.py @@ -12,7 +12,7 @@ class FacebookLiveVideo(BaseResource, LikesEdge): - def get_info( + async def get_info( self, live_video_id: Optional[str], fields: Optional[Union[str, list, tuple]] = None, @@ -32,7 +32,7 @@ def get_info( if fields is None: fields = const.LIVE_VIDEO_PUBLIC_FIELDS - data = self.client.get_object( + data = await self.client.get_object( object_id=live_video_id, fields=enf_comma_separated(field="fields", value=fields), ) @@ -41,7 +41,7 @@ def get_info( else: return LiveVideo.new_from_json_dict(data=data) - def get_batch( + async def get_batch( self, ids: Optional[Union[str, list, tuple]], fields: Optional[Union[str, list, tuple]] = None, @@ -63,7 +63,7 @@ def get_batch( if fields is None: fields = const.LIVE_VIDEO_PUBLIC_FIELDS - data = self.client.get_objects( + data = await self.client.get_objects( ids=ids, fields=enf_comma_separated(field="fields", value=fields), ) diff --git a/pyfacebook/api/facebook/resource/message.py b/pyfacebook/api/facebook/resource/message.py index c694456d..51beb9ba 100644 --- a/pyfacebook/api/facebook/resource/message.py +++ b/pyfacebook/api/facebook/resource/message.py @@ -11,7 +11,7 @@ class FacebookMessage(BaseResource): - def get_info( + async def get_info( self, message_id: Optional[str], fields: Optional[Union[str, list, tuple]] = None, @@ -31,7 +31,7 @@ def get_info( if fields is None: fields = const.MESSAGE_FIELDS - data = self.client.get_object( + data = await self.client.get_object( object_id=message_id, fields=enf_comma_separated(field="fields", value=fields), ) @@ -40,7 +40,7 @@ def get_info( else: return Message.new_from_json_dict(data=data) - def get_batch( + async def get_batch( self, ids: Optional[Union[str, list, tuple]], fields: Optional[Union[str, list, tuple]] = None, @@ -62,7 +62,7 @@ def get_batch( if fields is None: fields = const.MESSAGE_FIELDS - data = self.client.get_objects( + data = await self.client.get_objects( ids=ids, fields=enf_comma_separated(field="fields", value=fields), ) diff --git a/pyfacebook/api/facebook/resource/page.py b/pyfacebook/api/facebook/resource/page.py index c206af79..bf2bb081 100644 --- a/pyfacebook/api/facebook/resource/page.py +++ b/pyfacebook/api/facebook/resource/page.py @@ -31,7 +31,7 @@ class FacebookPage( LiveVideosEdge, CommentsEdge, ): - def get_info( + async def get_info( self, page_id: Optional[str] = None, username: Optional[str] = None, @@ -61,7 +61,7 @@ def get_info( if fields is None: fields = const.PAGE_PUBLIC_FIELDS - data = self.client.get_object( + data = await self.client.get_object( object_id=target, fields=enf_comma_separated(field="fields", value=fields), ) @@ -70,7 +70,7 @@ def get_info( else: return Page.new_from_json_dict(data=data) - def get_batch( + async def get_batch( self, ids: Optional[Union[str, list, tuple]], fields: Optional[Union[str, list, tuple]] = None, @@ -92,7 +92,7 @@ def get_batch( if fields is None: fields = const.PAGE_PUBLIC_FIELDS - data = self.client.get_objects( + data = await self.client.get_objects( ids=ids, fields=enf_comma_separated(field="fields", value=fields) ) if return_json: @@ -102,7 +102,7 @@ def get_batch( page_id: Page.new_from_json_dict(item) for page_id, item in data.items() } - def get_posts( + async def get_posts( self, object_id: str, fields: Optional[Union[str, list, dict]] = None, @@ -127,7 +127,7 @@ def get_posts( Or return json data. Default is false. :return: Posts response information """ - return self._get_feed( + return await self._get_feed( object_id=object_id, fields=fields, since=since, @@ -138,7 +138,7 @@ def get_posts( return_json=return_json, ) - def get_published_posts( + async def get_published_posts( self, object_id: str, fields: Optional[Union[str, list, dict]] = None, @@ -163,7 +163,7 @@ def get_published_posts( Or return json data. Default is false. :return: Posts response information """ - return self._get_feed( + return await self._get_feed( object_id=object_id, fields=fields, since=since, @@ -174,7 +174,7 @@ def get_published_posts( return_json=return_json, ) - def get_tagged_posts( + async def get_tagged_posts( self, object_id: str, fields: Optional[Union[str, list, dict]] = None, @@ -199,7 +199,7 @@ def get_tagged_posts( Or return json data. Default is false. :return: Posts response information. """ - return self._get_feed( + return await self._get_feed( object_id=object_id, fields=fields, since=since, @@ -210,7 +210,7 @@ def get_tagged_posts( return_json=return_json, ) - def search( + async def search( self, q: str, fields: Optional[Union[str, list, dict]] = None, @@ -234,7 +234,7 @@ def search( if fields is None: fields = const.SEARCH_PAGES_PUBLIC_FIELDS - data = self.client.get_full_connections( + data = await self.client.get_full_connections( object_id="pages", connection="search", count=count, diff --git a/pyfacebook/api/facebook/resource/photo.py b/pyfacebook/api/facebook/resource/photo.py index 57b05c45..76e72cfd 100644 --- a/pyfacebook/api/facebook/resource/photo.py +++ b/pyfacebook/api/facebook/resource/photo.py @@ -12,7 +12,7 @@ class FacebookPhoto(BaseResource, LikesEdge): - def get_info( + async def get_info( self, photo_id: Optional[str], fields: Optional[Union[str, list, tuple]] = None, @@ -32,7 +32,7 @@ def get_info( if fields is None: fields = const.PHOTO_PUBLIC_FIELDS - data = self.client.get_object( + data = await self.client.get_object( object_id=photo_id, fields=enf_comma_separated(field="fields", value=fields), ) @@ -41,7 +41,7 @@ def get_info( else: return Photo.new_from_json_dict(data=data) - def get_batch( + async def get_batch( self, ids: Optional[Union[str, list, tuple]], fields: Optional[Union[str, list, tuple]] = None, @@ -63,7 +63,7 @@ def get_batch( if fields is None: fields = const.PHOTO_PUBLIC_FIELDS - data = self.client.get_objects( + data = await self.client.get_objects( ids=ids, fields=enf_comma_separated(field="fields", value=fields), ) diff --git a/pyfacebook/api/facebook/resource/post.py b/pyfacebook/api/facebook/resource/post.py index 81a367b0..233b29c3 100644 --- a/pyfacebook/api/facebook/resource/post.py +++ b/pyfacebook/api/facebook/resource/post.py @@ -12,7 +12,7 @@ class FacebookPost(BaseResource, CommentsEdge, LikesEdge): - def get_info( + async def get_info( self, post_id: Optional[str], fields: Optional[Union[str, list, tuple]] = None, @@ -32,7 +32,7 @@ def get_info( if fields is None: fields = const.POST_PUBLIC_FIELDS + const.POST_CONNECTIONS_SUMMERY_FIELDS - data = self.client.get_object( + data = await self.client.get_object( object_id=post_id, fields=enf_comma_separated(field="fields", value=fields) ) if return_json: @@ -40,7 +40,7 @@ def get_info( else: return Post.new_from_json_dict(data=data) - def get_batch( + async def get_batch( self, ids: Optional[Union[str, list, tuple]], fields: Optional[Union[str, list, tuple]] = None, @@ -62,7 +62,7 @@ def get_batch( if fields is None: fields = const.POST_PUBLIC_FIELDS + const.POST_CONNECTIONS_SUMMERY_FIELDS - data = self.client.get_objects( + data = await self.client.get_objects( ids=ids, fields=enf_comma_separated(field="fields", value=fields) ) if return_json: diff --git a/pyfacebook/api/facebook/resource/user.py b/pyfacebook/api/facebook/resource/user.py index 194d726c..1589595d 100644 --- a/pyfacebook/api/facebook/resource/user.py +++ b/pyfacebook/api/facebook/resource/user.py @@ -30,7 +30,7 @@ class FacebookUser( LikesEdge, LiveVideosEdge, ): - def get_info( + async def get_info( self, user_id: str, fields: Optional[Union[str, list, tuple]] = None, @@ -49,7 +49,7 @@ def get_info( if fields is None: fields = const.USER_PUBLIC_FIELDS - data = self.client.get_object( + data = await self.client.get_object( object_id=user_id, fields=enf_comma_separated(field="fields", value=fields), ) @@ -58,7 +58,7 @@ def get_info( else: return User.new_from_json_dict(data=data) - def get_batch( + async def get_batch( self, ids: Optional[Union[str, list, tuple]], fields: Optional[Union[str, list, tuple]] = None, @@ -79,7 +79,7 @@ def get_batch( if fields is None: fields = const.USER_PUBLIC_FIELDS - data = self.client.get_objects( + data = await self.client.get_objects( ids=ids, fields=enf_comma_separated(field="fields", value=fields) ) if return_json: @@ -89,7 +89,7 @@ def get_batch( user_id: User.new_from_json_dict(item) for user_id, item in data.items() } - def get_accounts( + async def get_accounts( self, user_id: str, fields: Optional[Union[str, list, tuple]] = None, @@ -119,7 +119,7 @@ def get_accounts( if fields is None: fields = const.PAGE_PUBLIC_FIELDS - data = self.client.get_full_connections( + data = await self.client.get_full_connections( object_id=user_id, connection="accounts", count=count, @@ -134,7 +134,7 @@ def get_accounts( else: return PagesResponse.new_from_json_dict(data) - def get_posts( + async def get_posts( self, object_id: str, fields: Optional[Union[str, list, dict]] = None, @@ -159,7 +159,7 @@ def get_posts( Or return json data. Default is false. :return: Posts information and paging """ - return self._get_feed( + return await self._get_feed( object_id=object_id, fields=fields, since=since, @@ -170,7 +170,7 @@ def get_posts( return_json=return_json, ) - def get_businesses( + async def get_businesses( self, user_id: str, fields: Optional[Union[str, list, dict]] = None, @@ -195,7 +195,7 @@ def get_businesses( if fields is None: fields = const.BUSINESS_PUBLIC_FIELDS - data = self.client.get_full_connections( + data = await self.client.get_full_connections( object_id=user_id, connection="businesses", count=count, diff --git a/pyfacebook/api/facebook/resource/video.py b/pyfacebook/api/facebook/resource/video.py index 79055f67..1f187dc9 100644 --- a/pyfacebook/api/facebook/resource/video.py +++ b/pyfacebook/api/facebook/resource/video.py @@ -12,7 +12,7 @@ class FacebookVideo(BaseResource, LikesEdge): - def get_info( + async def get_info( self, video_id: Optional[str], fields: Optional[Union[str, list, tuple]] = None, @@ -30,7 +30,7 @@ def get_info( """ if fields is None: fields = const.VIDEO_PUBLIC_FIELDS - data = self.client.get_object( + data = await self.client.get_object( object_id=video_id, fields=enf_comma_separated(field="fields", value=fields), ) @@ -39,7 +39,7 @@ def get_info( else: return Video.new_from_json_dict(data=data) - def get_batch( + async def get_batch( self, ids: Optional[Union[str, list, tuple]], fields: Optional[Union[str, list, tuple]] = None, @@ -61,7 +61,7 @@ def get_batch( if fields is None: fields = const.VIDEO_PUBLIC_FIELDS - data = self.client.get_objects( + data = await self.client.get_objects( ids=ids, fields=enf_comma_separated(field="fields", value=fields), ) diff --git a/pyfacebook/api/graph.py b/pyfacebook/api/graph.py index cfe76e83..c4a86ee0 100644 --- a/pyfacebook/api/graph.py +++ b/pyfacebook/api/graph.py @@ -1,22 +1,27 @@ """ This module contains the GraphAPI class, its subclass BasicDisplayAPI and the class ServerSentEventAPI. """ - +import asyncio import hashlib import hmac import logging import re import time +from json import dumps from urllib.parse import parse_qsl, urlparse from typing import Any, Dict, List, Optional, Tuple, Union from warnings import warn import requests +import httpx +from authlib.common.encoding import to_unicode from requests import Response +from authlib.integrations.httpx_client import AsyncOAuth2Client from requests_oauthlib.oauth2_session import OAuth2Session from requests_oauthlib.compliance_fixes.facebook import facebook_compliance_fix from pyfacebook import RateLimit, PercentSecond, FacebookError, LibraryError +from pyfacebook.utils.facebook_compliance_fix import facebook_compliance_fix logger = logging.getLogger(__name__) @@ -44,7 +49,7 @@ def __init__( app_id: Optional[str] = None, app_secret: Optional[str] = None, access_token: Optional[str] = None, - application_only_auth: bool = False, + # application_only_auth: bool = False, oauth_flow: bool = False, version: Optional[str] = None, ignore_version_check: Optional[bool] = False, @@ -65,6 +70,7 @@ def __init__( self.access_token = access_token self.session = requests.Session() + self.client = httpx.AsyncClient() self.__timeout = timeout self.proxies = proxies self.sleep_on_rate_limit = sleep_on_rate_limit @@ -108,9 +114,10 @@ def __init__( # Token if access_token: self.access_token = access_token - elif application_only_auth and all([self.app_id, self.app_secret]): - data = self.get_app_token() - self.access_token = data["access_token"] + # I dont think we can automatically get app token now with async + # elif application_only_auth and all([self.app_id, self.app_secret]): + # data = self.get_app_token() + # self.access_token = data["access_token"] elif oauth_flow and all([self.app_id, self.app_secret]): pass else: @@ -171,7 +178,7 @@ def _append_token(self, args: Optional[dict]) -> dict: args["appsecret_proof"] = secret_proof return args - def _request( + async def _request( self, url: str, args: Optional[dict] = None, @@ -202,17 +209,17 @@ def _request( url = self.base_url + url try: - response = self.session.request( + response = await self.client.request( method=verb, url=url, timeout=self.__timeout, params=args, data=post_args, files=files, - proxies=self.proxies, + # proxies=self.proxies, TODO: setup proxies **kwargs, ) - except requests.HTTPError as ex: + except httpx.HTTPError as ex: raise LibraryError({"message": ex.args}) # check headers @@ -222,7 +229,7 @@ def _request( sleep_seconds = self.rate_limit.get_sleep_seconds( sleep_data=self.sleep_seconds_mapping ) - time.sleep(sleep_seconds) + await asyncio.sleep(sleep_seconds) return response def _parse_response(self, response: Response) -> dict: @@ -253,7 +260,7 @@ def _check_graph_error(data: dict): if "error" in data: raise FacebookError(data) - def get(self, path, args): + async def get(self, path, args) -> dict: """ Send GET request. @@ -261,14 +268,14 @@ def get(self, path, args): :param args: args for request. :return: Response data """ - resp = self._request( + resp = await self._request( url=f"{self.version}/{path}", args=args, ) data = self._parse_response(resp) return data - def get_object(self, object_id: str, fields: str = "", **kwargs) -> dict: + async def get_object(self, object_id: str, fields: str = "", **kwargs) -> dict: """ Get object information by object id. @@ -281,14 +288,14 @@ def get_object(self, object_id: str, fields: str = "", **kwargs) -> dict: if kwargs: args.update(kwargs) - resp = self._request( + resp = await self._request( url=f"{self.version}/{object_id}", args=args, ) data = self._parse_response(resp) return data - def get_objects(self, ids: str, fields: str = "", **kwargs) -> dict: + async def get_objects(self, ids: str, fields: str = "", **kwargs) -> dict: """ Get objects information by multi object ids. @@ -301,11 +308,11 @@ def get_objects(self, ids: str, fields: str = "", **kwargs) -> dict: if kwargs: args.update(kwargs) - resp = self._request(url=f"{self.version}", args=args) + resp = await self._request(url=f"{self.version}", args=args) data = self._parse_response(resp) return data - def get_connection( + async def get_connection( self, object_id: str, connection: str, @@ -319,13 +326,13 @@ def get_connection( :param kwargs: Additional parameters for different connections. :return: Response data """ - resp = self._request( + resp = await self._request( url=f"{self.version}/{object_id}/{connection}", args=kwargs ) data = self._parse_response(resp) return data - def get_full_connections( + async def get_full_connections( self, object_id: str, connection: str, @@ -351,7 +358,7 @@ def get_full_connections( if limit is not None: kwargs["limit"] = limit - data = self.get_connection( + data = await self.get_connection( object_id=object_id, connection=connection, **kwargs, @@ -375,7 +382,7 @@ def get_full_connections( data["data"] = data_set return data - def discovery_user_media( + async def discovery_user_media( self, username: str, fields: str = "", @@ -417,7 +424,7 @@ def discovery_user_media( ) args = {"fields": fds} - data = self.get( + data = await self.get( path=self.instagram_business_id, args=args, ) @@ -440,7 +447,7 @@ def discovery_user_media( data["data"] = media_set return data - def post_object( + async def post_object( self, object_id: str, connection: Optional[str] = None, @@ -465,7 +472,7 @@ def post_object( if connection: path += f"/{connection}" - resp = self._request( + resp = await self._request( url=path, args=params, post_args=data, @@ -476,7 +483,7 @@ def post_object( data = self._parse_response(resp) return data - def delete_object( + async def delete_object( self, object_id: str, connection: Optional[str] = None, @@ -494,7 +501,7 @@ def delete_object( if connection: path += f"/{connection}" - resp = self._request( + resp = await self._request( url=path, verb="DELETE", **kwargs, @@ -508,7 +515,7 @@ def _get_oauth_session( scope: Optional[Union[List[str], str]] = None, state: Optional[str] = None, **kwargs, - ) -> OAuth2Session: + ) -> AsyncOAuth2Client: """ :param redirect_uri: The URL that you want to redirect the person logging in back to. :param scope: A list of permission string to request from the person using your app. @@ -527,14 +534,15 @@ def _get_oauth_session( if state is None: state = self.state - session = OAuth2Session( + session = AsyncOAuth2Client( client_id=self.app_id, scope=scope, redirect_uri=redirect_uri, state=state, **kwargs, ) - session = facebook_compliance_fix(session) + + session.register_compliance_hook("access_token_response", facebook_compliance_fix) return session def get_authorization_url( @@ -561,12 +569,13 @@ def get_authorization_url( redirect_uri=redirect_uri, scope=scope, state=state, **kwargs ) url_kwargs = {} if url_kwargs is None else url_kwargs - authorization_url, state = session.authorization_url( - url=self.authorization_url, **url_kwargs + state = self.state if state is None else state + authorization_url, state = session.create_authorization_url( + url=self.authorization_url, state=state, **url_kwargs ) return authorization_url, state - def exchange_user_access_token( + async def exchange_user_access_token( self, response: str, redirect_uri: Optional[str] = None, @@ -586,16 +595,16 @@ def exchange_user_access_token( redirect_uri=redirect_uri, scope=scope, state=state, **kwargs ) - session.fetch_token( - self.access_token_url, + await session.fetch_token( + url=self.access_token_url, client_secret=self.app_secret, authorization_response=response, ) - self.access_token = session.access_token + self.access_token = session.token["access_token"] return session.token - def exchange_page_access_token( + async def exchange_page_access_token( self, page_id: str, access_token: Optional[str] = None ) -> str: """ @@ -612,7 +621,7 @@ def exchange_page_access_token( if access_token is None: access_token = self.access_token - resp = self._request( + resp = await self._request( url=f"{self.version}/{page_id}", args={"fields": "access_token", "access_token": access_token}, auth_need=False, @@ -629,7 +638,7 @@ def exchange_page_access_token( ) return data["access_token"] - def exchange_long_lived_user_access_token(self, access_token=None) -> dict: + async def exchange_long_lived_user_access_token(self, access_token=None) -> dict: """ Generate long-lived token by short-lived token, Long-lived token generally lasts about 60 days. @@ -645,7 +654,7 @@ def exchange_long_lived_user_access_token(self, access_token=None) -> dict: "fb_exchange_token": access_token, } - resp = self._request( + resp = await self._request( url=self.access_token_url, args=args, auth_need=False, @@ -653,7 +662,7 @@ def exchange_long_lived_user_access_token(self, access_token=None) -> dict: data = self._parse_response(resp) return data - def exchange_long_lived_page_access_token( + async def exchange_long_lived_page_access_token( self, user_id: str, access_token: Optional[str] = None ) -> dict: """ @@ -664,14 +673,14 @@ def exchange_long_lived_page_access_token( :return: Data for Long-lived page token """ - data = self.get_connection( + data = await self.get_connection( object_id=user_id, connection="accounts", access_token=access_token, ) return data - def get_app_token( + async def get_app_token( self, app_id: Optional[str] = None, app_secret: Optional[str] = None ) -> dict: """ @@ -692,7 +701,7 @@ def get_app_token( if app_secret is None: app_secret = self.app_secret - resp = self._request( + resp = await self._request( url=self.access_token_url, args={ "grant_type": "client_credentials", @@ -704,7 +713,7 @@ def get_app_token( data = self._parse_response(resp) return data - def debug_token(self, input_token: str, access_token: Optional[str] = None) -> dict: + async def debug_token(self, input_token: str, access_token: Optional[str] = None) -> dict: """ Get information (such as the scopes or the token expiration dates) about the ``input_token`` given optionally the an ``access_token``, which is an app token. @@ -726,7 +735,7 @@ def debug_token(self, input_token: str, access_token: Optional[str] = None) -> d if access_token is None: access_token = self.access_token - resp = self._request( + resp = await self._request( url=f"{self.version}/debug_token", args={"input_token": input_token, "access_token": access_token}, auth_need=False, @@ -785,7 +794,7 @@ def _generate_secret_proof( """ return None - def exchange_user_access_token( + async def exchange_user_access_token( self, response: str, redirect_uri: Optional[str] = None, @@ -805,17 +814,17 @@ def exchange_user_access_token( redirect_uri=redirect_uri, scope=scope, state=state, **kwargs ) - session.fetch_token( + await session.fetch_token( self.access_token_url, client_secret=self.app_secret, authorization_response=response, include_client_id=True, ) - self.access_token = session.access_token + self.access_token = session.token['access_token'] return session.token - def exchange_long_lived_user_access_token(self, access_token=None) -> dict: + async def exchange_long_lived_user_access_token(self, access_token=None) -> dict: """ Exchange short-lived Instagram User Access Tokens for long-lived Instagram User Access Tokens. :param access_token: short-lived user token. @@ -829,7 +838,7 @@ def exchange_long_lived_user_access_token(self, access_token=None) -> dict: "client_secret": self.app_secret, "access_token": access_token, } - resp = self._request( + resp = await self._request( url=f"access_token", args=args, auth_need=False, @@ -837,13 +846,13 @@ def exchange_long_lived_user_access_token(self, access_token=None) -> dict: data = self._parse_response(resp) return data - def refresh_access_token(self, access_token: str): + async def refresh_access_token(self, access_token: str): """ :param access_token: The valid (unexpired) long-lived Instagram User Access Token that you want to refresh. :return: New access token. """ args = {"grant_type": "ig_refresh_token", "access_token": access_token} - resp = self._request( + resp = await self._request( url="refresh_access_token", args=args, ) @@ -915,12 +924,13 @@ def get_authorization_url( redirect_uri=redirect_uri, scope=scope, state=state, **kwargs ) url_kwargs = {} if url_kwargs is None else url_kwargs - authorization_url, state = session.authorization_url( - url=self.authorization_url, **url_kwargs + state = self.state if state is None else state + authorization_url, state = session.create_authorization_url( + url=self.authorization_url, state=state, **url_kwargs ) return authorization_url, state - def exchange_user_access_token( + async def exchange_user_access_token( self, response: str, redirect_uri: Optional[str] = None, @@ -944,17 +954,17 @@ def exchange_user_access_token( redirect_uri=redirect_uri, scope=scope, state=state, **kwargs ) - session.fetch_token( + await session.fetch_token( self.access_token_url, client_secret=self.app_secret, authorization_response=response, include_client_id=True, ) - self.access_token = session.access_token + self.access_token = session.token["access_token"] return session.token - def exchange_long_lived_user_access_token(self, access_token=None) -> dict: + async def exchange_long_lived_user_access_token(self, access_token=None) -> dict: """ Generate long-lived token by short-lived token, Long-lived token generally lasts about 60 days. @@ -970,7 +980,7 @@ def exchange_long_lived_user_access_token(self, access_token=None) -> dict: "access_token": access_token, } - resp = self._request( + resp = await self._request( url=self.access_token_url, args=args, auth_need=False, @@ -978,13 +988,13 @@ def exchange_long_lived_user_access_token(self, access_token=None) -> dict: data = self._parse_response(resp) return data - def refresh_access_token(self, access_token: str): + async def refresh_access_token(self, access_token: str): """ :param access_token: The valid (unexpired) long-lived Instagram User Access Token that you want to refresh. :return: New access token. """ args = {"grant_type": "th_refresh_token", "access_token": access_token} - resp = self._request( + resp = await self._request( url="refresh_access_token", args=args, ) diff --git a/pyfacebook/api/instagram_basic/resource/media.py b/pyfacebook/api/instagram_basic/resource/media.py index 8dc465bc..f45c1b9b 100644 --- a/pyfacebook/api/instagram_basic/resource/media.py +++ b/pyfacebook/api/instagram_basic/resource/media.py @@ -11,7 +11,7 @@ class IGBasicMedia(BaseResource): - def get_info( + async def get_info( self, media_id: str, fields: Optional[Union[str, list, tuple]] = None, @@ -31,7 +31,7 @@ def get_info( if fields is None: fields = const.IG_BASIC_MEDIA_FIELDS - data = self.client.get_object( + data = await self.client.get_object( object_id=media_id, fields=enf_comma_separated(field="fields", value=fields), ) @@ -41,7 +41,7 @@ def get_info( else: return IgBasicMedia.new_from_json_dict(data) - def get_batch( + async def get_batch( self, ids: Optional[Union[str, list, tuple]], fields: Optional[Union[str, list, tuple]] = None, @@ -61,7 +61,7 @@ def get_batch( ids = enf_comma_separated(field="ids", value=ids) if fields is None: fields = const.IG_BUSINESS_MEDIA_PUBLIC_FIELDS - data = self.client.get_objects( + data = await self.client.get_objects( ids=ids, fields=enf_comma_separated(field="fields", value=fields) ) if return_json: @@ -72,7 +72,7 @@ def get_batch( for media_id, item in data.items() } - def get_children( + async def get_children( self, media_id: str, fields: Optional[Union[str, list, tuple]] = None, @@ -92,7 +92,7 @@ def get_children( if fields is None: fields = const.IG_BASIC_MEDIA_CHILDREN_FIELDS - data = self.client.get_connection( + data = await self.client.get_connection( object_id=media_id, connection="children", fields=enf_comma_separated(field="fields", value=fields), diff --git a/pyfacebook/api/instagram_basic/resource/user.py b/pyfacebook/api/instagram_basic/resource/user.py index 408af6c8..8519ad4c 100644 --- a/pyfacebook/api/instagram_basic/resource/user.py +++ b/pyfacebook/api/instagram_basic/resource/user.py @@ -11,7 +11,7 @@ class IGBasicUser(BaseResource): - def get_info( + async def get_info( self, user_id: Optional[str] = "me", fields: Optional[Union[str, list, tuple]] = None, @@ -30,7 +30,7 @@ def get_info( if fields is None: fields = const.IG_BASIC_USER_FIELDS - data = self.client.get_object( + data = await self.client.get_object( object_id=user_id, fields=enf_comma_separated(field="fields", value=fields), ) @@ -39,7 +39,7 @@ def get_info( else: return IgBasicUser.new_from_json_dict(data=data) - def get_media( + async def get_media( self, user_id: Optional[str] = "me", fields: Optional[Union[str, list, tuple]] = None, @@ -67,7 +67,7 @@ def get_media( if fields is None: fields = const.IG_BASIC_MEDIA_FIELDS - data = self.client.get_full_connections( + data = await self.client.get_full_connections( object_id=user_id, connection="media", count=count, diff --git a/pyfacebook/api/instagram_business/resource/comment.py b/pyfacebook/api/instagram_business/resource/comment.py index a427b9f4..bbeddafa 100644 --- a/pyfacebook/api/instagram_business/resource/comment.py +++ b/pyfacebook/api/instagram_business/resource/comment.py @@ -11,7 +11,7 @@ class IGBusinessComment(BaseResource): - def get_info( + async def get_info( self, comment_id: str, fields: Optional[Union[str, list, tuple]] = None, @@ -30,7 +30,7 @@ def get_info( if fields is None: fields = const.IG_BUSINESS_MEDIA_PUBLIC_FIELDS - data = self.client.get_object( + data = await self.client.get_object( object_id=comment_id, fields=enf_comma_separated(field="fields", value=fields), ) @@ -39,7 +39,7 @@ def get_info( else: return IgBusComment.new_from_json_dict(data=data) - def get_batch( + async def get_batch( self, ids: Optional[Union[str, list, tuple]], fields: Optional[Union[str, list, tuple]] = None, @@ -60,7 +60,7 @@ def get_batch( if fields is None: fields = const.IG_BUSINESS_COMMENT_PUBLIC_FIELDS - data = self.client.get_objects( + data = await self.client.get_objects( ids=ids, fields=enf_comma_separated(field="fields", value=fields) ) if return_json: @@ -71,7 +71,7 @@ def get_batch( for comment_id, item in data.items() } - def get_replies( + async def get_replies( self, comment_id: str, fields: Optional[Union[str, list, tuple]] = None, @@ -96,7 +96,7 @@ def get_replies( if fields is None: fields = const.IG_BUSINESS_REPLY_PUBLIC_FIELDS - data = self.client.get_full_connections( + data = await self.client.get_full_connections( object_id=comment_id, connection="replies", fields=enf_comma_separated(field="fields", value=fields), @@ -110,7 +110,7 @@ def get_replies( class IGBusinessReply(BaseResource): - def get_info( + async def get_info( self, reply_id: str, fields: Optional[Union[str, list, tuple]] = None, @@ -129,7 +129,7 @@ def get_info( if fields is None: fields = const.IG_BUSINESS_REPLY_PUBLIC_FIELDS - data = self.client.get_object( + data = await self.client.get_object( object_id=reply_id, fields=enf_comma_separated(field="fields", value=fields), ) @@ -138,7 +138,7 @@ def get_info( else: return IgBusComment.new_from_json_dict(data=data) - def get_batch( + async def get_batch( self, ids: Optional[Union[str, list, tuple]], fields: Optional[Union[str, list, tuple]] = None, @@ -159,7 +159,7 @@ def get_batch( if fields is None: fields = const.IG_BUSINESS_REPLY_PUBLIC_FIELDS - data = self.client.get_objects( + data = await self.client.get_objects( ids=ids, fields=enf_comma_separated(field="fields", value=fields) ) if return_json: diff --git a/pyfacebook/api/instagram_business/resource/container.py b/pyfacebook/api/instagram_business/resource/container.py index 9e2fd817..dbcc7c0b 100644 --- a/pyfacebook/api/instagram_business/resource/container.py +++ b/pyfacebook/api/instagram_business/resource/container.py @@ -11,7 +11,7 @@ class IGBusinessContainer(BaseResource): - def get_info( + async def get_info( self, container_id: str, fields: Optional[Union[str, list, tuple]] = None, @@ -30,7 +30,7 @@ def get_info( if fields is None: fields = const.IG_BUSINESS_CONTAINER_PUBLIC_FIELDS - data = self.client.get_object( + data = await self.client.get_object( object_id=container_id, fields=enf_comma_separated(field="fields", value=fields), ) @@ -39,7 +39,7 @@ def get_info( else: return IgBusContainer.new_from_json_dict(data=data) - def get_batch( + async def get_batch( self, ids: Optional[Union[str, list, tuple]], fields: Optional[Union[str, list, tuple]] = None, @@ -60,7 +60,7 @@ def get_batch( if fields is None: fields = const.IG_BUSINESS_CONTAINER_PUBLIC_FIELDS - data = self.client.get_objects( + data = await self.client.get_objects( ids=ids, fields=enf_comma_separated(field="fields", value=fields) ) if return_json: diff --git a/pyfacebook/api/instagram_business/resource/hashtag.py b/pyfacebook/api/instagram_business/resource/hashtag.py index 99803231..5ddc365b 100644 --- a/pyfacebook/api/instagram_business/resource/hashtag.py +++ b/pyfacebook/api/instagram_business/resource/hashtag.py @@ -11,7 +11,7 @@ class IGBusinessHashtag(BaseResource): - def get_info( + async def get_info( self, hashtag_id: str, fields: Optional[Union[str, list, tuple]] = None, @@ -30,7 +30,7 @@ def get_info( if fields is None: fields = const.IG_BUSINESS_HASHTAG_PUBLIC_FIELDS - data = self.client.get_object( + data = await self.client.get_object( object_id=hashtag_id, fields=enf_comma_separated(field="fields", value=fields), ) @@ -39,7 +39,7 @@ def get_info( else: return IgBusHashtag.new_from_json_dict(data=data) - def get_batch( + async def get_batch( self, ids: Optional[Union[str, list, tuple]], fields: Optional[Union[str, list, tuple]] = None, @@ -60,7 +60,7 @@ def get_batch( if fields is None: fields = const.IG_BUSINESS_HASHTAG_PUBLIC_FIELDS - data = self.client.get_objects( + data = await self.client.get_objects( ids=ids, fields=enf_comma_separated(field="fields", value=fields) ) if return_json: @@ -71,7 +71,7 @@ def get_batch( for tag_id, item in data.items() } - def get_top_media( + async def get_top_media( self, hashtag_id: str, fields: Optional[Union[str, list, tuple]] = None, @@ -96,7 +96,7 @@ def get_top_media( if fields is None: fields = const.IG_BUSINESS_MEDIA_PUBLIC_FIELDS - data = self.client.get_full_connections( + data = await self.client.get_full_connections( object_id=hashtag_id, connection="top_media", user_id=self.client.instagram_business_id, @@ -110,7 +110,7 @@ def get_top_media( else: return IgBusMediaResponse.new_from_json_dict(data) - def get_recent_media( + async def get_recent_media( self, hashtag_id: str, fields: Optional[Union[str, list, tuple]] = None, @@ -135,7 +135,7 @@ def get_recent_media( if fields is None: fields = const.IG_BUSINESS_MEDIA_PUBLIC_FIELDS - data = self.client.get_full_connections( + data = await self.client.get_full_connections( object_id=hashtag_id, connection="recent_media", user_id=self.client.instagram_business_id, diff --git a/pyfacebook/api/instagram_business/resource/media.py b/pyfacebook/api/instagram_business/resource/media.py index c8247c5f..31ce2a8c 100644 --- a/pyfacebook/api/instagram_business/resource/media.py +++ b/pyfacebook/api/instagram_business/resource/media.py @@ -17,7 +17,7 @@ class IGBusinessMedia(BaseResource): - def get_info( + async def get_info( self, media_id: str, fields: Optional[Union[str, list, tuple]] = None, @@ -36,7 +36,7 @@ def get_info( if fields is None: fields = const.IG_BUSINESS_MEDIA_PUBLIC_FIELDS - data = self.client.get_object( + data = await self.client.get_object( object_id=media_id, fields=enf_comma_separated(field="fields", value=fields), ) @@ -45,7 +45,7 @@ def get_info( else: return IgBusMedia.new_from_json_dict(data=data) - def get_batch( + async def get_batch( self, ids: Optional[Union[str, list, tuple]], fields: Optional[Union[str, list, tuple]] = None, @@ -66,7 +66,7 @@ def get_batch( if fields is None: fields = const.IG_BUSINESS_MEDIA_PUBLIC_FIELDS - data = self.client.get_objects( + data = await self.client.get_objects( ids=ids, fields=enf_comma_separated(field="fields", value=fields) ) if return_json: @@ -77,7 +77,7 @@ def get_batch( for media_id, item in data.items() } - def get_comments( + async def get_comments( self, media_id: str, fields: Optional[Union[str, list, tuple]] = None, @@ -102,7 +102,7 @@ def get_comments( if fields is None: fields = const.IG_BUSINESS_COMMENT_PUBLIC_FIELDS - data = self.client.get_full_connections( + data = await self.client.get_full_connections( object_id=media_id, connection="comments", count=count, @@ -114,7 +114,7 @@ def get_comments( else: return IgBusCommentResponse.new_from_json_dict(data) - def get_children( + async def get_children( self, media_id: str, fields: Optional[Union[str, list, tuple]] = None, @@ -134,7 +134,7 @@ def get_children( if fields is None: fields = const.IG_BUSINESS_MEDIA_CHILDREN_PUBLIC_FIELDS - data = self.client.get_connection( + data = await self.client.get_connection( object_id=media_id, connection="children", fields=enf_comma_separated(field="fields", value=fields), @@ -144,7 +144,7 @@ def get_children( else: return IgBusMediaChildren.new_from_json_dict(data) - def get_insights( + async def get_insights( self, media_id: str, metric: Union[str, list, tuple], @@ -168,7 +168,7 @@ def get_insights( if breakdown: args["breakdown"] = enf_comma_separated(field="breakdown", value=breakdown) - data = self.client.get_connection( + data = await self.client.get_connection( object_id=media_id, connection="insights", **args ) @@ -177,7 +177,7 @@ def get_insights( else: return IgBusInsightsResponse.new_from_json_dict(data) - def get_product_tags( + async def get_product_tags( self, media_id: str, return_json: bool = False ) -> Union[IgBusProductTagsResponse, dict]: """ @@ -188,7 +188,7 @@ def get_product_tags( :return: Media product tags """ - data = self.client.get_connection( + data = await self.client.get_connection( object_id=media_id, connection="product_tags", ) diff --git a/pyfacebook/api/instagram_business/resource/user.py b/pyfacebook/api/instagram_business/resource/user.py index fa13232e..cdc276f9 100644 --- a/pyfacebook/api/instagram_business/resource/user.py +++ b/pyfacebook/api/instagram_business/resource/user.py @@ -24,7 +24,7 @@ class IGBusinessUser(BaseResource): - def get_info( + async def get_info( self, fields: Optional[Union[str, list, tuple]] = None, return_json: bool = False, @@ -45,7 +45,7 @@ def get_info( if fields is None: fields = const.IG_BUSINESS_USER_PUBLIC_FIELDS - data = self.client.get_object( + data = await self.client.get_object( object_id=self.client.instagram_business_id, fields=enf_comma_separated(field="fields", value=fields), ) @@ -54,7 +54,7 @@ def get_info( else: return IgBusUser.new_from_json_dict(data=data) - def discovery_user( + async def discovery_user( self, username: str, fields: Optional[Union[str, list, tuple]] = None, @@ -76,7 +76,7 @@ def discovery_user( metric = enf_comma_separated(field="fields", value=fields) - data = self.client.get_object( + data = await self.client.get_object( object_id=self.client.instagram_business_id, fields=f"business_discovery.username({username}){{{metric}}}", ) @@ -85,7 +85,7 @@ def discovery_user( else: return IgBusDiscoveryUserResponse.new_from_json_dict(data=data) - def discovery_user_medias( + async def discovery_user_medias( self, username: str, fields: Optional[Union[str, list, tuple]] = None, @@ -123,7 +123,7 @@ def discovery_user_medias( since_str = f".since({since})" if since is not None else "" until_str = f".until({until})" if until is not None else "" - data = self.client.get_object( + data = await self.client.get_object( object_id=self.client.instagram_business_id, fields=( f"business_discovery.username({username})" @@ -136,7 +136,7 @@ def discovery_user_medias( else: return IgBusDiscoveryUserMediaResponse.new_from_json_dict(data) - def get_content_publishing_limit( + async def get_content_publishing_limit( self, fields: Optional[Union[str, list, tuple]] = None, return_json: bool = False, @@ -154,7 +154,7 @@ def get_content_publishing_limit( if fields is None: fields = const.IG_BUSINESS_CONTENT_PUBLISH_LIMIT_FIELDS - data = self.client.get_connection( + data = await self.client.get_connection( object_id=self.client.instagram_business_id, connection="content_publishing_limit", fields=enf_comma_separated(field="fields", value=fields), @@ -164,7 +164,7 @@ def get_content_publishing_limit( else: return IgBusPublishLimitResponse.new_from_json_dict(data) - def get_insights( + async def get_insights( self, metric: Union[str, list, tuple], period: str, @@ -218,7 +218,7 @@ def get_insights( if access_token: args["access_token"] = access_token - data = self.client.get_connection( + data = await self.client.get_connection( object_id=user_id, connection="insights", **args, @@ -228,7 +228,7 @@ def get_insights( else: return IgBusInsightsResponse.new_from_json_dict(data) - def get_media( + async def get_media( self, fields: Optional[Union[str, list, tuple]] = None, since: Optional[str] = None, @@ -255,7 +255,7 @@ def get_media( if fields is None: fields = const.IG_BUSINESS_MEDIA_PUBLIC_FIELDS - data = self.client.get_full_connections( + data = await self.client.get_full_connections( object_id=self.client.instagram_business_id, connection="media", count=count, @@ -269,7 +269,7 @@ def get_media( else: return IgBusMediaResponse.new_from_json_dict(data) - def get_live_media( + async def get_live_media( self, fields: Optional[Union[str, list, tuple]] = None, since: Optional[str] = None, @@ -297,7 +297,7 @@ def get_live_media( if fields is None: fields = const.IG_BUSINESS_MEDIA_PUBLIC_FIELDS - data = self.client.get_full_connections( + data = await self.client.get_full_connections( object_id=self.client.instagram_business_id, connection="live_media", count=count, @@ -311,7 +311,7 @@ def get_live_media( else: return IgBusMediaResponse.new_from_json_dict(data) - def get_mentioned_comment( + async def get_mentioned_comment( self, comment_id: str, fields: Optional[Union[str, list, tuple]] = None, @@ -332,7 +332,7 @@ def get_mentioned_comment( fields = const.IG_BUSINESS_COMMENT_PUBLIC_FIELDS fields_inline = enf_comma_separated(field="fields", value=fields) - data = self.client.get_object( + data = await self.client.get_object( object_id=self.client.instagram_business_id, fields=f"mentioned_comment.comment_id({comment_id}){{{fields_inline}}}", ) @@ -342,7 +342,7 @@ def get_mentioned_comment( else: return IgBusMentionedCommentResponse.new_from_json_dict(data) - def get_mentioned_media( + async def get_mentioned_media( self, media_id: str, fields: Optional[Union[str, list, tuple]] = None, @@ -363,7 +363,7 @@ def get_mentioned_media( fields = const.IG_BUSINESS_MENTION_MEDIA_FIELDS fields_inline = enf_comma_separated(field="fields", value=fields) - data = self.client.get_object( + data = await self.client.get_object( object_id=self.client.instagram_business_id, fields=f"mentioned_media.media_id({media_id}){{{fields_inline}}}", ) @@ -372,7 +372,7 @@ def get_mentioned_media( else: return IgBusMentionedMediaResponse.new_from_json_dict(data) - def get_hashtag_search( + async def get_hashtag_search( self, q: str, return_json: bool = False, @@ -385,7 +385,7 @@ def get_hashtag_search( Or return json data. Default is false. :return: User stories response information. """ - data = self.client.get( + data = await self.client.get( path="ig_hashtag_search", args={ "fields": "id,name", @@ -398,7 +398,7 @@ def get_hashtag_search( else: return IgBusHashtagsResponse.new_from_json_dict(data) - def get_recently_searched_hashtags( + async def get_recently_searched_hashtags( self, fields: Optional[Union[str, list, tuple]] = None, count: Optional[int] = 25, @@ -420,7 +420,7 @@ def get_recently_searched_hashtags( if fields is None: fields = const.IG_BUSINESS_HASHTAG_PUBLIC_FIELDS - data = self.client.get_full_connections( + data = await self.client.get_full_connections( object_id=self.client.instagram_business_id, connection="recently_searched_hashtags", fields=enf_comma_separated(field="fields", value=fields), @@ -432,7 +432,7 @@ def get_recently_searched_hashtags( else: return IgBusHashtagsResponse.new_from_json_dict(data) - def get_stories( + async def get_stories( self, fields: Optional[Union[str, list, tuple]] = None, count: Optional[int] = 10, @@ -455,7 +455,7 @@ def get_stories( if fields is None: fields = const.IG_BUSINESS_MEDIA_PUBLIC_FIELDS - data = self.client.get_full_connections( + data = await self.client.get_full_connections( object_id=self.client.instagram_business_id, connection="stories", count=count, @@ -467,7 +467,7 @@ def get_stories( else: return IgBusMediaResponse.new_from_json_dict(data) - def get_tagged_media( + async def get_tagged_media( self, fields: Optional[Union[str, list, tuple]] = None, count: Optional[int] = 25, @@ -490,7 +490,7 @@ def get_tagged_media( if fields is None: fields = const.IG_BUSINESS_MEDIA_PUBLIC_FIELDS - data = self.client.get_full_connections( + data = await self.client.get_full_connections( object_id=self.client.instagram_business_id, connection="tags", count=count, @@ -502,7 +502,7 @@ def get_tagged_media( else: return IgBusMediaResponse.new_from_json_dict(data) - def get_available_catalogs( + async def get_available_catalogs( self, return_json: bool = False ) -> Union[IgBusCatalogsResponse, dict]: """ @@ -513,7 +513,7 @@ def get_available_catalogs( :return: catalog data """ - data = self.client.get_connection( + data = await self.client.get_connection( object_id=self.client.instagram_business_id, connection="available_catalogs", ) @@ -522,7 +522,7 @@ def get_available_catalogs( else: return IgBusCatalogsResponse.new_from_json_dict(data) - def get_catalog_product_search( + async def get_catalog_product_search( self, catalog_id: str, q: Optional[str] = None, @@ -544,7 +544,7 @@ def get_catalog_product_search( :return: Catalog products data. """ - data = self.client.get_full_connections( + data = await self.client.get_full_connections( object_id=self.client.instagram_business_id, connection="catalog_product_search", catalog_id=catalog_id, @@ -557,7 +557,7 @@ def get_catalog_product_search( else: return IgBusProductsResponse.new_from_json_dict(data) - def get_product_appeal( + async def get_product_appeal( self, product_id: str, return_json: bool = False, @@ -571,7 +571,7 @@ def get_product_appeal( :return: Product appeals data. """ - data = self.client.get_connection( + data = await self.client.get_connection( object_id=self.client.instagram_business_id, connection="product_appeal", product_id=product_id, diff --git a/pyfacebook/utils/facebook_compliance_fix.py b/pyfacebook/utils/facebook_compliance_fix.py new file mode 100644 index 00000000..1d7466a2 --- /dev/null +++ b/pyfacebook/utils/facebook_compliance_fix.py @@ -0,0 +1,25 @@ +from json import dumps +from urllib.parse import parse_qsl + +from authlib.common.encoding import to_unicode + + +def facebook_compliance_fix(r): + # if Facebook claims to be sending us json, let's trust them. + if "application/json" in r.headers.get("content-type", {}): + return r + + # Facebook returns a content-type of text/plain when sending their + # x-www-form-urlencoded responses, along with a 200. If not, let's + # assume we're getting JSON and bail on the fix. + if "text/plain" in r.headers.get("content-type", {}) and r.status_code == 200: + token = dict(parse_qsl(r.text, keep_blank_values=True)) + else: + return r + + expires = token.get("expires") + if expires is not None: + token["expires_in"] = expires + token["token_type"] = "Bearer" + r._content = to_unicode(dumps(token)).encode("UTF-8") + return r \ No newline at end of file diff --git a/pyproject.toml b/pyproject.toml index 3f775f84..67442e54 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -31,17 +31,21 @@ packages = [ [tool.poetry.dependencies] -python = "^3.7" +python = "^3.9" requests = "^2.27" requests-oauthlib = "^1.2.0" dataclasses-json = ">=0.5.7" +httpx = "^0.28.1" +authlib = "^1.6.0" [tool.poetry.group.dev.dependencies] -pytest = "^7.1" +pytest = "^8.2" pytest-cov = "^3.0.0" +pytest-asyncio = "^1.0.0" responses = ">=0.23.1" tox = "^4.0.0" black = "^23.3.0" +respx = "^0.22.0" [build-system] requires = ["poetry-core"] diff --git a/tests/facebook/edges/test_likes.py b/tests/facebook/edges/test_likes.py index 27d7d7ff..c8a118ff 100644 --- a/tests/facebook/edges/test_likes.py +++ b/tests/facebook/edges/test_likes.py @@ -1,49 +1,70 @@ """ Tests for likes edges. """ +import pytest +import respx -import responses - -def test_get_likes(helpers, fb_api): +@pytest.mark.asyncio +async def test_get_likes(helpers, fb_api): object_id = "post_id" - with responses.RequestsMock() as m: - m.add( - method=responses.GET, - url=f"https://graph.facebook.com/{fb_api.version}/{object_id}/likes", - json=helpers.load_json("testdata/facebook/apidata/likes/likes_resp.json"), + with respx.mock: + respx.get(f"https://graph.facebook.com/{fb_api.version}/{object_id}/likes").mock( + return_value=respx.MockResponse( + status_code=200, + json=helpers.load_json("testdata/facebook/apidata/likes/likes_resp.json") + ), ) + # m.add( + # method=responses.GET, + # url=f"https://graph.facebook.com/{fb_api.version}/{object_id}/likes", + # json=helpers.load_json("testdata/facebook/apidata/likes/likes_resp.json"), + # ) - likes_resp = fb_api.post.get_likes( + likes_resp = await fb_api.post.get_likes( object_id=object_id, ) assert likes_resp.data[0].profile_type == "user" - likes_json = fb_api.post.get_likes( + likes_json = await fb_api.post.get_likes( object_id=object_id, fields="id,name", return_json=True ) assert len(likes_json["data"]) == 2 -def test_create_like(fb_api): +@pytest.mark.asyncio +async def test_create_like(fb_api): object_id = "post_id" - with responses.RequestsMock() as m: - m.add( - method=responses.POST, - url=f"https://graph.facebook.com/{fb_api.version}/{object_id}/likes", - json={"success": True}, + with respx.mock: + respx.post(f"https://graph.facebook.com/{fb_api.version}/{object_id}/likes").mock( + return_value=respx.MockResponse( + status_code=200, + json={"success": True}, + ), ) - data = fb_api.post.creat_like(object_id=object_id) + # m.add( + # method=responses.POST, + # url=f"https://graph.facebook.com/{fb_api.version}/{object_id}/likes", + # json={"success": True}, + # ) + data = await fb_api.post.creat_like(object_id=object_id) assert data["success"] -def test_delete_like(fb_api): +@pytest.mark.asyncio +async def test_delete_like(fb_api): object_id = "post_id" - with responses.RequestsMock() as m: - m.add( - method=responses.DELETE, - url=f"https://graph.facebook.com/{fb_api.version}/{object_id}/likes", - json={"success": True}, + with respx.mock: + respx.delete(f"https://graph.facebook.com/{fb_api.version}/{object_id}/likes").mock( + return_value=respx.MockResponse( + status_code=200, + json={"success": True}, + ), ) - data = fb_api.post.delete_like(object_id=object_id) + # m.add( + # method=responses.DELETE, + # url=f"https://graph.facebook.com/{fb_api.version}/{object_id}/likes", + # json={"success": True}, + # ) + data = await fb_api.post.delete_like(object_id=object_id) assert data["success"] diff --git a/tests/facebook/test_album.py b/tests/facebook/test_album.py index ee38636b..0f3fbc87 100644 --- a/tests/facebook/test_album.py +++ b/tests/facebook/test_album.py @@ -1,34 +1,52 @@ """ Tests for albums """ +import pytest -import responses +import respx -def test_get_info(helpers, fb_api): +@pytest.mark.asyncio +async def test_get_info(helpers, fb_api): ab_id = "10153867132423553" - with responses.RequestsMock() as m: - m.add( - method=responses.GET, - url=f"https://graph.facebook.com/{fb_api.version}/{ab_id}", - json=helpers.load_json( + with respx.mock: + respx.get(f"https://graph.facebook.com/{fb_api.version}/{ab_id}").mock( + side_effect=[ + respx.MockResponse( + status_code=200, + json=helpers.load_json( "testdata/facebook/apidata/albums/album_info_fields.json" ), - ) - m.add( - method=responses.GET, - url=f"https://graph.facebook.com/{fb_api.version}/{ab_id}", - json=helpers.load_json( + ), + respx.MockResponse( + status_code=200, + json=helpers.load_json( "testdata/facebook/apidata/albums/album_info_default.json" ), + ), + ], ) + # m.add( + # method=responses.GET, + # url=f"https://graph.facebook.com/{fb_api.version}/{ab_id}", + # json=helpers.load_json( + # "testdata/facebook/apidata/albums/album_info_fields.json" + # ), + # ) + # m.add( + # method=responses.GET, + # url=f"https://graph.facebook.com/{fb_api.version}/{ab_id}", + # json=helpers.load_json( + # "testdata/facebook/apidata/albums/album_info_default.json" + # ), + # ) - album = fb_api.album.get_info(album_id=ab_id) + album = await fb_api.album.get_info(album_id=ab_id) assert album.id == ab_id assert album.updated_time == "2016-08-17T22:25:25+0000" - album_json = fb_api.album.get_info( + album_json = await fb_api.album.get_info( album_id=ab_id, fields="id,name,created_time", return_json=True, @@ -36,29 +54,46 @@ def test_get_info(helpers, fb_api): assert album_json["id"] == ab_id -def test_get_batch(helpers, fb_api): +@pytest.mark.asyncio +async def test_get_batch(helpers, fb_api): ab_ids = ["10153867132423553", "10151067477123553"] - with responses.RequestsMock() as m: - m.add( - method=responses.GET, - url=f"https://graph.facebook.com/{fb_api.version}", - json=helpers.load_json( + with respx.mock: + respx.get(f"https://graph.facebook.com/{fb_api.version}").mock( + side_effect=[ + respx.MockResponse( + status_code=200, + json=helpers.load_json( "testdata/facebook/apidata/albums/albums_info_fields.json" ), - ) - m.add( - method=responses.GET, - url=f"https://graph.facebook.com/{fb_api.version}", - json=helpers.load_json( + ), + respx.MockResponse( + status_code=200, + json=helpers.load_json( "testdata/facebook/apidata/albums/albums_info_default.json" ), + ), + ], ) + # m.add( + # method=responses.GET, + # url=f"https://graph.facebook.com/{fb_api.version}", + # json=helpers.load_json( + # "testdata/facebook/apidata/albums/albums_info_fields.json" + # ), + # ) + # m.add( + # method=responses.GET, + # url=f"https://graph.facebook.com/{fb_api.version}", + # json=helpers.load_json( + # "testdata/facebook/apidata/albums/albums_info_default.json" + # ), + # ) - albums = fb_api.album.get_batch(ids=ab_ids) + albums = await fb_api.album.get_batch(ids=ab_ids) assert albums[ab_ids[0]].id == ab_ids[0] - albums_json = fb_api.album.get_batch( + albums_json = await fb_api.album.get_batch( ids=ab_ids, fields="id,name,created_time", return_json=True, diff --git a/tests/facebook/test_application.py b/tests/facebook/test_application.py index 04e988c1..22cae22e 100644 --- a/tests/facebook/test_application.py +++ b/tests/facebook/test_application.py @@ -1,43 +1,61 @@ """ Tests for application. """ +import pytest +import respx -import responses - -def test_get_info(helpers, fb_api): - with responses.RequestsMock() as m: - m.add( - method=responses.GET, - url=f"https://graph.facebook.com/{fb_api.version}/{fb_api.app_id}", - json=helpers.load_json( +@pytest.mark.asyncio +async def test_get_info(helpers, fb_api): + with respx.mock: + respx.get(f"https://graph.facebook.com/{fb_api.version}/{fb_api.app_id}").mock( + return_value=respx.MockResponse( + status_code=200, + json=helpers.load_json( "testdata/facebook/apidata/applications/application_info.json" ), + ), ) - - app = fb_api.application.get_info() + # m.add( + # method=responses.GET, + # url=f"https://graph.facebook.com/{fb_api.version}/{fb_api.app_id}", + # json=helpers.load_json( + # "testdata/facebook/apidata/applications/application_info.json" + # ), + # ) + + app = await fb_api.application.get_info() assert app.id == "123456789" - app_json = fb_api.application.get_info( + app_json = await fb_api.application.get_info( fields="id,category,description,link,name,namespace", return_json=True ) assert app_json["id"] == "123456789" -def test_get_accounts(helpers, fb_api): - with responses.RequestsMock() as m: - m.add( - method=responses.GET, - url=f"https://graph.facebook.com/{fb_api.version}/{fb_api.app_id}/accounts", - json=helpers.load_json( +@pytest.mark.asyncio +async def test_get_accounts(helpers, fb_api): + with respx.mock: + respx.get(f"https://graph.facebook.com/{fb_api.version}/{fb_api.app_id}/accounts").mock( + return_value=respx.MockResponse( + status_code=200, + json=helpers.load_json( "testdata/facebook/apidata/applications/application_accounts.json", ), + ), ) - - accounts = fb_api.application.get_accounts(count=None) + # m.add( + # method=responses.GET, + # url=f"https://graph.facebook.com/{fb_api.version}/{fb_api.app_id}/accounts", + # json=helpers.load_json( + # "testdata/facebook/apidata/applications/application_accounts.json", + # ), + # ) + + accounts = await fb_api.application.get_accounts(count=None) assert len(accounts.data) == 4 - accounts_json = fb_api.application.get_accounts( + accounts_json = await fb_api.application.get_accounts( fields="id,login_url", count=3, limit=4, return_json=True ) assert accounts_json["data"][0]["id"] == "123456789" diff --git a/tests/facebook/test_business.py b/tests/facebook/test_business.py index 1220686e..3222a06f 100644 --- a/tests/facebook/test_business.py +++ b/tests/facebook/test_business.py @@ -1,27 +1,37 @@ """ Tests for business """ +import pytest -import responses +import respx -def test_get_info(helpers, fb_api): +@pytest.mark.asyncio +async def test_get_info(helpers, fb_api): b_id = "123456789" - with responses.RequestsMock() as m: - m.add( - method=responses.GET, - url=f"https://graph.facebook.com/{fb_api.version}/{b_id}", - json=helpers.load_json( - "testdata/facebook/apidata/business/business_info.json" + with respx.mock: + respx.get(f"https://graph.facebook.com/{fb_api.version}/{b_id}").mock( + return_value=respx.MockResponse( + status_code=200, + json=helpers.load_json( + "testdata/facebook/apidata/business/business_info.json" + ), ), ) + # m.add( + # method=responses.GET, + # url=f"https://graph.facebook.com/{fb_api.version}/{b_id}", + # json=helpers.load_json( + # "testdata/facebook/apidata/business/business_info.json" + # ), + # ) - business = fb_api.business.get_info(business_id=b_id) + business = await fb_api.business.get_info(business_id=b_id) assert business.id == b_id assert business.updated_time == "2021-08-31T08:27:35+0000" - business_json = fb_api.business.get_info( + business_json = await fb_api.business.get_info( business_id=b_id, fields="id,name,link,created_time,updated_time,verification_status,profile_picture_uri", return_json=True, @@ -29,22 +39,31 @@ def test_get_info(helpers, fb_api): assert business_json["id"] == b_id -def test_get_batch(helpers, fb_api): +@pytest.mark.asyncio +async def test_get_batch(helpers, fb_api): b_ids = ["123456789", "987654321"] - with responses.RequestsMock() as m: - m.add( - method=responses.GET, - url=f"https://graph.facebook.com/{fb_api.version}", - json=helpers.load_json( + with respx.mock: + respx.get(f"https://graph.facebook.com/{fb_api.version}").mock( + return_value=respx.MockResponse( + status_code=200, + json=helpers.load_json( "testdata/facebook/apidata/business/businesses_info.json" ), + ), ) + # m.add( + # method=responses.GET, + # url=f"https://graph.facebook.com/{fb_api.version}", + # json=helpers.load_json( + # "testdata/facebook/apidata/business/businesses_info.json" + # ), + # ) - businesses = fb_api.business.get_batch(ids=b_ids) + businesses = await fb_api.business.get_batch(ids=b_ids) assert businesses[b_ids[0]].id == b_ids[0] - business_json = fb_api.business.get_batch( + business_json = await fb_api.business.get_batch( ids=b_ids, fields="id,name,link,created_time,updated_time,verification_status,profile_picture_uri", return_json=True, diff --git a/tests/facebook/test_comment.py b/tests/facebook/test_comment.py index 36615bbe..16612435 100644 --- a/tests/facebook/test_comment.py +++ b/tests/facebook/test_comment.py @@ -1,27 +1,36 @@ """ Tests for comment. """ +import pytest +import respx -import responses - -def test_get_info(helpers, fb_api): +@pytest.mark.asyncio +async def test_get_info(helpers, fb_api): cm_id = "10158371815748553_10158371930463553" - with responses.RequestsMock() as m: - m.add( - method=responses.GET, - url=f"https://graph.facebook.com/{fb_api.version}/{cm_id}", - json=helpers.load_json( - "testdata/facebook/apidata/comments/comment_info.json" + with respx.mock: + respx.get(f"https://graph.facebook.com/{fb_api.version}/{cm_id}").mock( + return_value=respx.MockResponse( + status_code=200, + json=helpers.load_json( + "testdata/facebook/apidata/comments/comment_info.json" + ), ), ) + # m.add( + # method=responses.GET, + # url=f"https://graph.facebook.com/{fb_api.version}/{cm_id}", + # json=helpers.load_json( + # "testdata/facebook/apidata/comments/comment_info.json" + # ), + # ) - comment = fb_api.comment.get_info(comment_id=cm_id) + comment = await fb_api.comment.get_info(comment_id=cm_id) assert comment.id == cm_id assert comment.created_time == "2021-07-21T21:05:14+0000" - comment_json = fb_api.comment.get_info( + comment_json = await fb_api.comment.get_info( comment_id=cm_id, fields="id,comment_count,created_time,like_count,message,permalink_url", return_json=True, @@ -29,25 +38,34 @@ def test_get_info(helpers, fb_api): assert comment_json["id"] == cm_id -def test_get_batch(helpers, fb_api): +@pytest.mark.asyncio +async def test_get_batch(helpers, fb_api): cm_ids = [ "10158371815748553_10158371930463553", "10158371815748553_10158372268858553", ] - with responses.RequestsMock() as m: - m.add( - method=responses.GET, - url=f"https://graph.facebook.com/{fb_api.version}", - json=helpers.load_json( - "testdata/facebook/apidata/comments/comments_info.json" + with respx.mock: + respx.get(f"https://graph.facebook.com/{fb_api.version}").mock( + return_value=respx.MockResponse( + status_code=200, + json=helpers.load_json( + "testdata/facebook/apidata/comments/comments_info.json" + ), ), ) + # m.add( + # method=responses.GET, + # url=f"https://graph.facebook.com/{fb_api.version}", + # json=helpers.load_json( + # "testdata/facebook/apidata/comments/comments_info.json" + # ), + # ) - comments = fb_api.comment.get_batch(ids=cm_ids) + comments = await fb_api.comment.get_batch(ids=cm_ids) assert comments[cm_ids[0]].id == cm_ids[0] - comments_json = fb_api.comment.get_batch( + comments_json = await fb_api.comment.get_batch( ids=cm_ids, fields="id,comment_count,created_time,like_count,message,permalink_url", return_json=True, @@ -55,34 +73,43 @@ def test_get_batch(helpers, fb_api): assert comments_json[cm_ids[1]]["id"] == cm_ids[1] -def test_create_comment(helpers, fb_api): +@pytest.mark.asyncio +async def test_create_comment(helpers, fb_api): object_id = "post_id" - with responses.RequestsMock() as m: - m.add( - method=responses.POST, - url=f"https://graph.facebook.com/{fb_api.version}/{object_id}/comments", - json=helpers.load_json( - "testdata/facebook/apidata/comments/create_resp.json" + with respx.mock: + respx.post(f"https://graph.facebook.com/{fb_api.version}/{object_id}/comments").mock( + return_value=respx.MockResponse( + status_code=200, + json=helpers.load_json( + "testdata/facebook/apidata/comments/create_resp.json" + ), ), ) - comment = fb_api.comment.create(object_id=object_id, message="message from api") + # m.add( + # method=responses.POST, + # url=f"https://graph.facebook.com/{fb_api.version}/{object_id}/comments", + # json=helpers.load_json( + # "testdata/facebook/apidata/comments/create_resp.json" + # ), + # ) + comment = await fb_api.comment.create(object_id=object_id, message="message from api") assert comment.id - comment = fb_api.comment.create( + comment = await fb_api.comment.create( object_id=object_id, message="message from api", attachment_url="photo url", ) assert comment.id - comment = fb_api.comment.create( + comment = await fb_api.comment.create( object_id=object_id, message="message from api", attachment_share_url="gif url", ) assert comment.id - comment_json = fb_api.comment.create( + comment_json = await fb_api.comment.create( object_id=object_id, attachment_id="123245", message="message from api", @@ -91,17 +118,26 @@ def test_create_comment(helpers, fb_api): assert comment_json["id"] -def test_update_comment(helpers, fb_api): +@pytest.mark.asyncio +async def test_update_comment(helpers, fb_api): comment_id = "245778718206154_229887283291835" - with responses.RequestsMock() as m: - m.add( - method=responses.POST, - url=f"https://graph.facebook.com/{fb_api.version}/{comment_id}", - json=helpers.load_json( - "testdata/facebook/apidata/comments/create_resp.json" + with respx.mock: + respx.post(f"https://graph.facebook.com/{fb_api.version}/{comment_id}").mock( + return_value=respx.MockResponse( + status_code=200, + json=helpers.load_json( + "testdata/facebook/apidata/comments/create_resp.json" + ), ), ) - comment = fb_api.comment.update( + # m.add( + # method=responses.POST, + # url=f"https://graph.facebook.com/{fb_api.version}/{comment_id}", + # json=helpers.load_json( + # "testdata/facebook/apidata/comments/create_resp.json" + # ), + # ) + comment = await fb_api.comment.update( comment_id=comment_id, attachment_id="123245", message="message from api", @@ -109,7 +145,7 @@ def test_update_comment(helpers, fb_api): ) assert comment.id - comment = fb_api.comment.update( + comment = await fb_api.comment.update( comment_id=comment_id, attachment_url="photo url", message="message from api", @@ -117,7 +153,7 @@ def test_update_comment(helpers, fb_api): ) assert comment.id - comment = fb_api.comment.update( + comment = await fb_api.comment.update( comment_id=comment_id, message="message from api", files={"image data": b"bytes for open files"}, @@ -126,33 +162,46 @@ def test_update_comment(helpers, fb_api): ) assert comment["id"] - with responses.RequestsMock() as m: - m.add( - method=responses.POST, - url=f"https://graph.facebook.com/{fb_api.version}/{comment_id}", - json={"success": True}, + with respx.mock: + respx.post(f"https://graph.facebook.com/{fb_api.version}/{comment_id}").mock( + return_value=respx.MockResponse( + status_code=200, + json={"success": True}, + ), ) - comment = fb_api.comment.update( + # m.add( + # method=responses.POST, + # url=f"https://graph.facebook.com/{fb_api.version}/{comment_id}", + # json={"success": True}, + # ) + comment = await fb_api.comment.update( comment_id=comment_id, message="message from api", attachment_share_url="gif url", ) assert comment["success"] - comment_hidden = fb_api.comment.update( + comment_hidden = await fb_api.comment.update( comment_id=comment_id, is_hidden=True, ) assert comment_hidden["success"] -def test_delete_comment(fb_api): +@pytest.mark.asyncio +async def test_delete_comment(fb_api): comment_id = "245778718206154_229887283291835" - with responses.RequestsMock() as m: - m.add( - method=responses.DELETE, - url=f"https://graph.facebook.com/{fb_api.version}/{comment_id}", - json={"success": True}, + with respx.mock: + respx.delete(f"https://graph.facebook.com/{fb_api.version}/{comment_id}").mock( + return_value=respx.MockResponse( + status_code=200, + json={"success": True}, + ), ) - data = fb_api.comment.delete(comment_id=comment_id) + # m.add( + # method=responses.DELETE, + # url=f"https://graph.facebook.com/{fb_api.version}/{comment_id}", + # json={"success": True}, + # ) + data = await fb_api.comment.delete(comment_id=comment_id) assert data["success"] diff --git a/tests/facebook/test_conversation.py b/tests/facebook/test_conversation.py index 91d88d43..18659631 100644 --- a/tests/facebook/test_conversation.py +++ b/tests/facebook/test_conversation.py @@ -1,27 +1,36 @@ """ Tests for conversation. """ +import pytest +import respx -import responses - -def test_get_info(helpers, fb_api): +@pytest.mark.asyncio +async def test_get_info(helpers, fb_api): cvs_id = "t_587956915396498" - with responses.RequestsMock() as m: - m.add( - method=responses.GET, - url=f"https://graph.facebook.com/{fb_api.version}/{cvs_id}", - json=helpers.load_json( - "testdata/facebook/apidata/conversations/cvs_info.json" + with respx.mock: + respx.get(f"https://graph.facebook.com/{fb_api.version}/{cvs_id}").mock( + return_value=respx.MockResponse( + status_code=200, + json=helpers.load_json( + "testdata/facebook/apidata/conversations/cvs_info.json" + ), ), ) + # m.add( + # method=responses.GET, + # url=f"https://graph.facebook.com/{fb_api.version}/{cvs_id}", + # json=helpers.load_json( + # "testdata/facebook/apidata/conversations/cvs_info.json" + # ), + # ) - cvs = fb_api.conversation.get_info(conversation_id=cvs_id) + cvs = await fb_api.conversation.get_info(conversation_id=cvs_id) assert cvs.id == cvs_id assert cvs.updated_time == "2021-07-23T09:28:50+0000" - cvs_json = fb_api.conversation.get_info( + cvs_json = await fb_api.conversation.get_info( conversation_id=cvs_id, fields="id,link,message_count,snippet,unread_count,updated_time", return_json=True, @@ -29,22 +38,31 @@ def test_get_info(helpers, fb_api): assert cvs_json["id"] == cvs_id -def test_get_batch(helpers, fb_api): +@pytest.mark.asyncio +async def test_get_batch(helpers, fb_api): cvs_ids = ["t_587956915396498", "t_233782918546745"] - with responses.RequestsMock() as m: - m.add( - method=responses.GET, - url=f"https://graph.facebook.com/{fb_api.version}", - json=helpers.load_json( - "testdata/facebook/apidata/conversations/cvses_info.json" + with respx.mock: + respx.get(f"https://graph.facebook.com/{fb_api.version}").mock( + return_value=respx.MockResponse( + status_code=200, + json=helpers.load_json( + "testdata/facebook/apidata/conversations/cvses_info.json" + ), ), ) + # m.add( + # method=responses.GET, + # url=f"https://graph.facebook.com/{fb_api.version}", + # json=helpers.load_json( + # "testdata/facebook/apidata/conversations/cvses_info.json" + # ), + # ) - cvses = fb_api.conversation.get_batch(ids=cvs_ids) + cvses = await fb_api.conversation.get_batch(ids=cvs_ids) assert cvses[cvs_ids[0]].id == cvs_ids[0] - cvses_json = fb_api.conversation.get_batch( + cvses_json = await fb_api.conversation.get_batch( ids=cvs_ids, fields="id,link,message_count,snippet,unread_count,updated_time", return_json=True, diff --git a/tests/facebook/test_event.py b/tests/facebook/test_event.py index 40265e25..fcd5aeb2 100644 --- a/tests/facebook/test_event.py +++ b/tests/facebook/test_event.py @@ -1,25 +1,32 @@ """ Tests for event. """ +import pytest +import respx -import responses - -def test_get_info(helpers, fb_api): +@pytest.mark.asyncio +async def test_get_info(helpers, fb_api): evt_id = "5971414199599456" - with responses.RequestsMock() as m: - m.add( - method=responses.GET, - url=f"https://graph.facebook.com/{fb_api.version}/{evt_id}", - json=helpers.load_json("testdata/facebook/apidata/events/event_info.json"), + with respx.mock: + respx.get(f"https://graph.facebook.com/{fb_api.version}/{evt_id}").mock( + return_value=respx.MockResponse( + status_code=200, + json=helpers.load_json("testdata/facebook/apidata/events/event_info.json"), + ), ) + # m.add( + # method=responses.GET, + # url=f"https://graph.facebook.com/{fb_api.version}/{evt_id}", + # json=helpers.load_json("testdata/facebook/apidata/events/event_info.json"), + # ) - event = fb_api.event.get_info(event_id=evt_id) + event = await fb_api.event.get_info(event_id=evt_id) assert event.id == evt_id assert event.start_time == "2021-07-23T21:00:00+0800" - event_json = fb_api.event.get_info( + event_json = await fb_api.event.get_info( event_id=evt_id, fields="id,name,description,start_time,end_time,place,event_times", return_json=True, @@ -27,20 +34,27 @@ def test_get_info(helpers, fb_api): assert event_json["id"] == evt_id -def test_get_batch(helpers, fb_api): +@pytest.mark.asyncio +async def test_get_batch(helpers, fb_api): et_ids = ["5971414199599456", "512411373205445"] - with responses.RequestsMock() as m: - m.add( - method=responses.GET, - url=f"https://graph.facebook.com/{fb_api.version}", - json=helpers.load_json("testdata/facebook/apidata/events/events_info.json"), + with respx.mock: + respx.get(f"https://graph.facebook.com/{fb_api.version}").mock( + return_value=respx.MockResponse( + status_code=200, + json=helpers.load_json("testdata/facebook/apidata/events/events_info.json"), + ), ) + # m.add( + # method=responses.GET, + # url=f"https://graph.facebook.com/{fb_api.version}", + # json=helpers.load_json("testdata/facebook/apidata/events/events_info.json"), + # ) - events = fb_api.event.get_batch(ids=et_ids) + events = await fb_api.event.get_batch(ids=et_ids) assert events[et_ids[0]].id == et_ids[0] - events_json = fb_api.event.get_batch( + events_json = await fb_api.event.get_batch( ids=et_ids, fields="id,name,description,start_time,end_time,place,event_times", return_json=True, diff --git a/tests/facebook/test_group.py b/tests/facebook/test_group.py index 81140fc5..5997108e 100644 --- a/tests/facebook/test_group.py +++ b/tests/facebook/test_group.py @@ -1,34 +1,51 @@ """ Tests for groups """ +import pytest +import respx -import responses - -def test_get_info(helpers, fb_api): +@pytest.mark.asyncio +async def test_get_info(helpers, fb_api): gp_id = "251560641854558" - with responses.RequestsMock() as m: - m.add( - method=responses.GET, - url=f"https://graph.facebook.com/{fb_api.version}/{gp_id}", - json=helpers.load_json( - "testdata/facebook/apidata/groups/group_default_fields.json" - ), - ) - m.add( - method=responses.GET, - url=f"https://graph.facebook.com/{fb_api.version}/{gp_id}", - json=helpers.load_json( - "testdata/facebook/apidata/groups/group_fields.json" - ), + with respx.mock: + respx.get(f"https://graph.facebook.com/{fb_api.version}/{gp_id}").mock( + side_effect=[ + respx.MockResponse( + status_code=200, + json=helpers.load_json( + "testdata/facebook/apidata/groups/group_default_fields.json" + ), + ), + respx.MockResponse( + status_code=200, + json=helpers.load_json( + "testdata/facebook/apidata/groups/group_fields.json" + ), + ), + ] ) + # m.add( + # method=responses.GET, + # url=f"https://graph.facebook.com/{fb_api.version}/{gp_id}", + # json=helpers.load_json( + # "testdata/facebook/apidata/groups/group_default_fields.json" + # ), + # ) + # m.add( + # method=responses.GET, + # url=f"https://graph.facebook.com/{fb_api.version}/{gp_id}", + # json=helpers.load_json( + # "testdata/facebook/apidata/groups/group_fields.json" + # ), + # ) - gp = fb_api.group.get_info(group_id=gp_id) + gp = await fb_api.group.get_info(group_id=gp_id) assert gp.id == gp_id assert gp.member_count == 188310 - gp_json = fb_api.group.get_info( + gp_json = await fb_api.group.get_info( group_id=gp_id, fields="id,name,description,created_time,member_count", return_json=True, @@ -36,29 +53,46 @@ def test_get_info(helpers, fb_api): assert gp_json["id"] == gp_id -def test_get_batch(helpers, fb_api): +@pytest.mark.asyncio +async def test_get_batch(helpers, fb_api): gp_ids = ["2260975870792283", "251560641854558"] - with responses.RequestsMock() as m: - m.add( - method=responses.GET, - url=f"https://graph.facebook.com/{fb_api.version}", - json=helpers.load_json( - "testdata/facebook/apidata/groups/groups_default_fields.json" - ), - ) - m.add( - method=responses.GET, - url=f"https://graph.facebook.com/{fb_api.version}", - json=helpers.load_json( - "testdata/facebook/apidata/groups/groups_fields.json" - ), + with respx.mock: + respx.get(f"https://graph.facebook.com/{fb_api.version}").mock( + side_effect=[ + respx.MockResponse( + status_code=200, + json=helpers.load_json( + "testdata/facebook/apidata/groups/groups_default_fields.json" + ), + ), + respx.MockResponse( + status_code=200, + json=helpers.load_json( + "testdata/facebook/apidata/groups/groups_fields.json" + ), + ), + ] ) + # m.add( + # method=responses.GET, + # url=f"https://graph.facebook.com/{fb_api.version}", + # json=helpers.load_json( + # "testdata/facebook/apidata/groups/groups_default_fields.json" + # ), + # ) + # m.add( + # method=responses.GET, + # url=f"https://graph.facebook.com/{fb_api.version}", + # json=helpers.load_json( + # "testdata/facebook/apidata/groups/groups_fields.json" + # ), + # ) - gps = fb_api.group.get_batch(ids=gp_ids) + gps = await fb_api.group.get_batch(ids=gp_ids) assert gps[gp_ids[0]].id == gp_ids[0] - gps_json = fb_api.group.get_batch( + gps_json = await fb_api.group.get_batch( ids=gp_ids, fields="id,name,description,created_time,member_count", return_json=True, @@ -66,20 +100,33 @@ def test_get_batch(helpers, fb_api): assert gps_json[gp_ids[1]]["id"] == gp_ids[1] -def test_get_feed(helpers, fb_api): +@pytest.mark.asyncio +async def test_get_feed(helpers, fb_api): gp_id = "124" - with responses.RequestsMock() as m: - m.add( - method=responses.GET, - url=f"https://graph.facebook.com/{fb_api.version}/{gp_id}/feed", - json=helpers.load_json("testdata/facebook/apidata/groups/feed_p1.json"), - ) - m.add( - method=responses.GET, - url=f"https://graph.facebook.com/{fb_api.version}/{gp_id}/feed", - json=helpers.load_json("testdata/facebook/apidata/groups/feed_p2.json"), + with respx.mock: + respx.get(f"https://graph.facebook.com/{fb_api.version}/{gp_id}/feed").mock( + side_effect=[ + respx.MockResponse( + status_code=200, + json=helpers.load_json("testdata/facebook/apidata/groups/feed_p1.json"), + ), + respx.MockResponse( + status_code=200, + json=helpers.load_json("testdata/facebook/apidata/groups/feed_p2.json"), + ), + ] ) + # m.add( + # method=responses.GET, + # url=f"https://graph.facebook.com/{fb_api.version}/{gp_id}/feed", + # json=helpers.load_json("testdata/facebook/apidata/groups/feed_p1.json"), + # ) + # m.add( + # method=responses.GET, + # url=f"https://graph.facebook.com/{fb_api.version}/{gp_id}/feed", + # json=helpers.load_json("testdata/facebook/apidata/groups/feed_p2.json"), + # ) - feed = fb_api.group.get_feed(object_id=gp_id) + feed = await fb_api.group.get_feed(object_id=gp_id) assert len(feed.data) == 10 diff --git a/tests/facebook/test_live_video.py b/tests/facebook/test_live_video.py index a1c289ae..f8c54634 100644 --- a/tests/facebook/test_live_video.py +++ b/tests/facebook/test_live_video.py @@ -1,34 +1,51 @@ """ Tests for live video. """ +import pytest +import respx -import responses - -def test_get_info(helpers, fb_api): +@pytest.mark.asyncio +async def test_get_info(helpers, fb_api): lv_id = "10158276101223553" - with responses.RequestsMock() as m: - m.add( - method=responses.GET, - url=f"https://graph.facebook.com/{fb_api.version}/{lv_id}", - json=helpers.load_json( - "testdata/facebook/apidata/live_videos/live_video_fields.json" - ), - ) - m.add( - method=responses.GET, - url=f"https://graph.facebook.com/{fb_api.version}/{lv_id}", - json=helpers.load_json( - "testdata/facebook/apidata/live_videos/live_video_default.json" - ), + with respx.mock: + respx.get(f"https://graph.facebook.com/{fb_api.version}/{lv_id}").mock( + side_effect=[ + respx.MockResponse( + status_code=200, + json=helpers.load_json( + "testdata/facebook/apidata/live_videos/live_video_fields.json" + ), + ), + respx.MockResponse( + status_code=200, + json=helpers.load_json( + "testdata/facebook/apidata/live_videos/live_video_default.json" + ), + ), + ] ) + # m.add( + # method=responses.GET, + # url=f"https://graph.facebook.com/{fb_api.version}/{lv_id}", + # json=helpers.load_json( + # "testdata/facebook/apidata/live_videos/live_video_fields.json" + # ), + # ) + # m.add( + # method=responses.GET, + # url=f"https://graph.facebook.com/{fb_api.version}/{lv_id}", + # json=helpers.load_json( + # "testdata/facebook/apidata/live_videos/live_video_default.json" + # ), + # ) - live_video = fb_api.live_video.get_info(live_video_id=lv_id) + live_video = await fb_api.live_video.get_info(live_video_id=lv_id) assert live_video.id == lv_id assert live_video.status == "VOD" - live_video_json = fb_api.live_video.get_info( + live_video_json = await fb_api.live_video.get_info( live_video_id=lv_id, fields="id,title,status,embed_html", return_json=True, @@ -37,29 +54,46 @@ def test_get_info(helpers, fb_api): assert live_video_json["title"] == "F8 Refresh 2021 Sessions" -def test_get_batch(helpers, fb_api): +@pytest.mark.asyncio +async def test_get_batch(helpers, fb_api): lv_ids = ["10158276101223553", "10158275863243553"] - with responses.RequestsMock() as m: - m.add( - method=responses.GET, - url=f"https://graph.facebook.com/{fb_api.version}", - json=helpers.load_json( - "testdata/facebook/apidata/live_videos/live_videos_fields.json" - ), - ) - m.add( - method=responses.GET, - url=f"https://graph.facebook.com/{fb_api.version}", - json=helpers.load_json( - "testdata/facebook/apidata/live_videos/live_videos_default.json" - ), + with respx.mock: + respx.get(f"https://graph.facebook.com/{fb_api.version}").mock( + side_effect=[ + respx.MockResponse( + status_code=200, + json=helpers.load_json( + "testdata/facebook/apidata/live_videos/live_videos_fields.json" + ), + ), + respx.MockResponse( + status_code=200, + json=helpers.load_json( + "testdata/facebook/apidata/live_videos/live_videos_default.json" + ), + ), + ] ) + # m.add( + # method=responses.GET, + # url=f"https://graph.facebook.com/{fb_api.version}", + # json=helpers.load_json( + # "testdata/facebook/apidata/live_videos/live_videos_fields.json" + # ), + # ) + # m.add( + # method=responses.GET, + # url=f"https://graph.facebook.com/{fb_api.version}", + # json=helpers.load_json( + # "testdata/facebook/apidata/live_videos/live_videos_default.json" + # ), + # ) - live_videos = fb_api.live_video.get_batch(ids=lv_ids) + live_videos = await fb_api.live_video.get_batch(ids=lv_ids) assert live_videos[lv_ids[0]].id == lv_ids[0] - live_videos_json = fb_api.live_video.get_batch( + live_videos_json = await fb_api.live_video.get_batch( ids=lv_ids, fields="id,title,status,embed_html", return_json=True, diff --git a/tests/facebook/test_message.py b/tests/facebook/test_message.py index dc631e8a..c5224a37 100644 --- a/tests/facebook/test_message.py +++ b/tests/facebook/test_message.py @@ -1,27 +1,37 @@ """ Tests for message. """ +import pytest -import responses +import respx -def test_get_info(helpers, fb_api): +@pytest.mark.asyncio +async def test_get_info(helpers, fb_api): msg_id = "m_ToF35NI1OImBjyUIgplSaBMylUFmkYY4bHogy9C1otLISU6SGhccB5NK-THX_W4EdVQiKVv5SgCW9m-_C78mRA" - with responses.RequestsMock() as m: - m.add( - method=responses.GET, - url=f"https://graph.facebook.com/{fb_api.version}/{msg_id}", - json=helpers.load_json( - "testdata/facebook/apidata/messages/message_info.json" + with respx.mock: + respx.get(f"https://graph.facebook.com/{fb_api.version}/{msg_id}").mock( + return_value=respx.MockResponse( + status_code=200, + json=helpers.load_json( + "testdata/facebook/apidata/messages/message_info.json" + ), ), ) + # m.add( + # method=responses.GET, + # url=f"https://graph.facebook.com/{fb_api.version}/{msg_id}", + # json=helpers.load_json( + # "testdata/facebook/apidata/messages/message_info.json" + # ), + # ) - message = fb_api.message.get_info(message_id=msg_id) + message = await fb_api.message.get_info(message_id=msg_id) assert message.id == msg_id assert message.created_time == "2021-07-23T09:50:48+0000" - message_json = fb_api.message.get_info( + message_json = await fb_api.message.get_info( message_id=msg_id, fields="id,created_time,from,to,tags,attachments", return_json=True, @@ -29,25 +39,34 @@ def test_get_info(helpers, fb_api): assert message_json["id"] == msg_id -def test_get_batch(helpers, fb_api): +@pytest.mark.asyncio +async def test_get_batch(helpers, fb_api): msg_ids = [ "m_ToF35NI1OImBjyUIgplSaBMylUFmkYY4bHogy9C1otLISU6SGhccB5NK-THX_W4EdVQiKVv5SgCW9m-_C78mRA", "m_rU2w1v7XMh8JR7jDSmW3pBMylUFmkYY4bHogy9C1otJUI2R3JKjhRblmg6DHQeVLHU6GP0qwqSqdSg0gnkE8tw", ] - with responses.RequestsMock() as m: - m.add( - method=responses.GET, - url=f"https://graph.facebook.com/{fb_api.version}", - json=helpers.load_json( - "testdata/facebook/apidata/messages/messages_info.json" + with respx.mock: + respx.get(f"https://graph.facebook.com/{fb_api.version}").mock( + return_value=respx.MockResponse( + status_code=200, + json=helpers.load_json( + "testdata/facebook/apidata/messages/messages_info.json" + ), ), ) + # m.add( + # method=responses.GET, + # url=f"https://graph.facebook.com/{fb_api.version}", + # json=helpers.load_json( + # "testdata/facebook/apidata/messages/messages_info.json" + # ), + # ) - messages = fb_api.message.get_batch(ids=msg_ids) + messages = await fb_api.message.get_batch(ids=msg_ids) assert messages[msg_ids[0]].id == msg_ids[0] - messages_json = fb_api.message.get_batch( + messages_json = await fb_api.message.get_batch( ids=msg_ids, fields="id,created_time,from,to,tags,attachments", return_json=True, diff --git a/tests/facebook/test_page.py b/tests/facebook/test_page.py index 022a27d6..d1d6c85b 100644 --- a/tests/facebook/test_page.py +++ b/tests/facebook/test_page.py @@ -1,36 +1,60 @@ """ Tests for page api """ +from itertools import repeat, chain import pytest -import responses +import respx from pyfacebook.exceptions import LibraryError -def test_get_info(helpers, fb_api): +@pytest.mark.asyncio +async def test_get_info(helpers, fb_api): pid = "20531316728" - with responses.RequestsMock() as m: - m.add( - method=responses.GET, - url=f"https://graph.facebook.com/{fb_api.version}/{pid}", - json=helpers.load_json( - "testdata/facebook/apidata/pages/single_fields_page.json" - ), + with respx.mock: + respx.get(f"https://graph.facebook.com/{fb_api.version}/{pid}").mock( + side_effect=[ + respx.MockResponse( + status_code=200, + json=helpers.load_json( + "testdata/facebook/apidata/pages/single_fields_page.json" + ), + ), + ] ) - m.add( - method=responses.GET, - url=f"https://graph.facebook.com/{fb_api.version}/facebookapp", - json=helpers.load_json( - "testdata/facebook/apidata/pages/single_default_page.json" - ), + respx.get(f"https://graph.facebook.com/{fb_api.version}/facebookapp").mock( + side_effect=[ + respx.MockResponse( + status_code=200, + json=helpers.load_json( + "testdata/facebook/apidata/pages/single_default_page.json" + ), + ), + ] ) - page = fb_api.page.get_info(page_id=pid) + # m.add( + # method=responses.GET, + # url=f"https://graph.facebook.com/{fb_api.version}/{pid}", + # json=helpers.load_json( + # "testdata/facebook/apidata/pages/single_fields_page.json" + # ), + # ) + # + # m.add( + # method=responses.GET, + # url=f"https://graph.facebook.com/{fb_api.version}/facebookapp", + # json=helpers.load_json( + # "testdata/facebook/apidata/pages/single_default_page.json" + # ), + # ) + + page = await fb_api.page.get_info(page_id=pid) assert page.id == pid - page_json = fb_api.page.get_info( + page_json = await fb_api.page.get_info( username="facebookapp", fields="id,about,can_checkin,category,category_list,checkins,contact_address,cover,current_location,description,description_html,display_subtext,emails,engagement,fan_count,founded,general_info,global_brand_page_name,global_brand_root_id,link,name,phone,picture,rating_count,single_line_address,start_info,talking_about_count,username,verification_status,website,were_here_count,whatsapp_number", return_json=True, @@ -38,231 +62,365 @@ def test_get_info(helpers, fb_api): assert page_json["id"] == pid with pytest.raises(LibraryError): - fb_api.page.get_info() + await fb_api.page.get_info() -def test_get_batch(helpers, fb_api): +@pytest.mark.asyncio +async def test_get_batch(helpers, fb_api): page_ids = ["20531316728", "19292868552"] - with responses.RequestsMock() as m: - m.add( - method=responses.GET, - url=f"https://graph.facebook.com/{fb_api.version}", - json=helpers.load_json( - "testdata/facebook/apidata/pages/multi_default_fields.json" - ), + with respx.mock: + respx.get(f"https://graph.facebook.com/{fb_api.version}").mock( + side_effect=chain([ + respx.MockResponse( + status_code=200, + json=helpers.load_json( + "testdata/facebook/apidata/pages/multi_default_fields.json" + ), + )], + repeat(respx.MockResponse( + status_code=200, + json=helpers.load_json("testdata/facebook/apidata/pages/multi_pages.json"), + )), + ) ) - m.add( - method=responses.GET, - url=f"https://graph.facebook.com/{fb_api.version}", - json=helpers.load_json("testdata/facebook/apidata/pages/multi_pages.json"), - ) - - data = fb_api.page.get_batch(ids=page_ids) + # m.add( + # method=responses.GET, + # url=f"https://graph.facebook.com/{fb_api.version}", + # json=helpers.load_json( + # "testdata/facebook/apidata/pages/multi_default_fields.json" + # ), + # ) + # m.add( + # method=responses.GET, + # url=f"https://graph.facebook.com/{fb_api.version}", + # json=helpers.load_json("testdata/facebook/apidata/pages/multi_pages.json"), + # ) + + data = await fb_api.page.get_batch(ids=page_ids) assert page_ids[0] in data.keys() - data_json = fb_api.page.get_batch( + data_json = await fb_api.page.get_batch( ids=page_ids, fields="id,name,username,fan_count", return_json=True ) assert data_json[page_ids[0]]["id"] == page_ids[0] -def test_get_feed(helpers, fb_api): +@pytest.mark.asyncio +async def test_get_feed(helpers, fb_api): pid = "19292868552" - with responses.RequestsMock() as m: - m.add( - method=responses.GET, - url=f"https://graph.facebook.com/{fb_api.version}/{pid}/feed", - json=helpers.load_json( - "testdata/facebook/apidata/posts/feeds_default_fields_p1.json" - ), + with respx.mock: + respx.get(f"https://graph.facebook.com/{fb_api.version}/{pid}/feed").mock( + side_effect=chain([ + respx.MockResponse( + status_code=200, + json=helpers.load_json( + "testdata/facebook/apidata/posts/feeds_default_fields_p1.json" + ), + )], + repeat(respx.MockResponse( + status_code=200, + json=helpers.load_json( + "testdata/facebook/apidata/posts/feeds_default_fields_p2.json" + ), + )), + ) ) - m.add( - method=responses.GET, - url=f"https://graph.facebook.com/{fb_api.version}/{pid}/feed", - json=helpers.load_json( - "testdata/facebook/apidata/posts/feeds_default_fields_p2.json" - ), - ) - - feed = fb_api.page.get_feed(object_id=pid, count=None, limit=5) + # m.add( + # method=responses.GET, + # url=f"https://graph.facebook.com/{fb_api.version}/{pid}/feed", + # json=helpers.load_json( + # "testdata/facebook/apidata/posts/feeds_default_fields_p1.json" + # ), + # ) + # m.add( + # method=responses.GET, + # url=f"https://graph.facebook.com/{fb_api.version}/{pid}/feed", + # json=helpers.load_json( + # "testdata/facebook/apidata/posts/feeds_default_fields_p2.json" + # ), + # ) + + feed = await fb_api.page.get_feed(object_id=pid, count=None, limit=5) assert len(feed.data) == 10 assert feed.data[0].id == "19292868552_10158349356748553" -def test_get_posts(helpers, fb_api): +@pytest.mark.asyncio +async def test_get_posts(helpers, fb_api): pid = "19292868552" - with responses.RequestsMock() as m: - m.add( - method=responses.GET, - url=f"https://graph.facebook.com/{fb_api.version}/{pid}/posts", - json=helpers.load_json( - "testdata/facebook/apidata/posts/feeds_default_fields_p1.json" - ), + with respx.mock: + respx.get(f"https://graph.facebook.com/{fb_api.version}/{pid}/posts").mock( + side_effect=[ + respx.MockResponse( + status_code=200, + json=helpers.load_json( + "testdata/facebook/apidata/posts/feeds_default_fields_p1.json" + ), + ), + ] ) - - feed_json = fb_api.page.get_posts( + # m.add( + # method=responses.GET, + # url=f"https://graph.facebook.com/{fb_api.version}/{pid}/posts", + # json=helpers.load_json( + # "testdata/facebook/apidata/posts/feeds_default_fields_p1.json" + # ), + # ) + + feed_json = await fb_api.page.get_posts( object_id=pid, count=4, limit=5, return_json=True ) assert len(feed_json["data"]) == 4 assert feed_json["data"][0]["id"] == "19292868552_10158349356748553" -def test_get_published_posts(helpers, fb_api): +@pytest.mark.asyncio +async def test_get_published_posts(helpers, fb_api): pid = "19292868552" - with responses.RequestsMock() as m: - m.add( - method=responses.GET, - url=f"https://graph.facebook.com/{fb_api.version}/{pid}/published_posts", - json=helpers.load_json( - "testdata/facebook/apidata/posts/feeds_default_fields_p1.json" - ), + with respx.mock: + respx.get(f"https://graph.facebook.com/{fb_api.version}/{pid}/published_posts").mock( + side_effect=[ + respx.MockResponse( + status_code=200, + json=helpers.load_json( + "testdata/facebook/apidata/posts/feeds_default_fields_p1.json" + ), + ), + ] ) - - feed = fb_api.page.get_published_posts(object_id=pid, count=4) + # m.add( + # method=responses.GET, + # url=f"https://graph.facebook.com/{fb_api.version}/{pid}/published_posts", + # json=helpers.load_json( + # "testdata/facebook/apidata/posts/feeds_default_fields_p1.json" + # ), + # ) + + feed = await fb_api.page.get_published_posts(object_id=pid, count=4) assert len(feed.data) == 4 assert feed.data[0].id == "19292868552_10158349356748553" -def test_get_tagged_posts(helpers, fb_api): +@pytest.mark.asyncio +async def test_get_tagged_posts(helpers, fb_api): pid = "19292868552" - with responses.RequestsMock() as m: - m.add( - method=responses.GET, - url=f"https://graph.facebook.com/{fb_api.version}/{pid}/tagged", - json=helpers.load_json( - "testdata/facebook/apidata/posts/feeds_default_fields_p1.json" - ), + with respx.mock: + respx.get(f"https://graph.facebook.com/{fb_api.version}/{pid}/tagged").mock( + side_effect=[ + respx.MockResponse( + status_code=200, + json=helpers.load_json( + "testdata/facebook/apidata/posts/feeds_default_fields_p1.json" + ), + ), + ] ) - - feed = fb_api.page.get_tagged_posts(object_id=pid, count=4) + # m.add( + # method=responses.GET, + # url=f"https://graph.facebook.com/{fb_api.version}/{pid}/tagged", + # json=helpers.load_json( + # "testdata/facebook/apidata/posts/feeds_default_fields_p1.json" + # ), + # ) + + feed = await fb_api.page.get_tagged_posts(object_id=pid, count=4) assert len(feed.data) == 4 assert feed.data[0].id == "19292868552_10158349356748553" -def test_get_albums(helpers, fb_api): +@pytest.mark.asyncio +async def test_get_albums(helpers, fb_api): pid = "2121008874780932" - with responses.RequestsMock() as m: - m.add( - method=responses.GET, - url=f"https://graph.facebook.com/{fb_api.version}/{pid}/albums", - json=helpers.load_json( - "testdata/facebook/apidata/albums/albums_list_p1.json" - ), - ) - m.add( - method=responses.GET, - url=f"https://graph.facebook.com/{fb_api.version}/{pid}/albums", - json=helpers.load_json( - "testdata/facebook/apidata/albums/albums_list_p2.json" - ), + with respx.mock: + respx.get(f"https://graph.facebook.com/{fb_api.version}/{pid}/albums").mock( + side_effect=chain([ + respx.MockResponse( + status_code=200, + json=helpers.load_json( + "testdata/facebook/apidata/albums/albums_list_p1.json" + ), + )], + repeat(respx.MockResponse( + status_code=200, + json=helpers.load_json( + "testdata/facebook/apidata/albums/albums_list_p2.json" + ), + )), + ) ) - - albums = fb_api.page.get_albums(object_id=pid, count=None, limit=3) + # m.add( + # method=responses.GET, + # url=f"https://graph.facebook.com/{fb_api.version}/{pid}/albums", + # json=helpers.load_json( + # "testdata/facebook/apidata/albums/albums_list_p1.json" + # ), + # ) + # m.add( + # method=responses.GET, + # url=f"https://graph.facebook.com/{fb_api.version}/{pid}/albums", + # json=helpers.load_json( + # "testdata/facebook/apidata/albums/albums_list_p2.json" + # ), + # ) + + albums = await fb_api.page.get_albums(object_id=pid, count=None, limit=3) assert len(albums.data) == 6 assert albums.data[0].id == "2312974342251050" - albums_json = fb_api.page.get_albums( + albums_json = await fb_api.page.get_albums( object_id=pid, count=3, limit=3, return_json=True ) assert len(albums_json["data"]) == 3 -def test_get_photos(helpers, fb_api): +@pytest.mark.asyncio +async def test_get_photos(helpers, fb_api): pid = "108824017345866" - with responses.RequestsMock() as m: - m.add( - method=responses.GET, - url=f"https://graph.facebook.com/{fb_api.version}/{pid}/photos", - json=helpers.load_json("testdata/facebook/apidata/photos/photos_p1.json"), - ) - m.add( - method=responses.GET, - url=f"https://graph.facebook.com/{fb_api.version}/{pid}/photos", - json=helpers.load_json("testdata/facebook/apidata/photos/photos_p2.json"), + with respx.mock: + respx.get(f"https://graph.facebook.com/{fb_api.version}/{pid}/photos").mock( + side_effect=chain([ + respx.MockResponse( + status_code=200, + json=helpers.load_json("testdata/facebook/apidata/photos/photos_p1.json"), + )], + repeat(respx.MockResponse( + status_code=200, + json=helpers.load_json("testdata/facebook/apidata/photos/photos_p2.json"), + )), + ) ) - - photos = fb_api.page.get_photos(object_id=pid, count=None, limit=2) + # m.add( + # method=responses.GET, + # url=f"https://graph.facebook.com/{fb_api.version}/{pid}/photos", + # json=f"https://graph.facebook.com/{fb_api.version}/{pid}/photos", + # ) + # m.add( + # method=responses.GET, + # url=f"https://graph.facebook.com/{fb_api.version}/{pid}/photos", + # json=helpers.load_json("testdata/facebook/apidata/photos/photos_p2.json"), + # ) + + photos = await fb_api.page.get_photos(object_id=pid, count=None, limit=2) assert len(photos.data) == 4 assert photos.data[0].id == "336596487901950" - photos_json = fb_api.page.get_photos( + photos_json = await fb_api.page.get_photos( object_id=pid, count=2, limit=2, return_json=True ) assert len(photos_json["data"]) == 2 -def test_get_live_videos(helpers, fb_api): +@pytest.mark.asyncio +async def test_get_live_videos(helpers, fb_api): pid = "20531316728" - with responses.RequestsMock() as m: - m.add( - method=responses.GET, - url=f"https://graph.facebook.com/{fb_api.version}/{pid}/live_videos", - json=helpers.load_json( - "testdata/facebook/apidata/live_videos/live_videos_p1.json" - ), - ) - m.add( - method=responses.GET, - url=f"https://graph.facebook.com/{fb_api.version}/{pid}/live_videos", - json=helpers.load_json( - "testdata/facebook/apidata/live_videos/live_videos_p2.json" - ), + with respx.mock: + respx.get(f"https://graph.facebook.com/{fb_api.version}/{pid}/live_videos").mock( + side_effect=chain([ + respx.MockResponse( + status_code=200, + json=helpers.load_json( + "testdata/facebook/apidata/live_videos/live_videos_p1.json" + ), + )], + repeat(respx.MockResponse( + status_code=200, + json=helpers.load_json( + "testdata/facebook/apidata/live_videos/live_videos_p2.json" + ), + )), + ) ) - - live_videos = fb_api.page.get_live_videos(object_id=pid, count=None, limit=3) + # m.add( + # method=responses.GET, + # url=f"https://graph.facebook.com/{fb_api.version}/{pid}/live_videos", + # json=helpers.load_json( + # "testdata/facebook/apidata/live_videos/live_videos_p1.json" + # ), + # ) + # m.add( + # method=responses.GET, + # url=f"https://graph.facebook.com/{fb_api.version}/{pid}/live_videos", + # json=helpers.load_json( + # "testdata/facebook/apidata/live_videos/live_videos_p2.json" + # ), + # ) + + live_videos = await fb_api.page.get_live_videos(object_id=pid, count=None, limit=3) assert len(live_videos.data) == 6 assert live_videos.data[0].id == "10160659411121729" - live_videos_json = fb_api.page.get_live_videos( + live_videos_json = await fb_api.page.get_live_videos( object_id=pid, count=3, limit=3, return_json=True ) assert len(live_videos_json["data"]) == 3 -def test_get_videos(helpers, fb_api): +@pytest.mark.asyncio +async def test_get_videos(helpers, fb_api): pid = "19292868552" - with responses.RequestsMock() as m: - m.add( - method=responses.GET, - url=f"https://graph.facebook.com/{fb_api.version}/{pid}/videos", - json=helpers.load_json("testdata/facebook/apidata/videos/videos_p1.json"), - ) - m.add( - method=responses.GET, - url=f"https://graph.facebook.com/{fb_api.version}/{pid}/videos", - json=helpers.load_json("testdata/facebook/apidata/videos/videos_p2.json"), + with respx.mock: + respx.get(f"https://graph.facebook.com/{fb_api.version}/{pid}/videos").mock( + side_effect=chain([ + respx.MockResponse( + status_code=200, + json=helpers.load_json("testdata/facebook/apidata/videos/videos_p1.json"), + )], + repeat(respx.MockResponse( + status_code=200, + json=helpers.load_json("testdata/facebook/apidata/videos/videos_p2.json"), + )), + ) ) - - videos = fb_api.page.get_videos(object_id=pid, count=None, limit=3) + # m.add( + # method=responses.GET, + # url=f"https://graph.facebook.com/{fb_api.version}/{pid}/videos", + # json=helpers.load_json("testdata/facebook/apidata/videos/videos_p1.json"), + # ) + # m.add( + # method=responses.GET, + # url=f"https://graph.facebook.com/{fb_api.version}/{pid}/videos", + # json=helpers.load_json("testdata/facebook/apidata/videos/videos_p2.json"), + # ) + + videos = await fb_api.page.get_videos(object_id=pid, count=None, limit=3) assert len(videos.data) == 5 assert videos.data[0].id == "1002065083862711" - videos_json = fb_api.page.get_videos( + videos_json = await fb_api.page.get_videos( object_id=pid, count=2, limit=3, return_json=True ) assert len(videos_json["data"]) == 2 -def test_search_pages(helpers, fb_api): - with responses.RequestsMock() as m: - m.add( - method=responses.GET, - url=f"https://graph.facebook.com/{fb_api.version}/pages/search", - json=helpers.load_json("testdata/facebook/apidata/pages/search_pages.json"), +@pytest.mark.asyncio +async def test_search_pages(helpers, fb_api): + with respx.mock: + respx.get(f"https://graph.facebook.com/{fb_api.version}/pages/search").mock( + side_effect=chain( + repeat(respx.MockResponse( + status_code=200, + json=helpers.load_json("testdata/facebook/apidata/pages/search_pages.json"), + )), + ) ) + # m.add( + # method=responses.GET, + # url=f"https://graph.facebook.com/{fb_api.version}/pages/search", + # json=helpers.load_json("testdata/facebook/apidata/pages/search_pages.json"), + # ) - pages = fb_api.page.search(q="facebook", count=5, limit=5) + pages = await fb_api.page.search(q="facebook", count=5, limit=5) assert len(pages.data) == 5 assert pages.data[0].id == "108824017345866" - pages_json = fb_api.page.search(q="facebook", count=4, return_json=True) + pages_json = await fb_api.page.search(q="facebook", count=4, return_json=True) assert len(pages_json["data"]) == 4 diff --git a/tests/facebook/test_photo.py b/tests/facebook/test_photo.py index d18a2d49..9f3d4f48 100644 --- a/tests/facebook/test_photo.py +++ b/tests/facebook/test_photo.py @@ -3,32 +3,49 @@ """ import pytest -import responses +import respx -def test_get_info(helpers, fb_api): +@pytest.mark.asyncio +async def test_get_info(helpers, fb_api): photo_id = "10158249017468553" - with responses.RequestsMock() as m: - m.add( - method=responses.GET, - url=f"https://graph.facebook.com/{fb_api.version}/{photo_id}", - json=helpers.load_json( - "testdata/facebook/apidata/photos/photo_info_fields.json" - ), - ) - m.add( - method=responses.GET, - url=f"https://graph.facebook.com/{fb_api.version}/{photo_id}", - json=helpers.load_json( - "testdata/facebook/apidata/photos/photo_info_default.json" - ), + with respx.mock: + respx.get(f"https://graph.facebook.com/{fb_api.version}/{photo_id}").mock( + side_effect=[ + respx.MockResponse( + status_code=200, + json=helpers.load_json( + "testdata/facebook/apidata/photos/photo_info_fields.json" + ), + ), + respx.MockResponse( + status_code=200, + json=helpers.load_json( + "testdata/facebook/apidata/photos/photo_info_default.json" + ), + ), + ] ) + # m.add( + # method=responses.GET, + # url=f"https://graph.facebook.com/{fb_api.version}/{photo_id}", + # json=helpers.load_json( + # "testdata/facebook/apidata/photos/photo_info_fields.json" + # ), + # ) + # m.add( + # method=responses.GET, + # url=f"https://graph.facebook.com/{fb_api.version}/{photo_id}", + # json=helpers.load_json( + # "testdata/facebook/apidata/photos/photo_info_default.json" + # ), + # ) - photo = fb_api.photo.get_info(photo_id=photo_id) + photo = await fb_api.photo.get_info(photo_id=photo_id) assert photo.id == photo_id - photo_json = fb_api.photo.get_info( + photo_json = await fb_api.photo.get_info( photo_id=photo_id, fields="id,created_time", return_json=True, @@ -36,29 +53,46 @@ def test_get_info(helpers, fb_api): assert photo_json["id"] == photo_id -def test_get_batch(helpers, fb_api): +@pytest.mark.asyncio +async def test_get_batch(helpers, fb_api): ids = ["10157415047288553", "10158249017468553"] - with responses.RequestsMock() as m: - m.add( - method=responses.GET, - url=f"https://graph.facebook.com/{fb_api.version}", - json=helpers.load_json( - "testdata/facebook/apidata/photos/photos_info_fields.json" - ), - ) - m.add( - method=responses.GET, - url=f"https://graph.facebook.com/{fb_api.version}", - json=helpers.load_json( - "testdata/facebook/apidata/photos/photos_info_default.json" - ), + with respx.mock: + respx.get(f"https://graph.facebook.com/{fb_api.version}").mock( + side_effect=[ + respx.MockResponse( + status_code=200, + json=helpers.load_json( + "testdata/facebook/apidata/photos/photos_info_fields.json" + ), + ), + respx.MockResponse( + status_code=200, + json=helpers.load_json( + "testdata/facebook/apidata/photos/photos_info_default.json" + ), + ), + ] ) + # m.add( + # method=responses.GET, + # url=f"https://graph.facebook.com/{fb_api.version}", + # json=helpers.load_json( + # "testdata/facebook/apidata/photos/photos_info_fields.json" + # ), + # ) + # m.add( + # method=responses.GET, + # url=f"https://graph.facebook.com/{fb_api.version}", + # json=helpers.load_json( + # "testdata/facebook/apidata/photos/photos_info_default.json" + # ), + # ) - data = fb_api.photo.get_batch(ids=ids) + data = await fb_api.photo.get_batch(ids=ids) assert ids[0] in data.keys() - data_json = fb_api.photo.get_batch( + data_json = await fb_api.photo.get_batch( ids=ids, fields="id,created_time", return_json=True, diff --git a/tests/facebook/test_post.py b/tests/facebook/test_post.py index 85b095e4..19b58964 100644 --- a/tests/facebook/test_post.py +++ b/tests/facebook/test_post.py @@ -1,86 +1,125 @@ """ Tests for post api """ +from itertools import repeat, chain -import responses +import pytest +import respx -def test_post_info(helpers, fb_api): +@pytest.mark.asyncio +async def test_post_info(helpers, fb_api): post_id = "175154750010052_424924701699721" - with responses.RequestsMock() as m: - m.add( - method=responses.GET, - url=f"https://graph.facebook.com/{fb_api.version}/{post_id}", - json=helpers.load_json( - "testdata/facebook/apidata/posts/single_post_data.json" - ), - ) - m.add( - method=responses.GET, - url=f"https://graph.facebook.com/{fb_api.version}/{post_id}", - json=helpers.load_json( - "testdata/facebook/apidata/posts/single_post_data_fields.json" - ), + with respx.mock: + respx.get(f"https://graph.facebook.com/{fb_api.version}/{post_id}").mock( + side_effect=[ + respx.MockResponse( + status_code=200, + json=helpers.load_json( + "testdata/facebook/apidata/posts/single_post_data.json" + ), + ), + respx.MockResponse( + status_code=200, + json=helpers.load_json( + "testdata/facebook/apidata/posts/single_post_data_fields.json" + ), + ), + ] ) - post = fb_api.post.get_info(post_id=post_id) + post = await fb_api.post.get_info(post_id=post_id) assert post.id == post_id - post_json = fb_api.post.get_info( + post_json = await fb_api.post.get_info( post_id=post_id, fields="id,created_time,message", return_json=True ) assert post_json["id"] == post_id -def test_batch_posts(helpers, fb_api): +@pytest.mark.asyncio +async def test_batch_posts(helpers, fb_api): ids = ["19292868552_10158347294088553", "19292868552_10158345715593553"] - with responses.RequestsMock() as m: - m.add( - method=responses.GET, - url=f"https://graph.facebook.com/{fb_api.version}", - json=helpers.load_json( - "testdata/facebook/apidata/posts/multi_posts_data.json" - ), - ) - m.add( - method=responses.GET, - url=f"https://graph.facebook.com/{fb_api.version}", - json=helpers.load_json( - "testdata/facebook/apidata/posts/multi_posts_data_fields.json" - ), + with respx.mock: + respx.get(f"https://graph.facebook.com/{fb_api.version}").mock( + side_effect=[ + respx.MockResponse( + status_code=200, + json=helpers.load_json( + "testdata/facebook/apidata/posts/multi_posts_data.json" + ), + ), + respx.MockResponse( + status_code=200, + json=helpers.load_json( + "testdata/facebook/apidata/posts/multi_posts_data_fields.json" + ), + ), + ] ) - - data = fb_api.post.get_batch(ids=ids) + # m.add( + # method=responses.GET, + # url=f"https://graph.facebook.com/{fb_api.version}", + # json=helpers.load_json( + # "testdata/facebook/apidata/posts/multi_posts_data.json" + # ), + # ) + # m.add( + # method=responses.GET, + # url=f"https://graph.facebook.com/{fb_api.version}", + # json=helpers.load_json( + # "testdata/facebook/apidata/posts/multi_posts_data_fields.json" + # ), + # ) + + data = await fb_api.post.get_batch(ids=ids) assert ids[0] in data.keys() - data_json = fb_api.post.get_batch( + data_json = await fb_api.post.get_batch( ids=ids, fields="id,message,created_time,updated_time", return_json=True ) assert data_json[ids[0]]["id"] == ids[0] -def test_get_comments(helpers, fb_api): +@pytest.mark.asyncio +async def test_get_comments(helpers, fb_api): post_id = "19292868552_10158407654328553" - with responses.RequestsMock() as m: - m.add( - method=responses.GET, - url=f"https://graph.facebook.com/{fb_api.version}/{post_id}/comments", - json=helpers.load_json( - "testdata/facebook/apidata/comments/comments_p1.json" - ), + with respx.mock: + respx.get(f"https://graph.facebook.com/{fb_api.version}/{post_id}/comments").mock( + side_effect=chain([ + respx.MockResponse( + status_code=200, + json=helpers.load_json( + "testdata/facebook/apidata/comments/comments_p1.json" + ), + )], + repeat(respx.MockResponse( + status_code=200, + json=helpers.load_json( + "testdata/facebook/apidata/comments/comments_p2.json" + ), + )), + ) ) - m.add( - method=responses.GET, - url=f"https://graph.facebook.com/{fb_api.version}/{post_id}/comments", - json=helpers.load_json( - "testdata/facebook/apidata/comments/comments_p2.json" - ), - ) - - comments = fb_api.post.get_comments( + # m.add( + # method=responses.GET, + # url=f"https://graph.facebook.com/{fb_api.version}/{post_id}/comments", + # json=helpers.load_json( + # "testdata/facebook/apidata/comments/comments_p1.json" + # ), + # ) + # m.add( + # method=responses.GET, + # url=f"https://graph.facebook.com/{fb_api.version}/{post_id}/comments", + # json=helpers.load_json( + # "testdata/facebook/apidata/comments/comments_p2.json" + # ), + # ) + + comments = await fb_api.post.get_comments( object_id=post_id, count=None, limit=10, @@ -88,7 +127,7 @@ def test_get_comments(helpers, fb_api): assert len(comments.data) == 15 assert comments.summary.total_count == 18 - comments_json = fb_api.post.get_comments( + comments_json = await fb_api.post.get_comments( object_id=post_id, count=5, limit=5, return_json=True ) assert len(comments_json["data"]) == 5 diff --git a/tests/facebook/test_user.py b/tests/facebook/test_user.py index e3da7722..ec8b2cb1 100644 --- a/tests/facebook/test_user.py +++ b/tests/facebook/test_user.py @@ -1,24 +1,28 @@ """ Tests for user api """ +from itertools import repeat, chain -import responses +import pytest +import respx -def test_get_info(helpers, fb_api): +@pytest.mark.asyncio +async def test_get_info(helpers, fb_api): uid = "4" - with responses.RequestsMock() as m: - m.add( - method=responses.GET, - url=f"https://graph.facebook.com/{fb_api.version}/{uid}", - json=helpers.load_json("testdata/facebook/apidata/users/user.json"), + with respx.mock: + respx.get(f"https://graph.facebook.com/{fb_api.version}/{uid}").mock( + return_value=respx.MockResponse( + status_code=200, + json=helpers.load_json("testdata/facebook/apidata/users/user.json"), + ) ) - user = fb_api.user.get_info(user_id=uid) + user = await fb_api.user.get_info(user_id=uid) assert user.id == uid - user_json = fb_api.user.get_info( + user_json = await fb_api.user.get_info( user_id=uid, fields="id,first_name,last_name,middle_name,name,name_format,picture,short_name", return_json=True, @@ -26,97 +30,108 @@ def test_get_info(helpers, fb_api): assert user_json["id"] == uid -def test_get_batches(helpers, fb_api): +@pytest.mark.asyncio +async def test_get_batches(helpers, fb_api): ids = ["4", "5"] - with responses.RequestsMock() as m: - m.add( - method=responses.GET, - url=f"https://graph.facebook.com/{fb_api.version}", - json=helpers.load_json( - "testdata/facebook/apidata/users/users_default.json" - ), - ) - m.add( - method=responses.GET, - url=f"https://graph.facebook.com/{fb_api.version}", - json=helpers.load_json("testdata/facebook/apidata/users/users_fields.json"), + with respx.mock: + respx.get(f"https://graph.facebook.com/{fb_api.version}").mock( + side_effect=chain([ + respx.MockResponse( + status_code=200, + json=helpers.load_json( + "testdata/facebook/apidata/users/users_default.json" + ), + )], + repeat(respx.MockResponse( + status_code=200, + json=helpers.load_json("testdata/facebook/apidata/users/users_fields.json"), + )) + ) ) - users = fb_api.user.get_batch(ids=ids, fields="") + users = await fb_api.user.get_batch(ids=ids, fields="") assert users[ids[0]].id == ids[0] - users_json = fb_api.user.get_batch(ids=ids, return_json=True) + users_json = await fb_api.user.get_batch(ids=ids, return_json=True) assert users_json[ids[0]]["id"] == ids[0] -def test_get_feed(helpers, fb_api): +@pytest.mark.asyncio +async def test_get_feed(helpers, fb_api): uid = "4" - with responses.RequestsMock() as m: - m.add( - method=responses.GET, - url=f"https://graph.facebook.com/{fb_api.version}/{uid}/feed", - json=helpers.load_json( - "testdata/facebook/apidata/posts/feed_fields_p1.json" - ), - ) - m.add( - method=responses.GET, - url=f"https://graph.facebook.com/{fb_api.version}/{uid}/feed", - json=helpers.load_json( - "testdata/facebook/apidata/posts/feed_fields_p2.json" - ), + with respx.mock: + respx.get(f"https://graph.facebook.com/{fb_api.version}/{uid}/feed").mock( + side_effect=chain([ + respx.MockResponse( + status_code=200, + json=helpers.load_json( + "testdata/facebook/apidata/posts/feed_fields_p1.json" + ), + )], + repeat(respx.MockResponse( + status_code=200, + json=helpers.load_json( + "testdata/facebook/apidata/posts/feed_fields_p2.json" + ), + )) + ) ) - feed = fb_api.user.get_feed(object_id=uid, count=None, limit=5) + feed = await fb_api.user.get_feed(object_id=uid, count=None, limit=5) assert len(feed.data) == 10 assert feed.data[0].id == "4_10113477241177441" -def test_get_posts(helpers, fb_api): +@pytest.mark.asyncio +async def test_get_posts(helpers, fb_api): uid = "4" - with responses.RequestsMock() as m: - m.add( - method=responses.GET, - url=f"https://graph.facebook.com/{fb_api.version}/{uid}/posts", - json=helpers.load_json( - "testdata/facebook/apidata/posts/feed_fields_p1.json" - ), + with respx.mock: + respx.get(f"https://graph.facebook.com/{fb_api.version}/{uid}/posts").mock( + return_value=respx.MockResponse( + status_code=200, + json=helpers.load_json( + "testdata/facebook/apidata/posts/feed_fields_p1.json" + ), + ) ) - feed_json = fb_api.user.get_posts( + feed_json = await fb_api.user.get_posts( object_id=uid, count=4, limit=5, return_json=True ) assert len(feed_json["data"]) == 4 assert feed_json["data"][0]["id"] == "4_10113477241177441" -def test_get_accounts(helpers, fb_api): +@pytest.mark.asyncio +async def test_get_accounts(helpers, fb_api): uid = "4" - with responses.RequestsMock() as m: - m.add( - method=responses.GET, - url=f"https://graph.facebook.com/{fb_api.version}/{uid}/accounts", - json=helpers.load_json( - "testdata/facebook/apidata/users/user_accounts_p1.json" - ), - ) - m.add( - method=responses.GET, - url=f"https://graph.facebook.com/{fb_api.version}/{uid}/accounts", - json=helpers.load_json( - "testdata/facebook/apidata/users/user_accounts_p2.json" - ), + with respx.mock: + respx.get(f"https://graph.facebook.com/{fb_api.version}/{uid}/accounts").mock( + side_effect=chain([ + respx.MockResponse( + status_code=200, + json=helpers.load_json( + "testdata/facebook/apidata/users/user_accounts_p1.json" + ), + )], + repeat(respx.MockResponse( + status_code=200, + json=helpers.load_json( + "testdata/facebook/apidata/users/user_accounts_p2.json" + ), + )) + ) ) - accounts = fb_api.user.get_accounts(user_id=uid, count=None, limit=4) + accounts = await fb_api.user.get_accounts(user_id=uid, count=None, limit=4) assert len(accounts.data) == 6 assert accounts.data[0].access_token == "access_token" - accounts_json = fb_api.user.get_accounts( + accounts_json = await fb_api.user.get_accounts( user_id=uid, count=2, limit=2, @@ -125,26 +140,29 @@ def test_get_accounts(helpers, fb_api): assert len(accounts_json["data"]) == 2 -def test_get_businesses(helpers, fb_api): +@pytest.mark.asyncio +async def test_get_businesses(helpers, fb_api): uid = "123" - with responses.RequestsMock() as m: - m.add( - method=responses.GET, - url=f"https://graph.facebook.com/{fb_api.version}/{uid}/businesses", - json=helpers.load_json( - "testdata/facebook/apidata/users/user_businesses.json" - ), + with respx.mock: + respx.get(f"https://graph.facebook.com/{fb_api.version}/{uid}/businesses").mock( + return_value=respx.MockResponse( + status_code=200, + json=helpers.load_json( + "testdata/facebook/apidata/users/user_businesses.json" + ), + ) ) - businesses = fb_api.user.get_businesses(user_id=uid, count=None, limit=2) + businesses = await fb_api.user.get_businesses(user_id=uid, count=None, limit=2) assert len(businesses.data) == 2 assert businesses.data[0].id == "123456789" - businesses_json = fb_api.user.get_businesses( + businesses_json = await fb_api.user.get_businesses( user_id=uid, count=2, limit=2, return_json=True, ) + assert len(businesses_json["data"]) == 2 diff --git a/tests/facebook/test_video.py b/tests/facebook/test_video.py index 005261bb..b23fdfc2 100644 --- a/tests/facebook/test_video.py +++ b/tests/facebook/test_video.py @@ -1,34 +1,53 @@ """ Tests for videos. """ +from itertools import chain, repeat -import responses +import pytest +import respx -def test_get_info(helpers, fb_api): +@pytest.mark.asyncio +async def test_get_info(helpers, fb_api): video_id = "1192957457884299" - with responses.RequestsMock() as m: - m.add( - method=responses.GET, - url=f"https://graph.facebook.com/{fb_api.version}/{video_id}", - json=helpers.load_json( - "testdata/facebook/apidata/videos/video_info_fields.json" - ), - ) - m.add( - method=responses.GET, - url=f"https://graph.facebook.com/{fb_api.version}/{video_id}", - json=helpers.load_json( - "testdata/facebook/apidata/videos/video_info_default.json" - ), + with respx.mock: + respx.get(f"https://graph.facebook.com/{fb_api.version}/{video_id}").mock( + side_effect=chain([ + respx.MockResponse( + status_code=200, + json=helpers.load_json( + "testdata/facebook/apidata/videos/video_info_fields.json" + ), + )], + repeat(respx.MockResponse( + status_code=200, + json=helpers.load_json( + "testdata/facebook/apidata/videos/video_info_default.json" + ), + )), + ) ) + # m.add( + # method=responses.GET, + # url=f"https://graph.facebook.com/{fb_api.version}/{video_id}", + # json=helpers.load_json( + # "testdata/facebook/apidata/videos/video_info_fields.json" + # ), + # ) + # m.add( + # method=responses.GET, + # url=f"https://graph.facebook.com/{fb_api.version}/{video_id}", + # json=helpers.load_json( + # "testdata/facebook/apidata/videos/video_info_default.json" + # ), + # ) - video = fb_api.video.get_info(video_id=video_id) + video = await fb_api.video.get_info(video_id=video_id) assert video.id == video_id assert video.updated_time == "2021-07-20T01:55:06+0000" - video_json = fb_api.video.get_info( + video_json = await fb_api.video.get_info( video_id=video_id, fields="id,description,updated_time", return_json=True, @@ -36,29 +55,46 @@ def test_get_info(helpers, fb_api): assert video_json["id"] == video_id -def test_get_batch(helpers, fb_api): +@pytest.mark.asyncio +async def test_get_batch(helpers, fb_api): video_ids = ["1192957457884299", "334712884667245"] - with responses.RequestsMock() as m: - m.add( - method=responses.GET, - url=f"https://graph.facebook.com/{fb_api.version}", - json=helpers.load_json( - "testdata/facebook/apidata/videos/videos_info_fields.json" - ), - ) - m.add( - method=responses.GET, - url=f"https://graph.facebook.com/{fb_api.version}", - json=helpers.load_json( - "testdata/facebook/apidata/videos/videos_info_default.json" - ), + with respx.mock: + respx.get(f"https://graph.facebook.com/{fb_api.version}").mock( + side_effect=chain([ + respx.MockResponse( + status_code=200, + json=helpers.load_json( + "testdata/facebook/apidata/videos/videos_info_fields.json" + ), + )], + repeat(respx.MockResponse( + status_code=200, + json=helpers.load_json( + "testdata/facebook/apidata/videos/videos_info_default.json" + ), + )), + ) ) + # m.add( + # method=responses.GET, + # url=f"https://graph.facebook.com/{fb_api.version}", + # json=helpers.load_json( + # "testdata/facebook/apidata/videos/videos_info_fields.json" + # ), + # ) + # m.add( + # method=responses.GET, + # url=f"https://graph.facebook.com/{fb_api.version}", + # json=helpers.load_json( + # "testdata/facebook/apidata/videos/videos_info_default.json" + # ), + # ) - videos = fb_api.video.get_batch(ids=video_ids) + videos = await fb_api.video.get_batch(ids=video_ids) assert videos[video_ids[0]].id == video_ids[0] - videos_json = fb_api.video.get_batch( + videos_json = await fb_api.video.get_batch( ids=video_ids, fields="id,name,created_time", return_json=True, diff --git a/tests/instagram_basic/test_media.py b/tests/instagram_basic/test_media.py index d77dcb2f..12ac5fc6 100644 --- a/tests/instagram_basic/test_media.py +++ b/tests/instagram_basic/test_media.py @@ -1,25 +1,34 @@ """ Tests for basic media api. """ +import pytest +import respx -import responses - -def test_get_info(helpers, api): +@pytest.mark.asyncio +async def test_get_info(helpers, api): media_id = "18027939643230671" - with responses.RequestsMock() as m: - m.add( - method=responses.GET, - url=f"https://graph.instagram.com/{api.version}/{media_id}", - json=helpers.load_json( - "testdata/instagram_basic/apidata/media/media_info.json" - ), + with respx.mock: + respx.get(f"https://graph.instagram.com/{api.version}/{media_id}").mock( + return_value=respx.MockResponse( + status_code=200, + json=helpers.load_json( + "testdata/instagram_basic/apidata/media/media_info.json" + ), + ) ) + # m.add( + # method=responses.GET, + # url=f"https://graph.instagram.com/{api.version}/{media_id}", + # json=helpers.load_json( + # "testdata/instagram_basic/apidata/media/media_info.json" + # ), + # ) - media = api.media.get_info(media_id=media_id) + media = await api.media.get_info(media_id=media_id) assert media.id == media_id - media_json = api.media.get_info( + media_json = await api.media.get_info( media_id=media_id, fields="caption,id,media_type,media_url,permalink,timestamp", return_json=True, @@ -27,21 +36,30 @@ def test_get_info(helpers, api): assert media_json["id"] == media_id -def test_get_batch(helpers, api): +@pytest.mark.asyncio +async def test_get_batch(helpers, api): ids = ["18027939643230671", "17846368219941692"] - with responses.RequestsMock() as m: - m.add( - method=responses.GET, - url=f"https://graph.instagram.com/{api.version}", - json=helpers.load_json( - "testdata/instagram_basic/apidata/media/medias_info.json" - ), + with respx.mock: + respx.get(f"https://graph.instagram.com/{api.version}").mock( + return_value=respx.MockResponse( + status_code=200, + json=helpers.load_json( + "testdata/instagram_basic/apidata/media/medias_info.json" + ), + ) ) + # m.add( + # method=responses.GET, + # url=f"https://graph.instagram.com/{api.version}", + # json=helpers.load_json( + # "testdata/instagram_basic/apidata/media/medias_info.json" + # ), + # ) - medias = api.media.get_batch(ids=ids) + medias = await api.media.get_batch(ids=ids) assert medias[ids[0]].id == ids[0] - medias_json = api.media.get_batch( + medias_json = await api.media.get_batch( ids=ids, fields="id,media_type,media_url,permalink,timestamp", return_json=True, @@ -49,22 +67,31 @@ def test_get_batch(helpers, api): assert medias_json[ids[0]]["id"] == ids[0] -def test_get_children(helpers, api): +@pytest.mark.asyncio +async def test_get_children(helpers, api): media_id = "18027939643230671" - with responses.RequestsMock() as m: - m.add( - method=responses.GET, - url=f"https://graph.instagram.com/{api.version}/{media_id}/children", - json=helpers.load_json( - "testdata/instagram_basic/apidata/media/media_children.json" - ), + with respx.mock: + respx.get(f"https://graph.instagram.com/{api.version}/{media_id}/children").mock( + return_value=respx.MockResponse( + status_code=200, + json=helpers.load_json( + "testdata/instagram_basic/apidata/media/media_children.json" + ), + ) ) + # m.add( + # method=responses.GET, + # url=f"https://graph.instagram.com/{api.version}/{media_id}/children", + # json=helpers.load_json( + # "testdata/instagram_basic/apidata/media/media_children.json" + # ), + # ) - children = api.media.get_children(media_id=media_id) + children = await api.media.get_children(media_id=media_id) assert len(children.data) == 2 - children_json = api.media.get_children( + children_json = await api.media.get_children( media_id=media_id, fields="id,media_type,media_url", return_json=True, diff --git a/tests/instagram_basic/test_user.py b/tests/instagram_basic/test_user.py index 8c7726e1..041b4517 100644 --- a/tests/instagram_basic/test_user.py +++ b/tests/instagram_basic/test_user.py @@ -1,51 +1,79 @@ """ Tests for basic user api. """ +from itertools import repeat, chain -import responses +import pytest +import respx -def test_get_info(helpers, api): +@pytest.mark.asyncio +async def test_get_info(helpers, api): uid = "17841406338772941" - with responses.RequestsMock() as m: - m.add( - method=responses.GET, - url=f"https://graph.instagram.com/{api.version}/me", - json=helpers.load_json( - "testdata/instagram_basic/apidata/user/user_info.json" - ), + with respx.mock: + respx.get(f"https://graph.instagram.com/{api.version}/me").mock( + return_value=respx.MockResponse( + status_code=200, + json=helpers.load_json( + "testdata/instagram_basic/apidata/user/user_info.json" + ), + ) ) + # m.add( + # method=responses.GET, + # url=f"https://graph.instagram.com/{api.version}/me", + # json=helpers.load_json( + # "testdata/instagram_basic/apidata/user/user_info.json" + # ), + # ) - user = api.user.get_info() + user = await api.user.get_info() assert user.id == uid - user_json = api.user.get_info( + user_json = await api.user.get_info( fields="account_type,id,media_count,username", return_json=True, ) assert user_json["id"] == uid -def test_user_media(helpers, api): +@pytest.mark.asyncio +async def test_user_media(helpers, api): uid = "17841406338772941" - with responses.RequestsMock() as m: - m.add( - method=responses.GET, - url=f"https://graph.instagram.com/{api.version}/{uid}/media", - json=helpers.load_json( - "testdata/instagram_basic/apidata/user/user_medias_p1.json" - ), - ) - m.add( - method=responses.GET, - url=f"https://graph.instagram.com/{api.version}/{uid}/media", - json=helpers.load_json( - "testdata/instagram_basic/apidata/user/user_medias_p2.json" - ), + with respx.mock: + respx.get(f"https://graph.instagram.com/{api.version}/{uid}/media").mock( + side_effect=chain([ + respx.MockResponse( + status_code=200, + json=helpers.load_json( + "testdata/instagram_basic/apidata/user/user_medias_p1.json" + ), + )], + repeat(respx.MockResponse( + status_code=200, + json=helpers.load_json( + "testdata/instagram_basic/apidata/user/user_medias_p2.json" + ), + )), + ) ) + # m.add( + # method=responses.GET, + # url=f"https://graph.instagram.com/{api.version}/{uid}/media", + # json=helpers.load_json( + # "testdata/instagram_basic/apidata/user/user_medias_p1.json" + # ), + # ) + # m.add( + # method=responses.GET, + # url=f"https://graph.instagram.com/{api.version}/{uid}/media", + # json=helpers.load_json( + # "testdata/instagram_basic/apidata/user/user_medias_p2.json" + # ), + # ) - media = api.user.get_media( + media = await api.user.get_media( user_id=uid, count=None, limit=2, @@ -53,6 +81,6 @@ def test_user_media(helpers, api): assert len(media.data) == 4 assert media.data[0].id == "17846368219941692" - media_json = api.user.get_media(user_id=uid, limit=2, return_json=True) + media_json = await api.user.get_media(user_id=uid, limit=2, return_json=True) assert len(media_json["data"]) == 2 assert media_json["data"][0]["id"] == "17896189813249754" diff --git a/tests/instagram_business/test_comment.py b/tests/instagram_business/test_comment.py index 90f80291..df8f19e8 100644 --- a/tests/instagram_business/test_comment.py +++ b/tests/instagram_business/test_comment.py @@ -1,26 +1,35 @@ """ Tests for comment. """ +import pytest +import respx -import responses - -def test_comment_get_info(helpers, api): +@pytest.mark.asyncio +async def test_comment_get_info(helpers, api): comment_id = "17892250648466172" - with responses.RequestsMock() as m: - m.add( - method=responses.GET, - url=f"https://graph.facebook.com/{api.version}/{comment_id}", - json=helpers.load_json( - "testdata/instagram/apidata/comments/comment_fields.json" - ), + with respx.mock: + respx.get(f"https://graph.facebook.com/{api.version}/{comment_id}").mock( + return_value=respx.MockResponse( + status_code=200, + json=helpers.load_json( + "testdata/instagram/apidata/comments/comment_fields.json" + ), + ) ) - - comment = api.comment.get_info(comment_id=comment_id) + # m.add( + # method=responses.GET, + # url=f"https://graph.facebook.com/{api.version}/{comment_id}", + # json=helpers.load_json( + # "testdata/instagram/apidata/comments/comment_fields.json" + # ), + # ) + + comment = await api.comment.get_info(comment_id=comment_id) assert comment.id == comment_id - comment_json = api.comment.get_info( + comment_json = await api.comment.get_info( comment_id=comment_id, fields="id,like_count,text,timestamp,username", return_json=True, @@ -28,22 +37,31 @@ def test_comment_get_info(helpers, api): assert comment_json["id"] == comment_id -def test_comment_get_batch(helpers, api): +@pytest.mark.asyncio +async def test_comment_get_batch(helpers, api): comment_ids = ["17892250648466172", "17858154961981086"] - with responses.RequestsMock() as m: - m.add( - method=responses.GET, - url=f"https://graph.facebook.com/{api.version}", - json=helpers.load_json( - "testdata/instagram/apidata/comments/comments_fields.json" - ), + with respx.mock: + respx.get(f"https://graph.facebook.com/{api.version}").mock( + return_value=respx.MockResponse( + status_code=200, + json=helpers.load_json( + "testdata/instagram/apidata/comments/comments_fields.json" + ), + ) ) - - comments = api.comment.get_batch(ids=comment_ids) + # m.add( + # method=responses.GET, + # url=f"https://graph.facebook.com/{api.version}", + # json=helpers.load_json( + # "testdata/instagram/apidata/comments/comments_fields.json" + # ), + # ) + + comments = await api.comment.get_batch(ids=comment_ids) assert comments[comment_ids[0]].id == comment_ids[0] - comments_json = api.comment.get_batch( + comments_json = await api.comment.get_batch( ids=comment_ids, fields="id,like_count,text,timestamp,username", return_json=True, @@ -51,39 +69,55 @@ def test_comment_get_batch(helpers, api): assert comments_json[comment_ids[0]]["id"] == comment_ids[0] -def test_comment_get_replies(helpers, api): +@pytest.mark.asyncio +async def test_comment_get_replies(helpers, api): comment_id = "17874875428706419" - with responses.RequestsMock() as m: - m.add( - method=responses.GET, - url=f"https://graph.facebook.com/{api.version}/{comment_id}/replies", - json=helpers.load_json("testdata/instagram/apidata/comments/replies.json"), + with respx.mock: + respx.get(f"https://graph.facebook.com/{api.version}/{comment_id}/replies").mock( + return_value=respx.MockResponse( + status_code=200, + json=helpers.load_json("testdata/instagram/apidata/comments/replies.json"), + ) ) + # m.add( + # method=responses.GET, + # url=f"https://graph.facebook.com/{api.version}/{comment_id}/replies", + # json=helpers.load_json("testdata/instagram/apidata/comments/replies.json"), + # ) - replies = api.comment.get_replies(comment_id=comment_id) + replies = await api.comment.get_replies(comment_id=comment_id) assert len(replies.data) == 1 - replies_json = api.comment.get_replies(comment_id=comment_id, return_json=True) + replies_json = await api.comment.get_replies(comment_id=comment_id, return_json=True) assert replies_json["data"][0]["id"] == "17847314687296283" -def test_reply_get_info(helpers, api): +@pytest.mark.asyncio +async def test_reply_get_info(helpers, api): reply_id = "17892250648466172" - with responses.RequestsMock() as m: - m.add( - method=responses.GET, - url=f"https://graph.facebook.com/{api.version}/{reply_id}", - json=helpers.load_json( - "testdata/instagram/apidata/comments/comment_fields.json" - ), + with respx.mock: + respx.get(f"https://graph.facebook.com/{api.version}/{reply_id}").mock( + return_value=respx.MockResponse( + status_code=200, + json=helpers.load_json( + "testdata/instagram/apidata/comments/comment_fields.json" + ), + ) ) - - reply = api.reply.get_info(reply_id=reply_id) + # m.add( + # method=responses.GET, + # url=f"https://graph.facebook.com/{api.version}/{reply_id}", + # json=helpers.load_json( + # "testdata/instagram/apidata/comments/comment_fields.json" + # ), + # ) + + reply = await api.reply.get_info(reply_id=reply_id) assert reply.id == reply_id - reply_json = api.reply.get_info( + reply_json = await api.reply.get_info( reply_id=reply_id, fields="id,like_count,text,timestamp,username", return_json=True, @@ -91,22 +125,31 @@ def test_reply_get_info(helpers, api): assert reply_json["id"] == reply_id -def test_reply_get_batch(helpers, api): +@pytest.mark.asyncio +async def test_reply_get_batch(helpers, api): reply_ids = ["17892250648466172", "17858154961981086"] - with responses.RequestsMock() as m: - m.add( - method=responses.GET, - url=f"https://graph.facebook.com/{api.version}", - json=helpers.load_json( - "testdata/instagram/apidata/comments/comments_fields.json" - ), + with respx.mock: + respx.get(f"https://graph.facebook.com/{api.version}").mock( + return_value=respx.MockResponse( + status_code=200, + json=helpers.load_json( + "testdata/instagram/apidata/comments/comments_fields.json" + ), + ) ) - - replies = api.reply.get_batch(ids=reply_ids) + # m.add( + # method=responses.GET, + # url=f"https://graph.facebook.com/{api.version}", + # json=helpers.load_json( + # "testdata/instagram/apidata/comments/comments_fields.json" + # ), + # ) + + replies = await api.reply.get_batch(ids=reply_ids) assert replies[reply_ids[0]].id == reply_ids[0] - replies_json = api.reply.get_batch( + replies_json = await api.reply.get_batch( ids=reply_ids, fields="id,like_count,text,timestamp,username", return_json=True, diff --git a/tests/instagram_business/test_hashtag.py b/tests/instagram_business/test_hashtag.py index 95d5eb17..2bb66b02 100644 --- a/tests/instagram_business/test_hashtag.py +++ b/tests/instagram_business/test_hashtag.py @@ -1,26 +1,37 @@ """ Tests for hashtag. """ +from itertools import chain, repeat -import responses +import pytest +import respx -def test_get_info(helpers, api): +@pytest.mark.asyncio +async def test_get_info(helpers, api): hashtag_id = "17843826142012701" - with responses.RequestsMock() as m: - m.add( - method=responses.GET, - url=f"https://graph.facebook.com/{api.version}/{hashtag_id}", - json=helpers.load_json( - "testdata/instagram/apidata/hashtags/hashtag_info.json" - ), + with respx.mock: + respx.get(f"https://graph.facebook.com/{api.version}/{hashtag_id}").mock( + return_value=respx.MockResponse( + status_code=200, + json=helpers.load_json( + "testdata/instagram/apidata/hashtags/hashtag_info.json" + ), + ) ) - - hashtag = api.hashtag.get_info(hashtag_id=hashtag_id) + # m.add( + # method=responses.GET, + # url=f"https://graph.facebook.com/{api.version}/{hashtag_id}", + # json=helpers.load_json( + # "testdata/instagram/apidata/hashtags/hashtag_info.json" + # ), + # ) + + hashtag = await api.hashtag.get_info(hashtag_id=hashtag_id) assert hashtag.id == hashtag_id - hashtag_json = api.hashtag.get_info( + hashtag_json = await api.hashtag.get_info( hashtag_id=hashtag_id, fields="id,name", return_json=True, @@ -28,22 +39,31 @@ def test_get_info(helpers, api): assert hashtag_json["id"] == hashtag_id -def test_get_batch(helpers, api): +@pytest.mark.asyncio +async def test_get_batch(helpers, api): hashtag_ids = ["17843826142012701", "17841593698074073"] - with responses.RequestsMock() as m: - m.add( - method=responses.GET, - url=f"https://graph.facebook.com/{api.version}", - json=helpers.load_json( - "testdata/instagram/apidata/hashtags/hashtags_info.json" - ), + with respx.mock: + respx.get(f"https://graph.facebook.com/{api.version}").mock( + return_value=respx.MockResponse( + status_code=200, + json=helpers.load_json( + "testdata/instagram/apidata/hashtags/hashtags_info.json" + ), + ) ) - - hashtags = api.hashtag.get_batch(ids=hashtag_ids) + # m.add( + # method=responses.GET, + # url=f"https://graph.facebook.com/{api.version}", + # json=helpers.load_json( + # "testdata/instagram/apidata/hashtags/hashtags_info.json" + # ), + # ) + + hashtags = await api.hashtag.get_batch(ids=hashtag_ids) assert hashtags[hashtag_ids[0]].id == hashtag_ids[0] - hashtags_json = api.hashtag.get_batch( + hashtags_json = await api.hashtag.get_batch( ids=hashtag_ids, fields="id,name", return_json=True, @@ -51,33 +71,50 @@ def test_get_batch(helpers, api): assert hashtags_json[hashtag_ids[0]]["id"] == hashtag_ids[0] -def test_get_top_media(helpers, api): +@pytest.mark.asyncio +async def test_get_top_media(helpers, api): hashtag_id = "17841562426109234" - with responses.RequestsMock() as m: - m.add( - method=responses.GET, - url=f"https://graph.facebook.com/{api.version}/{hashtag_id}/top_media", - json=helpers.load_json( - "testdata/instagram/apidata/hashtags/hashtag_top_medias_p1.json" - ), - ) - m.add( - method=responses.GET, - url=f"https://graph.facebook.com/{api.version}/{hashtag_id}/top_media", - json=helpers.load_json( - "testdata/instagram/apidata/hashtags/hashtag_top_medias_p2.json" - ), + with respx.mock: + respx.get(f"https://graph.facebook.com/{api.version}/{hashtag_id}/top_media").mock( + side_effect=chain([ + respx.MockResponse( + status_code=200, + json=helpers.load_json( + "testdata/instagram/apidata/hashtags/hashtag_top_medias_p1.json" + ), + )], + repeat(respx.MockResponse( + status_code=200, + json=helpers.load_json( + "testdata/instagram/apidata/hashtags/hashtag_top_medias_p2.json" + ), + )), + ) ) - - top_media = api.hashtag.get_top_media( + # m.add( + # method=responses.GET, + # url=f"https://graph.facebook.com/{api.version}/{hashtag_id}/top_media", + # json=helpers.load_json( + # "testdata/instagram/apidata/hashtags/hashtag_top_medias_p1.json" + # ), + # ) + # m.add( + # method=responses.GET, + # url=f"https://graph.facebook.com/{api.version}/{hashtag_id}/top_media", + # json=helpers.load_json( + # "testdata/instagram/apidata/hashtags/hashtag_top_medias_p2.json" + # ), + # ) + + top_media = await api.hashtag.get_top_media( hashtag_id=hashtag_id, count=None, limit=25, ) assert len(top_media.data) == 50 - top_media_json = api.hashtag.get_top_media( + top_media_json = await api.hashtag.get_top_media( hashtag_id=hashtag_id, count=10, return_json=True, @@ -85,33 +122,50 @@ def test_get_top_media(helpers, api): assert len(top_media_json["data"]) == 10 -def test_get_recent_media(helpers, api): +@pytest.mark.asyncio +async def test_get_recent_media(helpers, api): hashtag_id = "17841562426109234" - with responses.RequestsMock() as m: - m.add( - method=responses.GET, - url=f"https://graph.facebook.com/{api.version}/{hashtag_id}/recent_media", - json=helpers.load_json( - "testdata/instagram/apidata/hashtags/hashtag_recent_medias_p1.json" - ), - ) - m.add( - method=responses.GET, - url=f"https://graph.facebook.com/{api.version}/{hashtag_id}/recent_media", - json=helpers.load_json( - "testdata/instagram/apidata/hashtags/hashtag_recent_medias_p2.json" - ), + with respx.mock: + respx.get(f"https://graph.facebook.com/{api.version}/{hashtag_id}/recent_media").mock( + side_effect=chain([ + respx.MockResponse( + status_code=200, + json=helpers.load_json( + "testdata/instagram/apidata/hashtags/hashtag_recent_medias_p1.json" + ), + )], + repeat(respx.MockResponse( + status_code=200, + json=helpers.load_json( + "testdata/instagram/apidata/hashtags/hashtag_recent_medias_p2.json" + ), + )), + ) ) - - top_media = api.hashtag.get_recent_media( + # m.add( + # method=responses.GET, + # url=f"https://graph.facebook.com/{api.version}/{hashtag_id}/recent_media", + # json=helpers.load_json( + # "testdata/instagram/apidata/hashtags/hashtag_recent_medias_p1.json" + # ), + # ) + # m.add( + # method=responses.GET, + # url=f"https://graph.facebook.com/{api.version}/{hashtag_id}/recent_media", + # json=helpers.load_json( + # "testdata/instagram/apidata/hashtags/hashtag_recent_medias_p2.json" + # ), + # ) + + top_media = await api.hashtag.get_recent_media( hashtag_id=hashtag_id, count=None, limit=5, ) assert len(top_media.data) == 10 - top_media_json = api.hashtag.get_recent_media( + top_media_json = await api.hashtag.get_recent_media( hashtag_id=hashtag_id, count=5, return_json=True, diff --git a/tests/instagram_business/test_media.py b/tests/instagram_business/test_media.py index 4fc5a1be..9de63d44 100644 --- a/tests/instagram_business/test_media.py +++ b/tests/instagram_business/test_media.py @@ -1,26 +1,37 @@ """ Tests for media. """ +from itertools import chain, repeat -import responses +import pytest +import respx -def test_get_info(helpers, api): +@pytest.mark.asyncio +async def test_get_info(helpers, api): media_id = "17896129349106152" - with responses.RequestsMock() as m: - m.add( - method=responses.GET, - url=f"https://graph.facebook.com/{api.version}/{media_id}", - json=helpers.load_json( - "testdata/instagram/apidata/medias/media_default.json" - ), + with respx.mock: + respx.get(f"https://graph.facebook.com/{api.version}/{media_id}").mock( + return_value=respx.MockResponse( + status_code=200, + json=helpers.load_json( + "testdata/instagram/apidata/medias/media_default.json" + ), + ) ) - - media = api.media.get_info(media_id=media_id) + # m.add( + # method=responses.GET, + # url=f"https://graph.facebook.com/{api.version}/{media_id}", + # json=helpers.load_json( + # "testdata/instagram/apidata/medias/media_default.json" + # ), + # ) + + media = await api.media.get_info(media_id=media_id) assert media.id == media_id - media_json = api.media.get_info( + media_json = await api.media.get_info( media_id=media_id, fields="id,caption,comments_count,children{id,media_type,media_url,permalink,shortcode,thumbnail_url,timestamp},like_count,media_type,media_url,permalink,timestamp", return_json=True, @@ -28,22 +39,31 @@ def test_get_info(helpers, api): assert media_json["id"] == media_id -def test_get_batch(helpers, api): +@pytest.mark.asyncio +async def test_get_batch(helpers, api): media_ids = ["17893085207160254", "17896129349106152"] - with responses.RequestsMock() as m: - m.add( - method=responses.GET, - url=f"https://graph.facebook.com/{api.version}", - json=helpers.load_json( - "testdata/instagram/apidata/medias/medias_fields.json" - ), + with respx.mock: + respx.get(f"https://graph.facebook.com/{api.version}").mock( + return_value=respx.MockResponse( + status_code=200, + json=helpers.load_json( + "testdata/instagram/apidata/medias/medias_fields.json" + ), + ) ) - - medias = api.media.get_batch(ids=media_ids) + # m.add( + # method=responses.GET, + # url=f"https://graph.facebook.com/{api.version}", + # json=helpers.load_json( + # "testdata/instagram/apidata/medias/medias_fields.json" + # ), + # ) + + medias = await api.media.get_batch(ids=media_ids) assert medias[media_ids[0]].id == media_ids[0] - medias_json = api.media.get_batch( + medias_json = await api.media.get_batch( ids=media_ids, fields="id,caption,comments_count,children{id,media_type,media_url,permalink,shortcode,thumbnail_url,timestamp},like_count,media_type,media_url,permalink,timestamp", return_json=True, @@ -51,107 +71,155 @@ def test_get_batch(helpers, api): assert medias_json[media_ids[0]]["id"] == media_ids[0] -def test_get_comments(helpers, api): +@pytest.mark.asyncio +async def test_get_comments(helpers, api): media_id = "17846368219941692" - with responses.RequestsMock() as m: - m.add( - method=responses.GET, - url=f"https://graph.facebook.com/{api.version}/{media_id}/comments", - json=helpers.load_json( - "testdata/instagram/apidata/medias/comments_p1.json" - ), - ) - m.add( - method=responses.GET, - url=f"https://graph.facebook.com/{api.version}/{media_id}/comments", - json=helpers.load_json( - "testdata/instagram/apidata/medias/comments_p2.json" - ), + with respx.mock: + respx.get(f"https://graph.facebook.com/{api.version}/{media_id}/comments").mock( + side_effect=chain( + [respx.MockResponse( + status_code=200, + json=helpers.load_json( + "testdata/instagram/apidata/medias/comments_p1.json" + ), + )], + repeat(respx.MockResponse( + status_code=200, + json=helpers.load_json( + "testdata/instagram/apidata/medias/comments_p2.json" + ), + )), + ) ) - - comments = api.media.get_comments(media_id=media_id) + # m.add( + # method=responses.GET, + # url=f"https://graph.facebook.com/{api.version}/{media_id}/comments", + # json=helpers.load_json( + # "testdata/instagram/apidata/medias/comments_p1.json" + # ), + # ) + # m.add( + # method=responses.GET, + # url=f"https://graph.facebook.com/{api.version}/{media_id}/comments", + # json=helpers.load_json( + # "testdata/instagram/apidata/medias/comments_p2.json" + # ), + # ) + + comments = await api.media.get_comments(media_id=media_id) assert len(comments.data) == 4 assert comments.data[0].id == "17858154961981086" - comments_json = api.media.get_comments( + comments_json = await api.media.get_comments( media_id=media_id, return_json=True, ) assert comments_json["data"][0]["id"] == "17892250648466172" -def test_get_children(helpers, api): +@pytest.mark.asyncio +async def test_get_children(helpers, api): media_id = "17846368219941692" - with responses.RequestsMock() as m: - m.add( - method=responses.GET, - url=f"https://graph.facebook.com/{api.version}/{media_id}/children", - json=helpers.load_json("testdata/instagram/apidata/medias/children.json"), + with respx.mock: + respx.get(f"https://graph.facebook.com/{api.version}/{media_id}/children").mock( + return_value=respx.MockResponse( + status_code=200, + json=helpers.load_json("testdata/instagram/apidata/medias/children.json") + ) ) + # m.add( + # method=responses.GET, + # url=f"https://graph.facebook.com/{api.version}/{media_id}/children", + # json=helpers.load_json("testdata/instagram/apidata/medias/children.json"), + # ) - children = api.media.get_children(media_id=media_id) + children = await api.media.get_children(media_id=media_id) assert len(children.data) == 2 assert children.data[0].media_type == "IMAGE" - children_json = api.media.get_children( + children_json = await api.media.get_children( media_id=media_id, return_json=True, ) assert children_json["data"][0]["id"] == "17924318443748703" -def test_get_insights(helpers, api): +@pytest.mark.asyncio +async def test_get_insights(helpers, api): media_id = "17846368219941692" - with responses.RequestsMock() as m: - m.add( - method=responses.GET, - url=f"https://graph.facebook.com/{api.version}/{media_id}/insights", - json=helpers.load_json("testdata/instagram/apidata/medias/insights.json"), + with respx.mock: + respx.get(f"https://graph.facebook.com/{api.version}/{media_id}/insights").mock( + return_value=respx.MockResponse( + status_code=200, + json=helpers.load_json("testdata/instagram/apidata/medias/insights.json"), + ) ) + # m.add( + # method=responses.GET, + # url=f"https://graph.facebook.com/{api.version}/{media_id}/insights", + # json=helpers.load_json("testdata/instagram/apidata/medias/insights.json"), + # ) - insights = api.media.get_insights( + insights = await api.media.get_insights( media_id=media_id, metric="impressions,reach,saved", ) assert len(insights.data) == 3 assert insights.data[0].name == "impressions" - insights_json = api.media.get_insights( + insights_json = await api.media.get_insights( media_id=media_id, metric="impressions,reach,saved", return_json=True, ) assert insights_json["data"][0]["name"] == "impressions" - with responses.RequestsMock() as m: - m.add( - method=responses.GET, - url=f"https://graph.facebook.com/{api.version}/{media_id}/insights", - json=helpers.load_json( - "testdata/instagram/apidata/medias/insights_new.json" - ), + with respx.mock: + respx.get(f"https://graph.facebook.com/{api.version}/{media_id}/insights").mock( + return_value=respx.MockResponse( + status_code=200, + json=helpers.load_json( + "testdata/instagram/apidata/medias/insights_new.json" + ), + ) ) - - insights = api.media.get_insights( + # m.add( + # method=responses.GET, + # url=f"https://graph.facebook.com/{api.version}/{media_id}/insights", + # json=helpers.load_json( + # "testdata/instagram/apidata/medias/insights_new.json" + # ), + # ) + + insights = await api.media.get_insights( media_id=media_id, metric="profile_activity", breakdown=["action_type"] ) assert insights.data[0].total_value.breakdowns[0].results[0].value == 11 -def test_get_product_tags(helpers, api): +@pytest.mark.asyncio +async def test_get_product_tags(helpers, api): media_id = "90010778325754" - with responses.RequestsMock() as m: - m.add( - method=responses.GET, - url=f"https://graph.facebook.com/{api.version}/{media_id}/product_tags", - json=helpers.load_json( - "testdata/instagram/apidata/medias/product_tags.json" - ), + with respx.mock: + respx.get(f"https://graph.facebook.com/{api.version}/{media_id}/product_tags").mock( + return_value=respx.MockResponse( + status_code=200, + json=helpers.load_json( + "testdata/instagram/apidata/medias/product_tags.json" + ), + ) ) - - tags = api.media.get_product_tags(media_id=media_id) + # m.add( + # method=responses.GET, + # url=f"https://graph.facebook.com/{api.version}/{media_id}/product_tags", + # json=helpers.load_json( + # "testdata/instagram/apidata/medias/product_tags.json" + # ), + # ) + + tags = await api.media.get_product_tags(media_id=media_id) assert len(tags.data) == 1 assert tags.data[0].product_id == 3231775643511089 - tags_json = api.media.get_product_tags(media_id=media_id, return_json=True) + tags_json = await api.media.get_product_tags(media_id=media_id, return_json=True) assert tags_json["data"][0]["review_status"] == "approved" diff --git a/tests/instagram_business/test_publish.py b/tests/instagram_business/test_publish.py index 09ec5f92..b8db124d 100644 --- a/tests/instagram_business/test_publish.py +++ b/tests/instagram_business/test_publish.py @@ -1,26 +1,35 @@ """ Tests for publishes. """ +import pytest +import respx -import responses - -def test_get_info(helpers, api): +@pytest.mark.asyncio +async def test_get_info(helpers, api): container_id = "17966279578433554" - with responses.RequestsMock() as m: - m.add( - method=responses.GET, - url=f"https://graph.facebook.com/{api.version}/{container_id}", - json=helpers.load_json( - "testdata/instagram/apidata/publish/container_info.json" - ), + with respx.mock: + respx.get(f"https://graph.facebook.com/{api.version}/{container_id}").mock( + return_value=respx.MockResponse( + status_code=200, + json=helpers.load_json( + "testdata/instagram/apidata/publish/container_info.json" + ), + ) ) + # m.add( + # method=responses.GET, + # url=f"https://graph.facebook.com/{api.version}/{container_id}", + # json=helpers.load_json( + # "testdata/instagram/apidata/publish/container_info.json" + # ), + # ) - container = api.container.get_info(container_id=container_id) + container = await api.container.get_info(container_id=container_id) assert container.id == container_id - container_json = api.container.get_info( + container_json = await api.container.get_info( container_id=container_id, fields="id,status_code", return_json=True, @@ -28,22 +37,31 @@ def test_get_info(helpers, api): assert container_json["id"] == container_id -def test_get_batch(helpers, api): +@pytest.mark.asyncio +async def test_get_batch(helpers, api): container_ids = ["17883019676403955", "17966279578433554"] - with responses.RequestsMock() as m: - m.add( - method=responses.GET, - url=f"https://graph.facebook.com/{api.version}", - json=helpers.load_json( - "testdata/instagram/apidata/publish/containers_info.json" - ), + with respx.mock: + respx.get(f"https://graph.facebook.com/{api.version}").mock( + return_value=respx.MockResponse( + status_code=200, + json=helpers.load_json( + "testdata/instagram/apidata/publish/containers_info.json" + ), + ) ) + # m.add( + # method=responses.GET, + # url=f"https://graph.facebook.com/{api.version}", + # json=helpers.load_json( + # "testdata/instagram/apidata/publish/containers_info.json" + # ), + # ) - containers = api.container.get_batch(ids=container_ids) + containers = await api.container.get_batch(ids=container_ids) assert containers[container_ids[0]].id == container_ids[0] - containers_json = api.container.get_batch( + containers_json = await api.container.get_batch( ids=container_ids, fields="id,status_code", return_json=True, diff --git a/tests/instagram_business/test_user.py b/tests/instagram_business/test_user.py index bdf71b33..7eaf05c1 100644 --- a/tests/instagram_business/test_user.py +++ b/tests/instagram_business/test_user.py @@ -1,71 +1,106 @@ """ Tests for user api. """ +from itertools import chain, repeat -import responses +import pytest +import respx -def test_get_info(helpers, api): - with responses.RequestsMock() as m: - m.add( - method=responses.GET, - url=f"https://graph.facebook.com/{api.version}/{api.instagram_business_id}", - json=helpers.load_json("testdata/instagram/apidata/users/user_fields.json"), +@pytest.mark.asyncio +async def test_get_info(helpers, api): + with respx.mock: + respx.get(f"https://graph.facebook.com/{api.version}/{api.instagram_business_id}").mock( + return_value=respx.MockResponse( + status_code=200, + json=helpers.load_json("testdata/instagram/apidata/users/user_fields.json"), + ) ) + # m.add( + # method=responses.GET, + # url=f"https://graph.facebook.com/{api.version}/{api.instagram_business_id}", + # json=helpers.load_json("testdata/instagram/apidata/users/user_fields.json"), + # ) - user = api.user.get_info() + user = await api.user.get_info() assert user.id == api.instagram_business_id - user_json = api.user.get_info( + user_json = await api.user.get_info( fields="id,biography,name,username,profile_picture_url,followers_count,media_count", return_json=True, ) assert user_json["id"] == api.instagram_business_id -def test_discovery_user(helpers, api): +@pytest.mark.asyncio +async def test_discovery_user(helpers, api): username = "facebookfordevelopers" - with responses.RequestsMock() as m: - m.add( - method=responses.GET, - url=f"https://graph.facebook.com/{api.version}/{api.instagram_business_id}", - json=helpers.load_json( - "testdata/instagram/apidata/discovery/medias_p1.json" - ), + with respx.mock: + respx.get(f"https://graph.facebook.com/{api.version}/{api.instagram_business_id}").mock( + return_value=respx.MockResponse( + status_code=200, + json=helpers.load_json( + "testdata/instagram/apidata/discovery/medias_p1.json" + ), + ) ) - - user = api.user.discovery_user(username=username) + # m.add( + # method=responses.GET, + # url=f"https://graph.facebook.com/{api.version}/{api.instagram_business_id}", + # json=helpers.load_json( + # "testdata/instagram/apidata/discovery/medias_p1.json" + # ), + # ) + + user = await api.user.discovery_user(username=username) assert user.business_discovery.id == "17841407673135339" - user_json = api.user.discovery_user( + user_json = await api.user.discovery_user( username=username, return_json=True, ) assert user_json["business_discovery"]["id"] == "17841407673135339" -def test_discovery_media(helpers, api): +@pytest.mark.asyncio +async def test_discovery_media(helpers, api): username = "facebookfordevelopers" - with responses.RequestsMock() as m: - m.add( - method=responses.GET, - url=f"https://graph.facebook.com/{api.version}/{api.instagram_business_id}", - json=helpers.load_json( - "testdata/instagram/apidata/discovery/medias_p1.json" - ), - ) - m.add( - method=responses.GET, - url=f"https://graph.facebook.com/{api.version}/{api.instagram_business_id}", - json=helpers.load_json( - "testdata/instagram/apidata/discovery/medias_p2.json" - ), + with respx.mock: + respx.get(f"https://graph.facebook.com/{api.version}/{api.instagram_business_id}").mock( + side_effect=[ + respx.MockResponse( + status_code=200, + json=helpers.load_json( + "testdata/instagram/apidata/discovery/medias_p1.json" + ), + ), + respx.MockResponse( + status_code=200, + json=helpers.load_json( + "testdata/instagram/apidata/discovery/medias_p2.json" + ), + ), + ] ) - - media_p1 = api.user.discovery_user_medias(username=username, limit=2) - media_p2 = api.user.discovery_user_medias( + # m.add( + # method=responses.GET, + # url=f"https://graph.facebook.com/{api.version}/{api.instagram_business_id}", + # json=helpers.load_json( + # "testdata/instagram/apidata/discovery/medias_p1.json" + # ), + # ) + # m.add( + # method=responses.GET, + # url=f"https://graph.facebook.com/{api.version}/{api.instagram_business_id}", + # json=helpers.load_json( + # "testdata/instagram/apidata/discovery/medias_p2.json" + # ), + # ) + + media_p1 = await api.user.discovery_user_medias(username=username, limit=2) + media_p2 = await api.user.discovery_user_medias( username=username, limit=2, after=media_p1.business_discovery.media.paging.cursors.after, @@ -73,71 +108,105 @@ def test_discovery_media(helpers, api): assert len(media_p1.business_discovery.media.data) == 2 assert len(media_p2.business_discovery.media.data) == 2 - with responses.RequestsMock() as m: - m.add( - method=responses.GET, - url=f"https://graph.facebook.com/{api.version}/{api.instagram_business_id}", - json=helpers.load_json( - "testdata/instagram/apidata/discovery/medias_p2.json" - ), + with respx.mock: + respx.get(f"https://graph.facebook.com/{api.version}/{api.instagram_business_id}").mock( + return_value=respx.MockResponse( + status_code=200, + json=helpers.load_json( + "testdata/instagram/apidata/discovery/medias_p2.json" + ), + ) ) - - media = api.user.discovery_user_medias( + # m.add( + # method=responses.GET, + # url=f"https://graph.facebook.com/{api.version}/{api.instagram_business_id}", + # json=helpers.load_json( + # "testdata/instagram/apidata/discovery/medias_p2.json" + # ), + # ) + + media = await api.user.discovery_user_medias( username=username, limit=2, before="before", return_json=True ) assert len(media["business_discovery"]["media"]["data"]) == 2 -def test_get_content_publishing_limit(helpers, api): - with responses.RequestsMock() as m: - m.add( - method=responses.GET, - url=f"https://graph.facebook.com/{api.version}/{api.instagram_business_id}/content_publishing_limit", - json=helpers.load_json( - "testdata/instagram/apidata/users/content_publish_limit.json" - ), +@pytest.mark.asyncio +async def test_get_content_publishing_limit(helpers, api): + with respx.mock: + respx.get(f"https://graph.facebook.com/{api.version}/{api.instagram_business_id}/content_publishing_limit").mock( + return_value=respx.MockResponse( + status_code=200, + json=helpers.load_json( + "testdata/instagram/apidata/users/content_publish_limit.json" + ), + ) ) - - limit = api.user.get_content_publishing_limit() + # m.add( + # method=responses.GET, + # url=f"https://graph.facebook.com/{api.version}/{api.instagram_business_id}/content_publishing_limit", + # json=helpers.load_json( + # "testdata/instagram/apidata/users/content_publish_limit.json" + # ), + # ) + + limit = await api.user.get_content_publishing_limit() assert limit.data[0].config.quota_total == 25 - limit_json = api.user.get_content_publishing_limit(return_json=True) + limit_json = await api.user.get_content_publishing_limit(return_json=True) assert limit_json["data"][0]["quota_usage"] == 0 -def test_get_user_insights(helpers, api): - with responses.RequestsMock() as m: - m.add( - method=responses.GET, - url=f"https://graph.facebook.com/{api.version}/{api.instagram_business_id}/insights", - json=helpers.load_json( - "testdata/instagram/apidata/users/user_insights.json" - ), +@pytest.mark.asyncio +async def test_get_user_insights(helpers, api): + with respx.mock: + respx.get(f"https://graph.facebook.com/{api.version}/{api.instagram_business_id}/insights").mock( + return_value=respx.MockResponse( + status_code=200, + json=helpers.load_json( + "testdata/instagram/apidata/users/user_insights.json" + ), + ) ) - - insights = api.user.get_insights( + # m.add( + # method=responses.GET, + # url=f"https://graph.facebook.com/{api.version}/{api.instagram_business_id}/insights", + # json=helpers.load_json( + # "testdata/instagram/apidata/users/user_insights.json" + # ), + # ) + + insights = await api.user.get_insights( metric="impressions,reach,profile_views", period="day", access_token=api.access_token, ) assert len(insights.data) == 3 - insights_json = api.user.get_insights( + insights_json = await api.user.get_insights( metric="impressions,reach,profile_views", period="day", return_json=True, ) assert len(insights_json["data"]) == 3 - with responses.RequestsMock() as m: - m.add( - method=responses.GET, - url=f"https://graph.facebook.com/{api.version}/{api.instagram_business_id}/insights", - json=helpers.load_json( - "testdata/instagram/apidata/users/user_insights_new.json" - ), + with respx.mock: + respx.get(f"https://graph.facebook.com/{api.version}/{api.instagram_business_id}/insights").mock( + return_value=respx.MockResponse( + status_code=200, + json=helpers.load_json( + "testdata/instagram/apidata/users/user_insights_new.json" + ), + ) ) - insights = api.user.get_insights( + # m.add( + # method=responses.GET, + # url=f"https://graph.facebook.com/{api.version}/{api.instagram_business_id}/insights", + # json=helpers.load_json( + # "testdata/instagram/apidata/users/user_insights_new.json" + # ), + # ) + insights = await api.user.get_insights( metric="reach", period="day", metric_type="total_value", @@ -148,208 +217,307 @@ def test_get_user_insights(helpers, api): assert insights.data[0].total_value.value == 14516883 -def test_get_user_media(helpers, api): - with responses.RequestsMock() as m: - m.add( - method=responses.GET, - url=f"https://graph.facebook.com/{api.version}/{api.instagram_business_id}/media", - json=helpers.load_json("testdata/instagram/apidata/users/medias_p1.json"), +@pytest.mark.asyncio +async def test_get_user_media(helpers, api): + with respx.mock: + respx.get(f"https://graph.facebook.com/{api.version}/{api.instagram_business_id}/media").mock( + side_effect=chain( + [respx.MockResponse( + status_code=200, + json=helpers.load_json("testdata/instagram/apidata/users/medias_p1.json"), + )], + repeat(respx.MockResponse( + status_code=200, + json=helpers.load_json("testdata/instagram/apidata/users/medias_p2.json"), + )), + ) ) - m.add( - method=responses.GET, - url=f"https://graph.facebook.com/{api.version}/{api.instagram_business_id}/media", - json=helpers.load_json("testdata/instagram/apidata/users/medias_p2.json"), - ) - - medias = api.user.get_media(count=3, limit=2) + # m.add( + # method=responses.GET, + # url=f"https://graph.facebook.com/{api.version}/{api.instagram_business_id}/media", + # json=helpers.load_json("testdata/instagram/apidata/users/medias_p1.json"), + # ) + # m.add( + # method=responses.GET, + # url=f"https://graph.facebook.com/{api.version}/{api.instagram_business_id}/media", + # json=helpers.load_json("testdata/instagram/apidata/users/medias_p2.json"), + # ) + + medias = await api.user.get_media(count=3, limit=2) assert len(medias.data) == 3 assert medias.data[0].id == "17895731045244887" - medias_json = api.user.get_media( + medias_json = await api.user.get_media( limit=2, return_json=True, ) assert len(medias_json["data"]) == 2 -def test_get_mentioned_comment(helpers, api): +@pytest.mark.asyncio +async def test_get_mentioned_comment(helpers, api): cm_id = "17967008497439572" - with responses.RequestsMock() as m: - m.add( - method=responses.GET, - url=f"https://graph.facebook.com/{api.version}/{api.instagram_business_id}", - json=helpers.load_json( - "testdata/instagram/apidata/users/mentioned_comment.json" - ), + with respx.mock: + respx.get(f"https://graph.facebook.com/{api.version}/{api.instagram_business_id}").mock( + return_value=respx.MockResponse( + status_code=200, + json=helpers.load_json( + "testdata/instagram/apidata/users/mentioned_comment.json" + ), + ) ) - - comment = api.user.get_mentioned_comment(comment_id=cm_id) + # m.add( + # method=responses.GET, + # url=f"https://graph.facebook.com/{api.version}/{api.instagram_business_id}", + # json=helpers.load_json( + # "testdata/instagram/apidata/users/mentioned_comment.json" + # ), + # ) + + comment = await api.user.get_mentioned_comment(comment_id=cm_id) assert comment.mentioned_comment.id == cm_id - comment_json = api.user.get_mentioned_comment( + comment_json = await api.user.get_mentioned_comment( comment_id=cm_id, return_json=True ) assert comment_json["mentioned_comment"]["id"] == cm_id -def test_get_mentioned_media(helpers, api): +@pytest.mark.asyncio +async def test_get_mentioned_media(helpers, api): media_id = "17889114512354744" - with responses.RequestsMock() as m: - m.add( - method=responses.GET, - url=f"https://graph.facebook.com/{api.version}/{api.instagram_business_id}", - json=helpers.load_json( - "testdata/instagram/apidata/users/mentioned_media.json" - ), + with respx.mock: + respx.get(f"https://graph.facebook.com/{api.version}/{api.instagram_business_id}").mock( + return_value=respx.MockResponse( + status_code=200, + json=helpers.load_json( + "testdata/instagram/apidata/users/mentioned_media.json" + ), + ) ) - - media = api.user.get_mentioned_media(media_id=media_id) + # m.add( + # method=responses.GET, + # url=f"https://graph.facebook.com/{api.version}/{api.instagram_business_id}", + # json=helpers.load_json( + # "testdata/instagram/apidata/users/mentioned_media.json" + # ), + # ) + + media = await api.user.get_mentioned_media(media_id=media_id) assert media.mentioned_media.id == media_id - media_json = api.user.get_mentioned_media(media_id=media_id, return_json=True) + media_json = await api.user.get_mentioned_media(media_id=media_id, return_json=True) assert media_json["mentioned_media"]["id"] == media_id -def test_get_hashtag_search(helpers, api): - with responses.RequestsMock() as m: - m.add( - method=responses.GET, - url=f"https://graph.facebook.com/{api.version}/ig_hashtag_search", - json=helpers.load_json( - "testdata/instagram/apidata/users/hashtag_search.json" - ), +@pytest.mark.asyncio +async def test_get_hashtag_search(helpers, api): + with respx.mock: + respx.get(f"https://graph.facebook.com/{api.version}/ig_hashtag_search",).mock( + return_value=respx.MockResponse( + status_code=200, + json=helpers.load_json( + "testdata/instagram/apidata/users/hashtag_search.json" + ), + ) ) - - hashtag = api.user.get_hashtag_search(q="developers") + # m.add( + # method=responses.GET, + # url=f"https://graph.facebook.com/{api.version}/ig_hashtag_search", + # json=helpers.load_json( + # "testdata/instagram/apidata/users/hashtag_search.json" + # ), + # ) + + hashtag = await api.user.get_hashtag_search(q="developers") assert hashtag.data[0].id == "17841562426109234" - hashtag_json = api.user.get_hashtag_search(q="developers", return_json=True) + hashtag_json = await api.user.get_hashtag_search(q="developers", return_json=True) assert hashtag_json["data"][0]["id"] == "17841562426109234" -def test_get_recently_searched_hashtags(helpers, api): - with responses.RequestsMock() as m: - m.add( - method=responses.GET, - url=f"https://graph.facebook.com/{api.version}/{api.instagram_business_id}/recently_searched_hashtags", - json=helpers.load_json( - "testdata/instagram/apidata/users/recently_searched_hashtags.json" - ), +@pytest.mark.asyncio +async def test_get_recently_searched_hashtags(helpers, api): + with respx.mock: + respx.get(f"https://graph.facebook.com/{api.version}/{api.instagram_business_id}/recently_searched_hashtags").mock( + return_value=respx.MockResponse( + status_code=200, + json=helpers.load_json( + "testdata/instagram/apidata/users/recently_searched_hashtags.json" + ), + ) ) - - hashtags = api.user.get_recently_searched_hashtags() + # m.add( + # method=responses.GET, + # url=f"https://graph.facebook.com/{api.version}/{api.instagram_business_id}/recently_searched_hashtags", + # json=helpers.load_json( + # "testdata/instagram/apidata/users/recently_searched_hashtags.json" + # ), + # ) + + hashtags = await api.user.get_recently_searched_hashtags() assert len(hashtags.data) == 2 assert hashtags.data[0].id == "17841562426109234" - hashtags_json = api.user.get_recently_searched_hashtags(return_json=True) + hashtags_json = await api.user.get_recently_searched_hashtags(return_json=True) assert len(hashtags_json["data"]) == 2 -def test_get_stories(helpers, api): - with responses.RequestsMock() as m: - m.add( - method=responses.GET, - url=f"https://graph.facebook.com/{api.version}/{api.instagram_business_id}/stories", - json=helpers.load_json("testdata/instagram/apidata/users/stories.json"), +@pytest.mark.asyncio +async def test_get_stories(helpers, api): + with respx.mock: + respx.get(f"https://graph.facebook.com/{api.version}/{api.instagram_business_id}/stories").mock( + return_value=respx.MockResponse( + status_code=200, + json=helpers.load_json("testdata/instagram/apidata/users/stories.json"), + ) ) + # m.add( + # method=responses.GET, + # url=f"https://graph.facebook.com/{api.version}/{api.instagram_business_id}/stories", + # json=helpers.load_json("testdata/instagram/apidata/users/stories.json"), + # ) - stories = api.user.get_stories() + stories = await api.user.get_stories() assert len(stories.data) == 2 assert stories.data[0].id == "17879450117512729" - stories_json = api.user.get_stories(return_json=True) + stories_json = await api.user.get_stories(return_json=True) assert len(stories_json["data"]) == 2 -def test_get_tagged_media(helpers, api): - with responses.RequestsMock() as m: - m.add( - method=responses.GET, - url=f"https://graph.facebook.com/{api.version}/{api.instagram_business_id}/tags", - json=helpers.load_json( - "testdata/instagram/apidata/users/tagged_medias.json" - ), +@pytest.mark.asyncio +async def test_get_tagged_media(helpers, api): + with respx.mock: + respx.get(f"https://graph.facebook.com/{api.version}/{api.instagram_business_id}/tags").mock( + return_value=respx.MockResponse( + status_code=200, + json=helpers.load_json( + "testdata/instagram/apidata/users/tagged_medias.json" + ), + ) ) - - medias = api.user.get_tagged_media() + # m.add( + # method=responses.GET, + # url=f"https://graph.facebook.com/{api.version}/{api.instagram_business_id}/tags", + # json=helpers.load_json( + # "testdata/instagram/apidata/users/tagged_medias.json" + # ), + # ) + + medias = await api.user.get_tagged_media() assert len(medias.data) == 5 assert medias.data[0].id == "17892354025952911" - medias_json = api.user.get_tagged_media(return_json=True) + medias_json = await api.user.get_tagged_media(return_json=True) assert len(medias_json["data"]) == 5 -def test_get_live_media(helpers, api): - with responses.RequestsMock() as m: - m.add( - method=responses.GET, - url=f"https://graph.facebook.com/{api.version}/{api.instagram_business_id}/live_media", - json=helpers.load_json("testdata/instagram/apidata/users/live_medias.json"), +@pytest.mark.asyncio +async def test_get_live_media(helpers, api): + with respx.mock: + respx.get(f"https://graph.facebook.com/{api.version}/{api.instagram_business_id}/live_media").mock( + return_value=respx.MockResponse( + status_code=200, + json=helpers.load_json("testdata/instagram/apidata/users/live_medias.json"), + ) ) + # m.add( + # method=responses.GET, + # url=f"https://graph.facebook.com/{api.version}/{api.instagram_business_id}/live_media", + # json=helpers.load_json("testdata/instagram/apidata/users/live_medias.json"), + # ) - medias = api.user.get_live_media() + medias = await api.user.get_live_media() assert len(medias.data) == 1 assert medias.data[0].id == "90010498116233" - medias_json = api.user.get_live_media(return_json=True) + medias_json = await api.user.get_live_media(return_json=True) assert len(medias_json["data"]) == 1 -def test_get_available_catalogs(helpers, api): - with responses.RequestsMock() as m: - m.add( - method=responses.GET, - url=f"https://graph.facebook.com/{api.version}/{api.instagram_business_id}/available_catalogs", - json=helpers.load_json( - "testdata/instagram/apidata/users/available_catalogs.json" - ), +@pytest.mark.asyncio +async def test_get_available_catalogs(helpers, api): + with respx.mock: + respx.get(f"https://graph.facebook.com/{api.version}/{api.instagram_business_id}/available_catalogs").mock( + return_value=respx.MockResponse( + status_code=200, + json=helpers.load_json( + "testdata/instagram/apidata/users/available_catalogs.json" + ), + ) ) - - catalogs = api.user.get_available_catalogs() + # m.add( + # method=responses.GET, + # url=f"https://graph.facebook.com/{api.version}/{api.instagram_business_id}/available_catalogs", + # json=helpers.load_json( + # "testdata/instagram/apidata/users/available_catalogs.json" + # ), + # ) + + catalogs = await api.user.get_available_catalogs() assert len(catalogs.data) == 1 assert catalogs.data[0].catalog_id == "960179311066902" - catalogs_json = api.user.get_available_catalogs(return_json=True) + catalogs_json = await api.user.get_available_catalogs(return_json=True) assert len(catalogs_json["data"]) == 1 -def test_get_catalog_product_search(helpers, api): +@pytest.mark.asyncio +async def test_get_catalog_product_search(helpers, api): catalog_id = "960179311066902" - with responses.RequestsMock() as m: - m.add( - method=responses.GET, - url=f"https://graph.facebook.com/{api.version}/{api.instagram_business_id}/catalog_product_search", - json=helpers.load_json( - "testdata/instagram/apidata/users/catalog_product_search.json" - ), + with respx.mock: + respx.get(f"https://graph.facebook.com/{api.version}/{api.instagram_business_id}/catalog_product_search").mock( + return_value=respx.MockResponse( + status_code=200, + json=helpers.load_json( + "testdata/instagram/apidata/users/catalog_product_search.json" + ), + ) ) - - products = api.user.get_catalog_product_search(catalog_id=catalog_id) + # m.add( + # method=responses.GET, + # url=f"https://graph.facebook.com/{api.version}/{api.instagram_business_id}/catalog_product_search", + # json=helpers.load_json( + # "testdata/instagram/apidata/users/catalog_product_search.json" + # ), + # ) + + products = await api.user.get_catalog_product_search(catalog_id=catalog_id) assert len(products.data) == 1 assert products.data[0].product_id == 3231775643511089 - products_json = api.user.get_catalog_product_search( + products_json = await api.user.get_catalog_product_search( catalog_id=catalog_id, return_json=True ) assert len(products_json["data"]) == 1 -def test_get_product_appeal(helpers, api): +@pytest.mark.asyncio +async def test_get_product_appeal(helpers, api): product_id = 4029274203846188 - with responses.RequestsMock() as m: - m.add( - method=responses.GET, - url=f"https://graph.facebook.com/{api.version}/{api.instagram_business_id}/product_appeal", - json=helpers.load_json( - "testdata/instagram/apidata/users/product_appeal.json" - ), + with respx.mock: + respx.get(f"https://graph.facebook.com/{api.version}/{api.instagram_business_id}/product_appeal").mock( + return_value=respx.MockResponse( + status_code=200, + json=helpers.load_json( + "testdata/instagram/apidata/users/product_appeal.json" + ), + ) ) - - appeals = api.user.get_product_appeal(product_id=product_id) + # m.add( + # method=responses.GET, + # url=f"https://graph.facebook.com/{api.version}/{api.instagram_business_id}/product_appeal", + # json=helpers.load_json( + # "testdata/instagram/apidata/users/product_appeal.json" + # ), + # ) + + appeals = await api.user.get_product_appeal(product_id=product_id) assert len(appeals.data) == 1 assert appeals.data[0].product_id == product_id - appeals_json = api.user.get_product_appeal( + appeals_json = await api.user.get_product_appeal( product_id=product_id, return_json=True ) assert len(appeals_json["data"]) == 1 diff --git a/tests/test_basic_display_api.py b/tests/test_basic_display_api.py index f8894919..3dc33ae0 100644 --- a/tests/test_basic_display_api.py +++ b/tests/test_basic_display_api.py @@ -3,59 +3,80 @@ """ import pytest -import responses +import respx from pyfacebook import BasicDisplayAPI from pyfacebook.exceptions import LibraryError -def test_oath_flow(helpers): +@pytest.mark.asyncio +async def test_oath_flow(helpers): api = BasicDisplayAPI(app_id="id", app_secret="secret", oauth_flow=True) # test get authorization url _, state = api.get_authorization_url() # if user give authorize. - resp = "https://localhost/?code=code&state=PyFacebook#_" - - with responses.RequestsMock() as m: - m.add( - method=responses.POST, - url=api.EXCHANGE_ACCESS_TOKEN_URL, - json=helpers.load_json("testdata/base/basic_display_api_user_token.json"), + resp = "https://localhost/?code=code&state=PyFacebook" + + with respx.mock: + respx.post(api.EXCHANGE_ACCESS_TOKEN_URL).mock( + return_value=respx.MockResponse( + status_code=200, + json=helpers.load_json("testdata/base/basic_display_api_user_token.json"), + ) ) + # m.add( + # method=responses.POST, + # url=api.EXCHANGE_ACCESS_TOKEN_URL, + # json=helpers.load_json("testdata/base/basic_display_api_user_token.json"), + # ) - r = api.exchange_user_access_token(response=resp) + r = await api.exchange_user_access_token(response=resp) assert r["access_token"] == "token" -def test_exchange_long_lived_token(helpers): +@pytest.mark.asyncio +async def test_exchange_long_lived_token(helpers): api = BasicDisplayAPI(access_token="token") # test exchange long-lived page token - with responses.RequestsMock() as m: - m.add( - method=responses.GET, - url=f"https://graph.instagram.com/access_token", - json=helpers.load_json("testdata/base/long_term_token.json"), + with respx.mock: + respx.get(f"https://graph.instagram.com/access_token").mock( + return_value=respx.MockResponse( + status_code=200, + json=helpers.load_json("testdata/base/long_term_token.json"), + ) ) + # m.add( + # method=responses.GET, + # url=f"https://graph.instagram.com/access_token", + # json=helpers.load_json("testdata/base/long_term_token.json"), + # ) - res = api.exchange_long_lived_user_access_token() + res = await api.exchange_long_lived_user_access_token() assert res["access_token"] == "token" -def test_refresh_token(helpers): +@pytest.mark.asyncio +async def test_refresh_token(helpers): api = BasicDisplayAPI(access_token="token") # test exchange long-lived page token - with responses.RequestsMock() as m: - m.add( - method=responses.GET, - url=f"https://graph.instagram.com/refresh_access_token", - json=helpers.load_json("testdata/base/long_term_token.json"), + with respx.mock: + respx.get(f"https://graph.instagram.com/refresh_access_token").mock( + return_value=respx.MockResponse( + status_code=200, + json=helpers.load_json("testdata/base/long_term_token.json"), + ) ) + # m.add( + # method=responses.GET, + # url=f"https://graph.instagram.com/refresh_access_token", + # json=helpers.load_json("testdata/base/long_term_token.json"), + # ) - res = api.refresh_access_token(access_token=api.access_token) + res = await api.refresh_access_token(access_token=api.access_token) assert res["access_token"] == "token" diff --git a/tests/test_graph.py b/tests/test_graph.py index 13355256..be26149b 100644 --- a/tests/test_graph.py +++ b/tests/test_graph.py @@ -1,10 +1,10 @@ """ tests for base graph api. """ - +import httpx import pytest import requests -import responses +import respx from pyfacebook import GraphAPI, LibraryError, FacebookError @@ -41,24 +41,24 @@ def test_sleep_seconds_mapping(pubg_api): assert r is None -def test_request_error(pubg_api): +@pytest.mark.asyncio +async def test_request_error(pubg_api): with pytest.raises(LibraryError): - with responses.RequestsMock() as m: - m.add( - "GET", "https://graph.facebook.com/", body=requests.HTTPError("Wrong") - ) + with respx.mock: + respx.get("https://graph.facebook.com/").mock(side_effect=httpx.HTTPError("Wrong")) - pubg_api._request( + await pubg_api._request( url="https://graph.facebook.com/", ) def test_parse_response(pubg_api): - resp = requests.Response() - resp.headers["Content-Type"] = "multipart/form-data" + with respx.mock: + resp = respx.MockResponse() - with pytest.raises(LibraryError): - pubg_api._parse_response(response=resp) + resp.headers["Content-Type"] = "multipart/form-data" + with pytest.raises(LibraryError): + pubg_api._parse_response(response=resp) def test_graph_error(helpers, pubg_api): @@ -68,29 +68,35 @@ def test_graph_error(helpers, pubg_api): pubg_api._check_graph_error(data) -def test_get_object(helpers, pubg_api): +@pytest.mark.asyncio +async def test_get_object(helpers, pubg_api): obj_id = "20531316728" # test default field - with responses.RequestsMock() as m: - m.add( - method="GET", - url=f"https://graph.facebook.com/{pubg_api.version}/{obj_id}", - json=helpers.load_json("testdata/base/object_default.json"), + with respx.mock: + respx.get(f"https://graph.facebook.com/{pubg_api.version}/{obj_id}").mock( + return_value=respx.MockResponse( + status_code=200, + json=helpers.load_json("testdata/base/object_default.json") + ) ) - res = pubg_api.get_object(object_id=obj_id, fields="") + res = await pubg_api.get_object(object_id=obj_id, fields="") + print("Response type:", type(res)) + print("Response content:", res) + assert res["id"] == obj_id # test more fields - with responses.RequestsMock() as m: - m.add( - method="GET", - url=f"https://graph.facebook.com/{pubg_api.version}/{obj_id}", - json=helpers.load_json("testdata/base/object_data.json"), + with respx.mock: + respx.get(f"https://graph.facebook.com/{pubg_api.version}/{obj_id}").mock( + return_value=respx.MockResponse( + status_code=200, + json=helpers.load_json("testdata/base/object_data.json"), + ) ) - res = pubg_api.get_object( + res = await pubg_api.get_object( object_id=obj_id, fields="id,about,can_checkin,category,category_list,checkins,contact_address,cover,current_location,description,description_html,display_subtext,emails,engagement,fan_count,founded,general_info,global_brand_page_name,global_brand_root_id,link,name,phone,picture,rating_count,single_line_address,start_info,talking_about_count,username,verification_status,website,were_here_count,whatsapp_number", appsecret_proof="xxxx", @@ -99,21 +105,23 @@ def test_get_object(helpers, pubg_api): assert res["engagement"]["count"] == 209924887 -def test_get_objects(helpers, pubg_api): +@pytest.mark.asyncio +async def test_get_objects(helpers, pubg_api): ids = "20531316728,19292868552" - with responses.RequestsMock() as m: - m.add( - method="GET", - url=f"https://graph.facebook.com/{pubg_api.version}", - json=helpers.load_json("testdata/base/objects_default.json"), + with respx.mock: + respx.get(f"https://graph.facebook.com/{pubg_api.version}").mock( + return_value=respx.MockResponse( + status_code=200, + json=helpers.load_json("testdata/base/objects_default.json"), + ) ) - res = pubg_api.get_objects(ids=ids) + res = await pubg_api.get_objects(ids=ids) assert len(res) == 2 assert res["20531316728"]["id"] == "20531316728" - res = pubg_api.get_objects( + res = await pubg_api.get_objects( ids=ids, fields="id,name", appsecret_proof="xxxx", @@ -121,19 +129,21 @@ def test_get_objects(helpers, pubg_api): assert len(res) == 2 -def test_get_connection(helpers, pubg_api): +@pytest.mark.asyncio +async def test_get_connection(helpers, pubg_api): api = GraphAPI(access_token="token") obj_id = "19292868552" # image - with responses.RequestsMock() as m: - m.add( - method=responses.GET, - url=f"https://graph.facebook.com/{pubg_api.version}/{obj_id}/picture", - body=helpers.load_file_binary("testdata/base/19292868552-picture.png"), - content_type="image/png", + with respx.mock: + respx.get(f"https://graph.facebook.com/{pubg_api.version}/{obj_id}/picture").mock( + return_value=respx.MockResponse( + status_code=200, + content=helpers.load_file_binary("testdata/base/19292868552-picture.png"), + content_type="image/png", + ) ) - res = api.get_connection( + res = await api.get_connection( object_id=obj_id, connection="picture", redirect=0, @@ -141,30 +151,37 @@ def test_get_connection(helpers, pubg_api): assert res["content-type"] == "image/png" # data - with responses.RequestsMock() as m: - m.add( - method=responses.GET, - url=f"https://graph.facebook.com/{pubg_api.version}/{obj_id}/picture", - json=helpers.load_json("testdata/base/connecion_data.json"), + with respx.mock: + respx.get(f"https://graph.facebook.com/{pubg_api.version}/{obj_id}/picture").mock( + return_value=respx.MockResponse( + status_code=200, + json=helpers.load_json("testdata/base/connecion_data.json") + ) ) - - res = api.get_connection(object_id=obj_id, connection="picture") + res = await api.get_connection(object_id=obj_id, connection="picture") assert res["data"]["height"] == 50 -def test_get_full_connections(helpers): +@pytest.mark.asyncio +async def test_get_full_connections(helpers): obj_id = "19292868552" api = GraphAPI(access_token="token", version="v11.0") # test with count - with responses.RequestsMock() as m: - m.add( - method=responses.GET, - url="https://graph.facebook.com/v11.0/19292868552/feed", - json=helpers.load_json("testdata/base/full_connecions_p1.json"), + with respx.mock: + respx.get("https://graph.facebook.com/v11.0/19292868552/feed").mock( + return_value=respx.MockResponse( + status_code=200, + json=helpers.load_json("testdata/base/full_connecions_p1.json") + ) ) + # m.add( + # method=responses.GET, + # url="https://graph.facebook.com/v11.0/19292868552/feed", + # json=helpers.load_json("testdata/base/full_connecions_p1.json"), + # ) - feed = api.get_full_connections( + feed = await api.get_full_connections( object_id=obj_id, connection="feed", count=3, @@ -174,19 +191,32 @@ def test_get_full_connections(helpers): assert len(feed["data"]) == 3 # test with no next - with responses.RequestsMock() as m: - m.add( - method=responses.GET, - url="https://graph.facebook.com/v11.0/19292868552/feed", - json=helpers.load_json("testdata/base/full_connecions_p1.json"), - ) - m.add( - method=responses.GET, - url="https://graph.facebook.com/v11.0/19292868552/feed", - json=helpers.load_json("testdata/base/full_connecions_p2.json"), + with respx.mock: + respx.get("https://graph.facebook.com/v11.0/19292868552/feed").mock( + side_effect=[ + respx.MockResponse( + status_code=200, + json=helpers.load_json("testdata/base/full_connecions_p1.json") + ), + respx.MockResponse( + status_code=200, + json=helpers.load_json("testdata/base/full_connecions_p2.json") + ) + ] ) - feed = api.get_full_connections( + # m.add( + # method=responses.GET, + # url="https://graph.facebook.com/v11.0/19292868552/feed", + # json=helpers.load_json("testdata/base/full_connecions_p1.json"), + # ) + # m.add( + # method=responses.GET, + # url="https://graph.facebook.com/v11.0/19292868552/feed", + # json=helpers.load_json("testdata/base/full_connecions_p2.json"), + # ) + + feed = await api.get_full_connections( object_id=obj_id, connection="feed", count=None, @@ -196,19 +226,26 @@ def test_get_full_connections(helpers): assert len(feed["data"]) == 8 -def test_discovery_user_media(helpers): +@pytest.mark.asyncio +async def test_discovery_user_media(helpers): username = "facebook" api = GraphAPI(access_token="token", instagram_business_id="id") # test with count - with responses.RequestsMock() as m: - m.add( - method=responses.GET, - url=f"https://graph.facebook.com/{api.version}/id", - json=helpers.load_json("testdata/base/discovery_medias_p1.json"), + with respx.mock: + respx.get(f"https://graph.facebook.com/{api.version}/id").mock( + return_value=respx.MockResponse( + status_code=200, + json=helpers.load_json("testdata/base/discovery_medias_p1.json") + ) ) + # m.add( + # method=responses.GET, + # url=f"https://graph.facebook.com/{api.version}/id", + # json=helpers.load_json("testdata/base/discovery_medias_p1.json"), + # ) - media = api.discovery_user_media( + media = await api.discovery_user_media( username=username, fields="comments_count,id,like_count,media_type,media_url,permalink,timestamp", count=3, @@ -217,20 +254,32 @@ def test_discovery_user_media(helpers): assert len(media["data"]) == 3 # test with no next - with responses.RequestsMock() as m: - m.add( - method=responses.GET, - url=f"https://graph.facebook.com/{api.version}/id", - json=helpers.load_json("testdata/base/discovery_medias_p1.json"), + with respx.mock: + respx.get(f"https://graph.facebook.com/{api.version}/id").mock( + side_effect=[ + respx.MockResponse( + status_code=200, + json=helpers.load_json("testdata/base/discovery_medias_p1.json") + ), + respx.MockResponse( + status_code=200, + json=helpers.load_json("testdata/base/discovery_medias_p2.json") + ), + ] ) - - m.add( - method=responses.GET, - url=f"https://graph.facebook.com/{api.version}/id", - json=helpers.load_json("testdata/base/discovery_medias_p2.json"), - ) - - media = api.discovery_user_media( + # m.add( + # method=responses.GET, + # url=f"https://graph.facebook.com/{api.version}/id", + # json=helpers.load_json("testdata/base/discovery_medias_p1.json"), + # ) + + # m.add( + # method=responses.GET, + # url=f"https://graph.facebook.com/{api.version}/id", + # json=helpers.load_json("testdata/base/discovery_medias_p2.json"), + # ) + + media = await api.discovery_user_media( username=username, fields="comments_count,id,like_count,media_type,media_url,permalink,timestamp", count=None, @@ -239,16 +288,23 @@ def test_discovery_user_media(helpers): assert len(media["data"]) == 10 -def test_post_object(helpers, user_api): +@pytest.mark.asyncio +async def test_post_object(helpers, user_api): obj_id = "123456789" - with responses.RequestsMock() as m: - m.add( - method="POST", - url=f"https://graph.facebook.com/{user_api.version}/{obj_id}/comments", - json={"success": True}, + with respx.mock: + respx.post(f"https://graph.facebook.com/{user_api.version}/{obj_id}/comments").mock( + return_value=respx.MockResponse( + status_code=200, + json={"success": True}, + ) ) + # m.add( + # method="POST", + # url=f"https://graph.facebook.com/{user_api.version}/{obj_id}/comments", + # json={"success": True}, + # ) - res = user_api.post_object( + res = await user_api.post_object( object_id=obj_id, connection="comments", data={"message": "data"}, @@ -256,22 +312,30 @@ def test_post_object(helpers, user_api): assert res["success"] -def test_delete_object(helpers, user_api): +@pytest.mark.asyncio +async def test_delete_object(helpers, user_api): obj_id = "123456789" - with responses.RequestsMock() as m: - m.add( - method="DELETE", - url=f"https://graph.facebook.com/{user_api.version}/{obj_id}", - json={"success": True}, + with respx.mock: + respx.delete(f"https://graph.facebook.com/{user_api.version}/{obj_id}").mock( + return_value=respx.MockResponse( + status_code=200, + json={"success": True}, + ) ) + # m.add( + # method="DELETE", + # url=f"https://graph.facebook.com/{user_api.version}/{obj_id}", + # json={"success": True}, + # ) - res = user_api.delete_object( + res = await user_api.delete_object( object_id=obj_id, ) assert res["success"] -def test_oauth_flow(helpers): +@pytest.mark.asyncio +async def test_oauth_flow(helpers): with pytest.raises(LibraryError): api = GraphAPI(access_token="token") api.get_authorization_url() @@ -282,99 +346,145 @@ def test_oauth_flow(helpers): _, state = api.get_authorization_url() # if user give authorize. - resp = "https://localhost/?code=code&state=PyFacebook#_=_" + resp = "https://localhost/?code=code&state=PyFacebook" - with responses.RequestsMock() as m: - m.add( - method=responses.POST, - url=api.EXCHANGE_ACCESS_TOKEN_URL, - json=helpers.load_json("testdata/base/long_term_token.json"), + with respx.mock: + respx.post(api.EXCHANGE_ACCESS_TOKEN_URL).mock( + return_value=respx.MockResponse( + status_code=200, + json=helpers.load_json("testdata/base/long_term_token.json"), + ) ) + # m.add( + # method=responses.POST, + # url=api.EXCHANGE_ACCESS_TOKEN_URL, + # json=helpers.load_json("testdata/base/long_term_token.json"), + # ) - r = api.exchange_user_access_token(response=resp) + r = await api.exchange_user_access_token(response=resp) assert r["access_token"] == "token" -def test_exchange_token(helpers): +@pytest.mark.asyncio +async def test_exchange_token(helpers): api = GraphAPI(access_token="token") page_id = "19292868552" # test page access token - with responses.RequestsMock() as m: - m.add( - method=responses.GET, - url=f"https://graph.facebook.com/{api.version}/{page_id}", - json={"id": "19292868552", "access_token": "token"}, + with respx.mock: + respx.get(f"https://graph.facebook.com/{api.version}/{page_id}").mock( + return_value=respx.MockResponse( + status_code=200, + json={"id": "19292868552", "access_token": "token"}, + ) ) - token = api.exchange_page_access_token(page_id=page_id) + # m.add( + # method=responses.GET, + # url=f"https://graph.facebook.com/{api.version}/{page_id}", + # json={"id": "19292868552", "access_token": "token"}, + # ) + token = await api.exchange_page_access_token(page_id=page_id) assert token == "token" # test can not exchange page access token - with responses.RequestsMock() as m: - m.add( - method=responses.GET, - url=f"https://graph.facebook.com/{api.version}/{page_id}", - json={"id": "19292868552"}, + with respx.mock: + respx.get(f"https://graph.facebook.com/{api.version}/{page_id}").mock( + return_value=respx.MockResponse( + status_code=200, + json={"id": "19292868552"}, + ) ) + # m.add( + # method=responses.GET, + # url=f"https://graph.facebook.com/{api.version}/{page_id}", + # json={"id": "19292868552"}, + # ) with pytest.raises(LibraryError): - api.exchange_page_access_token(page_id=page_id) + await api.exchange_page_access_token(page_id=page_id) # test exchange long-lived user token - with responses.RequestsMock() as m: - m.add( - method=responses.GET, - url=f"https://graph.facebook.com/oauth/access_token", - json={ - "access_token": "token", - "token_type": "bearer", - "expires_in": 5184000, - }, + with respx.mock: + respx.get(f"https://graph.facebook.com/oauth/access_token").mock( + return_value=respx.MockResponse( + status_code=200, + json={ + "access_token": "token", + "token_type": "bearer", + "expires_in": 5184000, + }, + ) ) - - res = api.exchange_long_lived_user_access_token() + # m.add( + # method=responses.GET, + # url=f"https://graph.facebook.com/oauth/access_token", + # json={ + # "access_token": "token", + # "token_type": "bearer", + # "expires_in": 5184000, + # }, + # ) + + res = await api.exchange_long_lived_user_access_token() assert res["access_token"] == "token" user_id = "123456" # test exchange long-lived page token - with responses.RequestsMock() as m: - m.add( - method=responses.GET, - url=f"https://graph.facebook.com/{api.version}/{user_id}/accounts", - json=helpers.load_json("testdata/base/user_accounts_token.json"), + with respx.mock: + respx.get(f"https://graph.facebook.com/{api.version}/{user_id}/accounts").mock( + return_value=respx.MockResponse( + status_code=200, + json=helpers.load_json("testdata/base/user_accounts_token.json"), + ) ) + # m.add( + # method=responses.GET, + # url=f"https://graph.facebook.com/{api.version}/{user_id}/accounts", + # json=helpers.load_json("testdata/base/user_accounts_token.json"), + # ) - res = api.exchange_long_lived_page_access_token(user_id=user_id) + res = await api.exchange_long_lived_page_access_token(user_id=user_id) assert len(res["data"]) == 1 assert res["data"][0]["access_token"] == "access_token" -def test_get_app_token(): +@pytest.mark.asyncio +async def test_get_app_token(): api = GraphAPI(app_id="id", app_secret="secret", access_token="token") - with responses.RequestsMock() as m: - m.add( - method=responses.GET, - url=f"https://graph.facebook.com/oauth/access_token", - json={"access_token": "access_token", "token_type": "bearer"}, + with respx.mock: + respx.get(f"https://graph.facebook.com/oauth/access_token").mock( + return_value=respx.MockResponse( + status_code=200, + json={"access_token": "access_token", "token_type": "bearer"}, + ) ) + # m.add( + # method=responses.GET, + # url=f"https://graph.facebook.com/oauth/access_token", + # json={"access_token": "access_token", "token_type": "bearer"}, + # ) - data = api.get_app_token() + data = await api.get_app_token() assert data["access_token"] == "access_token" - # initial with application_only_auth - GraphAPI(app_id="id", app_secret="secret", application_only_auth=True) - -def test_debug_token(helpers, pubg_api): +@pytest.mark.asyncio +async def test_debug_token(helpers, pubg_api): input_token = "token" - with responses.RequestsMock() as m: - m.add( - method=responses.GET, - url=f"https://graph.facebook.com/{pubg_api.version}/debug_token", - json=helpers.load_json("testdata/base/token_info.json"), + with respx.mock: + respx.get(f"https://graph.facebook.com/{pubg_api.version}/debug_token").mock( + return_value=respx.MockResponse( + status_code=200, + json=helpers.load_json("testdata/base/token_info.json"), + ) ) + # m.add( + # method=responses.GET, + # url=f"https://graph.facebook.com/{pubg_api.version}/debug_token", + # json=helpers.load_json("testdata/base/token_info.json"), + # ) - res = pubg_api.debug_token(input_token=input_token) + res = await pubg_api.debug_token(input_token=input_token) assert res["data"]["type"] == "USER" assert res["data"]["is_valid"] diff --git a/tests/test_threads_graph_api.py b/tests/test_threads_graph_api.py index eda23aac..9554ed42 100644 --- a/tests/test_threads_graph_api.py +++ b/tests/test_threads_graph_api.py @@ -1,8 +1,8 @@ """ tests for threads graph api """ - -import responses +import pytest +import respx from pyfacebook import ThreadsGraphAPI @@ -11,49 +11,71 @@ def test_threads_get_authorization_url(): api = ThreadsGraphAPI(app_id="id", app_secret="secret", oauth_flow=True) url, state = api.get_authorization_url(scope=["threads_basic"]) + print(url) assert ( url == "https://threads.net/oauth/authorize?response_type=code&client_id=id&redirect_uri=https%3A%2F%2Flocalhost%2F&scope=threads_basic&state=PyFacebook" ) -def test_threads_exchange_user_access_token(helpers): +@pytest.mark.asyncio +async def test_threads_exchange_user_access_token(helpers): api = ThreadsGraphAPI(app_id="id", app_secret="secret", oauth_flow=True) - resp = "https://localhost/?code=code&state=PyFacebook#_" + resp = "https://localhost/?code=code&state=PyFacebook" - with responses.RequestsMock() as m: - m.add( - method=responses.POST, - url=api.EXCHANGE_ACCESS_TOKEN_URL, - json=helpers.load_json("testdata/base/threads_user_token.json"), + with respx.mock: + respx.post(api.EXCHANGE_ACCESS_TOKEN_URL).mock( + return_value=respx.MockResponse( + status_code=200, + json=helpers.load_json("testdata/base/threads_user_token.json"), + ) ) + # m.add( + # method=responses.POST, + # url=api.EXCHANGE_ACCESS_TOKEN_URL, + # json=helpers.load_json("testdata/base/threads_user_token.json"), + # ) - r = api.exchange_user_access_token(response=resp, scope=["threads_basic"]) + r = await api.exchange_user_access_token(response=resp, scope=["threads_basic"]) assert r["access_token"] == "THQVJ..." -def test_threads_exchange_long_lived_user_access_token(helpers): +@pytest.mark.asyncio +async def test_threads_exchange_long_lived_user_access_token(helpers): api = ThreadsGraphAPI(app_id="id", app_secret="secret", access_token="token") - with responses.RequestsMock() as m: - m.add( - method=responses.GET, - url=f"https://graph.threads.net/oauth/access_token", - json=helpers.load_json("testdata/base/threads_user_long_lived_token.json"), + with respx.mock: + respx.get(f"https://graph.threads.net/oauth/access_token").mock( + return_value=respx.MockResponse( + status_code=200, + json=helpers.load_json("testdata/base/threads_user_long_lived_token.json"), + ) ) + # m.add( + # method=responses.GET, + # url=f"https://graph.threads.net/oauth/access_token", + # json=helpers.load_json("testdata/base/threads_user_long_lived_token.json"), + # ) - r = api.exchange_long_lived_user_access_token() + r = await api.exchange_long_lived_user_access_token() assert r["access_token"] == "THQVJ..." -def test_threads_refresh_access_token(helpers): +@pytest.mark.asyncio +async def test_threads_refresh_access_token(helpers): api = ThreadsGraphAPI(app_id="id", app_secret="secret", access_token="token") - with responses.RequestsMock() as m: - m.add( - method=responses.GET, - url=f"https://graph.threads.net/refresh_access_token", - json=helpers.load_json("testdata/base/threads_user_long_lived_token.json"), + with respx.mock: + respx.get(f"https://graph.threads.net/refresh_access_token").mock( + return_value=respx.MockResponse( + status_code=200, + json=helpers.load_json("testdata/base/threads_user_long_lived_token.json"), + ) ) + # m.add( + # method=responses.GET, + # url=f"https://graph.threads.net/refresh_access_token", + # json=helpers.load_json("testdata/base/threads_user_long_lived_token.json"), + # ) - r = api.refresh_access_token(access_token=api.access_token) + r = await api.refresh_access_token(access_token=api.access_token) assert r["access_token"] == "THQVJ..." From 4a24b06eb2bb65390b32189004f2c6116b61cbcf Mon Sep 17 00:00:00 2001 From: David McKenna Date: Sun, 3 Aug 2025 16:27:26 -0300 Subject: [PATCH 2/5] Updating Readme --- README.rst | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/README.rst b/README.rst index 21d246ec..12734ce3 100644 --- a/README.rst +++ b/README.rst @@ -79,14 +79,14 @@ You can initialize a ``GraphAPI`` object with three different methods, depending # ('https://www.facebook.com/dialog/oauth?response_type=code&client_id=id&redirect_uri=https%3A%2F%2Flocalhost%2F&scope=public_profile&state=PyFacebook', 'PyFacebook') # let user to do oauth at the browser opened by link. # then get the response url - >>> api.exchange_user_access_token(response="url redirected") + >>> await api.exchange_user_access_token(response="url redirected") # Now the api will get the user access token. For more info about the different access tokens, see https://developers.facebook.com/docs/facebook-login/guides/access-tokens. Once you have the user access token, you can get the Facebook data. For example, - >>> api.get_object(object_id="20531316728") + >>> await api.get_object(object_id="20531316728") >>> {'name': 'Facebook App', 'id': '20531316728'} See the code for more operations. @@ -97,12 +97,12 @@ FacebookAPI To get the user data:: - >>> fb.user.get_info(user_id="413140042878187") + >>> await fb.user.get_info(user_id="413140042878187") >>> User(id='413140042878187', name='Kun Liu') To get the page data:: - >>> fb.page.get_info(page_id="20531316728") + >>> await fb.page.get_info(page_id="20531316728") >>> Page(id='20531316728', name='Facebook App') For more info, please, see the code or the docs. From 2c9a62456667786b96200d9dc5765596c0d1b0b6 Mon Sep 17 00:00:00 2001 From: David McKenna Date: Sun, 3 Aug 2025 16:29:32 -0300 Subject: [PATCH 3/5] Updating Docs --- docs/docs/usage/graph-api.md | 12 ++++++------ docs/docs/usage/threads-graph-api.md | 4 ++-- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/docs/docs/usage/graph-api.md b/docs/docs/usage/graph-api.md index 54143545..5d9989ad 100644 --- a/docs/docs/usage/graph-api.md +++ b/docs/docs/usage/graph-api.md @@ -33,14 +33,14 @@ After initial API. Now we can access facebook api with this API. You can get data of an object, such as page,post,photo and so on. ```python -api.get_object(object_id="108824017345866") +await api.get_object(object_id="108824017345866") # {'name': 'Meta', 'id': '108824017345866'} ``` You can get the data of some objects. ```python -api.get_objects(ids="108824017345866,20531316728") +await api.get_objects(ids="108824017345866,20531316728") # {'108824017345866': {'name': 'Meta', 'id': '108824017345866'}, '20531316728': {'name': 'Facebook App', 'id': '20531316728'}} ``` @@ -48,21 +48,21 @@ If you want to get data for an object's edge. For example, a User node can have can have comments connected to it. ```python -api.get_connection(object_id="20531316728", connection="posts") +await api.get_connection(object_id="20531316728", connection="posts") # {'data': [{'created_time': '2021-11-26T20:01:40+0000', 'message': "Do Black Friday right with the #BuyBlack Friday Show! For the finale, host Elaine Welteroth will be talking with Sir Darius Brown, the owner of Beaux and Paws. We'll also have special guests D-Nice, Iddris Sandu., and Jaden Smith! Join this fun shop-a-thon in partnership with Meta for Business \n\nShop directly on [Beaux and Paws] Facebook page so you can get your pet looking proper. fb.me/buyblackfri", 'story': 'Facebook App was live.', 'id': '20531316728_3789869301238646'}], 'paging': {'cursors': {'before': 'before', 'after': 'after'}, 'next': 'https://graph.facebook.com/v12.0/20531316728/posts?access_token=access_token&limit=1&after=after'}} ``` If you want to get all data for an object's edge. Auto paging inside. ```python -api.get_full_connections(object_id="20531316728", connection="posts") +await api.get_full_connections(object_id="20531316728", connection="posts") # {'data': [{'created_time': '2021-11-26T20:01:40+0000', 'message': "Do Black Friday right with the #BuyBlack Friday Show! For the finale, host Elaine Welteroth will be talking with Sir Darius Brown, the owner of Beaux and Paws. We'll also have special guests D-Nice, Iddris Sandu., and Jaden Smith! Join this fun shop-a-thon in partnership with Meta for Business \n\nShop directly on [Beaux and Paws] Facebook page so you can get your pet looking proper. fb.me/buyblackfri", 'story': 'Facebook App was live.', 'id': '20531316728_3789869301238646'}], 'paging': {'cursors': {'before': 'before', 'after': 'after'}, 'next': 'https://graph.facebook.com/v12.0/20531316728/posts?access_token=access_token&limit=1&after=after'}} ``` If you have permissions to publish data. you can use `post` to create data. ```python -api.post_object(object_id="2121008874780932_404879271158877", connection="comments", +await api.post_object(object_id="2121008874780932_404879271158877", connection="comments", data={"message": "Comment by the api"}) # {'id': '404879271158877_405046241142180'} ``` @@ -70,6 +70,6 @@ api.post_object(object_id="2121008874780932_404879271158877", connection="commen If you have permissions to delete data. ```python -api.delete_object(object_id="404879271158877_405046241142180") +await api.delete_object(object_id="404879271158877_405046241142180") # {'success': True} ``` diff --git a/docs/docs/usage/threads-graph-api.md b/docs/docs/usage/threads-graph-api.md index e487f27d..8b154b08 100644 --- a/docs/docs/usage/threads-graph-api.md +++ b/docs/docs/usage/threads-graph-api.md @@ -27,7 +27,7 @@ api.get_authorization_url() # Once the user has authorized your app, you will get the redirected URL. # like `https://example.com/callback?code=AQBZzYhLZB&state=PyFacebook#_` -token = api.exchange_user_access_token(response="Your response url") +token = await api.exchange_user_access_token(response="Your response url") print(token) # {'access_token': 'access_token', 'user_id': 12342412} ``` @@ -37,7 +37,7 @@ After those steps, you can use the `api` object to call the Threads API. For example: ```python -api.get_object(object_id="me", fields=["id"]) +await api.get_object(object_id="me", fields=["id"]) # {'id': '12342412'} ``` From 39cf0192dafb44c1d6579ac9690b1a8411202d0b Mon Sep 17 00:00:00 2001 From: David McKenna Date: Sun, 3 Aug 2025 17:07:52 -0300 Subject: [PATCH 4/5] Changing GraphAPI proxies to proxy_mounts for httpx --- pyfacebook/api/graph.py | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/pyfacebook/api/graph.py b/pyfacebook/api/graph.py index c4a86ee0..c3a27d31 100644 --- a/pyfacebook/api/graph.py +++ b/pyfacebook/api/graph.py @@ -57,7 +57,7 @@ def __init__( sleep_seconds_mapping: Optional[Dict[int, int]] = None, base_url: Optional[str] = None, timeout: Optional[int] = None, - proxies: Optional[dict] = None, + proxy_mounts: Optional[dict] = None, instagram_business_id: Optional[str] = None, authorization_url: Optional[str] = None, access_token_url: Optional[str] = None, @@ -68,11 +68,8 @@ def __init__( self.app_id = app_id self.app_secret = app_secret self.access_token = access_token - - self.session = requests.Session() - self.client = httpx.AsyncClient() + self.client = httpx.AsyncClient(mounts=proxy_mounts) self.__timeout = timeout - self.proxies = proxies self.sleep_on_rate_limit = sleep_on_rate_limit self.sleep_seconds_mapping = self._build_sleep_seconds_resource( sleep_seconds_mapping=sleep_seconds_mapping @@ -216,7 +213,6 @@ async def _request( params=args, data=post_args, files=files, - # proxies=self.proxies, TODO: setup proxies **kwargs, ) except httpx.HTTPError as ex: From 197e58e7d99f3921d54c7c6f4f7795e88aa75dc4 Mon Sep 17 00:00:00 2001 From: David McKenna Date: Sun, 3 Aug 2025 17:50:50 -0300 Subject: [PATCH 5/5] Removing commented requests tests --- tests/facebook/edges/test_likes.py | 17 +-- tests/facebook/test_album.py | 28 ----- tests/facebook/test_application.py | 14 --- tests/facebook/test_business.py | 14 --- tests/facebook/test_comment.py | 42 +------- tests/facebook/test_conversation.py | 14 --- tests/facebook/test_event.py | 10 -- tests/facebook/test_group.py | 38 ------- tests/facebook/test_live_video.py | 28 ----- tests/facebook/test_message.py | 14 --- tests/facebook/test_page.py | 116 -------------------- tests/facebook/test_photo.py | 28 ----- tests/facebook/test_post.py | 28 ----- tests/facebook/test_video.py | 28 ----- tests/instagram_basic/test_media.py | 21 ---- tests/instagram_basic/test_user.py | 21 ---- tests/instagram_business/test_comment.py | 33 ------ tests/instagram_business/test_hashtag.py | 42 -------- tests/instagram_business/test_media.py | 52 --------- tests/instagram_business/test_publish.py | 14 --- tests/instagram_business/test_user.py | 132 +---------------------- tests/test_basic_display_api.py | 15 --- tests/test_graph.py | 83 -------------- tests/test_server_sent_events.py | 2 - tests/test_threads_graph_api.py | 15 --- 25 files changed, 7 insertions(+), 842 deletions(-) diff --git a/tests/facebook/edges/test_likes.py b/tests/facebook/edges/test_likes.py index c8a118ff..f2b85d3e 100644 --- a/tests/facebook/edges/test_likes.py +++ b/tests/facebook/edges/test_likes.py @@ -15,11 +15,6 @@ async def test_get_likes(helpers, fb_api): json=helpers.load_json("testdata/facebook/apidata/likes/likes_resp.json") ), ) - # m.add( - # method=responses.GET, - # url=f"https://graph.facebook.com/{fb_api.version}/{object_id}/likes", - # json=helpers.load_json("testdata/facebook/apidata/likes/likes_resp.json"), - # ) likes_resp = await fb_api.post.get_likes( object_id=object_id, @@ -42,11 +37,7 @@ async def test_create_like(fb_api): json={"success": True}, ), ) - # m.add( - # method=responses.POST, - # url=f"https://graph.facebook.com/{fb_api.version}/{object_id}/likes", - # json={"success": True}, - # ) + data = await fb_api.post.creat_like(object_id=object_id) assert data["success"] @@ -61,10 +52,6 @@ async def test_delete_like(fb_api): json={"success": True}, ), ) - # m.add( - # method=responses.DELETE, - # url=f"https://graph.facebook.com/{fb_api.version}/{object_id}/likes", - # json={"success": True}, - # ) + data = await fb_api.post.delete_like(object_id=object_id) assert data["success"] diff --git a/tests/facebook/test_album.py b/tests/facebook/test_album.py index 0f3fbc87..58118d5b 100644 --- a/tests/facebook/test_album.py +++ b/tests/facebook/test_album.py @@ -27,20 +27,6 @@ async def test_get_info(helpers, fb_api): ), ], ) - # m.add( - # method=responses.GET, - # url=f"https://graph.facebook.com/{fb_api.version}/{ab_id}", - # json=helpers.load_json( - # "testdata/facebook/apidata/albums/album_info_fields.json" - # ), - # ) - # m.add( - # method=responses.GET, - # url=f"https://graph.facebook.com/{fb_api.version}/{ab_id}", - # json=helpers.load_json( - # "testdata/facebook/apidata/albums/album_info_default.json" - # ), - # ) album = await fb_api.album.get_info(album_id=ab_id) assert album.id == ab_id @@ -75,20 +61,6 @@ async def test_get_batch(helpers, fb_api): ), ], ) - # m.add( - # method=responses.GET, - # url=f"https://graph.facebook.com/{fb_api.version}", - # json=helpers.load_json( - # "testdata/facebook/apidata/albums/albums_info_fields.json" - # ), - # ) - # m.add( - # method=responses.GET, - # url=f"https://graph.facebook.com/{fb_api.version}", - # json=helpers.load_json( - # "testdata/facebook/apidata/albums/albums_info_default.json" - # ), - # ) albums = await fb_api.album.get_batch(ids=ab_ids) assert albums[ab_ids[0]].id == ab_ids[0] diff --git a/tests/facebook/test_application.py b/tests/facebook/test_application.py index 22cae22e..09053f00 100644 --- a/tests/facebook/test_application.py +++ b/tests/facebook/test_application.py @@ -16,13 +16,6 @@ async def test_get_info(helpers, fb_api): ), ), ) - # m.add( - # method=responses.GET, - # url=f"https://graph.facebook.com/{fb_api.version}/{fb_api.app_id}", - # json=helpers.load_json( - # "testdata/facebook/apidata/applications/application_info.json" - # ), - # ) app = await fb_api.application.get_info() assert app.id == "123456789" @@ -44,13 +37,6 @@ async def test_get_accounts(helpers, fb_api): ), ), ) - # m.add( - # method=responses.GET, - # url=f"https://graph.facebook.com/{fb_api.version}/{fb_api.app_id}/accounts", - # json=helpers.load_json( - # "testdata/facebook/apidata/applications/application_accounts.json", - # ), - # ) accounts = await fb_api.application.get_accounts(count=None) assert len(accounts.data) == 4 diff --git a/tests/facebook/test_business.py b/tests/facebook/test_business.py index 3222a06f..7da3aa9f 100644 --- a/tests/facebook/test_business.py +++ b/tests/facebook/test_business.py @@ -19,13 +19,6 @@ async def test_get_info(helpers, fb_api): ), ), ) - # m.add( - # method=responses.GET, - # url=f"https://graph.facebook.com/{fb_api.version}/{b_id}", - # json=helpers.load_json( - # "testdata/facebook/apidata/business/business_info.json" - # ), - # ) business = await fb_api.business.get_info(business_id=b_id) assert business.id == b_id @@ -52,13 +45,6 @@ async def test_get_batch(helpers, fb_api): ), ), ) - # m.add( - # method=responses.GET, - # url=f"https://graph.facebook.com/{fb_api.version}", - # json=helpers.load_json( - # "testdata/facebook/apidata/business/businesses_info.json" - # ), - # ) businesses = await fb_api.business.get_batch(ids=b_ids) assert businesses[b_ids[0]].id == b_ids[0] diff --git a/tests/facebook/test_comment.py b/tests/facebook/test_comment.py index 16612435..e62553a3 100644 --- a/tests/facebook/test_comment.py +++ b/tests/facebook/test_comment.py @@ -18,13 +18,6 @@ async def test_get_info(helpers, fb_api): ), ), ) - # m.add( - # method=responses.GET, - # url=f"https://graph.facebook.com/{fb_api.version}/{cm_id}", - # json=helpers.load_json( - # "testdata/facebook/apidata/comments/comment_info.json" - # ), - # ) comment = await fb_api.comment.get_info(comment_id=cm_id) assert comment.id == cm_id @@ -54,13 +47,6 @@ async def test_get_batch(helpers, fb_api): ), ), ) - # m.add( - # method=responses.GET, - # url=f"https://graph.facebook.com/{fb_api.version}", - # json=helpers.load_json( - # "testdata/facebook/apidata/comments/comments_info.json" - # ), - # ) comments = await fb_api.comment.get_batch(ids=cm_ids) assert comments[cm_ids[0]].id == cm_ids[0] @@ -85,13 +71,7 @@ async def test_create_comment(helpers, fb_api): ), ), ) - # m.add( - # method=responses.POST, - # url=f"https://graph.facebook.com/{fb_api.version}/{object_id}/comments", - # json=helpers.load_json( - # "testdata/facebook/apidata/comments/create_resp.json" - # ), - # ) + comment = await fb_api.comment.create(object_id=object_id, message="message from api") assert comment.id @@ -130,13 +110,7 @@ async def test_update_comment(helpers, fb_api): ), ), ) - # m.add( - # method=responses.POST, - # url=f"https://graph.facebook.com/{fb_api.version}/{comment_id}", - # json=helpers.load_json( - # "testdata/facebook/apidata/comments/create_resp.json" - # ), - # ) + comment = await fb_api.comment.update( comment_id=comment_id, attachment_id="123245", @@ -169,11 +143,7 @@ async def test_update_comment(helpers, fb_api): json={"success": True}, ), ) - # m.add( - # method=responses.POST, - # url=f"https://graph.facebook.com/{fb_api.version}/{comment_id}", - # json={"success": True}, - # ) + comment = await fb_api.comment.update( comment_id=comment_id, message="message from api", @@ -198,10 +168,6 @@ async def test_delete_comment(fb_api): json={"success": True}, ), ) - # m.add( - # method=responses.DELETE, - # url=f"https://graph.facebook.com/{fb_api.version}/{comment_id}", - # json={"success": True}, - # ) + data = await fb_api.comment.delete(comment_id=comment_id) assert data["success"] diff --git a/tests/facebook/test_conversation.py b/tests/facebook/test_conversation.py index 18659631..007dfcf6 100644 --- a/tests/facebook/test_conversation.py +++ b/tests/facebook/test_conversation.py @@ -18,13 +18,6 @@ async def test_get_info(helpers, fb_api): ), ), ) - # m.add( - # method=responses.GET, - # url=f"https://graph.facebook.com/{fb_api.version}/{cvs_id}", - # json=helpers.load_json( - # "testdata/facebook/apidata/conversations/cvs_info.json" - # ), - # ) cvs = await fb_api.conversation.get_info(conversation_id=cvs_id) assert cvs.id == cvs_id @@ -51,13 +44,6 @@ async def test_get_batch(helpers, fb_api): ), ), ) - # m.add( - # method=responses.GET, - # url=f"https://graph.facebook.com/{fb_api.version}", - # json=helpers.load_json( - # "testdata/facebook/apidata/conversations/cvses_info.json" - # ), - # ) cvses = await fb_api.conversation.get_batch(ids=cvs_ids) assert cvses[cvs_ids[0]].id == cvs_ids[0] diff --git a/tests/facebook/test_event.py b/tests/facebook/test_event.py index fcd5aeb2..738ba82c 100644 --- a/tests/facebook/test_event.py +++ b/tests/facebook/test_event.py @@ -16,11 +16,6 @@ async def test_get_info(helpers, fb_api): json=helpers.load_json("testdata/facebook/apidata/events/event_info.json"), ), ) - # m.add( - # method=responses.GET, - # url=f"https://graph.facebook.com/{fb_api.version}/{evt_id}", - # json=helpers.load_json("testdata/facebook/apidata/events/event_info.json"), - # ) event = await fb_api.event.get_info(event_id=evt_id) assert event.id == evt_id @@ -45,11 +40,6 @@ async def test_get_batch(helpers, fb_api): json=helpers.load_json("testdata/facebook/apidata/events/events_info.json"), ), ) - # m.add( - # method=responses.GET, - # url=f"https://graph.facebook.com/{fb_api.version}", - # json=helpers.load_json("testdata/facebook/apidata/events/events_info.json"), - # ) events = await fb_api.event.get_batch(ids=et_ids) assert events[et_ids[0]].id == et_ids[0] diff --git a/tests/facebook/test_group.py b/tests/facebook/test_group.py index 5997108e..f79cd660 100644 --- a/tests/facebook/test_group.py +++ b/tests/facebook/test_group.py @@ -26,20 +26,6 @@ async def test_get_info(helpers, fb_api): ), ] ) - # m.add( - # method=responses.GET, - # url=f"https://graph.facebook.com/{fb_api.version}/{gp_id}", - # json=helpers.load_json( - # "testdata/facebook/apidata/groups/group_default_fields.json" - # ), - # ) - # m.add( - # method=responses.GET, - # url=f"https://graph.facebook.com/{fb_api.version}/{gp_id}", - # json=helpers.load_json( - # "testdata/facebook/apidata/groups/group_fields.json" - # ), - # ) gp = await fb_api.group.get_info(group_id=gp_id) assert gp.id == gp_id @@ -74,20 +60,6 @@ async def test_get_batch(helpers, fb_api): ), ] ) - # m.add( - # method=responses.GET, - # url=f"https://graph.facebook.com/{fb_api.version}", - # json=helpers.load_json( - # "testdata/facebook/apidata/groups/groups_default_fields.json" - # ), - # ) - # m.add( - # method=responses.GET, - # url=f"https://graph.facebook.com/{fb_api.version}", - # json=helpers.load_json( - # "testdata/facebook/apidata/groups/groups_fields.json" - # ), - # ) gps = await fb_api.group.get_batch(ids=gp_ids) assert gps[gp_ids[0]].id == gp_ids[0] @@ -117,16 +89,6 @@ async def test_get_feed(helpers, fb_api): ), ] ) - # m.add( - # method=responses.GET, - # url=f"https://graph.facebook.com/{fb_api.version}/{gp_id}/feed", - # json=helpers.load_json("testdata/facebook/apidata/groups/feed_p1.json"), - # ) - # m.add( - # method=responses.GET, - # url=f"https://graph.facebook.com/{fb_api.version}/{gp_id}/feed", - # json=helpers.load_json("testdata/facebook/apidata/groups/feed_p2.json"), - # ) feed = await fb_api.group.get_feed(object_id=gp_id) assert len(feed.data) == 10 diff --git a/tests/facebook/test_live_video.py b/tests/facebook/test_live_video.py index f8c54634..848288bf 100644 --- a/tests/facebook/test_live_video.py +++ b/tests/facebook/test_live_video.py @@ -26,20 +26,6 @@ async def test_get_info(helpers, fb_api): ), ] ) - # m.add( - # method=responses.GET, - # url=f"https://graph.facebook.com/{fb_api.version}/{lv_id}", - # json=helpers.load_json( - # "testdata/facebook/apidata/live_videos/live_video_fields.json" - # ), - # ) - # m.add( - # method=responses.GET, - # url=f"https://graph.facebook.com/{fb_api.version}/{lv_id}", - # json=helpers.load_json( - # "testdata/facebook/apidata/live_videos/live_video_default.json" - # ), - # ) live_video = await fb_api.live_video.get_info(live_video_id=lv_id) assert live_video.id == lv_id @@ -75,20 +61,6 @@ async def test_get_batch(helpers, fb_api): ), ] ) - # m.add( - # method=responses.GET, - # url=f"https://graph.facebook.com/{fb_api.version}", - # json=helpers.load_json( - # "testdata/facebook/apidata/live_videos/live_videos_fields.json" - # ), - # ) - # m.add( - # method=responses.GET, - # url=f"https://graph.facebook.com/{fb_api.version}", - # json=helpers.load_json( - # "testdata/facebook/apidata/live_videos/live_videos_default.json" - # ), - # ) live_videos = await fb_api.live_video.get_batch(ids=lv_ids) assert live_videos[lv_ids[0]].id == lv_ids[0] diff --git a/tests/facebook/test_message.py b/tests/facebook/test_message.py index c5224a37..ea615605 100644 --- a/tests/facebook/test_message.py +++ b/tests/facebook/test_message.py @@ -19,13 +19,6 @@ async def test_get_info(helpers, fb_api): ), ), ) - # m.add( - # method=responses.GET, - # url=f"https://graph.facebook.com/{fb_api.version}/{msg_id}", - # json=helpers.load_json( - # "testdata/facebook/apidata/messages/message_info.json" - # ), - # ) message = await fb_api.message.get_info(message_id=msg_id) assert message.id == msg_id @@ -55,13 +48,6 @@ async def test_get_batch(helpers, fb_api): ), ), ) - # m.add( - # method=responses.GET, - # url=f"https://graph.facebook.com/{fb_api.version}", - # json=helpers.load_json( - # "testdata/facebook/apidata/messages/messages_info.json" - # ), - # ) messages = await fb_api.message.get_batch(ids=msg_ids) assert messages[msg_ids[0]].id == msg_ids[0] diff --git a/tests/facebook/test_page.py b/tests/facebook/test_page.py index d1d6c85b..fd38b6c1 100644 --- a/tests/facebook/test_page.py +++ b/tests/facebook/test_page.py @@ -35,22 +35,6 @@ async def test_get_info(helpers, fb_api): ] ) - # m.add( - # method=responses.GET, - # url=f"https://graph.facebook.com/{fb_api.version}/{pid}", - # json=helpers.load_json( - # "testdata/facebook/apidata/pages/single_fields_page.json" - # ), - # ) - # - # m.add( - # method=responses.GET, - # url=f"https://graph.facebook.com/{fb_api.version}/facebookapp", - # json=helpers.load_json( - # "testdata/facebook/apidata/pages/single_default_page.json" - # ), - # ) - page = await fb_api.page.get_info(page_id=pid) assert page.id == pid @@ -84,18 +68,6 @@ async def test_get_batch(helpers, fb_api): )), ) ) - # m.add( - # method=responses.GET, - # url=f"https://graph.facebook.com/{fb_api.version}", - # json=helpers.load_json( - # "testdata/facebook/apidata/pages/multi_default_fields.json" - # ), - # ) - # m.add( - # method=responses.GET, - # url=f"https://graph.facebook.com/{fb_api.version}", - # json=helpers.load_json("testdata/facebook/apidata/pages/multi_pages.json"), - # ) data = await fb_api.page.get_batch(ids=page_ids) assert page_ids[0] in data.keys() @@ -127,20 +99,6 @@ async def test_get_feed(helpers, fb_api): )), ) ) - # m.add( - # method=responses.GET, - # url=f"https://graph.facebook.com/{fb_api.version}/{pid}/feed", - # json=helpers.load_json( - # "testdata/facebook/apidata/posts/feeds_default_fields_p1.json" - # ), - # ) - # m.add( - # method=responses.GET, - # url=f"https://graph.facebook.com/{fb_api.version}/{pid}/feed", - # json=helpers.load_json( - # "testdata/facebook/apidata/posts/feeds_default_fields_p2.json" - # ), - # ) feed = await fb_api.page.get_feed(object_id=pid, count=None, limit=5) assert len(feed.data) == 10 @@ -162,13 +120,6 @@ async def test_get_posts(helpers, fb_api): ), ] ) - # m.add( - # method=responses.GET, - # url=f"https://graph.facebook.com/{fb_api.version}/{pid}/posts", - # json=helpers.load_json( - # "testdata/facebook/apidata/posts/feeds_default_fields_p1.json" - # ), - # ) feed_json = await fb_api.page.get_posts( object_id=pid, count=4, limit=5, return_json=True @@ -192,13 +143,6 @@ async def test_get_published_posts(helpers, fb_api): ), ] ) - # m.add( - # method=responses.GET, - # url=f"https://graph.facebook.com/{fb_api.version}/{pid}/published_posts", - # json=helpers.load_json( - # "testdata/facebook/apidata/posts/feeds_default_fields_p1.json" - # ), - # ) feed = await fb_api.page.get_published_posts(object_id=pid, count=4) assert len(feed.data) == 4 @@ -220,13 +164,6 @@ async def test_get_tagged_posts(helpers, fb_api): ), ] ) - # m.add( - # method=responses.GET, - # url=f"https://graph.facebook.com/{fb_api.version}/{pid}/tagged", - # json=helpers.load_json( - # "testdata/facebook/apidata/posts/feeds_default_fields_p1.json" - # ), - # ) feed = await fb_api.page.get_tagged_posts(object_id=pid, count=4) assert len(feed.data) == 4 @@ -254,20 +191,6 @@ async def test_get_albums(helpers, fb_api): )), ) ) - # m.add( - # method=responses.GET, - # url=f"https://graph.facebook.com/{fb_api.version}/{pid}/albums", - # json=helpers.load_json( - # "testdata/facebook/apidata/albums/albums_list_p1.json" - # ), - # ) - # m.add( - # method=responses.GET, - # url=f"https://graph.facebook.com/{fb_api.version}/{pid}/albums", - # json=helpers.load_json( - # "testdata/facebook/apidata/albums/albums_list_p2.json" - # ), - # ) albums = await fb_api.page.get_albums(object_id=pid, count=None, limit=3) assert len(albums.data) == 6 @@ -296,16 +219,6 @@ async def test_get_photos(helpers, fb_api): )), ) ) - # m.add( - # method=responses.GET, - # url=f"https://graph.facebook.com/{fb_api.version}/{pid}/photos", - # json=f"https://graph.facebook.com/{fb_api.version}/{pid}/photos", - # ) - # m.add( - # method=responses.GET, - # url=f"https://graph.facebook.com/{fb_api.version}/{pid}/photos", - # json=helpers.load_json("testdata/facebook/apidata/photos/photos_p2.json"), - # ) photos = await fb_api.page.get_photos(object_id=pid, count=None, limit=2) assert len(photos.data) == 4 @@ -338,20 +251,6 @@ async def test_get_live_videos(helpers, fb_api): )), ) ) - # m.add( - # method=responses.GET, - # url=f"https://graph.facebook.com/{fb_api.version}/{pid}/live_videos", - # json=helpers.load_json( - # "testdata/facebook/apidata/live_videos/live_videos_p1.json" - # ), - # ) - # m.add( - # method=responses.GET, - # url=f"https://graph.facebook.com/{fb_api.version}/{pid}/live_videos", - # json=helpers.load_json( - # "testdata/facebook/apidata/live_videos/live_videos_p2.json" - # ), - # ) live_videos = await fb_api.page.get_live_videos(object_id=pid, count=None, limit=3) assert len(live_videos.data) == 6 @@ -380,16 +279,6 @@ async def test_get_videos(helpers, fb_api): )), ) ) - # m.add( - # method=responses.GET, - # url=f"https://graph.facebook.com/{fb_api.version}/{pid}/videos", - # json=helpers.load_json("testdata/facebook/apidata/videos/videos_p1.json"), - # ) - # m.add( - # method=responses.GET, - # url=f"https://graph.facebook.com/{fb_api.version}/{pid}/videos", - # json=helpers.load_json("testdata/facebook/apidata/videos/videos_p2.json"), - # ) videos = await fb_api.page.get_videos(object_id=pid, count=None, limit=3) assert len(videos.data) == 5 @@ -412,11 +301,6 @@ async def test_search_pages(helpers, fb_api): )), ) ) - # m.add( - # method=responses.GET, - # url=f"https://graph.facebook.com/{fb_api.version}/pages/search", - # json=helpers.load_json("testdata/facebook/apidata/pages/search_pages.json"), - # ) pages = await fb_api.page.search(q="facebook", count=5, limit=5) assert len(pages.data) == 5 diff --git a/tests/facebook/test_photo.py b/tests/facebook/test_photo.py index 9f3d4f48..d9da7266 100644 --- a/tests/facebook/test_photo.py +++ b/tests/facebook/test_photo.py @@ -27,20 +27,6 @@ async def test_get_info(helpers, fb_api): ), ] ) - # m.add( - # method=responses.GET, - # url=f"https://graph.facebook.com/{fb_api.version}/{photo_id}", - # json=helpers.load_json( - # "testdata/facebook/apidata/photos/photo_info_fields.json" - # ), - # ) - # m.add( - # method=responses.GET, - # url=f"https://graph.facebook.com/{fb_api.version}/{photo_id}", - # json=helpers.load_json( - # "testdata/facebook/apidata/photos/photo_info_default.json" - # ), - # ) photo = await fb_api.photo.get_info(photo_id=photo_id) assert photo.id == photo_id @@ -74,20 +60,6 @@ async def test_get_batch(helpers, fb_api): ), ] ) - # m.add( - # method=responses.GET, - # url=f"https://graph.facebook.com/{fb_api.version}", - # json=helpers.load_json( - # "testdata/facebook/apidata/photos/photos_info_fields.json" - # ), - # ) - # m.add( - # method=responses.GET, - # url=f"https://graph.facebook.com/{fb_api.version}", - # json=helpers.load_json( - # "testdata/facebook/apidata/photos/photos_info_default.json" - # ), - # ) data = await fb_api.photo.get_batch(ids=ids) assert ids[0] in data.keys() diff --git a/tests/facebook/test_post.py b/tests/facebook/test_post.py index 19b58964..934eebcb 100644 --- a/tests/facebook/test_post.py +++ b/tests/facebook/test_post.py @@ -59,20 +59,6 @@ async def test_batch_posts(helpers, fb_api): ), ] ) - # m.add( - # method=responses.GET, - # url=f"https://graph.facebook.com/{fb_api.version}", - # json=helpers.load_json( - # "testdata/facebook/apidata/posts/multi_posts_data.json" - # ), - # ) - # m.add( - # method=responses.GET, - # url=f"https://graph.facebook.com/{fb_api.version}", - # json=helpers.load_json( - # "testdata/facebook/apidata/posts/multi_posts_data_fields.json" - # ), - # ) data = await fb_api.post.get_batch(ids=ids) assert ids[0] in data.keys() @@ -104,20 +90,6 @@ async def test_get_comments(helpers, fb_api): )), ) ) - # m.add( - # method=responses.GET, - # url=f"https://graph.facebook.com/{fb_api.version}/{post_id}/comments", - # json=helpers.load_json( - # "testdata/facebook/apidata/comments/comments_p1.json" - # ), - # ) - # m.add( - # method=responses.GET, - # url=f"https://graph.facebook.com/{fb_api.version}/{post_id}/comments", - # json=helpers.load_json( - # "testdata/facebook/apidata/comments/comments_p2.json" - # ), - # ) comments = await fb_api.post.get_comments( object_id=post_id, diff --git a/tests/facebook/test_video.py b/tests/facebook/test_video.py index b23fdfc2..9cf4aa75 100644 --- a/tests/facebook/test_video.py +++ b/tests/facebook/test_video.py @@ -28,20 +28,6 @@ async def test_get_info(helpers, fb_api): )), ) ) - # m.add( - # method=responses.GET, - # url=f"https://graph.facebook.com/{fb_api.version}/{video_id}", - # json=helpers.load_json( - # "testdata/facebook/apidata/videos/video_info_fields.json" - # ), - # ) - # m.add( - # method=responses.GET, - # url=f"https://graph.facebook.com/{fb_api.version}/{video_id}", - # json=helpers.load_json( - # "testdata/facebook/apidata/videos/video_info_default.json" - # ), - # ) video = await fb_api.video.get_info(video_id=video_id) assert video.id == video_id @@ -76,20 +62,6 @@ async def test_get_batch(helpers, fb_api): )), ) ) - # m.add( - # method=responses.GET, - # url=f"https://graph.facebook.com/{fb_api.version}", - # json=helpers.load_json( - # "testdata/facebook/apidata/videos/videos_info_fields.json" - # ), - # ) - # m.add( - # method=responses.GET, - # url=f"https://graph.facebook.com/{fb_api.version}", - # json=helpers.load_json( - # "testdata/facebook/apidata/videos/videos_info_default.json" - # ), - # ) videos = await fb_api.video.get_batch(ids=video_ids) assert videos[video_ids[0]].id == video_ids[0] diff --git a/tests/instagram_basic/test_media.py b/tests/instagram_basic/test_media.py index 12ac5fc6..d53ad7a1 100644 --- a/tests/instagram_basic/test_media.py +++ b/tests/instagram_basic/test_media.py @@ -17,13 +17,6 @@ async def test_get_info(helpers, api): ), ) ) - # m.add( - # method=responses.GET, - # url=f"https://graph.instagram.com/{api.version}/{media_id}", - # json=helpers.load_json( - # "testdata/instagram_basic/apidata/media/media_info.json" - # ), - # ) media = await api.media.get_info(media_id=media_id) assert media.id == media_id @@ -48,13 +41,6 @@ async def test_get_batch(helpers, api): ), ) ) - # m.add( - # method=responses.GET, - # url=f"https://graph.instagram.com/{api.version}", - # json=helpers.load_json( - # "testdata/instagram_basic/apidata/media/medias_info.json" - # ), - # ) medias = await api.media.get_batch(ids=ids) assert medias[ids[0]].id == ids[0] @@ -80,13 +66,6 @@ async def test_get_children(helpers, api): ), ) ) - # m.add( - # method=responses.GET, - # url=f"https://graph.instagram.com/{api.version}/{media_id}/children", - # json=helpers.load_json( - # "testdata/instagram_basic/apidata/media/media_children.json" - # ), - # ) children = await api.media.get_children(media_id=media_id) assert len(children.data) == 2 diff --git a/tests/instagram_basic/test_user.py b/tests/instagram_basic/test_user.py index 041b4517..693dc962 100644 --- a/tests/instagram_basic/test_user.py +++ b/tests/instagram_basic/test_user.py @@ -19,13 +19,6 @@ async def test_get_info(helpers, api): ), ) ) - # m.add( - # method=responses.GET, - # url=f"https://graph.instagram.com/{api.version}/me", - # json=helpers.load_json( - # "testdata/instagram_basic/apidata/user/user_info.json" - # ), - # ) user = await api.user.get_info() assert user.id == uid @@ -58,20 +51,6 @@ async def test_user_media(helpers, api): )), ) ) - # m.add( - # method=responses.GET, - # url=f"https://graph.instagram.com/{api.version}/{uid}/media", - # json=helpers.load_json( - # "testdata/instagram_basic/apidata/user/user_medias_p1.json" - # ), - # ) - # m.add( - # method=responses.GET, - # url=f"https://graph.instagram.com/{api.version}/{uid}/media", - # json=helpers.load_json( - # "testdata/instagram_basic/apidata/user/user_medias_p2.json" - # ), - # ) media = await api.user.get_media( user_id=uid, diff --git a/tests/instagram_business/test_comment.py b/tests/instagram_business/test_comment.py index df8f19e8..500c3c52 100644 --- a/tests/instagram_business/test_comment.py +++ b/tests/instagram_business/test_comment.py @@ -18,13 +18,6 @@ async def test_comment_get_info(helpers, api): ), ) ) - # m.add( - # method=responses.GET, - # url=f"https://graph.facebook.com/{api.version}/{comment_id}", - # json=helpers.load_json( - # "testdata/instagram/apidata/comments/comment_fields.json" - # ), - # ) comment = await api.comment.get_info(comment_id=comment_id) assert comment.id == comment_id @@ -50,13 +43,6 @@ async def test_comment_get_batch(helpers, api): ), ) ) - # m.add( - # method=responses.GET, - # url=f"https://graph.facebook.com/{api.version}", - # json=helpers.load_json( - # "testdata/instagram/apidata/comments/comments_fields.json" - # ), - # ) comments = await api.comment.get_batch(ids=comment_ids) assert comments[comment_ids[0]].id == comment_ids[0] @@ -80,11 +66,6 @@ async def test_comment_get_replies(helpers, api): json=helpers.load_json("testdata/instagram/apidata/comments/replies.json"), ) ) - # m.add( - # method=responses.GET, - # url=f"https://graph.facebook.com/{api.version}/{comment_id}/replies", - # json=helpers.load_json("testdata/instagram/apidata/comments/replies.json"), - # ) replies = await api.comment.get_replies(comment_id=comment_id) assert len(replies.data) == 1 @@ -106,13 +87,6 @@ async def test_reply_get_info(helpers, api): ), ) ) - # m.add( - # method=responses.GET, - # url=f"https://graph.facebook.com/{api.version}/{reply_id}", - # json=helpers.load_json( - # "testdata/instagram/apidata/comments/comment_fields.json" - # ), - # ) reply = await api.reply.get_info(reply_id=reply_id) assert reply.id == reply_id @@ -138,13 +112,6 @@ async def test_reply_get_batch(helpers, api): ), ) ) - # m.add( - # method=responses.GET, - # url=f"https://graph.facebook.com/{api.version}", - # json=helpers.load_json( - # "testdata/instagram/apidata/comments/comments_fields.json" - # ), - # ) replies = await api.reply.get_batch(ids=reply_ids) assert replies[reply_ids[0]].id == reply_ids[0] diff --git a/tests/instagram_business/test_hashtag.py b/tests/instagram_business/test_hashtag.py index 2bb66b02..30670225 100644 --- a/tests/instagram_business/test_hashtag.py +++ b/tests/instagram_business/test_hashtag.py @@ -20,13 +20,6 @@ async def test_get_info(helpers, api): ), ) ) - # m.add( - # method=responses.GET, - # url=f"https://graph.facebook.com/{api.version}/{hashtag_id}", - # json=helpers.load_json( - # "testdata/instagram/apidata/hashtags/hashtag_info.json" - # ), - # ) hashtag = await api.hashtag.get_info(hashtag_id=hashtag_id) assert hashtag.id == hashtag_id @@ -52,13 +45,6 @@ async def test_get_batch(helpers, api): ), ) ) - # m.add( - # method=responses.GET, - # url=f"https://graph.facebook.com/{api.version}", - # json=helpers.load_json( - # "testdata/instagram/apidata/hashtags/hashtags_info.json" - # ), - # ) hashtags = await api.hashtag.get_batch(ids=hashtag_ids) assert hashtags[hashtag_ids[0]].id == hashtag_ids[0] @@ -92,20 +78,6 @@ async def test_get_top_media(helpers, api): )), ) ) - # m.add( - # method=responses.GET, - # url=f"https://graph.facebook.com/{api.version}/{hashtag_id}/top_media", - # json=helpers.load_json( - # "testdata/instagram/apidata/hashtags/hashtag_top_medias_p1.json" - # ), - # ) - # m.add( - # method=responses.GET, - # url=f"https://graph.facebook.com/{api.version}/{hashtag_id}/top_media", - # json=helpers.load_json( - # "testdata/instagram/apidata/hashtags/hashtag_top_medias_p2.json" - # ), - # ) top_media = await api.hashtag.get_top_media( hashtag_id=hashtag_id, @@ -143,20 +115,6 @@ async def test_get_recent_media(helpers, api): )), ) ) - # m.add( - # method=responses.GET, - # url=f"https://graph.facebook.com/{api.version}/{hashtag_id}/recent_media", - # json=helpers.load_json( - # "testdata/instagram/apidata/hashtags/hashtag_recent_medias_p1.json" - # ), - # ) - # m.add( - # method=responses.GET, - # url=f"https://graph.facebook.com/{api.version}/{hashtag_id}/recent_media", - # json=helpers.load_json( - # "testdata/instagram/apidata/hashtags/hashtag_recent_medias_p2.json" - # ), - # ) top_media = await api.hashtag.get_recent_media( hashtag_id=hashtag_id, diff --git a/tests/instagram_business/test_media.py b/tests/instagram_business/test_media.py index 9de63d44..2ba07a2a 100644 --- a/tests/instagram_business/test_media.py +++ b/tests/instagram_business/test_media.py @@ -20,13 +20,6 @@ async def test_get_info(helpers, api): ), ) ) - # m.add( - # method=responses.GET, - # url=f"https://graph.facebook.com/{api.version}/{media_id}", - # json=helpers.load_json( - # "testdata/instagram/apidata/medias/media_default.json" - # ), - # ) media = await api.media.get_info(media_id=media_id) assert media.id == media_id @@ -52,13 +45,6 @@ async def test_get_batch(helpers, api): ), ) ) - # m.add( - # method=responses.GET, - # url=f"https://graph.facebook.com/{api.version}", - # json=helpers.load_json( - # "testdata/instagram/apidata/medias/medias_fields.json" - # ), - # ) medias = await api.media.get_batch(ids=media_ids) assert medias[media_ids[0]].id == media_ids[0] @@ -91,20 +77,6 @@ async def test_get_comments(helpers, api): )), ) ) - # m.add( - # method=responses.GET, - # url=f"https://graph.facebook.com/{api.version}/{media_id}/comments", - # json=helpers.load_json( - # "testdata/instagram/apidata/medias/comments_p1.json" - # ), - # ) - # m.add( - # method=responses.GET, - # url=f"https://graph.facebook.com/{api.version}/{media_id}/comments", - # json=helpers.load_json( - # "testdata/instagram/apidata/medias/comments_p2.json" - # ), - # ) comments = await api.media.get_comments(media_id=media_id) assert len(comments.data) == 4 @@ -127,11 +99,6 @@ async def test_get_children(helpers, api): json=helpers.load_json("testdata/instagram/apidata/medias/children.json") ) ) - # m.add( - # method=responses.GET, - # url=f"https://graph.facebook.com/{api.version}/{media_id}/children", - # json=helpers.load_json("testdata/instagram/apidata/medias/children.json"), - # ) children = await api.media.get_children(media_id=media_id) assert len(children.data) == 2 @@ -154,11 +121,6 @@ async def test_get_insights(helpers, api): json=helpers.load_json("testdata/instagram/apidata/medias/insights.json"), ) ) - # m.add( - # method=responses.GET, - # url=f"https://graph.facebook.com/{api.version}/{media_id}/insights", - # json=helpers.load_json("testdata/instagram/apidata/medias/insights.json"), - # ) insights = await api.media.get_insights( media_id=media_id, @@ -183,13 +145,6 @@ async def test_get_insights(helpers, api): ), ) ) - # m.add( - # method=responses.GET, - # url=f"https://graph.facebook.com/{api.version}/{media_id}/insights", - # json=helpers.load_json( - # "testdata/instagram/apidata/medias/insights_new.json" - # ), - # ) insights = await api.media.get_insights( media_id=media_id, metric="profile_activity", breakdown=["action_type"] @@ -209,13 +164,6 @@ async def test_get_product_tags(helpers, api): ), ) ) - # m.add( - # method=responses.GET, - # url=f"https://graph.facebook.com/{api.version}/{media_id}/product_tags", - # json=helpers.load_json( - # "testdata/instagram/apidata/medias/product_tags.json" - # ), - # ) tags = await api.media.get_product_tags(media_id=media_id) assert len(tags.data) == 1 diff --git a/tests/instagram_business/test_publish.py b/tests/instagram_business/test_publish.py index b8db124d..89514b3e 100644 --- a/tests/instagram_business/test_publish.py +++ b/tests/instagram_business/test_publish.py @@ -18,13 +18,6 @@ async def test_get_info(helpers, api): ), ) ) - # m.add( - # method=responses.GET, - # url=f"https://graph.facebook.com/{api.version}/{container_id}", - # json=helpers.load_json( - # "testdata/instagram/apidata/publish/container_info.json" - # ), - # ) container = await api.container.get_info(container_id=container_id) assert container.id == container_id @@ -50,13 +43,6 @@ async def test_get_batch(helpers, api): ), ) ) - # m.add( - # method=responses.GET, - # url=f"https://graph.facebook.com/{api.version}", - # json=helpers.load_json( - # "testdata/instagram/apidata/publish/containers_info.json" - # ), - # ) containers = await api.container.get_batch(ids=container_ids) assert containers[container_ids[0]].id == container_ids[0] diff --git a/tests/instagram_business/test_user.py b/tests/instagram_business/test_user.py index 7eaf05c1..8f66ad8c 100644 --- a/tests/instagram_business/test_user.py +++ b/tests/instagram_business/test_user.py @@ -16,11 +16,6 @@ async def test_get_info(helpers, api): json=helpers.load_json("testdata/instagram/apidata/users/user_fields.json"), ) ) - # m.add( - # method=responses.GET, - # url=f"https://graph.facebook.com/{api.version}/{api.instagram_business_id}", - # json=helpers.load_json("testdata/instagram/apidata/users/user_fields.json"), - # ) user = await api.user.get_info() assert user.id == api.instagram_business_id @@ -45,13 +40,6 @@ async def test_discovery_user(helpers, api): ), ) ) - # m.add( - # method=responses.GET, - # url=f"https://graph.facebook.com/{api.version}/{api.instagram_business_id}", - # json=helpers.load_json( - # "testdata/instagram/apidata/discovery/medias_p1.json" - # ), - # ) user = await api.user.discovery_user(username=username) assert user.business_discovery.id == "17841407673135339" @@ -84,20 +72,6 @@ async def test_discovery_media(helpers, api): ), ] ) - # m.add( - # method=responses.GET, - # url=f"https://graph.facebook.com/{api.version}/{api.instagram_business_id}", - # json=helpers.load_json( - # "testdata/instagram/apidata/discovery/medias_p1.json" - # ), - # ) - # m.add( - # method=responses.GET, - # url=f"https://graph.facebook.com/{api.version}/{api.instagram_business_id}", - # json=helpers.load_json( - # "testdata/instagram/apidata/discovery/medias_p2.json" - # ), - # ) media_p1 = await api.user.discovery_user_medias(username=username, limit=2) media_p2 = await api.user.discovery_user_medias( @@ -117,13 +91,6 @@ async def test_discovery_media(helpers, api): ), ) ) - # m.add( - # method=responses.GET, - # url=f"https://graph.facebook.com/{api.version}/{api.instagram_business_id}", - # json=helpers.load_json( - # "testdata/instagram/apidata/discovery/medias_p2.json" - # ), - # ) media = await api.user.discovery_user_medias( username=username, limit=2, before="before", return_json=True @@ -142,13 +109,6 @@ async def test_get_content_publishing_limit(helpers, api): ), ) ) - # m.add( - # method=responses.GET, - # url=f"https://graph.facebook.com/{api.version}/{api.instagram_business_id}/content_publishing_limit", - # json=helpers.load_json( - # "testdata/instagram/apidata/users/content_publish_limit.json" - # ), - # ) limit = await api.user.get_content_publishing_limit() assert limit.data[0].config.quota_total == 25 @@ -168,13 +128,6 @@ async def test_get_user_insights(helpers, api): ), ) ) - # m.add( - # method=responses.GET, - # url=f"https://graph.facebook.com/{api.version}/{api.instagram_business_id}/insights", - # json=helpers.load_json( - # "testdata/instagram/apidata/users/user_insights.json" - # ), - # ) insights = await api.user.get_insights( metric="impressions,reach,profile_views", @@ -199,13 +152,7 @@ async def test_get_user_insights(helpers, api): ), ) ) - # m.add( - # method=responses.GET, - # url=f"https://graph.facebook.com/{api.version}/{api.instagram_business_id}/insights", - # json=helpers.load_json( - # "testdata/instagram/apidata/users/user_insights_new.json" - # ), - # ) + insights = await api.user.get_insights( metric="reach", period="day", @@ -232,17 +179,6 @@ async def test_get_user_media(helpers, api): )), ) ) - # m.add( - # method=responses.GET, - # url=f"https://graph.facebook.com/{api.version}/{api.instagram_business_id}/media", - # json=helpers.load_json("testdata/instagram/apidata/users/medias_p1.json"), - # ) - # m.add( - # method=responses.GET, - # url=f"https://graph.facebook.com/{api.version}/{api.instagram_business_id}/media", - # json=helpers.load_json("testdata/instagram/apidata/users/medias_p2.json"), - # ) - medias = await api.user.get_media(count=3, limit=2) assert len(medias.data) == 3 assert medias.data[0].id == "17895731045244887" @@ -266,13 +202,6 @@ async def test_get_mentioned_comment(helpers, api): ), ) ) - # m.add( - # method=responses.GET, - # url=f"https://graph.facebook.com/{api.version}/{api.instagram_business_id}", - # json=helpers.load_json( - # "testdata/instagram/apidata/users/mentioned_comment.json" - # ), - # ) comment = await api.user.get_mentioned_comment(comment_id=cm_id) assert comment.mentioned_comment.id == cm_id @@ -295,13 +224,6 @@ async def test_get_mentioned_media(helpers, api): ), ) ) - # m.add( - # method=responses.GET, - # url=f"https://graph.facebook.com/{api.version}/{api.instagram_business_id}", - # json=helpers.load_json( - # "testdata/instagram/apidata/users/mentioned_media.json" - # ), - # ) media = await api.user.get_mentioned_media(media_id=media_id) assert media.mentioned_media.id == media_id @@ -321,13 +243,6 @@ async def test_get_hashtag_search(helpers, api): ), ) ) - # m.add( - # method=responses.GET, - # url=f"https://graph.facebook.com/{api.version}/ig_hashtag_search", - # json=helpers.load_json( - # "testdata/instagram/apidata/users/hashtag_search.json" - # ), - # ) hashtag = await api.user.get_hashtag_search(q="developers") assert hashtag.data[0].id == "17841562426109234" @@ -347,13 +262,6 @@ async def test_get_recently_searched_hashtags(helpers, api): ), ) ) - # m.add( - # method=responses.GET, - # url=f"https://graph.facebook.com/{api.version}/{api.instagram_business_id}/recently_searched_hashtags", - # json=helpers.load_json( - # "testdata/instagram/apidata/users/recently_searched_hashtags.json" - # ), - # ) hashtags = await api.user.get_recently_searched_hashtags() assert len(hashtags.data) == 2 @@ -372,11 +280,6 @@ async def test_get_stories(helpers, api): json=helpers.load_json("testdata/instagram/apidata/users/stories.json"), ) ) - # m.add( - # method=responses.GET, - # url=f"https://graph.facebook.com/{api.version}/{api.instagram_business_id}/stories", - # json=helpers.load_json("testdata/instagram/apidata/users/stories.json"), - # ) stories = await api.user.get_stories() assert len(stories.data) == 2 @@ -397,13 +300,6 @@ async def test_get_tagged_media(helpers, api): ), ) ) - # m.add( - # method=responses.GET, - # url=f"https://graph.facebook.com/{api.version}/{api.instagram_business_id}/tags", - # json=helpers.load_json( - # "testdata/instagram/apidata/users/tagged_medias.json" - # ), - # ) medias = await api.user.get_tagged_media() assert len(medias.data) == 5 @@ -422,11 +318,6 @@ async def test_get_live_media(helpers, api): json=helpers.load_json("testdata/instagram/apidata/users/live_medias.json"), ) ) - # m.add( - # method=responses.GET, - # url=f"https://graph.facebook.com/{api.version}/{api.instagram_business_id}/live_media", - # json=helpers.load_json("testdata/instagram/apidata/users/live_medias.json"), - # ) medias = await api.user.get_live_media() assert len(medias.data) == 1 @@ -447,13 +338,6 @@ async def test_get_available_catalogs(helpers, api): ), ) ) - # m.add( - # method=responses.GET, - # url=f"https://graph.facebook.com/{api.version}/{api.instagram_business_id}/available_catalogs", - # json=helpers.load_json( - # "testdata/instagram/apidata/users/available_catalogs.json" - # ), - # ) catalogs = await api.user.get_available_catalogs() assert len(catalogs.data) == 1 @@ -475,13 +359,6 @@ async def test_get_catalog_product_search(helpers, api): ), ) ) - # m.add( - # method=responses.GET, - # url=f"https://graph.facebook.com/{api.version}/{api.instagram_business_id}/catalog_product_search", - # json=helpers.load_json( - # "testdata/instagram/apidata/users/catalog_product_search.json" - # ), - # ) products = await api.user.get_catalog_product_search(catalog_id=catalog_id) assert len(products.data) == 1 @@ -505,13 +382,6 @@ async def test_get_product_appeal(helpers, api): ), ) ) - # m.add( - # method=responses.GET, - # url=f"https://graph.facebook.com/{api.version}/{api.instagram_business_id}/product_appeal", - # json=helpers.load_json( - # "testdata/instagram/apidata/users/product_appeal.json" - # ), - # ) appeals = await api.user.get_product_appeal(product_id=product_id) assert len(appeals.data) == 1 diff --git a/tests/test_basic_display_api.py b/tests/test_basic_display_api.py index 3dc33ae0..ae04aeb1 100644 --- a/tests/test_basic_display_api.py +++ b/tests/test_basic_display_api.py @@ -26,11 +26,6 @@ async def test_oath_flow(helpers): json=helpers.load_json("testdata/base/basic_display_api_user_token.json"), ) ) - # m.add( - # method=responses.POST, - # url=api.EXCHANGE_ACCESS_TOKEN_URL, - # json=helpers.load_json("testdata/base/basic_display_api_user_token.json"), - # ) r = await api.exchange_user_access_token(response=resp) assert r["access_token"] == "token" @@ -48,11 +43,6 @@ async def test_exchange_long_lived_token(helpers): json=helpers.load_json("testdata/base/long_term_token.json"), ) ) - # m.add( - # method=responses.GET, - # url=f"https://graph.instagram.com/access_token", - # json=helpers.load_json("testdata/base/long_term_token.json"), - # ) res = await api.exchange_long_lived_user_access_token() assert res["access_token"] == "token" @@ -70,11 +60,6 @@ async def test_refresh_token(helpers): json=helpers.load_json("testdata/base/long_term_token.json"), ) ) - # m.add( - # method=responses.GET, - # url=f"https://graph.instagram.com/refresh_access_token", - # json=helpers.load_json("testdata/base/long_term_token.json"), - # ) res = await api.refresh_access_token(access_token=api.access_token) assert res["access_token"] == "token" diff --git a/tests/test_graph.py b/tests/test_graph.py index be26149b..323cfe7f 100644 --- a/tests/test_graph.py +++ b/tests/test_graph.py @@ -3,7 +3,6 @@ """ import httpx import pytest -import requests import respx from pyfacebook import GraphAPI, LibraryError, FacebookError @@ -175,11 +174,6 @@ async def test_get_full_connections(helpers): json=helpers.load_json("testdata/base/full_connecions_p1.json") ) ) - # m.add( - # method=responses.GET, - # url="https://graph.facebook.com/v11.0/19292868552/feed", - # json=helpers.load_json("testdata/base/full_connecions_p1.json"), - # ) feed = await api.get_full_connections( object_id=obj_id, @@ -205,17 +199,6 @@ async def test_get_full_connections(helpers): ] ) - # m.add( - # method=responses.GET, - # url="https://graph.facebook.com/v11.0/19292868552/feed", - # json=helpers.load_json("testdata/base/full_connecions_p1.json"), - # ) - # m.add( - # method=responses.GET, - # url="https://graph.facebook.com/v11.0/19292868552/feed", - # json=helpers.load_json("testdata/base/full_connecions_p2.json"), - # ) - feed = await api.get_full_connections( object_id=obj_id, connection="feed", @@ -239,11 +222,6 @@ async def test_discovery_user_media(helpers): json=helpers.load_json("testdata/base/discovery_medias_p1.json") ) ) - # m.add( - # method=responses.GET, - # url=f"https://graph.facebook.com/{api.version}/id", - # json=helpers.load_json("testdata/base/discovery_medias_p1.json"), - # ) media = await api.discovery_user_media( username=username, @@ -267,17 +245,6 @@ async def test_discovery_user_media(helpers): ), ] ) - # m.add( - # method=responses.GET, - # url=f"https://graph.facebook.com/{api.version}/id", - # json=helpers.load_json("testdata/base/discovery_medias_p1.json"), - # ) - - # m.add( - # method=responses.GET, - # url=f"https://graph.facebook.com/{api.version}/id", - # json=helpers.load_json("testdata/base/discovery_medias_p2.json"), - # ) media = await api.discovery_user_media( username=username, @@ -298,11 +265,6 @@ async def test_post_object(helpers, user_api): json={"success": True}, ) ) - # m.add( - # method="POST", - # url=f"https://graph.facebook.com/{user_api.version}/{obj_id}/comments", - # json={"success": True}, - # ) res = await user_api.post_object( object_id=obj_id, @@ -322,11 +284,6 @@ async def test_delete_object(helpers, user_api): json={"success": True}, ) ) - # m.add( - # method="DELETE", - # url=f"https://graph.facebook.com/{user_api.version}/{obj_id}", - # json={"success": True}, - # ) res = await user_api.delete_object( object_id=obj_id, @@ -355,11 +312,6 @@ async def test_oauth_flow(helpers): json=helpers.load_json("testdata/base/long_term_token.json"), ) ) - # m.add( - # method=responses.POST, - # url=api.EXCHANGE_ACCESS_TOKEN_URL, - # json=helpers.load_json("testdata/base/long_term_token.json"), - # ) r = await api.exchange_user_access_token(response=resp) assert r["access_token"] == "token" @@ -379,11 +331,6 @@ async def test_exchange_token(helpers): json={"id": "19292868552", "access_token": "token"}, ) ) - # m.add( - # method=responses.GET, - # url=f"https://graph.facebook.com/{api.version}/{page_id}", - # json={"id": "19292868552", "access_token": "token"}, - # ) token = await api.exchange_page_access_token(page_id=page_id) assert token == "token" # test can not exchange page access token @@ -394,11 +341,6 @@ async def test_exchange_token(helpers): json={"id": "19292868552"}, ) ) - # m.add( - # method=responses.GET, - # url=f"https://graph.facebook.com/{api.version}/{page_id}", - # json={"id": "19292868552"}, - # ) with pytest.raises(LibraryError): await api.exchange_page_access_token(page_id=page_id) @@ -414,16 +356,6 @@ async def test_exchange_token(helpers): }, ) ) - # m.add( - # method=responses.GET, - # url=f"https://graph.facebook.com/oauth/access_token", - # json={ - # "access_token": "token", - # "token_type": "bearer", - # "expires_in": 5184000, - # }, - # ) - res = await api.exchange_long_lived_user_access_token() assert res["access_token"] == "token" @@ -436,11 +368,6 @@ async def test_exchange_token(helpers): json=helpers.load_json("testdata/base/user_accounts_token.json"), ) ) - # m.add( - # method=responses.GET, - # url=f"https://graph.facebook.com/{api.version}/{user_id}/accounts", - # json=helpers.load_json("testdata/base/user_accounts_token.json"), - # ) res = await api.exchange_long_lived_page_access_token(user_id=user_id) assert len(res["data"]) == 1 @@ -458,11 +385,6 @@ async def test_get_app_token(): json={"access_token": "access_token", "token_type": "bearer"}, ) ) - # m.add( - # method=responses.GET, - # url=f"https://graph.facebook.com/oauth/access_token", - # json={"access_token": "access_token", "token_type": "bearer"}, - # ) data = await api.get_app_token() assert data["access_token"] == "access_token" @@ -479,11 +401,6 @@ async def test_debug_token(helpers, pubg_api): json=helpers.load_json("testdata/base/token_info.json"), ) ) - # m.add( - # method=responses.GET, - # url=f"https://graph.facebook.com/{pubg_api.version}/debug_token", - # json=helpers.load_json("testdata/base/token_info.json"), - # ) res = await pubg_api.debug_token(input_token=input_token) assert res["data"]["type"] == "USER" diff --git a/tests/test_server_sent_events.py b/tests/test_server_sent_events.py index f37787c1..5ef20907 100644 --- a/tests/test_server_sent_events.py +++ b/tests/test_server_sent_events.py @@ -5,8 +5,6 @@ import random from unittest.mock import patch -import pytest -import requests import responses from responses import matchers diff --git a/tests/test_threads_graph_api.py b/tests/test_threads_graph_api.py index 9554ed42..7170acec 100644 --- a/tests/test_threads_graph_api.py +++ b/tests/test_threads_graph_api.py @@ -31,11 +31,6 @@ async def test_threads_exchange_user_access_token(helpers): json=helpers.load_json("testdata/base/threads_user_token.json"), ) ) - # m.add( - # method=responses.POST, - # url=api.EXCHANGE_ACCESS_TOKEN_URL, - # json=helpers.load_json("testdata/base/threads_user_token.json"), - # ) r = await api.exchange_user_access_token(response=resp, scope=["threads_basic"]) assert r["access_token"] == "THQVJ..." @@ -51,11 +46,6 @@ async def test_threads_exchange_long_lived_user_access_token(helpers): json=helpers.load_json("testdata/base/threads_user_long_lived_token.json"), ) ) - # m.add( - # method=responses.GET, - # url=f"https://graph.threads.net/oauth/access_token", - # json=helpers.load_json("testdata/base/threads_user_long_lived_token.json"), - # ) r = await api.exchange_long_lived_user_access_token() assert r["access_token"] == "THQVJ..." @@ -71,11 +61,6 @@ async def test_threads_refresh_access_token(helpers): json=helpers.load_json("testdata/base/threads_user_long_lived_token.json"), ) ) - # m.add( - # method=responses.GET, - # url=f"https://graph.threads.net/refresh_access_token", - # json=helpers.load_json("testdata/base/threads_user_long_lived_token.json"), - # ) r = await api.refresh_access_token(access_token=api.access_token) assert r["access_token"] == "THQVJ..."