Пример #1
0
class ChatManager(object):
    def __init__(self,
                 username="",
                 password="",
                 server_url="",
                 ssl_verify=False):
        self.rocket = RocketChat(
            user=username,
            password=password,
            server_url=server_url,
            ssl_verify=ssl_verify,
        )

    def create_user(self, user, password):
        # Create the user's account on RocketChat
        if not self.rocket:
            return
        account = self.rocket.users_create(user.email, user.name, password,
                                           user.handle.replace(" ",
                                                               "_")).json()
        self.create_team(user.team, account)

    def create_team(self, team, account):
        # Create a private team group
        if options.teams:
            group = self.has_group(team)
            if not group:
                groups = self.rocket.groups_create(
                    team.name.replace(" ", "_").lower()).json()
                group = groups["group"]
            self.rocket.groups_invite(group["_id"], account["user"]["_id"])

    def has_group(self, team):
        if not team:
            return False
        privaterooms = self.rocket.groups_list().json()
        if "groups" in privaterooms:
            for group in privaterooms["groups"]:
                if group["name"] == team.name.replace(" ", "_").lower():
                    return group
        return False

    def post_message(self, message, channel=None):
        # Send a global message
        if not channel:
            default_channel = self.get_default_channel()
            if default_channel:
                channel = "#%s" % default_channel
        if channel:
            self.rocket.chat_post_message(channel=channel, text=message)

    def get_default_channel(self):
        # Get the default channel
        channels = self.rocket.channels_list().json()
        if "channels" in channels:
            for channel in channels["channels"]:
                if channel["default"]:
                    return channel["name"]
        return None
Пример #2
0
 def addUserGroup(self, roomID, userID):
     rocket = RocketChat('Admin', 'chat.service', server_url='http://www.chat.service', proxies=None)
     data = rocket.groups_invite(roomID, userID).json()
     status = data["success"]
     roomId = data["group"]['_id']
     return (status, roomID)
