Ejemplo n.º 1
0
class TestChannels(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.channels_add_owner('GENERAL', 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')

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

    def test_channels_list(self):
        channels_list = self.rocket.channels_list().json()
        self.assertTrue(channels_list.get('success'))
        self.assertIn('channels', channels_list)

    def test_channels_list_joined(self):
        channels_list_joined = self.rocket.channels_list_joined().json()
        self.assertTrue(channels_list_joined.get('success'))
        self.assertIn('channels', channels_list_joined)

    def test_channels_info(self):
        channels_info = self.rocket.channels_info(room_id='GENERAL').json()
        self.assertTrue(channels_info.get('success'))
        self.assertIn('channel', channels_info)
        self.assertEqual(channels_info.get('channel').get('_id'), 'GENERAL')
        channel_name = channels_info.get('channel').get('name')
        channels_info = self.rocket.channels_info(channel=channel_name).json()
        self.assertTrue(channels_info.get('success'))
        self.assertIn('channel', channels_info)
        self.assertEqual(channels_info.get('channel').get('_id'), 'GENERAL')
        self.assertEqual(
            channels_info.get('channel').get('name'), channel_name)

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

    def test_channels_history(self):
        channels_history = self.rocket.channels_history(
            room_id='GENERAL').json()
        self.assertTrue(channels_history.get('success'))
        self.assertIn('messages', channels_history)

    def test_channels_add_all(self):
        channels_add_all = self.rocket.channels_add_all('GENERAL').json()
        self.assertTrue(channels_add_all.get('success'))

    def test_channels_add_and_remove_moderator(self):
        me = self.rocket.me().json()
        channels_add_moderator = self.rocket.channels_add_moderator(
            'GENERAL', me.get('_id')).json()
        self.assertTrue(channels_add_moderator.get('success'))
        channels_remove_moderator = self.rocket.channels_remove_moderator(
            'GENERAL', me.get('_id')).json()
        self.assertTrue(channels_remove_moderator.get('success'))

    def test_channels_add_and_remove_owner(self):
        channels_add_owner = self.rocket.channels_add_owner(
            'GENERAL', user_id=self.testuser_id).json()
        self.assertTrue(channels_add_owner.get('success'),
                        channels_add_owner.get('error'))
        channels_remove_owner = self.rocket.channels_remove_owner(
            'GENERAL', user_id=self.testuser_id).json()
        self.assertTrue(channels_remove_owner.get('success'),
                        channels_remove_owner.get('error'))

        with self.assertRaises(RocketMissingParamException):
            self.rocket.channels_add_owner(room_id='GENERAL')

    def test_channels_archive_unarchive(self):
        channels_archive = self.rocket.channels_archive('GENERAL').json()
        self.assertTrue(channels_archive.get('success'))
        channels_unarchive = self.rocket.channels_unarchive('GENERAL').json()
        self.assertTrue(channels_unarchive.get('success'))

    def test_channels_close_open(self):
        channels_close = self.rocket.channels_close('GENERAL').json()
        self.assertTrue(channels_close.get('success'))
        channels_open = self.rocket.channels_open('GENERAL').json()
        self.assertTrue(channels_open.get('success'))

    def test_channels_create_delete(self):
        name = str(uuid.uuid1())
        channels_create = self.rocket.channels_create(name).json()
        self.assertTrue(channels_create.get('success'))
        self.assertEqual(name, channels_create.get('channel').get('name'))
        channels_delete = self.rocket.channels_delete(channel=name).json()
        self.assertTrue(channels_delete.get('success'))
        channels_create = self.rocket.channels_create(name).json()
        self.assertTrue(channels_create.get('success'))
        room_id = channels_create.get('channel').get('_id')
        channels_delete = self.rocket.channels_delete(room_id=room_id).json()
        self.assertTrue(channels_delete.get('success'))

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

    def test_channels_get_integrations(self):
        channels_get_integrations = self.rocket.channels_get_integrations(
            room_id='GENERAL').json()
        self.assertTrue(channels_get_integrations.get('success'))

    def test_channels_invite(self):
        channels_invite = self.rocket.channels_invite('GENERAL',
                                                      self.testuser_id).json()
        self.assertTrue(channels_invite.get('success'))

    def test_channels_kick(self):
        channels_kick = self.rocket.channels_kick('GENERAL',
                                                  self.testuser_id).json()
        self.assertTrue(channels_kick.get('success'))

    def test_channels_leave(self):
        channels_leave = self.rocket.channels_leave('GENERAL').json()
        self.assertFalse(channels_leave.get('success'))
        self.assertEqual(channels_leave.get('errorType'),
                         'error-you-are-last-owner')

        name = str(uuid.uuid1())
        channels_create = self.rocket.channels_create(name).json()
        self.rocket.channels_invite(
            room_id=channels_create.get('channel').get('_id'),
            user_id=self.testuser_id)
        self.rocket.channels_add_owner(
            channels_create.get('channel').get('_id'),
            user_id=self.testuser_id).json()
        channels_leave = self.rocket.channels_leave(
            channels_create.get('channel').get('_id')).json()
        self.assertTrue(channels_leave.get('success'))

    def test_channels_rename(self):
        name = str(uuid.uuid1())
        name2 = str(uuid.uuid1())
        channels_create = self.rocket.channels_create(name).json()
        channels_rename = self.rocket.channels_rename(
            room_id=channels_create.get('channel').get('_id'),
            name=name2).json()
        self.assertTrue(channels_rename.get('success'))
        self.assertEqual(channels_rename.get('channel').get('name'), name2)

    def test_channels_set_description(self):
        description = str(uuid.uuid1())
        channels_set_description = self.rocket.channels_set_description(
            'GENERAL', description).json()
        self.assertTrue(channels_set_description.get('success'))
        self.assertEqual(channels_set_description.get('description'),
                         description, 'Description does not match')

    def test_channels_set_join_code(self):
        join_code = str(uuid.uuid1())
        channels_set_join_code = self.rocket.channels_set_join_code(
            'GENERAL', join_code).json()
        self.assertTrue(channels_set_join_code.get('success'))

    def test_channels_set_read_only(self):
        channels_set_read_only = self.rocket.channels_set_read_only(
            'GENERAL', True).json()
        self.assertTrue(channels_set_read_only.get('success'))
        channels_set_read_only = self.rocket.channels_set_read_only(
            'GENERAL', False).json()
        self.assertTrue(channels_set_read_only.get('success'))

    def test_channels_set_topic(self):
        topic = str(uuid.uuid1())
        channels_set_topic = self.rocket.channels_set_topic('GENERAL',
                                                            topic).json()
        self.assertTrue(channels_set_topic.get('success'))
        self.assertEqual(channels_set_topic.get('topic'), topic,
                         'Topic does not match')

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

        channels_set_type = self.rocket.channels_set_type(
            channels_create.get('channel').get('_id'), 'p').json()
        self.assertTrue(channels_set_type.get('success'))
        self.assertTrue(channels_set_type.get('channel').get('t'), 'p')

        channels_set_type = self.rocket.channels_set_type(
            channels_create.get('channel').get('_id'), 'c').json()
        # should fail because this is no more a channel
        self.assertFalse(channels_set_type.get('success'))

    def test_channels_set_announcement(self):
        announcement = str(uuid.uuid1())
        channels_set_announcement = self.rocket.channels_set_announcement(
            'GENERAL', announcement).json()
        self.assertTrue(channels_set_announcement.get('success'))
        self.assertEqual(channels_set_announcement.get('announcement'),
                         announcement, 'Topic does not match')

    def test_channels_set_custom_fields(self):
        cf = {'key': 'value'}
        channels_set_custom_fields = self.rocket.channels_set_custom_fields(
            'GENERAL', cf).json()
        self.assertTrue(channels_set_custom_fields.get('success'))
        self.assertEqual(cf,
                         channels_set_custom_fields['channel']['customFields'])

    def test_channels_members(self):
        channels_members = self.rocket.channels_members(
            room_id='GENERAL').json()
        self.assertTrue(channels_members.get('success'))
        channels_members = self.rocket.channels_members(
            channel='general').json()
        self.assertTrue(channels_members.get('success'))

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

    def test_channels_roles(self):
        channels_roles = self.rocket.channels_roles(room_id='GENERAL').json()
        self.assertTrue(channels_roles.get('success'))
        self.assertIsNotNone(channels_roles.get('roles'))
        channels_roles = self.rocket.channels_roles(room_name='general').json()
        self.assertTrue(channels_roles.get('success'))
        self.assertIsNotNone(channels_roles.get('roles'))

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

    def test_channels_files(self):
        channels_files = self.rocket.channels_files(room_id='GENERAL').json()
        self.assertTrue(channels_files.get('success'))

        channels_files = self.rocket.channels_files(room_name='general').json()
        self.assertTrue(channels_files.get('success'))

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

    def test_channels_get_all_user_mentions_by_channel(self):
        channels_get_all_user_mentions_by_channel = self.rocket.channels_get_all_user_mentions_by_channel(
            room_id='GENERAL').json()
        self.assertTrue(
            channels_get_all_user_mentions_by_channel.get('success'))
    args = parse_arguments()

    config = yaml.load(open(args.config))
    papers = read_papers(args.papers)

    with sessions.Session() as session:
        rocket = RocketChat(
            config["username"],
            config["password"],
            server_url=config["server"],
            session=session,
        )

        for paper in papers:
            channel_name = "paper_" + paper["UID"]
            if not args.test:
                created = rocket.channels_create(channel_name).json()
                print(channel_name, created)
            channel_id = rocket.channels_info(
                channel=channel_name).json()["channel"]["_id"]

            # Change to topic of papers.
            topic = "%s - %s" % (
                paper["title"],
                paper["authors"],
            )
            if not args.test:
                rocket.channels_set_topic(channel_id, topic).json()

            print("Creating " + channel_name + " topic " + topic)
Ejemplo n.º 3
0
class TestChannels(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.channels_add_owner('GENERAL', username=self.user)
        self.rocket = RocketChat(self.user, self.password)
        self.testuser = self.rocket.users_create('*****@*****.**',
                                                 'testuser1', 'password',
                                                 'testuser1').json()
        if not self.testuser.get('success'):
            user_id = self.rocket.users_info(
                username='******').json().get('user').get('_id')
            self.rocket.users_delete(user_id)
            self.testuser = self.rocket.users_create('*****@*****.**',
                                                     'testuser1', 'password',
                                                     'testuser1').json()

    def tearDown(self):
        self.rocket.users_delete(self.testuser.get('user').get('_id'))

    def test_channels_list(self):
        channels_list = self.rocket.channels_list().json()
        self.assertTrue(channels_list.get('success'))
        self.assertIn('channels', channels_list)

    def test_channels_list_joined(self):
        channels_list_joined = self.rocket.channels_list_joined().json()
        self.assertTrue(channels_list_joined.get('success'))
        self.assertIn('channels', channels_list_joined)

    def test_channels_info(self):
        channels_info = self.rocket.channels_info(room_id='GENERAL').json()
        self.assertTrue(channels_info.get('success'))
        self.assertIn('channel', channels_info)
        self.assertEqual(channels_info.get('channel').get('_id'), 'GENERAL')

    def test_channels_history(self):
        channels_history = self.rocket.channels_history(
            room_id='GENERAL').json()
        self.assertTrue(channels_history.get('success'))
        self.assertIn('messages', channels_history)

    def test_channels_add_all(self):
        channels_add_all = self.rocket.channels_add_all('GENERAL').json()
        self.assertTrue(channels_add_all.get('success'))

    def test_channels_add_and_remove_moderator(self):
        me = self.rocket.me().json()
        channels_add_moderator = self.rocket.channels_add_moderator(
            'GENERAL', me.get('_id')).json()
        self.assertTrue(channels_add_moderator.get('success'))
        channels_remove_moderator = self.rocket.channels_remove_moderator(
            'GENERAL', me.get('_id')).json()
        self.assertTrue(channels_remove_moderator.get('success'))

    def test_channels_add_and_remove_owner(self):
        channels_add_owner = self.rocket.channels_add_owner(
            'GENERAL', user_id=self.testuser.get('user').get('_id')).json()
        self.assertTrue(channels_add_owner.get('success'),
                        channels_add_owner.get('error'))
        channels_remove_owner = self.rocket.channels_remove_owner(
            'GENERAL', user_id=self.testuser.get('user').get('_id')).json()
        self.assertTrue(channels_remove_owner.get('success'),
                        channels_remove_owner.get('error'))

    def test_channels_archive_unarchive(self):
        channels_archive = self.rocket.channels_archive('GENERAL').json()
        self.assertTrue(channels_archive.get('success'))
        channels_unarchive = self.rocket.channels_unarchive('GENERAL').json()
        self.assertTrue(channels_unarchive.get('success'))

    def test_channels_close_open(self):
        channels_close = self.rocket.channels_close('GENERAL').json()
        self.assertTrue(channels_close.get('success'))
        channels_open = self.rocket.channels_open('GENERAL').json()
        self.assertTrue(channels_open.get('success'))

    def test_channels_create(self):
        name = str(uuid.uuid1())
        channels_create = self.rocket.channels_create(name).json()
        self.assertTrue(channels_create.get('success'))
        self.assertEqual(name, channels_create.get('channel').get('name'))

    def test_channels_get_integrations(self):
        channels_get_integrations = self.rocket.channels_get_integrations(
            room_id='GENERAL').json()
        self.assertTrue(channels_get_integrations.get('success'))

    def test_channels_invite(self):
        channels_invite = self.rocket.channels_invite(
            'GENERAL',
            self.testuser.get('user').get('_id')).json()
        self.assertTrue(channels_invite.get('success'))
        self.assertIn(
            self.testuser.get('user').get('username'),
            channels_invite.get('channel').get('usernames'))

    def test_channels_kick(self):
        channels_kick = self.rocket.channels_kick(
            'GENERAL',
            self.testuser.get('user').get('_id')).json()
        self.assertTrue(channels_kick.get('success'))
        self.assertNotIn(
            self.testuser.get('user').get('username'),
            channels_kick.get('channel').get('usernames'))

    def test_channels_leave(self):
        channels_leave = self.rocket.channels_leave('GENERAL').json()
        self.assertFalse(channels_leave.get('success'))
        self.assertEqual(channels_leave.get('errorType'),
                         'error-you-are-last-owner')

        name = str(uuid.uuid1())
        channels_create = self.rocket.channels_create(name).json()
        self.rocket.channels_invite(
            room_id=channels_create.get('channel').get('_id'),
            user_id=self.testuser.get('user').get('_id'))
        self.rocket.channels_add_owner(
            channels_create.get('channel').get('_id'),
            user_id=self.testuser.get('user').get('_id')).json()
        channels_leave = self.rocket.channels_leave(
            channels_create.get('channel').get('_id')).json()
        self.assertTrue(channels_leave.get('success'))

    def test_channels_rename(self):
        name = str(uuid.uuid1())
        name2 = str(uuid.uuid1())
        channels_create = self.rocket.channels_create(name).json()
        channels_rename = self.rocket.channels_rename(
            room_id=channels_create.get('channel').get('_id'),
            name=name2).json()
        self.assertTrue(channels_rename.get('success'))
        self.assertEqual(channels_rename.get('channel').get('name'), name2)

    def test_channels_set_description(self):
        description = str(uuid.uuid1())
        channels_set_description = self.rocket.channels_set_description(
            'GENERAL', description).json()
        self.assertTrue(channels_set_description.get('success'))
        self.assertEqual(channels_set_description.get('description'),
                         description, 'Description does not match')

    def test_channels_set_join_code(self):
        join_code = str(uuid.uuid1())
        channels_set_join_code = self.rocket.channels_set_join_code(
            'GENERAL', join_code).json()
        self.assertTrue(channels_set_join_code.get('success'))

    def test_channels_set_read_only(self):
        channels_set_read_only = self.rocket.channels_set_read_only(
            'GENERAL', True).json()
        self.assertTrue(channels_set_read_only.get('success'))
        channels_set_read_only = self.rocket.channels_set_read_only(
            'GENERAL', False).json()
        self.assertTrue(channels_set_read_only.get('success'))

    def test_channels_set_topic(self):
        topic = str(uuid.uuid1())
        channels_set_topic = self.rocket.channels_set_topic('GENERAL',
                                                            topic).json()
        self.assertTrue(channels_set_topic.get('success'))
        self.assertEqual(channels_set_topic.get('topic'), topic,
                         'Topic does not match')

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

        channels_set_type = self.rocket.channels_set_type(
            channels_create.get('channel').get('_id'), 'p').json()
        self.assertTrue(channels_set_type.get('success'))
        self.assertTrue(channels_set_type.get('channel').get('t'), 'p')

        channels_set_type = self.rocket.channels_set_type(
            channels_create.get('channel').get('_id'), 'c').json()
        self.assertFalse(channels_set_type.get(
            'success'))  # should fail because this is no more a channel