示例#1
0
    def test_topic(self):
        """Test topic API call"""

        # Set up a mock HTTP server
        body = read_file('data/discourse_topic_1148.json')
        httpretty.register_uri(httpretty.GET,
                               DISCOURSE_TOPIC_URL_1148,
                               body=body, status=200)

        # Call API
        client = DiscourseClient(DISCOURSE_SERVER_URL, api_key='aaaa')
        response = client.topic(1148)

        self.assertEqual(response, body)

        # Check request params
        expected = {
            'api_key': ['aaaa'],
        }

        req = httpretty.last_request()

        self.assertEqual(req.method, 'GET')
        self.assertRegex(req.path, '/t/1148.json')
        self.assertDictEqual(req.querystring, expected)
    def test_post(self):
        """Test post API call"""

        # Set up a mock HTTP server
        body = read_file('data/discourse/discourse_post.json')
        httpretty.register_uri(httpretty.GET,
                               DISCOURSE_POST_URL_1,
                               body=body,
                               status=200)

        # Call API
        client = DiscourseClient(DISCOURSE_SERVER_URL,
                                 api_key='aaaa',
                                 sleep_time=0)
        response = client.post(21)

        self.assertEqual(response, body)

        # Check request params
        expected = {
            'api_key': ['aaaa'],
        }

        req = httpretty.last_request()

        self.assertEqual(req.method, 'GET')
        self.assertRegex(req.path, '/posts/21.json')
        self.assertDictEqual(req.querystring, expected)
示例#3
0
    def test_post(self):
        """Test post API call"""

        # Set up a mock HTTP server
        body = read_file('data/discourse/discourse_post.json')
        httpretty.register_uri(httpretty.GET,
                               DISCOURSE_POST_URL_1,
                               body=body,
                               status=200)

        # Call API
        client = DiscourseClient(DISCOURSE_SERVER_URL,
                                 api_key='aaaa',
                                 api_username='******',
                                 sleep_time=0)
        response = client.post(21)

        self.assertEqual(response, body)

        req = httpretty.last_request()

        self.assertEqual(req.method, 'GET')
        self.assertRegex(req.path, '/posts/21.json')
        self.assertDictEqual(req.querystring, {})
        self.assertEqual(req.headers[DiscourseClient.HKEY], 'aaaa')
        self.assertEqual(req.headers[DiscourseClient.HUSER], 'user')
示例#4
0
    def test_init(self):
        """Test whether attributes are initialized"""

        client = DiscourseClient(DISCOURSE_SERVER_URL)

        self.assertEqual(client.base_url, DISCOURSE_SERVER_URL)
        self.assertIsNone(client.api_key)
        self.assertIsNone(client.api_username)
        self.assertEqual(client.sleep_time, DEFAULT_SLEEP_TIME)
        self.assertEqual(client.max_retries, MAX_RETRIES)
        self.assertTrue(client.ssl_verify)

        client = DiscourseClient(DISCOURSE_SERVER_URL,
                                 api_key='aaaa',
                                 api_username='******',
                                 ssl_verify=False)

        self.assertEqual(client.base_url, DISCOURSE_SERVER_URL)
        self.assertEqual(client.api_key, 'aaaa')
        self.assertEqual(client.api_username, 'user')
        self.assertEqual(client.sleep_time, DEFAULT_SLEEP_TIME)
        self.assertEqual(client.max_retries, MAX_RETRIES)
        self.assertFalse(client.ssl_verify)

        client = DiscourseClient(DISCOURSE_SERVER_URL,
                                 api_key='aaaa',
                                 api_username='******',
                                 sleep_time=60,
                                 max_retries=30)
        self.assertEqual(client.base_url, DISCOURSE_SERVER_URL)
        self.assertEqual(client.api_key, 'aaaa')
        self.assertEqual(client.api_username, 'user')
        self.assertEqual(client.sleep_time, 60)
        self.assertEqual(client.max_retries, 30)
