def test_user_teams_channels_for_user_with_two_channels_returns_full_list( self): data = MagicMock() '''Mocked outputs''' user = User(user_id=0) channels = [ Channel(channel_id=1, team_id=0, name="TEST1", creator=ChannelCreator(user_id=0, username="******", first_name="TESTER", last_name="TESTER")), Channel(channel_id=2, team_id=0, name="TEST2", creator=ChannelCreator(user_id=0, username="******", first_name="TESTER", last_name="TESTER")), ] sys.modules[ "models.authentication"].Authenticator.authenticate.return_value = user sys.modules[ "daos.channels"].ChannelDatabaseClient.get_user_channels_by_user_id.return_value = channels response = UserService.channels_for_user(data) self.assertIsInstance(response, SuccessfulChannelsListResponse) self.assertEqual(2, len(response.channels))
def test_notify_channel_message_from_user_sends_notification(self): message = Message(sender_id=0, receiver_id=1, team_id=0, content="Sarasa", send_type=SendMessageType.DIRECT.value, message_type=MessageType.TEXT.value) is_user_receiver = False '''Mocked outputs''' user_sender = PublicUser(user_id=0, username="******", first_name="Test0", last_name="Test0") channel = Channel(channel_id=0, team_id=0, name="Test-Channel", creator=ChannelCreator(0, "Tester0", "Test0", "Test0")) team = Team(team_id=0, name="Test-Team") def send_notification(topic_name, message_title, message_body, data_message): from tests.test_services import test_notifications MockedNotificationServer.notification = Notification( topic_name, message_title, message_body, data_message) return {"failure": 0} sys.modules[ "daos.teams"].TeamDatabaseClient.get_team_by_id.return_value = team sys.modules[ "daos.users"].UserDatabaseClient.get_user_by_id.return_value = user_sender sys.modules[ "daos.channels"].ChannelDatabaseClient.get_channel_by_id.return_value = channel sys.modules["pyfcm"].FCMNotification( ).notify_topic_subscribers = MagicMock(side_effect=send_notification) NotificationService.notify_message(message, is_user_receiver) self.assertEqual(1, MockedNotificationServer.notification.topic_name) self.assertEqual("Hypechat", MockedNotificationServer.notification.message_title) self.assertEqual("You receive a channel message!", MockedNotificationServer.notification.message_body) self.assertEqual( "Test-Team", MockedNotificationServer.notification.data_message.get( "team_name")) self.assertEqual( "Test-Channel", MockedNotificationServer.notification.data_message.get( "channel_name")) self.assertEqual( 0, MockedNotificationServer.notification.data_message.get( "sender_id")) self.assertEqual( NotificationType.MESSAGE.value, MockedNotificationServer.notification.data_message.get( "notification_type"))
def test_notify_channel_invitation_sends_notification(self): user_channel = ChannelUser(user_id=1, channel_id=0) inviter_id = 0 '''Mocked outputs''' inviter_user = PublicUser(user_id=0, username="******", first_name="Test0", last_name="Test0") channel = Channel(channel_id=0, team_id=0, name="Test-Channel", creator=ChannelCreator(0, "Tester0", "Test0", "Test0")) team = Team(team_id=0, name="Test-Team") def send_notification(topic_name, message_title, message_body, data_message): from tests.test_services import test_notifications MockedNotificationServer.notification = Notification( topic_name, message_title, message_body, data_message) return {"failure": 0} sys.modules[ "daos.users"].UserDatabaseClient.get_user_by_id.return_value = inviter_user sys.modules[ "daos.teams"].TeamDatabaseClient.get_team_by_id.return_value = team sys.modules[ "daos.channels"].ChannelDatabaseClient.get_channel_by_id.return_value = channel sys.modules["pyfcm"].FCMNotification( ).notify_topic_subscribers = MagicMock(side_effect=send_notification) NotificationService.notify_channel_invitation(user_channel, inviter_id) self.assertEqual(1, MockedNotificationServer.notification.topic_name) self.assertEqual("Hypechat", MockedNotificationServer.notification.message_title) self.assertEqual( "You have been added to channel Test-Channel in team Test-Team!", MockedNotificationServer.notification.message_body) self.assertEqual( "Test-Team", MockedNotificationServer.notification.data_message.get( "team_name")) self.assertEqual( "Test-Channel", MockedNotificationServer.notification.data_message.get( "channel_name")) self.assertEqual( 0, MockedNotificationServer.notification.data_message.get( "inviter_id")) self.assertEqual( NotificationType.CHANNEL_INVITATION.value, MockedNotificationServer.notification.data_message.get( "notification_type"))
def to_channel(cls, channel_entry): return Channel(channel_id=channel_entry.channel_id, team_id=channel_entry.team_id, name=channel_entry.name, creator=ChannelCreator( user_id=channel_entry.user_id, username=channel_entry.username, first_name=channel_entry.first_name, last_name=channel_entry.last_name), visibility=channel_entry.visibility, description=channel_entry.description, welcome_message=channel_entry.welcome_message ) if channel_entry is not None else None
def to_team_channels(cls, channels_entries): channels = [] for channel_entry in channels_entries: channels += [ Channel(channel_id=channel_entry.channel_id, team_id=channel_entry.team_id, name=channel_entry.name, creator=ChannelCreator( user_id=channel_entry.user_id, username=channel_entry.username, first_name=channel_entry.first_name, last_name=channel_entry.last_name), visibility=channel_entry.visibility, description=channel_entry.description, welcome_message=channel_entry.welcome_message) ] return channels
def test_get_messages_from_channel_chat_set_offset_in_0_and_works_properly( self): data = MagicMock() '''Mocked ouputs''' user = PublicUser(user_id=0, username="******", first_name="Test0", last_name="Test0") user.team_id = 0 user.team_role = TeamRoles.MEMBER.value chat = Chat(user_id=0, chat_id=1, team_id=0, offset=1) channel = Channel(channel_id=2, name="Channel-Test", creator=ChannelCreator(user_id=5), team_id=0) sender1 = UserMessageSender(user_id=0, username="******", first_name="Test0", last_name="Test0") message1 = ChatMessage(message_id=1, sender=sender1, receiver_id=2, team_id=0, content="Test-Message-0", message_type="TEXT", timestamp=datetime.now() - timedelta(hours=1)) sender2 = UserMessageSender(user_id=1, username="******", first_name="Test1", last_name="Test1") message2 = ChatMessage(message_id=0, sender=sender2, receiver_id=2, team_id=0, content="Test-Message-1", message_type="TEXT", timestamp=datetime.now() - timedelta(hours=0)) channel_chats = [message1, message2] def add_update_chat(chat): from tests.test_services import test_messages MockedMessageDatabase.stored_chat = chat sys.modules[ "models.authentication"].Authenticator.authenticate_team.return_value = user sys.modules[ "daos.messages"].MessageDatabaseClient.get_chat_by_ids.return_value = chat sys.modules[ "daos.channels"].ChannelDatabaseClient.get_channel_by_id.return_value = channel sys.modules[ "daos.messages"].MessageDatabaseClient.get_channel_chat.return_value = channel_chats sys.modules[ "daos.messages"].MessageDatabaseClient.add_or_update_chat = MagicMock( side_effect=add_update_chat) response = MessageService.get_messages_from_chat(data) self.assertIsInstance(response, MessageListResponse) self.assertEqual(True, response.is_channel) self.assertEqual(2, len(response.messages)) self.assertEqual(1, response.messages[0].get("sender").get("id")) self.assertEqual(0, response.messages[1].get("sender").get("id")) self.assertEqual(0, MockedMessageDatabase.stored_chat.offset)