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_member_authenticates_just_to_channels_he_belongs(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
        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)
    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)
Example #6
0
    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_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)
Example #8
0
 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_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_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_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)
Example #13
0
 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 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"))