Пример #3
0
class TestGroups(unittest.TestCase):
    def setUp(self):
        self.rocket = RocketChat()
        self.user = '******'
        self.password = '******'
        self.email = '*****@*****.**'
        self.rocket.users_register(email=self.email,
                                   name=self.user,
                                   password=self.password,
                                   username=self.user)
        self.rocket = RocketChat(self.user, self.password)
        testuser = self.rocket.users_info(username='******').json()
        if not testuser.get('success'):
            testuser = self.rocket.users_create('*****@*****.**',
                                                'testuser1', 'password',
                                                'testuser1').json()
            if not testuser.get('success'):
                self.fail("can't create test user")

        self.testuser_id = testuser.get('user').get('_id')
        self.test_group_name = str(uuid.uuid1())
        self.test_group_id = self.rocket.groups_create(
            self.test_group_name).json().get('group').get('_id')

    def tearDown(self):
        self.rocket.users_delete(self.testuser_id)

    def test_groups_list_all(self):
        groups_list = self.rocket.groups_list_all().json()
        self.assertTrue(groups_list.get('success'))
        self.assertIn('groups', groups_list)

    def test_groups_list(self):
        groups_list = self.rocket.groups_list().json()
        self.assertTrue(groups_list.get('success'))
        self.assertIn('groups', groups_list)

    def test_groups_info(self):
        groups_info_by_id = self.rocket.groups_info(
            room_id=self.test_group_id).json()
        self.assertTrue(groups_info_by_id.get('success'))
        self.assertIn('group', groups_info_by_id)
        self.assertEqual(
            groups_info_by_id.get('group').get('_id'), self.test_group_id)

        groups_info_by_name = self.rocket.groups_info(
            room_name=self.test_group_name).json()
        self.assertTrue(groups_info_by_name.get('success'))
        self.assertIn('group', groups_info_by_name)
        self.assertEqual(
            groups_info_by_name.get('group').get('_id'), self.test_group_id)

        with self.assertRaises(RocketMissingParamException):
            self.rocket.groups_info()

    def test_groups_history(self):
        groups_history = self.rocket.groups_history(
            room_id=self.test_group_id).json()
        self.assertTrue(groups_history.get('success'))
        self.assertIn('messages', groups_history)

    def test_groups_add_and_remove_moderator(self):
        me = self.rocket.me().json()
        groups_add_moderator = self.rocket.groups_add_moderator(
            self.test_group_id, me.get('_id')).json()
        self.assertTrue(groups_add_moderator.get('success'))
        groups_remove_moderator = self.rocket.groups_remove_moderator(
            self.test_group_id, me.get('_id')).json()
        self.assertTrue(groups_remove_moderator.get('success'))

    def test_groups_add_and_remove_owner(self):
        self.rocket.groups_invite(self.test_group_id, self.testuser_id)
        groups_add_owner = self.rocket.groups_add_owner(
            self.test_group_id, user_id=self.testuser_id).json()
        self.assertTrue(groups_add_owner.get('success'),
                        groups_add_owner.get('error'))

        groups_remove_owner = self.rocket.groups_remove_owner(
            self.test_group_id, user_id=self.testuser_id).json()
        self.assertTrue(groups_remove_owner.get('success'),
                        groups_remove_owner.get('error'))

    def test_groups_archive_unarchive(self):
        groups_archive = self.rocket.groups_archive(self.test_group_id).json()
        self.assertTrue(groups_archive.get('success'))
        groups_unarchive = self.rocket.groups_unarchive(
            self.test_group_id).json()
        self.assertTrue(groups_unarchive.get('success'))

    def test_groups_close_open(self):
        groups_close = self.rocket.groups_close(self.test_group_id).json()
        self.assertTrue(groups_close.get('success'))
        groups_open = self.rocket.groups_open(self.test_group_id).json()
        self.assertTrue(groups_open.get('success'))

    def test_groups_create_delete(self):
        name = str(uuid.uuid1())
        groups_create = self.rocket.groups_create(name).json()
        self.assertTrue(groups_create.get('success'))
        self.assertEqual(name, groups_create.get('group').get('name'))
        groups_delete = self.rocket.groups_delete(group=name).json()
        self.assertTrue(groups_delete.get('success'))
        groups_create = self.rocket.groups_create(name).json()
        self.assertTrue(groups_create.get('success'))
        room_id = groups_create.get('group').get('_id')
        groups_delete = self.rocket.groups_delete(room_id=room_id).json()
        self.assertTrue(groups_delete.get('success'))

        with self.assertRaises(RocketMissingParamException):
            self.rocket.groups_delete()

    def test_groups_get_integrations(self):
        groups_get_integrations = self.rocket.groups_get_integrations(
            room_id=self.test_group_id).json()
        self.assertTrue(groups_get_integrations.get('success'))

    def test_groups_invite(self):
        groups_invite = self.rocket.groups_invite(self.test_group_id,
                                                  self.testuser_id).json()
        self.assertTrue(groups_invite.get('success'))

    def test_groups_kick(self):
        id_group_created = self.rocket.groups_create(str(
            uuid.uuid1())).json().get('group').get('_id')
        groups_invite = self.rocket.groups_invite(id_group_created,
                                                  self.testuser_id).json()
        self.assertTrue(groups_invite.get('success'))
        groups_kick = self.rocket.groups_kick(id_group_created,
                                              self.testuser_id).json()
        self.assertTrue(groups_kick.get('success'))

    def test_groups_leave(self):
        groups_leave = self.rocket.groups_leave(self.test_group_id).json()
        self.assertFalse(groups_leave.get('success'))
        self.assertEqual(groups_leave.get('errorType'),
                         'error-you-are-last-owner')

        name = str(uuid.uuid1())
        groups_create = self.rocket.groups_create(name).json()
        self.rocket.groups_invite(
            room_id=groups_create.get('group').get('_id'),
            user_id=self.testuser_id)
        self.rocket.groups_add_owner(groups_create.get('group').get('_id'),
                                     user_id=self.testuser_id).json()
        groups_leave = self.rocket.groups_leave(
            groups_create.get('group').get('_id')).json()
        self.assertTrue(groups_leave.get('success'))

    def test_groups_rename(self):
        name = str(uuid.uuid1())
        name2 = str(uuid.uuid1())
        groups_create = self.rocket.groups_create(name).json()
        groups_rename = self.rocket.groups_rename(
            room_id=groups_create.get('group').get('_id'), name=name2).json()
        self.assertTrue(groups_rename.get('success'))
        self.assertEqual(groups_rename.get('group').get('name'), name2)

    def test_groups_set_description(self):
        description = str(uuid.uuid1())
        groups_set_description = self.rocket.groups_set_description(
            self.test_group_id, description).json()
        self.assertTrue(groups_set_description.get('success'))
        self.assertEqual(groups_set_description.get('description'),
                         description, 'Description does not match')

    def test_groups_set_read_only(self):
        groups_set_read_only = self.rocket.groups_set_read_only(
            self.test_group_id, True).json()
        self.assertTrue(groups_set_read_only.get('success'))
        groups_set_read_only = self.rocket.groups_set_read_only(
            self.test_group_id, False).json()
        self.assertTrue(groups_set_read_only.get('success'))

    def test_groups_set_topic(self):
        topic = str(uuid.uuid1())
        groups_set_topic = self.rocket.groups_set_topic(
            self.test_group_id, topic).json()
        self.assertTrue(groups_set_topic.get('success'))
        self.assertEqual(groups_set_topic.get('topic'), topic,
                         'Topic does not match')

    def test_groups_set_type(self):
        name = str(uuid.uuid1())
        groups_create = self.rocket.groups_create(name).json()
        self.assertTrue(groups_create.get('success'))

        groups_set_type = self.rocket.groups_set_type(
            groups_create.get('group').get('_id'), 'c').json()
        self.assertTrue(groups_set_type.get('success'))
        self.assertTrue(groups_set_type.get('group').get('t'), 'p')

        groups_set_type = self.rocket.groups_set_type(
            groups_create.get('group').get('_id'), 'p').json()
        # should fail because this is no more a group
        self.assertFalse(groups_set_type.get('success'))

    def test_groups_members(self):
        groups_members = self.rocket.groups_members(
            room_id=self.test_group_id).json()
        self.assertTrue(groups_members.get('success'))
        groups_members = self.rocket.groups_members(
            group=self.test_group_name).json()
        self.assertTrue(groups_members.get('success'))

        with self.assertRaises(RocketMissingParamException):
            self.rocket.groups_members()

    def test_groups_roles(self):
        name = str(uuid.uuid1())
        groups_create = self.rocket.groups_create(name).json()
        self.assertTrue(groups_create.get('success'))

        groups_roles = self.rocket.groups_roles(
            room_id=groups_create.get('group').get('_id')).json()
        self.assertTrue(groups_roles.get('success'))
        self.assertIsNotNone(groups_roles.get('roles'))

        groups_roles = self.rocket.groups_roles(room_name=name).json()
        self.assertTrue(groups_roles.get('success'))
        self.assertIsNotNone(groups_roles.get('roles'))

        with self.assertRaises(RocketMissingParamException):
            self.rocket.groups_roles()

    def test_groups_files(self):
        name = str(uuid.uuid1())
        groups_create = self.rocket.groups_create(name).json()
        self.assertTrue(groups_create.get('success'))

        groups_files = self.rocket.groups_files(
            room_id=groups_create.get('group').get('_id')).json()
        self.assertTrue(groups_files.get('success'))

        groups_files = self.rocket.groups_files(room_name=name).json()
        self.assertTrue(groups_files.get('success'))

        with self.assertRaises(RocketMissingParamException):
            self.rocket.groups_files()