def test_dbt_cloud_run_job_trigger_job_with_wait_custom(): account_id = 1234 job_id = 1234 with responses.RequestsMock() as rsps: rsps.add( responses.POST, f"https://cloud.corp.getdbt.com/api/v2/accounts/{account_id}/jobs/{job_id}/run/", status=200, json={"data": {"id": 1}}, match=[matchers.header_matcher(USER_AGENT_HEADER)], ) rsps.add( responses.GET, f"https://cloud.corp.getdbt.com/api/v2/accounts/{account_id}/runs/1/", status=200, json={ "data": {"id": 1, "status": 10, "finished_at": "2019-08-24T14:15:22Z"} }, match=[matchers.header_matcher(USER_AGENT_HEADER)], ) rsps.add( responses.GET, f"https://cloud.corp.getdbt.com/api/v2/accounts/{account_id}/runs/1/artifacts/", status=200, json={"data": ["manifest.json", "run_results.json", "catalog.json"]}, match=[matchers.header_matcher(USER_AGENT_HEADER)], ) run_job = DbtCloudRunJob( cause="foo", account_id=account_id, job_id=job_id, token="foo", wait_for_job_run_completion=True, domain="cloud.corp.getdbt.com", ) r = run_job.run() assert r == { "id": 1, "status": 10, "finished_at": "2019-08-24T14:15:22Z", "artifact_urls": [ f"https://cloud.corp.getdbt.com/api/v2/accounts/{account_id}/runs/1/artifacts/manifest.json", f"https://cloud.corp.getdbt.com/api/v2/accounts/{account_id}/runs/1/artifacts/run_results.json", f"https://cloud.corp.getdbt.com/api/v2/accounts/{account_id}/runs/1/artifacts/catalog.json", ], }
def mock_user_response(mocker): responses.add( responses.GET, url=BASE_URL, json={ "@odata.context": "https://graph.microsoft.com/v1.0/$metadata#users/$entity", "businessPhones": ["+1 205 555 0108"], "displayName": "Diego Siciliani", "mobilePhone": None, "officeLocation": "dunhill", "updatedAt": "2021 -07-29T03:07:25Z", "birthday": "2000-09-04", "isActive": True, "age": 21, "gpa": 3.2, "id": "8f841f30-e6e3-439a-a812-ebd369559c36" }, status=200, match=[ matchers.header_matcher({"Content-Type": "application/json"}, strict_match=True) ]) session = requests.Session() prepped = session.prepare_request( requests.Request( method="GET", url=BASE_URL, )) prepped.headers = {"Content-Type": "application/json"} resp = session.send(prepped) return resp
def test_correct_api_key(settings, main_api_key, people_endpoint): settings.ACTIONNETWORK_API_KEYS = {"main": main_api_key} url = "https://actionnetwork.org/api/v2/people" responses.add( responses.GET, url=people_endpoint, status=200, match=[matchers.header_matcher({"OSDI-API-Token": main_api_key})], ) assert an.call_api(url).status_code == 200
def run(): url = "http://example.com/" responses.add( method=responses.GET, url=url, json={"success": True}, match=[matchers.header_matcher({"Accept": "application/json"})], ) responses.add( method=responses.GET, url=url, body="success", match=[matchers.header_matcher({"Accept": "text/plain"})], ) # the actual request can contain extra headers (requests always adds some itself anyway) resp = requests.get( url, headers={"Accept": "application/json", "Accept-Charset": "utf-8"} ) assert_response(resp, body='{"success": true}', content_type="application/json") resp = requests.get(url, headers={"Accept": "text/plain"}) assert_response(resp, body="success", content_type="text/plain")
def mock_users_response(mocker): responses.add( responses.GET, url=BASE_URL, json=[ { "@odata.context": "https://graph.microsoft.com/v1.0/$metadata#users/$entity", "businessPhones": ["+1 425 555 0109"], "displayName": "Adele Vance", "mobilePhone": None, "officeLocation": "dunhill", "updatedAt": "2017 -07-29T03:07:25Z", "birthday": "2000-09-04", "isActive": True, "age": 21, "gpa": 3.7, "id": "76cabd60-f9aa-4d23-8958-64f5539b826a" }, { "businessPhones": ["425-555-0100"], "displayName": "MOD Administrator", "mobilePhone": None, "officeLocation": "oval", "updatedAt": "2020 -07-29T03:07:25Z", "birthday": "1990-09-04", "isActive": True, "age": 32, "gpa": 3.9, "id": "f58411c7-ae78-4d3c-bb0d-3f24d948de41" }, ], status=200, match=[ matchers.header_matcher({"Content-Type": "application/json"}, strict_match=True) ]) session = requests.Session() prepped = session.prepare_request( requests.Request( method="GET", url=BASE_URL, )) prepped.headers = {"Content-Type": "application/json"} resp = session.send(prepped) return resp
def test_dbt_cloud_run_job_raises_failure(): account_id = 1234 job_id = 1234 with responses.RequestsMock() as rsps: rsps.add( responses.POST, f"https://cloud.getdbt.com/api/v2/accounts/{account_id}/jobs/{job_id}/run/", status=123, match=[matchers.header_matcher(USER_AGENT_HEADER)], ) run_job = DbtCloudRunJob( cause="foo", account_id=account_id, job_id=job_id, token="foo" ) with pytest.raises(TriggerDbtCloudRunFailed): run_job.run()
def run(): url = "http://example.com/" responses.add( method=responses.GET, url=url, body="success", match=[ matchers.header_matcher({"Accept": "text/plain"}, strict_match=True) ], ) # requests will add some extra headers of its own, so we have to use prepared requests session = requests.Session() # make sure we send *just* the header we're expectin prepped = session.prepare_request( requests.Request( method="GET", url=url, ) ) prepped.headers.clear() prepped.headers["Accept"] = "text/plain" resp = session.send(prepped) assert_response(resp, body="success", content_type="text/plain") # include the "Accept-Charset" header, which will fail to match prepped = session.prepare_request( requests.Request( method="GET", url=url, ) ) prepped.headers.clear() prepped.headers["Accept"] = "text/plain" prepped.headers["Accept-Charset"] = "utf-8" with pytest.raises(ConnectionError) as excinfo: session.send(prepped) msg = str(excinfo.value) assert ( "Headers do not match: {Accept: text/plain, Accept-Charset: utf-8} " "doesn't match {Accept: text/plain}" ) in msg
def run(): url = "http://example.com/" responses.add( method=responses.GET, url=url, json={"success": True}, match=[matchers.header_matcher({"Accept": "application/json"})], ) with pytest.raises(ConnectionError) as excinfo: requests.get(url, headers={"Accept": "application/xml"}) msg = str(excinfo.value) assert ( "Headers do not match: {Accept: application/xml} doesn't match " "{Accept: application/json}" ) in msg
def test_dbt_cloud_run_job_trigger_job(): account_id = 1234 job_id = 1234 with responses.RequestsMock() as rsps: rsps.add( responses.POST, f"https://cloud.getdbt.com/api/v2/accounts/{account_id}/jobs/{job_id}/run/", status=200, json={"data": {"foo": "bar"}}, match=[matchers.header_matcher(USER_AGENT_HEADER)], ) run_job = DbtCloudRunJob( cause="foo", account_id=account_id, job_id=job_id, token="foo" ) r = run_job.run() assert r == {"foo": "bar"}
def mock_no_content_response(mocker): responses.add(responses.GET, url=BASE_URL, status=204, match=[ matchers.header_matcher( {"Content-Type": "application/json"}, strict_match=True) ]) session = requests.Session() prepped = session.prepare_request( requests.Request( method="GET", url=BASE_URL, )) prepped.headers = {"Content-Type": "application/json"} resp = session.send(prepped) return resp
def simple_response(request_adapter): responses.add(responses.GET, url=BASE_URL, json={'error': 'not found'}, status=404, match=[ matchers.header_matcher( {"Content-Type": "application/json"}, strict_match=True) ]) session = requests.Session() prepped = session.prepare_request( requests.Request( method="GET", url=BASE_URL, )) prepped.headers = {"Content-Type": "application/json"} resp = session.send(prepped) return resp
def mock_primitive_collection_response(mocker): responses.add(responses.GET, url=BASE_URL, json=[12.1, 12.2, 12.3, 12.4, 12.5], status=200, match=[ matchers.header_matcher( {"Content-Type": "application/json"}, strict_match=True) ]) session = requests.Session() prepped = session.prepare_request( requests.Request( method="GET", url=BASE_URL, )) prepped.headers = {"Content-Type": "application/json"} resp = session.send(prepped) return resp