Refactor threads file structure
This commit is contained in:
48
tests/threads/test_group.py
Normal file
48
tests/threads/test_group.py
Normal file
@@ -0,0 +1,48 @@
|
||||
from fbchat import GroupData, User
|
||||
|
||||
|
||||
def test_group_from_graphql(session):
|
||||
data = {
|
||||
"name": "Group ABC",
|
||||
"thread_key": {"thread_fbid": "11223344"},
|
||||
"image": None,
|
||||
"is_group_thread": True,
|
||||
"all_participants": {
|
||||
"nodes": [
|
||||
{"messaging_actor": {"__typename": "User", "id": "1234"}},
|
||||
{"messaging_actor": {"__typename": "User", "id": "2345"}},
|
||||
{"messaging_actor": {"__typename": "User", "id": "3456"}},
|
||||
]
|
||||
},
|
||||
"customization_info": {
|
||||
"participant_customizations": [],
|
||||
"outgoing_bubble_color": None,
|
||||
"emoji": "😀",
|
||||
},
|
||||
"thread_admins": [{"id": "1234"}],
|
||||
"group_approval_queue": {"nodes": []},
|
||||
"approval_mode": 0,
|
||||
"joinable_mode": {"mode": "0", "link": ""},
|
||||
"event_reminders": {"nodes": []},
|
||||
}
|
||||
assert GroupData(
|
||||
session=session,
|
||||
id="11223344",
|
||||
photo=None,
|
||||
name="Group ABC",
|
||||
last_active=None,
|
||||
message_count=None,
|
||||
plan=None,
|
||||
participants=[
|
||||
User(session=session, id="1234"),
|
||||
User(session=session, id="2345"),
|
||||
User(session=session, id="3456"),
|
||||
],
|
||||
nicknames={},
|
||||
color="#0084ff",
|
||||
emoji="😀",
|
||||
admins={"1234"},
|
||||
approval_mode=False,
|
||||
approval_requests=set(),
|
||||
join_link="",
|
||||
) == GroupData._from_graphql(session, data)
|
22
tests/threads/test_page.py
Normal file
22
tests/threads/test_page.py
Normal file
@@ -0,0 +1,22 @@
|
||||
import fbchat
|
||||
from fbchat import PageData
|
||||
|
||||
|
||||
def test_page_from_graphql(session):
|
||||
data = {
|
||||
"id": "123456",
|
||||
"name": "Some school",
|
||||
"profile_picture": {"uri": "https://scontent-arn2-1.xx.fbcdn.net/v/..."},
|
||||
"url": "https://www.facebook.com/some-school/",
|
||||
"category_type": "SCHOOL",
|
||||
"city": None,
|
||||
}
|
||||
assert PageData(
|
||||
session=session,
|
||||
id="123456",
|
||||
photo=fbchat.Image(url="https://scontent-arn2-1.xx.fbcdn.net/v/..."),
|
||||
name="Some school",
|
||||
url="https://www.facebook.com/some-school/",
|
||||
city=None,
|
||||
category="SCHOOL",
|
||||
) == PageData._from_graphql(session, data)
|
79
tests/threads/test_thread.py
Normal file
79
tests/threads/test_thread.py
Normal file
@@ -0,0 +1,79 @@
|
||||
import pytest
|
||||
import fbchat
|
||||
from fbchat import ThreadABC, Thread, User, Group, Page
|
||||
|
||||
|
||||
def test_parse_color():
|
||||
assert "#0084ff" == ThreadABC._parse_color(None)
|
||||
assert "#0084ff" == ThreadABC._parse_color("")
|
||||
assert "#44bec7" == ThreadABC._parse_color("FF44BEC7")
|
||||
assert "#adbeef" == ThreadABC._parse_color("DEADBEEF")
|
||||
|
||||
|
||||
def test_thread_parse_customization_info_empty():
|
||||
default = {"color": "#0084ff", "emoji": None}
|
||||
assert default == ThreadABC._parse_customization_info(None)
|
||||
assert default == ThreadABC._parse_customization_info({"customization_info": None})
|
||||
|
||||
|
||||
def test_thread_parse_customization_info_group():
|
||||
data = {
|
||||
"thread_key": {"thread_fbid": "11111", "other_user_id": None},
|
||||
"customization_info": {
|
||||
"emoji": "🎉",
|
||||
"participant_customizations": [
|
||||
{"participant_id": "123456789", "nickname": "A"},
|
||||
{"participant_id": "987654321", "nickname": "B"},
|
||||
],
|
||||
"outgoing_bubble_color": "FFFF5CA1",
|
||||
},
|
||||
"customization_enabled": True,
|
||||
"thread_type": "GROUP",
|
||||
# ... Other irrelevant fields
|
||||
}
|
||||
expected = {
|
||||
"emoji": "🎉",
|
||||
"color": "#ff5ca1",
|
||||
"nicknames": {"123456789": "A", "987654321": "B"},
|
||||
}
|
||||
assert expected == ThreadABC._parse_customization_info(data)
|
||||
|
||||
|
||||
def test_thread_parse_customization_info_user():
|
||||
data = {
|
||||
"thread_key": {"thread_fbid": None, "other_user_id": "987654321"},
|
||||
"customization_info": {
|
||||
"emoji": None,
|
||||
"participant_customizations": [
|
||||
{"participant_id": "123456789", "nickname": "A"},
|
||||
{"participant_id": "987654321", "nickname": "B"},
|
||||
],
|
||||
"outgoing_bubble_color": None,
|
||||
},
|
||||
"customization_enabled": True,
|
||||
"thread_type": "ONE_TO_ONE",
|
||||
# ... Other irrelevant fields
|
||||
}
|
||||
expected = {"emoji": None, "color": "#0084ff", "own_nickname": "A", "nickname": "B"}
|
||||
assert expected == ThreadABC._parse_customization_info(data)
|
||||
|
||||
|
||||
def test_thread_parse_participants(session):
|
||||
nodes = [
|
||||
{"messaging_actor": {"__typename": "User", "id": "1234"}},
|
||||
{"messaging_actor": {"__typename": "User", "id": "2345"}},
|
||||
{"messaging_actor": {"__typename": "Page", "id": "3456"}},
|
||||
{"messaging_actor": {"__typename": "MessageThread", "id": "4567"}},
|
||||
{"messaging_actor": {"__typename": "UnavailableMessagingActor", "id": "5678"}},
|
||||
]
|
||||
assert [
|
||||
User(session=session, id="1234"),
|
||||
User(session=session, id="2345"),
|
||||
Page(session=session, id="3456"),
|
||||
Group(session=session, id="4567"),
|
||||
] == list(ThreadABC._parse_participants(session, {"nodes": nodes}))
|
||||
|
||||
|
||||
def test_thread_create_and_implements_thread_abc(session):
|
||||
thread = Thread(session=session, id="123")
|
||||
assert thread._parse_customization_info
|
199
tests/threads/test_user.py
Normal file
199
tests/threads/test_user.py
Normal file
@@ -0,0 +1,199 @@
|
||||
import pytest
|
||||
import datetime
|
||||
import fbchat
|
||||
from fbchat import UserData, ActiveStatus
|
||||
|
||||
|
||||
def test_user_from_graphql(session):
|
||||
data = {
|
||||
"id": "1234",
|
||||
"name": "Abc Def Ghi",
|
||||
"first_name": "Abc",
|
||||
"last_name": "Ghi",
|
||||
"profile_picture": {"uri": "https://scontent-arn2-1.xx.fbcdn.net/v/..."},
|
||||
"is_viewer_friend": True,
|
||||
"url": "https://www.facebook.com/profile.php?id=1234",
|
||||
"gender": "FEMALE",
|
||||
"viewer_affinity": 0.4560002,
|
||||
}
|
||||
assert UserData(
|
||||
session=session,
|
||||
id="1234",
|
||||
photo=fbchat.Image(url="https://scontent-arn2-1.xx.fbcdn.net/v/..."),
|
||||
name="Abc Def Ghi",
|
||||
url="https://www.facebook.com/profile.php?id=1234",
|
||||
first_name="Abc",
|
||||
last_name="Ghi",
|
||||
is_friend=True,
|
||||
gender="female_singular",
|
||||
affinity=0.4560002,
|
||||
color="#0084ff",
|
||||
) == UserData._from_graphql(session, data)
|
||||
|
||||
|
||||
def test_user_from_thread_fetch(session):
|
||||
data = {
|
||||
"thread_key": {"thread_fbid": None, "other_user_id": "1234"},
|
||||
"name": None,
|
||||
"last_message": {
|
||||
"nodes": [
|
||||
{
|
||||
"snippet": "aaa",
|
||||
"message_sender": {"messaging_actor": {"id": "1234"}},
|
||||
"timestamp_precise": "1500000000000",
|
||||
"commerce_message_type": None,
|
||||
"extensible_attachment": None,
|
||||
"sticker": None,
|
||||
"blob_attachments": [],
|
||||
}
|
||||
]
|
||||
},
|
||||
"unread_count": 0,
|
||||
"messages_count": 1111,
|
||||
"image": None,
|
||||
"updated_time_precise": "1500000000000",
|
||||
"mute_until": None,
|
||||
"is_pin_protected": False,
|
||||
"is_viewer_subscribed": True,
|
||||
"thread_queue_enabled": False,
|
||||
"folder": "INBOX",
|
||||
"has_viewer_archived": False,
|
||||
"is_page_follow_up": False,
|
||||
"cannot_reply_reason": None,
|
||||
"ephemeral_ttl_mode": 0,
|
||||
"customization_info": {
|
||||
"emoji": None,
|
||||
"participant_customizations": [
|
||||
{"participant_id": "4321", "nickname": "B"},
|
||||
{"participant_id": "1234", "nickname": "A"},
|
||||
],
|
||||
"outgoing_bubble_color": None,
|
||||
},
|
||||
"thread_admins": [],
|
||||
"approval_mode": None,
|
||||
"joinable_mode": {"mode": "0", "link": ""},
|
||||
"thread_queue_metadata": None,
|
||||
"event_reminders": {"nodes": []},
|
||||
"montage_thread": None,
|
||||
"last_read_receipt": {"nodes": [{"timestamp_precise": "1500000050000"}]},
|
||||
"related_page_thread": None,
|
||||
"rtc_call_data": {
|
||||
"call_state": "NO_ONGOING_CALL",
|
||||
"server_info_data": "",
|
||||
"initiator": None,
|
||||
},
|
||||
"associated_object": None,
|
||||
"privacy_mode": 1,
|
||||
"reactions_mute_mode": "REACTIONS_NOT_MUTED",
|
||||
"mentions_mute_mode": "MENTIONS_NOT_MUTED",
|
||||
"customization_enabled": True,
|
||||
"thread_type": "ONE_TO_ONE",
|
||||
"participant_add_mode_as_string": None,
|
||||
"is_canonical_neo_user": False,
|
||||
"participants_event_status": [],
|
||||
"page_comm_item": None,
|
||||
"all_participants": {
|
||||
"nodes": [
|
||||
{
|
||||
"messaging_actor": {
|
||||
"id": "1234",
|
||||
"__typename": "User",
|
||||
"name": "Abc Def Ghi",
|
||||
"gender": "FEMALE",
|
||||
"url": "https://www.facebook.com/profile.php?id=1234",
|
||||
"big_image_src": {
|
||||
"uri": "https://scontent-arn2-1.xx.fbcdn.net/v/..."
|
||||
},
|
||||
"short_name": "Abc",
|
||||
"username": "",
|
||||
"is_viewer_friend": True,
|
||||
"is_messenger_user": True,
|
||||
"is_verified": False,
|
||||
"is_message_blocked_by_viewer": False,
|
||||
"is_viewer_coworker": False,
|
||||
"is_employee": None,
|
||||
}
|
||||
},
|
||||
{
|
||||
"messaging_actor": {
|
||||
"id": "4321",
|
||||
"__typename": "User",
|
||||
"name": "Aaa Bbb Ccc",
|
||||
"gender": "NEUTER",
|
||||
"url": "https://www.facebook.com/aaabbbccc",
|
||||
"big_image_src": {
|
||||
"uri": "https://scontent-arn2-1.xx.fbcdn.net/v/..."
|
||||
},
|
||||
"short_name": "Aaa",
|
||||
"username": "aaabbbccc",
|
||||
"is_viewer_friend": False,
|
||||
"is_messenger_user": True,
|
||||
"is_verified": False,
|
||||
"is_message_blocked_by_viewer": False,
|
||||
"is_viewer_coworker": False,
|
||||
"is_employee": None,
|
||||
}
|
||||
},
|
||||
]
|
||||
},
|
||||
"read_receipts": ...,
|
||||
"delivery_receipts": ...,
|
||||
}
|
||||
assert UserData(
|
||||
session=session,
|
||||
id="1234",
|
||||
photo=fbchat.Image(url="https://scontent-arn2-1.xx.fbcdn.net/v/..."),
|
||||
name="Abc Def Ghi",
|
||||
last_active=datetime.datetime(2017, 7, 14, 2, 40, tzinfo=datetime.timezone.utc),
|
||||
message_count=1111,
|
||||
url="https://www.facebook.com/profile.php?id=1234",
|
||||
first_name="Abc",
|
||||
is_friend=True,
|
||||
gender="female_singular",
|
||||
nickname="A",
|
||||
own_nickname="B",
|
||||
color="#0084ff",
|
||||
emoji=None,
|
||||
) == UserData._from_thread_fetch(session, data)
|
||||
|
||||
|
||||
def test_user_from_all_fetch(session):
|
||||
data = {
|
||||
"id": "1234",
|
||||
"name": "Abc Def Ghi",
|
||||
"firstName": "Abc",
|
||||
"vanity": "",
|
||||
"thumbSrc": "https://scontent-arn2-1.xx.fbcdn.net/v/...",
|
||||
"uri": "https://www.facebook.com/profile.php?id=1234",
|
||||
"gender": 1,
|
||||
"i18nGender": 2,
|
||||
"type": "friend",
|
||||
"is_friend": True,
|
||||
"mThumbSrcSmall": None,
|
||||
"mThumbSrcLarge": None,
|
||||
"dir": None,
|
||||
"searchTokens": ["Abc", "Ghi"],
|
||||
"alternateName": "",
|
||||
"is_nonfriend_messenger_contact": False,
|
||||
"is_blocked": False,
|
||||
}
|
||||
assert UserData(
|
||||
session=session,
|
||||
id="1234",
|
||||
photo=fbchat.Image(url="https://scontent-arn2-1.xx.fbcdn.net/v/..."),
|
||||
name="Abc Def Ghi",
|
||||
url="https://www.facebook.com/profile.php?id=1234",
|
||||
first_name="Abc",
|
||||
is_friend=True,
|
||||
gender="female_singular",
|
||||
) == UserData._from_all_fetch(session, data)
|
||||
|
||||
|
||||
@pytest.mark.skip(reason="can't gather test data, the pulling is broken")
|
||||
def test_active_status_from_chatproxy_presence():
|
||||
assert ActiveStatus() == ActiveStatus._from_chatproxy_presence(data)
|
||||
|
||||
|
||||
@pytest.mark.skip(reason="can't gather test data, the pulling is broken")
|
||||
def test_active_status_from_buddylist_overlay():
|
||||
assert ActiveStatus() == ActiveStatus._from_buddylist_overlay(data)
|
Reference in New Issue
Block a user