示例#5
0
    def test_missing_credentials(self):
        """Test whether an exception is thrown when `api_key` and `api_username` aren't defined together"""

        with self.assertRaises(HttpClientError):
            _ = DiscourseClient(DISCOURSE_SERVER_URL, api_key='1234')

        with self.assertRaises(HttpClientError):
            _ = DiscourseClient(DISCOURSE_SERVER_URL, api_username='******')
示例#6
0
    def test_init(self):
        """Test whether attributes are initializated"""

        client = DiscourseClient(DISCOURSE_SERVER_URL, api_key='aaaa')

        self.assertEqual(client.base_url, DISCOURSE_SERVER_URL)
        self.assertEqual(client.api_key, 'aaaa')
    def test_init(self):
        """Test whether attributes are initializated"""

        client = DiscourseClient(DISCOURSE_SERVER_URL,
                                 api_key='aaaa')

        self.assertEqual(client.base_url, DISCOURSE_SERVER_URL)
        self.assertEqual(client.api_key, 'aaaa')
        self.assertEqual(client.sleep_time, DEFAULT_SLEEP_TIME)
        self.assertEqual(client.max_retries, MAX_RETRIES)

        client = DiscourseClient(DISCOURSE_SERVER_URL,
                                 api_key='aaaa', sleep_time=60, max_retries=30)
        self.assertEqual(client.base_url, DISCOURSE_SERVER_URL)
        self.assertEqual(client.api_key, 'aaaa')
        self.assertEqual(client.sleep_time, 60)
        self.assertEqual(client.max_retries, 30)
    def test_sanitize_for_archive(self):
        """Test whether the sanitize method works properly"""

        url = "http://example.com"
        headers = "headers-information"
        payload = {'api_key': 'aaaa'}

        url, headers, payload = DiscourseClient.sanitize_for_archive(None, None, payload)
        with self.assertRaises(KeyError):
            payload.pop("api_key")
示例#9
0
    def test_topics_page(self):
        """Test topics_page API call"""

        # Set up a mock HTTP server
        body = read_file('data/discourse/discourse_topics.json')
        httpretty.register_uri(httpretty.GET,
                               DISCOURSE_TOPICS_URL,
                               body=body,
                               status=200)

        # Call API without args
        client = DiscourseClient(DISCOURSE_SERVER_URL,
                                 api_key='aaaa',
                                 api_username='******',
                                 sleep_time=0)
        response = client.topics_page()

        self.assertEqual(response, body)

        # Check request params
        expected = {}

        req = httpretty.last_request()

        self.assertEqual(req.method, 'GET')
        self.assertRegex(req.path, '/latest.json')
        self.assertDictEqual(req.querystring, expected)

        # Call API selecting a page
        response = client.topics_page(page=1)

        self.assertEqual(response, body)

        # Check request params
        expected = {'page': ['1']}

        req = httpretty.last_request()

        self.assertDictEqual(req.querystring, expected)
        self.assertEqual(req.headers[DiscourseClient.HKEY], 'aaaa')
        self.assertEqual(req.headers[DiscourseClient.HUSER], 'user')
    def test_sanitize_for_archive_no_api_key(self):
        """Test whether the sanitize method works properly when the api_key does not exist"""

        url = "http://example.com"
        headers = "headers-information"
        payload = "payload-information"

        s_url, s_headers, s_payload = DiscourseClient.sanitize_for_archive(url, headers, payload)

        self.assertEqual(url, s_url)
        self.assertEqual(headers, s_headers)
        self.assertEqual(payload, s_payload)
示例#11
0
    def test_sanitize_for_archive(self):
        """Test whether the sanitize method works properly"""

        url = "http://example.com"
        headers = {DiscourseClient.HUSER: '******', DiscourseClient.HKEY: 'aaaa'}
        c_headers = copy.deepcopy(headers)
        payload = {}

        san_u, san_h, san_p = DiscourseClient.sanitize_for_archive(
            url, c_headers, payload)
        headers.pop(DiscourseClient.HUSER)
        headers.pop(DiscourseClient.HKEY)

        self.assertEqual(url, san_u)
        self.assertEqual(headers, san_h)
        self.assertEqual(payload, san_p)