def test_authenticating_to_non_existing_channel_channel_throws_exception( self): user_id = 0 team_id = 0 channel_id = 0 token = Authenticator.generate(user_id) authentication = MagicMock() authentication.token = token authentication.team_id = team_id authentication.channel_id = channel_id '''Mocked outputs''' user = User(user_id=user_id, token=token) team_user = PublicUser(user_id=user_id) team_user.team_role = TeamRoles.MEMBER.value team_user.team_id = team_id sys.modules[ "daos.users"].UserDatabaseClient.get_user_by_id.return_value = user sys.modules[ "daos.users"].UserDatabaseClient.get_team_user_by_ids.return_value = team_user sys.modules[ "daos.users"].UserDatabaseClient.get_channel_user_by_ids.return_value = None sys.modules[ "daos.channels"].ChannelDatabaseClient.get_channel_by_id.return_value = None self.assertRaises(ChannelNotFoundError, Authenticator.authenticate_channel, authentication)
def test_team_regular_member_authenticating_to_not_belonging_channel_throws_exception( self): user_id = 0 team_id = 0 channel_id = 0 token = Authenticator.generate(user_id) authentication = MagicMock() authentication.token = token authentication.team_id = team_id authentication.channel_id = channel_id '''Mocked outputs''' user = User(user_id=user_id, token=token) team_user = PublicUser(user_id=user_id) team_user.team_role = TeamRoles.MEMBER.value team_user.team_id = team_id channel = Channel(channel_id=channel_id, team_id=team_id, name="test", creator=None) sys.modules[ "daos.users"].UserDatabaseClient.get_user_by_id.return_value = user sys.modules[ "daos.users"].UserDatabaseClient.get_team_user_by_ids.return_value = team_user sys.modules[ "daos.users"].UserDatabaseClient.get_channel_user_by_ids.return_value = None sys.modules[ "daos.channels"].ChannelDatabaseClient.get_channel_by_id.return_value = channel self.assertRaises(NoPermissionsError, Authenticator.authenticate_channel, authentication)
def test_team_moderator_authenticates_to_every_team_channel(self): user_id = 0 team_id = 0 channel_id = 0 token = Authenticator.generate(user_id) authentication = MagicMock() authentication.token = token authentication.team_id = team_id authentication.channel_id = channel_id '''Mocked outputs''' user = User(user_id=user_id, token=token) team_user = PublicUser(user_id=user_id) team_user.team_role = TeamRoles.MODERATOR.value team_user.team_id = team_id sys.modules[ "daos.users"].UserDatabaseClient.get_user_by_id.return_value = user sys.modules[ "daos.users"].UserDatabaseClient.get_team_user_by_ids.return_value = team_user authenticated_user = Authenticator.authenticate_channel(authentication) self.assertEqual(user_id, authenticated_user.id) self.assertEqual(team_id, authenticated_user.team_id) self.assertEqual(channel_id, authenticated_user.channel_id) self.assertFalse(authenticated_user.is_channel_creator) self.assertEqual(TeamRoles.MODERATOR.value, authenticated_user.team_role)
def test_app_user_authenticating_to_team_with_role_verifying_authenticates( self): user_id = 0 team_id = 0 token = Authenticator.generate(user_id) authentication = MagicMock() authentication.token = token authentication.team_id = team_id def verifying_function(_): return True '''Mocked outputs''' user = User(user_id=user_id, token=token) team_user = PublicUser(user_id=user_id) team_user.team_role = TeamRoles.CREATOR.value team_user.team_id = team_id sys.modules[ "daos.users"].UserDatabaseClient.get_user_by_id.return_value = user sys.modules[ "daos.users"].UserDatabaseClient.get_team_user_by_ids.return_value = team_user authenticated_user = Authenticator.authenticate_team( authentication, verifying_function) self.assertEqual(user_id, authenticated_user.id)
def test_get_messages_from_direct_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) sender1 = UserMessageSender(user_id=0, username="******", first_name="Test0", last_name="Test0") message1 = ChatMessage(message_id=1, sender=sender1, receiver_id=1, 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=0, team_id=0, content="Test-Message-1", message_type="TEXT", timestamp=datetime.now() - timedelta(hours=0)) direct_chats = [message1, message2] def add_or_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 = None sys.modules[ "daos.messages"].MessageDatabaseClient.get_direct_chat.return_value = direct_chats sys.modules[ "daos.messages"].MessageDatabaseClient.add_or_update_chat = MagicMock( side_effect=add_or_update_chat) response = MessageService.get_messages_from_chat(data) self.assertIsInstance(response, MessageListResponse) self.assertEqual(False, 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)
def test_notify_team_invitation_to_registered_user_does_send_notification( self): invitation = MagicMock() inviter_id = MagicMock() '''Mocked outputs''' inviter_user = PublicUser(user_id=0, username="******", first_name="Test0", last_name="Test0") invited_user = PublicUser(user_id=1, username="******", first_name="Test1", last_name="Test1") 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.users"].UserDatabaseClient.get_user_by_email.return_value = invited_user sys.modules[ "daos.teams"].TeamDatabaseClient.get_team_by_id.return_value = team sys.modules["pyfcm"].FCMNotification( ).notify_topic_subscribers = MagicMock(side_effect=send_notification) NotificationService.notify_team_invitation(invitation, inviter_id) self.assertEqual(1, MockedNotificationServer.notification.topic_name) self.assertEqual("Hypechat", MockedNotificationServer.notification.message_title) self.assertEqual("You have been invited to join a team!", MockedNotificationServer.notification.message_body) self.assertEqual( "Test-Team", MockedNotificationServer.notification.data_message.get( "team_name")) self.assertEqual( 0, MockedNotificationServer.notification.data_message.get( "inviter_id")) self.assertEqual( NotificationType.TEAM_INVITATION.value, MockedNotificationServer.notification.data_message.get( "notification_type"))
def test_send_message_to_yourself_returns_bad_request(self): data = MagicMock() data.chat_id = 0 '''Mocked ouputs''' user = PublicUser(user_id=0, username="******", first_name="Test0", last_name="Test0") user.team_id = 0 user.team_role = TeamRoles.MEMBER.value sys.modules[ "models.authentication"].Authenticator.authenticate_team.return_value = user self.assertRaises(WrongActionError, MessageService.send_message, data)
def to_users(cls, users_entries): users = [] for user_entry in users_entries: user = PublicUser(user_id=user_entry.user_id, role=user_entry.role, online=user_entry.online, first_name=user_entry.first_name, last_name=user_entry.last_name, profile_pic=user_entry.profile_pic, email=user_entry.email, username=user_entry.username, created=user_entry.created) user.facebook_id = user_entry.facebook_id users += [user] return users
def to_team_members(cls, members_entries): users = [] for member_entry in members_entries: user = PublicUser(user_id=member_entry.user_id, username=member_entry.username, email=member_entry.email, first_name=member_entry.first_name, last_name=member_entry.last_name, profile_pic=member_entry.profile_pic, role=member_entry.role, online=member_entry.online) user.team_id = member_entry.team_id user.team_role = member_entry.team_role users += [user] return users
def test_notify_team_invitation_to_non_registered_user_does_not_send_notification( self): invitation = MagicMock() inviter_id = MagicMock() '''Mocked outputs''' inviter_user = PublicUser(user_id=0, username="******", first_name="Test0", last_name="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) sys.modules[ "daos.users"].UserDatabaseClient.get_user_by_id.return_value = inviter_user sys.modules[ "daos.users"].UserDatabaseClient.get_user_by_email.return_value = None sys.modules[ "daos.teams"].TeamDatabaseClient.get_team_by_id.return_value = team sys.modules["pyfcm"].FCMNotification( ).notify_topic_subscribers = MagicMock(side_effect=send_notification) NotificationService.notify_team_invitation(invitation, inviter_id) self.assertIsNone(MockedNotificationServer.notification)
def to_channel_user(cls, table_entry): if table_entry is None: return None user = PublicUser(user_id=table_entry.user_id, username=table_entry.username, email=table_entry.email, first_name=table_entry.first_name, last_name=table_entry.last_name, profile_pic=table_entry.profile_pic, online=table_entry.online, role=table_entry.role, created=table_entry.created) user.team_id = table_entry.team_id user.channel_id = table_entry.channel_id user.is_channel_creator = table_entry.creator == table_entry.user_id return user
def test_get_messages_from_non_existent_chat_returns_not_found(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 sys.modules[ "models.authentication"].Authenticator.authenticate_team.return_value = user sys.modules[ "daos.messages"].MessageDatabaseClient.get_chat_by_ids.return_value = None self.assertRaises(ChatNotFoundError, MessageService.get_messages_from_chat, data)
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_send_message_to_user_in_new_chat_works_properly(self): data = MagicMock() data.chat_id = 1 '''Mocked ouputs''' user = PublicUser(user_id=0, username="******", first_name="Test0", last_name="Test0") user.team_id = 0 user.team_role = TeamRoles.MEMBER.value receiver = MessageReceiver(user_id=1, team_id=0, is_user=True) def add_update_chat(chat): from tests.test_services import test_messages MockedMessageDatabase.stored_chats += [chat] def add_message(message): from tests.test_services import test_messages message.message_id = 0 MockedMessageDatabase.stored_messages += [message] return message sys.modules[ "models.authentication"].Authenticator.authenticate_team.return_value = user sys.modules[ "daos.messages"].MessageDatabaseClient.get_message_direct_receiver_by_ids.return_value = receiver sys.modules[ "daos.messages"].MessageDatabaseClient.get_chat_by_ids.return_value = None sys.modules[ "daos.messages"].MessageDatabaseClient.add_message = MagicMock( side_effect=add_message) sys.modules[ "daos.messages"].MessageDatabaseClient.add_or_update_chat = MagicMock( side_effect=add_update_chat) response = MessageService.send_message(data) self.assertIsInstance(response, SuccessfulMessageSentResponse) self.assertEqual(1, len(MockedMessageDatabase.stored_messages)) self.assertEqual(2, len(MockedMessageDatabase.stored_chats)) self.assertEqual(0, MockedMessageDatabase.stored_messages[0].message_id) self.assertEqual(MessageResponseStatus.SENT.value, response.json().get("status"))
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 test_send_message_to_not_member_from_team_returns_bad_request(self): data = MagicMock() data.chat_id = 1 '''Mocked ouputs''' user = PublicUser(user_id=0, username="******", first_name="Test0", last_name="Test0") user.team_id = 0 user.team_role = TeamRoles.MEMBER.value receiver = MessageReceiver(user_id=1, team_id=1, is_user=True) sys.modules[ "models.authentication"].Authenticator.authenticate_team.return_value = user sys.modules[ "daos.messages"].MessageDatabaseClient.get_message_direct_receiver_by_ids.return_value = receiver response = MessageService.send_message(data) self.assertIsInstance(response, BadRequestMessageSentResponse) self.assertEqual(TeamResponseStatus.USER_NOT_MEMBER.value, response.status)
def test_channel_regular_member_authenticating_to_channel_moderation_throws_exception( self): user_id = 0 team_id = 0 channel_id = 0 token = Authenticator.generate(user_id) authentication = MagicMock() authentication.token = token authentication.team_id = team_id authentication.channel_id = channel_id '''Mocked outputs''' user = User(user_id=user_id, token=token) team_user = PublicUser(user_id=user_id) team_user.team_role = TeamRoles.MEMBER.value team_user.team_id = team_id channel_user = PublicUser(user_id=user_id) channel_user.team_id = team_id channel_user.channel_id = channel_id channel_user.is_channel_creator = False sys.modules[ "daos.users"].UserDatabaseClient.get_user_by_id.return_value = user sys.modules[ "daos.users"].UserDatabaseClient.get_team_user_by_ids.return_value = team_user sys.modules[ "daos.users"].UserDatabaseClient.get_channel_user_by_ids.return_value = channel_user self.assertRaises(NoPermissionsError, Authenticator.authenticate_channel, authentication, TeamRoles.is_channel_creator) channel_user.is_channel_creator = True sys.modules[ "daos.users"].UserDatabaseClient.get_channel_user_by_ids.return_value = channel_user authenticated_user = Authenticator.authenticate_channel( authentication, TeamRoles.is_channel_creator) self.assertEqual(user_id, authenticated_user.id) self.assertEqual(team_id, authenticated_user.team_id) self.assertEqual(channel_id, authenticated_user.channel_id) self.assertTrue(authenticated_user.is_channel_creator)
def to_user_profile(cls, table_entries): user_with_teams = [] for table_entry in table_entries: user = PublicUser(user_id=table_entry.user_id, username=table_entry.username, email=table_entry.email, first_name=table_entry.first_name, last_name=table_entry.last_name, profile_pic=table_entry.profile_pic, online=True, role=table_entry.user_role, created=table_entry.created) user.team_id = table_entry.team_id user.team_name = table_entry.team_name user.team_picture = table_entry.picture user.team_location = table_entry.location user.team_description = table_entry.description user.team_welcome = table_entry.welcome_message user.team_role = table_entry.team_role user.team_messages = table_entry.team_messages or 0 user_with_teams += [user] return user_with_teams
def test_get_preview_messages_from_user_without_any_chats_return_empty_list( self): data = MagicMock() '''Mocked ouputs''' user = PublicUser(user_id=0) user.team_id = 0 user.team_role = TeamRoles.MEMBER.value direct_messages = [] channel_messages = [] sys.modules[ "models.authentication"].Authenticator.authenticate_team.return_value = user sys.modules[ "daos.messages"].MessageDatabaseClient.get_direct_messages_previews.return_value = direct_messages sys.modules[ "daos.messages"].MessageDatabaseClient.get_channel_messages_previews.return_value = channel_messages response = MessageService.get_preview_messages(data) self.assertIsInstance(response, ChatsListResponse) self.assertEqual(0, len(response.chats)) self.assertEqual(MessageResponseStatus.LIST.value, response.json().get("status"))
def to_channel_members(cls, users_entries): users = [] for user_entry in users_entries: users += [ PublicUser(user_id=user_entry.user_id, username=user_entry.username, email=user_entry.email, first_name=user_entry.first_name, last_name=user_entry.last_name, profile_pic=user_entry.profile_pic, role=user_entry.role, online=user_entry.online) ] return users
def test_notify_team_role_change_sends_notification(self): user_team = TeamUser(user_id=1, team_id=0, role=TeamRoles.MODERATOR.value) admin_id = 0 '''Mocked outputs''' admin = PublicUser(user_id=0, username="******", first_name="Test0", last_name="Test0") old_role = TeamRoles.MEMBER.value 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 = admin sys.modules[ "daos.teams"].TeamDatabaseClient.get_team_by_id.return_value = team sys.modules["pyfcm"].FCMNotification( ).notify_topic_subscribers = MagicMock(side_effect=send_notification) NotificationService.notify_change_role(user_team, old_role, admin_id) self.assertEqual(1, MockedNotificationServer.notification.topic_name) self.assertEqual("Hypechat", MockedNotificationServer.notification.message_title) self.assertEqual("You have been upgraded in team Test-Team!", MockedNotificationServer.notification.message_body) self.assertEqual( "Test-Team", MockedNotificationServer.notification.data_message.get( "team_name")) self.assertEqual( 0, MockedNotificationServer.notification.data_message.get("admin_id")) self.assertEqual( NotificationType.TEAM_ROLE_CHANGE.value, MockedNotificationServer.notification.data_message.get( "notification_type"))
def to_team_user(cls, table_entry): if table_entry is None: return None if table_entry.team_role == TeamRoles.BOT.value: user = PublicUser(user_id=table_entry.user_id, username=table_entry.bot_name) user.team_id = table_entry.team_id user.team_role = table_entry.team_role else: user = PublicUser(user_id=table_entry.user_id, username=table_entry.username, email=table_entry.email, first_name=table_entry.first_name, last_name=table_entry.last_name, profile_pic=table_entry.profile_pic, role=table_entry.user_role, online=table_entry.online, created=table_entry.created) user.team_id = table_entry.team_id user.team_role = table_entry.team_role return user
def test_get_preview_messages_from_user_with_non_emtpy_chat_lists_work_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 sender1 = UserMessageSender(user_id=0, username="******", first_name="Test0", last_name="Test0") direct1 = PreviewDirectMessage(message_id=0, sender=sender1, receiver_id=1, chat_name="Tester0", offset=1, content="Testeando1", message_type="TEXT", chat_online=False, chat_picture=None, timestamp=datetime.now() + timedelta(hours=-3)) sender2 = BotMessageSender(bot_id=5, bot_name="Test-Bot") direct2 = PreviewDirectMessage(message_id=1, sender=sender2, receiver_id=0, chat_name="Test-Bot", offset=0, content="Testeando2", message_type="TEXT", chat_online=False, chat_picture=None, timestamp=datetime.now() + timedelta(hours=-2)) sender3 = UserMessageSender(user_id=0, username="******", first_name="Test0", last_name="Test0") channel1 = PreviewChannelMessage(message_id=2, chat_id=3, chat_name="Channel-Test1", chat_picture=None, sender=sender3, content="Testeando3", message_type="TEXT", offset=0, timestamp=datetime.now() + timedelta(hours=-1)) sender4 = BotMessageSender(bot_id=5, bot_name="Test-Bot") channel2 = PreviewChannelMessage(message_id=3, chat_id=4, chat_name="Channel-Test2", chat_picture=None, sender=sender4, content="Testeando4", message_type="TEXT", offset=0, timestamp=datetime.now() + timedelta(hours=0)) direct_messages = [direct1, direct2] channel_messages = [channel1, channel2] sys.modules[ "models.authentication"].Authenticator.authenticate_team.return_value = user sys.modules[ "daos.messages"].MessageDatabaseClient.get_direct_messages_previews.return_value = direct_messages sys.modules[ "daos.messages"].MessageDatabaseClient.get_channel_messages_previews.return_value = channel_messages response = MessageService.get_preview_messages(data) self.assertIsInstance(response, ChatsListResponse) self.assertEqual(4, len(response.chats)) self.assertEqual(4, response.chats[0].get("chat_id")) self.assertEqual(False, response.chats[0].get("unseen")) self.assertEqual(5, response.chats[0].get("sender").get("id")) self.assertEqual(3, response.chats[1].get("chat_id")) self.assertEqual(False, response.chats[1].get("unseen")) self.assertEqual(0, response.chats[1].get("sender").get("id")) self.assertEqual(5, response.chats[2].get("chat_id")) self.assertEqual(False, response.chats[2].get("unseen")) self.assertEqual(5, response.chats[2].get("sender").get("id")) self.assertEqual(1, response.chats[3].get("chat_id")) self.assertEqual(True, response.chats[3].get("unseen")) self.assertEqual(0, response.chats[3].get("sender").get("id")) self.assertEqual(MessageResponseStatus.LIST.value, response.json().get("status"))