1+ import os
2+ import yaml
3+ from pytest import fixture , mark
4+ import re
5+ from datetime import datetime , timedelta
6+
7+ from symphony .bdk .core .config .loader import BdkConfigLoader
8+ from symphony .bdk .core .symphony_bdk import SymphonyBdk
9+
10+
11+
12+ NUMBER_OF_MESSAGES = 10
13+ STREAM_ID = os .getenv ("STREAM_ID" , "put-stream-id-to-env-vars" )
14+ CONFIG_PATH = "./config.yaml"
15+
16+ @fixture
17+ def bot_config ():
18+ bot_user = os .getenv ("BOT_USERNAME" , "put-useranme-to-env-vars" )
19+ sym_host = os .getenv ("SYMPHONY_HOST" , "put-symphony-host-to-env-vars" )
20+ key_path = "./key.pem"
21+ bot_config = {"host" : sym_host ,
22+ "bot" : {"username" : bot_user , "privateKey" : {"path" : key_path }}}
23+ with open (CONFIG_PATH , "w" ) as config_file :
24+ yaml .dump (bot_config , config_file )
25+
26+ with open (key_path , "w" ) as key_file :
27+ key_file .write (os .getenv ("TEST_RSA_KEY" , "PUT A KEY HERE OR INTO ENV VAR" ))
28+ yield
29+ os .remove (key_path ), os .remove (CONFIG_PATH )
30+
31+ async def send_messages (messages , stream_id , since ):
32+ for i in range (NUMBER_OF_MESSAGES ):
33+ await messages .send_message (stream_id , f"<messageML><b>{ i } -{ since } </b></messageML>" )
34+
35+ async def get_test_messages (bdk , since ):
36+ messages = await bdk .messages ().list_messages (STREAM_ID , since = since )
37+ cleaned_messages_text = [
38+ re .sub (r"<[^>]+>" , " " , msg ["message" ]).strip ()
39+ for msg in messages
40+ ]
41+ return cleaned_messages_text
42+
43+
44+ @mark .asyncio
45+ async def test_bot_read_write_messages (bot_config ):
46+ # Given: test execution start time
47+ since = int ((datetime .now () - timedelta (seconds = 2 )).timestamp ()) * 1000
48+ # Given: BDK is initialized with config
49+ config = BdkConfigLoader .load_from_symphony_dir (CONFIG_PATH )
50+ async with SymphonyBdk (config ) as bdk :
51+ # When: messages are sent via bot
52+ await send_messages (bdk .messages (), STREAM_ID , since )
53+ # Then: messages are readable with the same bot
54+ messages = await get_test_messages (bdk , since )
55+ # Then: Expected messages are posted to the room
56+ assert sorted (messages ) == [f"{ i } -{ since } " for i in range (NUMBER_OF_MESSAGES )]
0 commit comments