Skip to content

Commit

Permalink
test_: get expected message by content type
Browse files Browse the repository at this point in the history
  • Loading branch information
fbarbu15 committed Dec 10, 2024
1 parent e0518b0 commit b2ad64f
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 22 deletions.
Empty file.
22 changes: 22 additions & 0 deletions tests-functional/resources/enums.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
from enum import Enum

class MessageContentType(Enum):
UNKNOWN_CONTENT_TYPE = 0
TEXT_PLAIN = 1
STICKER = 2
STATUS = 3
EMOJI = 4
TRANSACTION_COMMAND = 5
SYSTEM_MESSAGE_CONTENT_PRIVATE_GROUP = 6
IMAGE = 7
AUDIO = 8
COMMUNITY = 9
SYSTEM_MESSAGE_GAP = 10
CONTACT_REQUEST = 11
DISCORD_MESSAGE = 12
IDENTITY_VERIFICATION = 13
SYSTEM_MESSAGE_PINNED_MESSAGE = 14
SYSTEM_MESSAGE_MUTUAL_EVENT_SENT = 15
SYSTEM_MESSAGE_MUTUAL_EVENT_ACCEPTED = 16
SYSTEM_MESSAGE_MUTUAL_EVENT_REMOVED = 17
BRIDGE_MESSAGE = 18
24 changes: 16 additions & 8 deletions tests-functional/tests/test_cases.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,17 +187,17 @@ def initialize_backend(self, await_signals, display_name=DEFAULT_DISPLAY_NAME, u
return backend


def validate_event_against_response(self, event, fields_to_validate, response, expected_message_id):
messages_in_event = event["event"]["messages"]
assert len(messages_in_event) > 0, "No messages found in the event"
response_chat = response["result"]["chats"][0]
def validate_signal_event_against_response(self, signal_event, fields_to_validate, expected_message):
expected_message_id = expected_message.get("id")
signal_event_messages = signal_event.get("event", {}).get("messages")
assert len(signal_event_messages) > 0, "No messages found in the signal event"

message = next((message for message in messages_in_event if message["id"] == expected_message_id), None)
assert message, f"Message with ID {expected_message_id} not found in the event"
message = next((message for message in signal_event_messages if message.get("id") == expected_message_id), None)
assert message, f"Message with ID {expected_message_id} not found in the signal event"

message_mismatch = []
for response_field, event_field in fields_to_validate.items():
response_value = response_chat["lastMessage"][response_field]
response_value = expected_message[response_field]
event_value = message[event_field]
if response_value != event_value:
message_mismatch.append(
Expand All @@ -224,4 +224,12 @@ def get_message_id(self, response):
if message_id is None:
raise ValueError("Failed to extract message_id from response")

return message_id
return message_id

def get_message_by_content_type(self, response, content_type):
messages = response.get("result", {}).get("messages", [])
for message in messages:
if message.get("contentType") == content_type:
return message

raise ValueError(f"Failed to find a message with contentType '{content_type}' in response")
12 changes: 6 additions & 6 deletions tests-functional/tests/test_contact_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from test_cases import OneToOneMessageTestCase
from constants import DEFAULT_DISPLAY_NAME
from clients.signals import SignalType
from resources.enums import MessageContentType

@pytest.mark.rpc
class TestContactRequests(OneToOneMessageTestCase):
Expand All @@ -28,9 +29,9 @@ def test_contact_request_baseline(self, execution_number=1):
existing_contacts = receiver.get_contacts()

response = sender.send_contact_request(pk_receiver, message_text)
message_id = self.get_message_id(response)
expected_message = self.get_message_by_content_type(response, content_type=MessageContentType.CONTACT_REQUEST.value)

messages_new_event = receiver.find_signal_containing_pattern(SignalType.MESSAGES_NEW.value, event_pattern=message_id, timeout=60)
messages_new_event = receiver.find_signal_containing_pattern(SignalType.MESSAGES_NEW.value, event_pattern=expected_message.get("id"), timeout=60)

signal_messages_texts = []
if "messages" in messages_new_event.get("event", {}):
Expand All @@ -43,11 +44,10 @@ def test_contact_request_baseline(self, execution_number=1):
if pk_sender not in str(existing_contacts): # we check that the contact request wasn"t already sent for this sender
assert f"@{pk_sender} sent you a contact request" in signal_messages_texts, "Couldn't find the signal corresponding to the contact request"

self.validate_event_against_response(
messages_new_event,
self.validate_signal_event_against_response(
signal_event=messages_new_event,
fields_to_validate={"text": "text"},
response=response,
expected_message_id=message_id
expected_message=expected_message
)

@pytest.mark.skip(reason="Skipping because of error 'Not enough status-backend containers, please add more'. Unkipping when we merge https://github.com/status-im/status-go/pull/6159")
Expand Down
16 changes: 8 additions & 8 deletions tests-functional/tests/test_one_to_one_messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from test_cases import OneToOneMessageTestCase
from constants import DEFAULT_DISPLAY_NAME
from clients.signals import SignalType
from resources.enums import MessageContentType

@pytest.mark.rpc
class TestOneToOneMessages(OneToOneMessageTestCase):
Expand All @@ -27,17 +28,16 @@ def test_one_to_one_message_baseline(self, message_count=1):
for i in range(message_count):
message_text = f"test_message_{i+1}_{uuid4()}"
response = self.sender.send_message(pk_receiver, message_text)
message_id = self.get_message_id(response)
sent_messages.append((message_id, response))
expected_message = self.get_message_by_content_type(response, content_type=MessageContentType.TEXT_PLAIN.value)
sent_messages.append(expected_message)
sleep(0.01)

for i, (message_id, response) in enumerate(sent_messages):
messages_new_event = self.receiver.find_signal_containing_pattern(SignalType.MESSAGES_NEW.value, event_pattern=message_id, timeout=60)
self.validate_event_against_response(
messages_new_event,
for i, expected_message in enumerate(sent_messages):
messages_new_event = self.receiver.find_signal_containing_pattern(SignalType.MESSAGES_NEW.value, event_pattern=expected_message.get("id"), timeout=60)
self.validate_signal_event_against_response(
signal_event=messages_new_event,
fields_to_validate={"text": "text"},
response=response,
expected_message_id=message_id
expected_message=expected_message
)

@pytest.mark.dependency(depends=["test_one_to_one_message_baseline"])
Expand Down

0 comments on commit b2ad64f

Please sign in to comment.