def test_get_comments_old_url(self): """Test if commits call works with the old URL schema""" body = read_file('data/askbot/askbot_2481_multicomments.json') httpretty.register_uri(httpretty.GET, ASKBOT_COMMENTS_API_URL, body='', status=404) httpretty.register_uri(httpretty.GET, ASKBOT_COMMENTS_API_URL_OLD, body=body, status=200) client = AskbotClient(ASKBOT_URL) result = client.get_comments(17) self.assertEqual(result, body) expected = { 'post_id': ['17'], 'post_type': ['answer'], 'avatar_size': ['0'] } reqs = httpretty.httpretty.latest_requests self.assertEqual(len(reqs), 2) self.assertEqual(reqs[0].method, 'GET') self.assertRegex(reqs[0].path, '/s/post_comments') self.assertDictEqual(reqs[0].querystring, expected) self.assertEqual(reqs[1].method, 'GET') self.assertRegex(reqs[1].path, '/post_comments') self.assertDictEqual(reqs[1].querystring, expected)
def test_get_html_question_multipage(self): """Test if HTML Questions multipage call works.""" body = read_file('data/askbot/askbot_question_multipage_2.html') httpretty.register_uri(httpretty.GET, ASKBOT_QUESTION_2488_URL, body=body, status=200) client = AskbotClient(ASKBOT_URL) result = client.get_html_question(2488, 2) self.assertEqual(result, body) expected = { 'page': ['2'], 'sort': ['votes'] } req = httpretty.last_request() self.assertEqual(req.method, 'GET') self.assertRegex(req.path, '/question/2488') self.assertDictEqual(req.querystring, expected)
def test_get_comments(self): """Test if comments call works""" body = read_file('data/askbot/askbot_2481_multicomments.json') httpretty.register_uri(httpretty.GET, ASKBOT_COMMENTS_API_URL, body=body, status=200) client = AskbotClient(ASKBOT_URL) result = client.get_comments(17) self.assertEqual(result, body) expected = { 'post_id': ['17'], 'post_type': ['answer'], 'avatar_size': ['0'] } req = httpretty.last_request() self.assertEqual(req.method, 'GET') self.assertRegex(req.path, 's/post_comments') self.assertDictEqual(req.querystring, expected)
def test_get_api_questions(self): """Test if API Questions call works""" body = read_file('data/askbot/askbot_api_questions.json') httpretty.register_uri(httpretty.GET, ASKBOT_QUESTIONS_API_URL, body=body, status=200) client = AskbotClient(ASKBOT_URL) result = next(client.get_api_questions('api/v1/questions')) self.assertEqual(result, json.loads(body)) expected = { 'page': ['1'], 'sort': ['activity-asc'] } req = httpretty.last_request() self.assertEqual(req.method, 'GET') self.assertRegex(req.path, '/api/v1/questions') self.assertDictEqual(req.querystring, expected)
def test_500_status_get_comments(self): """Test whether, when fetching comments, an exception is thrown if HTTPError is not 503""" httpretty.register_uri(httpretty.GET, ASKBOT_COMMENTS_API_URL, body="", status=500) client = AskbotClient(ASKBOT_URL) self.assertEqual(client.get_comments(5), '[]')
def test_wrong_status_get_comments(self): """Test whether, when fetching comments, an exception is thrown if HTTPError is not 404""" httpretty.register_uri(httpretty.GET, ASKBOT_COMMENTS_API_URL, body="", status=403) client = AskbotClient(ASKBOT_URL) with self.assertRaises(requests.exceptions.HTTPError): _ = client.get_comments(5)
def test_init(self): """Test initialization parameters""" ab = AskbotClient(ASKBOT_URL) self.assertEqual(ab.base_url, ASKBOT_URL) self.assertTrue(ab.ssl_verify) ab = AskbotClient(ASKBOT_URL, ssl_verify=False) self.assertEqual(ab.base_url, ASKBOT_URL) self.assertFalse(ab.ssl_verify)
def test_get_html_question(self): """Test if HTML Questions call works.""" body = read_file('data/askbot/askbot_question.html') httpretty.register_uri(httpretty.GET, ASKBOT_QUESTION_2481_URL, body=body, status=200) client = AskbotClient(ASKBOT_URL) result = client.get_html_question(2481) self.assertEqual(result, body) req = httpretty.last_request() self.assertEqual(req.method, 'GET') self.assertRegex(req.path, '/question/2481')
def test_get_html_question_empty(self): """Test if HTML Questions call (non-existing question) works.""" body = read_file('data/askbot/askbot_question_empty.html') httpretty.register_uri(httpretty.GET, ASKBOT_QUESTION_EMPTY_URL, body=body, status=404) client = AskbotClient(ASKBOT_URL) self.assertRaises(requests.exceptions.HTTPError, client.get_html_question, 0) req = httpretty.last_request() self.assertEqual(req.method, 'GET') self.assertRegex(req.path, '/question/0')
def test_init(self): """Test initialization parameters""" ab = AskbotClient(ASKBOT_URL) self.assertEqual(ab.base_url, ASKBOT_URL)