Пример #1
0
    def test_init(self):
        """Test initialization"""

        client = MattermostClient('https://mattermost.example.com/', 'aaaa')
        self.assertEqual(client.base_url, 'https://mattermost.example.com')
        self.assertEqual(client.api_token, 'aaaa')
        self.assertEqual(client.max_items, 60)
        self.assertFalse(client.sleep_for_rate)
        self.assertEqual(client.min_rate_to_sleep, 10)
        self.assertEqual(client.archive, None)
        self.assertFalse(client.from_archive)
        self.assertTrue(client.ssl_verify)

        client = MattermostClient('https://mattermost.example.com/',
                                  'aaaa',
                                  max_items=5,
                                  sleep_for_rate=True,
                                  min_rate_to_sleep=5,
                                  sleep_time=3,
                                  ssl_verify=False)
        self.assertEqual(client.base_url, 'https://mattermost.example.com')
        self.assertEqual(client.api_token, 'aaaa')
        self.assertEqual(client.max_items, 5)
        self.assertTrue(client.sleep_for_rate)
        self.assertEqual(client.min_rate_to_sleep, 5)
        self.assertEqual(client.sleep_time, 3)
        self.assertFalse(client.ssl_verify)
    def test_posts_with_pagination(self):
        """Test posts API call using pagination"""

        http_requests = setup_http_server()

        client = MattermostClient('https://mattermost.example.com/',
                                  'aaaa',
                                  max_items=5)

        # Call API with parameters
        posts = client.posts('abcdefghijkl', page=1)

        expected = [{
            'page': ['1'],
            'per_page': ['5'],
        }]

        self.assertEqual(len(http_requests), 1)

        for x in range(0, len(http_requests)):
            req = http_requests[x]
            self.assertEqual(req.method, 'GET')
            self.assertRegex(req.path, '/api/v4/channels/abcdefghijkl/posts')
            self.assertDictEqual(req.querystring, expected[x])
            self.assertEqual(req.headers['Authorization'], 'Bearer aaaa')
    def test_channel(self):
        """Test channel API call"""

        http_requests = setup_http_server()

        client = MattermostClient('https://mattermost.example.com/', 'aaaa')

        # Call API
        channel = client.channel('abcdefghijkl')

        self.assertEqual(len(http_requests), 1)

        expected = [
            {
                'channel_id': ['abcdefghijkl']
            }
        ]

        self.assertEqual(len(http_requests), 1)

        for x in range(0, len(http_requests)):
            req = http_requests[x]
            self.assertEqual(req.method, 'GET')
            self.assertRegex(req.path, '/api/v4/channels/abcdefghijkl')
            self.assertDictEqual(req.querystring, expected[x])
            self.assertEqual(req.headers['Authorization'], 'Bearer aaaa')
    def test_calculate_time_to_reset(self):
        """Test whether the time to reset is zero if the sleep time is negative"""

        user = read_file('data/mattermost/mattermost_user_sduenas.json', 'rb')
        httpretty.register_uri(httpretty.GET,
                               MATTERMOST_USER_SDUENAS,
                               body=user,
                               status=200,
                               forcing_headers={
                                   'X-RateLimit-Remaining': '20',
                                   'X-RateLimit-Reset': int(datetime_utcnow().replace(microsecond=0).timestamp())
                               })

        client = MattermostClient('https://mattermost.example.com/', 'aaaa')
        _ = client.user('8tbwn7uikpdy3gpse6fgiie5co')

        time_to_reset = client.calculate_time_to_reset()

        self.assertEqual(time_to_reset, 0)
    def test_user(self):
        """Test user API call"""

        http_requests = setup_http_server()

        client = MattermostClient('https://mattermost.example.com/', 'aaaa')

        # Call API
        user = client.user('8tbwn7uikpdy3gpse6fgiie5co')

        expected = [{}]

        self.assertEqual(len(http_requests), 1)

        for x in range(0, len(http_requests)):
            req = http_requests[x]
            self.assertEqual(req.method, 'GET')
            self.assertRegex(req.path,
                             '/api/v4/users/8tbwn7uikpdy3gpse6fgiie5co')
            self.assertDictEqual(req.querystring, expected[x])
            self.assertEqual(req.headers['Authorization'], 'Bearer aaaa')