Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
from logging import getLogger
from ehforwarderbot import EFBChannel, EFBMsg, EFBStatus, ChannelType, MsgType, coordinator, EFBChat
from ehforwarderbot.message import EFBMsgLinkAttribute, EFBMsgLocationAttribute
from ehforwarderbot.status import EFBMessageRemoval
from ehforwarderbot.types import ModuleID, MessageID
from ehforwarderbot.types import ChatID
class MockMasterChannel(EFBChannel):
channel_name: str = "Mock Master"
channel_emoji: str = "➕"
channel_id: ModuleID = ModuleID("tests.mocks.master.MockMasterChannel")
channel_type: ChannelType = ChannelType.Master
supported_message_types: Set[MsgType] = {MsgType.Text, MsgType.Link}
__version__: str = '0.0.1'
logger = getLogger(channel_id)
polling = threading.Event()
def poll(self):
self.polling.wait()
def send_status(self, status: EFBStatus):
self.logger.debug("Received status: %r", status)
def send_message(self, msg: EFBMsg) -> EFBMsg:
self.logger.debug("Received message: %r", msg)
return msg
def setup(tmp_path):
temp_dir = tmp_path
os.environ['EFB_DATA_PATH'] = str(temp_dir)
config_path = ehforwarderbot.utils.get_config_path()
config = {
"master_channel": "tests.mocks.master.MockMasterChannel",
"slave_channels": ["tests.mocks.slave.MockSlaveChannel"],
"middlewares": ["tests.mocks.middleware.MockMiddleware"]
}
with open(config_path, 'w') as f:
yaml.dump(config, f)
config = ehforwarderbot.config.load_config()
ehforwarderbot.__main__.init(config)
assert coordinator.master.channel_id == master.MockMasterChannel.channel_id
assert slave.MockSlaveChannel.channel_id in coordinator.slaves
assert coordinator.middlewares[0].middleware_id == middleware.MockMiddleware.middleware_id
os.environ['EFB_DATA_PATH'] = f
master_id = "tests.mocks.master.MockMasterChannel#instance1"
slave_ids = [
"tests.mocks.slave.MockSlaveChannel#instance1",
"tests.mocks.slave.MockSlaveChannel#instance2"
]
config = {
"master_channel": master_id,
"slave_channels": slave_ids
}
config = dump_and_load_config(config)
ehforwarderbot.__main__.init(config)
assert coordinator.master.channel_id == master_id
assert isinstance(coordinator.master, master.MockMasterChannel)
for i in slave_ids:
assert i in coordinator.slaves
assert isinstance(coordinator.slaves[i], slave.MockSlaveChannel)
def test_custom_path_module_loading():
with tempfile.TemporaryDirectory() as f:
os.environ['EFB_DATA_PATH'] = f
modules_path = ehforwarderbot.utils.get_custom_modules_path()
config = {
"master_channel": "master.MockMasterChannel",
"slave_channels": ["slave.MockSlaveChannel",
"slave.MockSlaveChannel#instance"],
"middlewares": ["middleware.MockMiddleware",
"middleware.MockMiddleware#instance"]
}
test_path: Path = Path(inspect.getfile(inspect.currentframe())).parent / 'mocks'
# noinspection PyTypeChecker
shutil.copy(test_path / 'master.py', modules_path)
# noinspection PyTypeChecker
shutil.copy(test_path / 'slave.py', modules_path)
# noinspection PyTypeChecker
shutil.copy(test_path / 'middleware.py', modules_path)
config = dump_and_load_config(config)
ehforwarderbot.__main__.init(config)
def setup(tmp_path):
temp_dir = tmp_path
os.environ['EFB_DATA_PATH'] = str(temp_dir)
config_path = ehforwarderbot.utils.get_config_path()
config = {
"master_channel": "tests.mocks.master.MockMasterChannel",
"slave_channels": ["tests.mocks.slave.MockSlaveChannel"],
"middlewares": ["tests.mocks.middleware.MockMiddleware"]
}
with open(config_path, 'w') as f:
yaml.dump(config, f)
config = ehforwarderbot.config.load_config()
ehforwarderbot.__main__.init(config)
assert coordinator.master.channel_id == master.MockMasterChannel.channel_id
assert slave.MockSlaveChannel.channel_id in coordinator.slaves
assert coordinator.middlewares[0].middleware_id == middleware.MockMiddleware.middleware_id
"tests.mocks.slave.MockSlaveChannel#instance1",
"tests.mocks.slave.MockSlaveChannel#instance2"
]
config = {
"master_channel": master_id,
"slave_channels": slave_ids
}
config = dump_and_load_config(config)
ehforwarderbot.__main__.init(config)
assert coordinator.master.channel_id == master_id
assert isinstance(coordinator.master, master.MockMasterChannel)
for i in slave_ids:
assert i in coordinator.slaves
assert isinstance(coordinator.slaves[i], slave.MockSlaveChannel)
import threading
from typing import Set, Optional, IO, List
from logging import getLogger
from ehforwarderbot import EFBChannel, EFBMsg, EFBStatus, ChannelType, MsgType, coordinator, EFBChat
from ehforwarderbot.message import EFBMsgLinkAttribute, EFBMsgLocationAttribute
from ehforwarderbot.status import EFBMessageRemoval
from ehforwarderbot.types import ModuleID, MessageID
from ehforwarderbot.types import ChatID
class MockMasterChannel(EFBChannel):
channel_name: str = "Mock Master"
channel_emoji: str = "➕"
channel_id: ModuleID = ModuleID("tests.mocks.master.MockMasterChannel")
channel_type: ChannelType = ChannelType.Master
supported_message_types: Set[MsgType] = {MsgType.Text, MsgType.Link}
__version__: str = '0.0.1'
logger = getLogger(channel_id)
polling = threading.Event()
def poll(self):
self.polling.wait()
def send_status(self, status: EFBStatus):
self.logger.debug("Received status: %r", status)
def send_message(self, msg: EFBMsg) -> EFBMsg:
self.logger.debug("Received message: %r", msg)
def setup(tmp_path):
temp_dir = tmp_path
os.environ['EFB_DATA_PATH'] = str(temp_dir)
config_path = ehforwarderbot.utils.get_config_path()
config = {
"master_channel": "tests.mocks.master.MockMasterChannel",
"slave_channels": ["tests.mocks.slave.MockSlaveChannel"],
"middlewares": ["tests.mocks.middleware.MockMiddleware"]
}
with open(config_path, 'w') as f:
yaml.dump(config, f)
config = ehforwarderbot.config.load_config()
ehforwarderbot.__main__.init(config)
assert coordinator.master.channel_id == master.MockMasterChannel.channel_id
assert slave.MockSlaveChannel.channel_id in coordinator.slaves
assert coordinator.middlewares[0].middleware_id == middleware.MockMiddleware.middleware_id
def send_text_msg(self):
slave = next(iter(coordinator.slaves.values()))
wonderland = slave.get_chat('wonderland001')
msg = EFBMsg()
msg.deliver_to = slave
msg.chat = wonderland
msg.author = EFBChat(self).self()
msg.type = MsgType.Text
msg.text = "Hello, world."
return coordinator.send_message(msg)
raise EFBMessageReactionNotPossible("Message reaction is rejected by flag.")
if self.accept_message_reactions == "reject_all":
raise EFBOperationNotSupported("All message reactions are rejected by flag.")
message = self.messages_sent.get(status.msg_id)
if message is None:
raise EFBOperationNotSupported("Message is not found.")
if status.reaction is None:
for idx, i in message.reactions.items():
message.reactions[idx] = [j for j in i if not isinstance(j, SelfChatMember)]
else:
if status.reaction not in message.reactions:
message.reactions[status.reaction] = []
message.reactions[status.reaction].append(message.chat.self)
coordinator.send_status(MessageReactionsUpdate(
chat=message.chat,
msg_id=message.uid,
reactions=message.reactions
))