Ejemplo n.º 1
0
    def update_information(cls, update_data):
        user = Authenticator.authenticate_channel(update_data.authentication, TeamRoles.is_channel_creator)
        channel = ChannelDatabaseClient.get_channel_by_id(user.channel_id)

        channel.name = \
            update_data.updated_channel["name"] if "name" in update_data.updated_channel else channel.name
        channel.visibility = \
            update_data.updated_channel["visibility"] if "visibility" in update_data.updated_channel \
                else channel.visibility
        channel.description = \
            update_data.updated_channel["description"] if "description" in update_data.updated_channel \
                else channel.description
        channel.welcome_message = \
            update_data.updated_channel["welcome_message"] if "welcome_message" in update_data.updated_channel \
                else channel.welcome_message

        try:
            ChannelDatabaseClient.update_channel(channel)
            DatabaseClient.commit()
            cls.logger().info(f"Channel {channel.channel_id} information updated by user {user.username}.")
            return SuccessfulChannelResponse(channel, ChannelResponseStatus.UPDATED.value)
        except IntegrityError:
            DatabaseClient.rollback()
            old_channel = ChannelDatabaseClient.get_channel_by_name(update_data.updated_team.get("name"))
            if old_channel is not None:
                cls.logger().info(f"Trying to update channel {user.channel_id}'s name with {old_channel.name}, "
                                  f"that currently exists.")
                return BadRequestChannelMessageResponse(f"Name {old_channel.name} is already in use!",
                                                        TeamResponseStatus.ALREADY_REGISTERED.value)
            else:
                cls.logger().error(f"Couldn't update channel {user.channel_id} information.")
                return UnsuccessfulTeamMessageResponse("Couldn't update channel information!")
    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)
Ejemplo n.º 3
0
    def delete_channel(cls, user_data):
        user = Authenticator.authenticate_channel(user_data)
        channel = ChannelDatabaseClient.get_channel_by_id(user.channel_id)

        try:
            ChannelDatabaseClient.delete_channel(channel)
            DatabaseClient.commit()
            cls.logger().info(f"Channel #{channel.channel_id} deleted by user {user.username}, with all messages.")
            return SuccessfulChannelMessageResponse("Channel removed!", ChannelResponseStatus.REMOVED.value)
        except IntegrityError:
            DatabaseClient.rollback()
            cls.logger().error(f"User #{user.id} couldn't remove channel #{user.channel_id}.")
            return UnsuccessfulChannelMessageResponse("Couldn't remove channel.")
Ejemplo n.º 4
0
    def leave_channel(cls, user_data):
        user = Authenticator.authenticate_channel(user_data)
        delete_user = ChannelDatabaseClient.get_user_in_channel_by_ids(user.id, user.channel_id)

        try:
            ChannelDatabaseClient.delete_channel_user(delete_user)
            DatabaseClient.commit()
            cls.logger().info(f"User #{user.id} leaved channel #{user.channel_id}.")
            return SuccessfulChannelMessageResponse("Channel leaved!", ChannelResponseStatus.REMOVED.value)
        except IntegrityError:
            DatabaseClient.rollback()
            cls.logger().error(f"User #{user.id} failing to leave channel #{userchannel_id}.")
            return UnsuccessfulTeamMessageResponse("Couldn't leave chanel.")
Ejemplo n.º 5
0
    def remove_member(cls, delete_data):
        user = Authenticator.authenticate_channel(delete_data.authentication, TeamRoles.is_channel_creator)
        delete_user = ChannelDatabaseClient.get_user_in_channel_by_ids(delete_data.delete_id, user.channel_id)

        if delete_user is not None:
            try:
                ChannelDatabaseClient.delete_channel_user(delete_user)
                DatabaseClient.commit()
                cls.logger().info(f"User #{delete_user.user_id} removed from channel #{delete_user.channel_id}.")
                return SuccessfulChannelMessageResponse("User removed!", ChannelResponseStatus.REMOVED.value)
            except IntegrityError:
                DatabaseClient.rollback()
                cls.logger().error(f"Failed at removing user #{delete_user.user_id} from channel "
                                   f"#{delete_user.channel_id}.")
                return UnsuccessfulChannelMessageResponse("Couldn't remove user.")
        else:
            cls.logger().info(f"User {user.id} trying to delete user #{delete_data.delete_id} from channel "
                              f"#{user.channel_id}, but it's not part of it!")
            return BadRequestChannelMessageResponse("User not part of channel!.",
                                                    ChannelResponseStatus.USER_NOT_MEMBER.value)
    def test_admin_user_authenticates_to_every_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, role=UserRoles.ADMIN.value)

        sys.modules[
            "daos.users"].UserDatabaseClient.get_user_by_id.return_value = 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(UserRoles.ADMIN.value, authenticated_user.role)
    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)
Ejemplo n.º 8
0
    def add_member(cls, invitation_data):
        user = Authenticator.authenticate_channel(invitation_data.authentication, TeamRoles.is_channel_creator)

        invited_user = UserDatabaseClient.get_user_by_id(invitation_data.user_invited_id)
        if invited_user is None:
            cls.logger().info(f"Trying to add user an nonexistent user to channel #{user.channel_id}.")
            return BadRequestChannelMessageResponse("Invited user not found!", UserResponseStatus.USER_NOT_FOUND.value)

        invited_user_in_team = TeamDatabaseClient.get_user_in_team_by_ids(invited_user.id, user.team_id)
        if invited_user_in_team is None:
            cls.logger().info(f"Trying to add user {invited_user.id} to channel #{user.channel_id}, but it's not part "
                              f"of the team #{user.team_id}.")
            return BadRequestChannelMessageResponse("User not part of the team!",
                                                    TeamResponseStatus.USER_NOT_MEMBER.value)

        try:
            new_channel_user = ChannelUser(user_id=invited_user.id, channel_id=user.channel_id)
            ChannelDatabaseClient.add_channel_user(new_channel_user)
            new_chat = Chat(user_id=invited_user.id, chat_id=user.channel_id, team_id=user.team_id)
            MessageDatabaseClient.add_chat(new_chat)
            DatabaseClient.commit()
            NotificationService.notify_channel_invitation(new_channel_user, user.id)
            cls.logger().info(f"User #{invited_user.id} added to channel #{user.channel_id} by {user.username}.")

        except IntegrityError:
            DatabaseClient.rollback()
            already_member = ChannelDatabaseClient.get_user_in_channel_by_ids(invited_user.id, user.channel_id)

            if already_member is not None:
                cls.logger().info(f"Failing to add user #{invited_user.id} to channel #{user.channel_id}. "
                                  f"The user is already part of it.")
                return BadRequestChannelMessageResponse(f"User already registered in channel.",
                                                        TeamResponseStatus.ALREADY_REGISTERED.value)
            else:
                cls.logger().error(f"Failing to add user #{invitation_data.user_invited_id} to channel"
                                   f"{invitation_data.authentication.channel_id}.")
                return UnsuccessfulChannelMessageResponse("Couldn't add user to channel.")
        else:
            return SuccessfulChannelMessageResponse("User added!", ChannelResponseStatus.ADDED.value)
Ejemplo n.º 9
0
 def channel_members(cls, authentication_data):
     user = Authenticator.authenticate_channel(authentication_data)
     members = ChannelDatabaseClient.get_all_channel_users_by_channel_id(user.channel_id)
     cls.logger().info(f"User {user.username} got {len(members)} users from channel #{user.channel_id}.")
     return SuccessfulUsersListResponse(cls._channel_users_list(members))