def test_phids(self): """Test if a set of PHIDs is returned""" http_requests = setup_http_server() client = ConduitClient(PHABRICATOR_URL, 'aaaa') _ = client.phids("PHID-APPS-PhabricatorHeraldApplication", "PHID-APPS-PhabricatorMockApplication") expected = [{ '__conduit__': ['True'], 'output': ['json'], 'params': { '__conduit__': { 'token': 'aaaa' }, 'phids': [ "PHID-APPS-PhabricatorHeraldApplication", "PHID-APPS-PhabricatorMockApplication" ] } }] self.assertEqual(len(http_requests), len(expected)) for i in range(len(expected)): rparams = http_requests[i].parsed_body rparams['params'] = json.loads(rparams['params'][0]) self.assertDictEqual(rparams, expected[i])
def test_transactions(self): """Test if a set of transactions is returned""" http_requests = setup_http_server() client = ConduitClient(PHABRICATOR_URL, 'aaaa') _ = client.transactions(69, 73, 78) expected = [{ '__conduit__': ['True'], 'output': ['json'], 'params': { '__conduit__': { 'token': 'aaaa' }, 'ids': [69, 73, 78] } }] self.assertEqual(len(http_requests), len(expected)) for i in range(len(expected)): rparams = http_requests[i].parsed_body rparams['params'] = json.loads(rparams['params'][0]) self.assertDictEqual(rparams, expected[i])
def test_users(self): """Test if a set of users is returned""" http_requests = setup_http_server() client = ConduitClient(PHABRICATOR_URL, 'aaaa') _ = client.users("PHID-USER-2uk52xorcqb6sjvp467y", "PHID-USER-bjxhrstz5fb5gkrojmev", "PHID-USER-pr5fcxy4xk5ofqsfqcfc", "PHID-USER-ojtcpympsmwenszuef7p") expected = [{ '__conduit__': ['True'], 'output': ['json'], 'params': { '__conduit__': { 'token': 'aaaa' }, 'phids': [ "PHID-USER-2uk52xorcqb6sjvp467y", "PHID-USER-bjxhrstz5fb5gkrojmev", "PHID-USER-pr5fcxy4xk5ofqsfqcfc", "PHID-USER-ojtcpympsmwenszuef7p" ] } }] self.assertEqual(len(http_requests), len(expected)) for i in range(len(expected)): rparams = http_requests[i].parsed_body rparams['params'] = json.loads(rparams['params'][0]) self.assertDictEqual(rparams, expected[i])
def test_init(self): """Test initialization parameters""" client = ConduitClient(PHABRICATOR_URL, 'aaaa') self.assertEqual(client.base_url, PHABRICATOR_URL) self.assertEqual(client.api_token, 'aaaa') self.assertEqual(client.max_retries, MAX_RETRIES) self.assertEqual(client.sleep_time, DEFAULT_SLEEP_TIME) self.assertTrue(client.ssl_verify) client = ConduitClient(PHABRICATOR_URL, 'aaaa', 2, 100, ssl_verify=False) self.assertEqual(client.base_url, PHABRICATOR_URL) self.assertEqual(client.api_token, 'aaaa') self.assertEqual(client.max_retries, 2) self.assertEqual(client.sleep_time, 100) self.assertFalse(client.ssl_verify) client = ConduitClient(PHABRICATOR_URL, 'aaaa', max_retries=2, sleep_time=100) self.assertEqual(client.base_url, PHABRICATOR_URL) self.assertEqual(client.api_token, 'aaaa') self.assertEqual(client.max_retries, 2) self.assertEqual(client.sleep_time, 100)
def test_retry_on_server_errors(self): """Test if the client retries when some HTTP errors are found""" reqs = [] body = read_file('data/phabricator/phabricator_tasks_empty.json', 'rb') responses = { PHABRICATOR_TASKS_URL: [(502, 'error'), (503, 'error'), (200, body)], PHABRICATOR_USERS_URL: [(503, 'error'), (503, 'error'), (503, 'error'), (503, 'error')], PHABRICATOR_PHIDS_URL: [(404, 'not found')] } def request_callback(method, uri, headers): reqs.append(httpretty.last_request()) resp = responses[uri].pop(0) return (resp[0], headers, resp[1]) httpretty.register_uri(httpretty.POST, PHABRICATOR_TASKS_URL, responses=[ httpretty.Response(body=request_callback) for _ in range(3) ]) httpretty.register_uri(httpretty.POST, PHABRICATOR_USERS_URL, responses=[ httpretty.Response(body=request_callback) for _ in range(4) ]) httpretty.register_uri(httpretty.POST, PHABRICATOR_PHIDS_URL, responses=[ httpretty.Response(body=request_callback) for _ in range(1) ]) # These tests are based on the maximum number of retries, # set by default to 3. The client only retries 502 and 503 # HTTP errors. client = ConduitClient(PHABRICATOR_URL, 'aaaa') # After 3 tries (request + 2 retries) it gets the result reqs = [] _ = [r for r in client.tasks()] self.assertEqual(len(reqs), 3) # After 4 tries (request + 3 retries) it fails reqs = [] with self.assertRaises(requests.exceptions.RetryError): _ = client.users("PHID-USER-2uk52xorcqb6sjvp467y") self.assertEqual(len(reqs), 4) # After 1 try if fails reqs = [] with self.assertRaises(requests.exceptions.HTTPError): _ = client.phids("PHID-APPS-PhabricatorHeraldApplication") self.assertEqual(len(reqs), 1)
def test_phabricator_error(self): """Test if an exception is raised when an error is returned by the server""" setup_http_server() client = ConduitClient(PHABRICATOR_URL, 'aaaa') with self.assertRaises(ConduitError): _ = client._call('error', {})
def test_tasks(self): """Test if a set of tasks is returned""" http_requests = setup_http_server() client = ConduitClient(PHABRICATOR_URL, 'aaaa') dt = datetime.datetime(2016, 5, 3, 0, 0, 0) result = client.tasks(from_date=dt) result = [r for r in result] self.assertEqual(len(result), 2) expected = [{ '__conduit__': ['True'], 'output': ['json'], 'params': { '__conduit__': { 'token': 'aaaa' }, 'attachments': { 'projects': True }, 'constraints': { 'modifiedStart': 1462233600 }, 'order': 'outdated' } }, { '__conduit__': ['True'], 'output': ['json'], 'params': { '__conduit__': { 'token': 'aaaa' }, 'after': '335', 'attachments': { 'projects': True }, 'constraints': { 'modifiedStart': 1462233600 }, 'order': 'outdated' } }] self.assertEqual(len(http_requests), len(expected)) for i in range(len(expected)): rparams = http_requests[i].parsed_body rparams['params'] = json.loads(rparams['params'][0]) self.assertDictEqual(rparams, expected[i])
def test_sanitize_for_archive_no_token(self): """Test whether the sanitize method works properly when a token is not given""" url = "http://example.com" headers = "headers-information" payload = {'__conduit__': True, 'output': 'json', 'params': '{"phids": ["PHID-APPS-PhabricatorHeraldApplication"]}'} s_url, s_headers, s_payload = ConduitClient.sanitize_for_archive(url, headers, copy.deepcopy(payload)) self.assertEqual(url, s_url) self.assertEqual(headers, s_headers) self.assertEqual(payload, s_payload)
def test_sanitize_for_archive_token(self): """Test whether the sanitize method works properly when a token is given""" url = "http://example.com" headers = "headers-information" payload = {'__conduit__': True, 'output': 'json', 'params': '{"__conduit__": {"token": "aaaa"}, ' '"phids": ["PHID-APPS-PhabricatorHeraldApplication"]}'} s_url, s_headers, s_payload = ConduitClient.sanitize_for_archive(url, headers, copy.deepcopy(payload)) params = json.loads(payload['params']) params.pop("__conduit__") payload['params'] = json.dumps(params, sort_keys=True) self.assertEqual(url, s_url) self.assertEqual(headers, s_headers) self.assertEqual(payload, s_payload)
def test_init(self): """Test initialization parameters""" client = ConduitClient(PHABRICATOR_URL, 'aaaa') self.assertEqual(client.base_url, PHABRICATOR_URL) self.assertEqual(client.api_token, 'aaaa')