def main(req: func.HttpRequest) -> func.HttpResponse: logging.info('Python HTTP trigger function processed a request.') username = req.params.get('username') repo = req.params.get('repo') req_body = req.get_json() secret = req_body.get('secret') # verifiy that the call is from prismic prismiSecret = os.environ["prismic_secret"] if prismiSecret != str(secret): return func.HttpRequest("Verification failed", status_code=400) data = {'event_type': 'prismic'} url = "https://api.github.com/repos/" + username + "/" + repo + "/dispatches" headers = { 'Authorization': 'token ' + secret, 'Accept': 'application/vnd.github.everest-preview+json' } response = requests.post(url, json=data, headers=headers) if response.ok: return func.HttpResponse("Action dispatched") else: return func.HttpRequest("Error", status_code=500)
def test_simulate_forwards(self): req = func.HttpRequest(method='GET', body=None, url='/api/simulateEquityPortfolio', params={ 'isin': self.isin, 'long_short': self.long_short, 'volume': self.volume, 'strike': self.strike, 'ttm': self.ttm }) # Call the function. resp = simulate(req) # Check the output. self.assertEqual( resp.get_body(), b'{"Expected Value": "[-1.68e-01, 1.74e+00, 6.61e+00, 6.94e+01, 8.40e+02, 2.39e+03, 6.88e+03, 8.58e+04, 5.42e+05, 1.28e+06, 6.76e+06]"}', ) # Test with JSON portfolio portfolio = { "forwards": [{ 'isin': 'isin_1', 'long_short': 'long', 'volume': 1000, 'strike': 16.4, 'ttm': 1.52 }, { 'isin': 'isin_1', 'long_short': 'short', 'volume': 500, 'strike': 12.3, 'ttm': 0.98 }] } portfolio_dump = json.dumps(portfolio) req = func.HttpRequest(method='POST', body=json.dumps(portfolio).encode('utf8'), url='/api/simulateEquityPortfolio', params=None) resp = simulate(req) self.assertEqual( resp.get_body(), b'{"Expected Value": "[-2550.88, -2795.45, -3586.95, -3115.19, -2332.38, -3337.22, -3391.31, -1350.66, -581.95, -1264.48, -1261.7 ]"}', )
async def test_durablefunctionsorchestrator_trigger(self): function_name = 'DurableFunctionsOrchestrator' instance_id = 'f86a9f49-ae1c-4c66-a60e-991c4c764fe5' starter = MagicMock() mock_request = func.HttpRequest( method='GET', body=None, url=f'http://localhost:7071/api/orchestrators{function_name}', route_params={'functionName': function_name}, params={'name': 'Test'}) mock_response = func.HttpResponse(body=None, status_code=200, headers={"Retry-After": 10}) with patch('azure.durable_functions.DurableOrchestrationClient', spec=df.DurableOrchestrationClient) as a_mock: a_mock.start_new = AsyncMock() a_mock().start_new.return_value = instance_id a_mock().create_check_status_response.return_value = mock_response response = await main(mock_request, starter) print(a_mock.mock_calls) self.assertIsNotNone(response.headers["Retry-After"]) self.assertEqual(10, response.headers["Retry-After"])
def test_pipeline_http_trigger(monkeypatch, mocker: MockFixture): """ Test main function for expected success of trigger filepath """ monkeypatch.setenv('SUBSCRIPTION_ID', 'mock_subscription_id') monkeypatch.setenv('RESOURCE_GROUP', 'mock_resource_group') monkeypatch.setenv('WORKSPACE_NAME', 'mock_ws_name') monkeypatch.setenv('PIPELINE_ENDPOINT_NAME', 'mock_pipeline_endpoint_name') monkeypatch.setenv('EXPERIMENT_NAME', 'mock_experiment_name') mock_aml_workspace = mocker.patch( 'src.PipelineHttpTrigger.pipeline_http_trigger.Workspace') mock_aml_workspace(subscription_id="mock_subscriptionId", resource_group="mock_resource_group", workspace_name="mock_ws_name", auth="mock_msi_auth_token") # Construct a mock HTTP request. req = func.HttpRequest(method='GET', body=None, url='/api/pipeline_http_trigger') # Call the function. main(req) # Check if http trigger is correctly called with parameter (file path) assert req
def test_with_missing_mandatory_param( self, mock_get_collection_link, mock_get_cosmos_client ): invalid_feedback_body = get_string("fixtures/missing_is_useful.json") invalid_feedback_body = bytearray(invalid_feedback_body, "utf8") # Construct a mock HTTP request. req = func.HttpRequest(method="POST", body=invalid_feedback_body, url=f"/api/") # Call the main Azure Function entry point with the request. resp = main(req) # Check status code self.assertEqual(resp.status_code, 400) # Check content type headers = dict(resp.headers) self.assertEqual(headers["content-type"], "application/json") # Do some checking of the returned error message. error_msg = json.loads(resp.get_body().decode("utf-8")) self.assertEqual(error_msg["errors"][0]["error"], "Bad Request") self.assertEqual( error_msg["errors"][0]["error_values"][0]["JSON Validation Error"], "'is_useful' is a required property", )
def test_heartbeat_create(self, mock_table_service, mock_os): # Arrange table_name = 'servers' request = func.HttpRequest(method='POST', url='/api/heartbeat', body=json.dumps({ 'ip': '192.168.1.1', 'port': '1337', 'status': 'ready', 'region': 'eastus' }).encode('utf8')) # Act result = main(request) server_id = json.loads(result.get_body()).get('server_id') inserted_data = mock_table_service( ).insert_or_replace_entity.call_args.args[1] # Assert assert result.status_code == 200 assert server_id mock_table_service().create_table.assert_called_with( table_name, fail_on_exist=False) mock_table_service().insert_or_replace_entity.assert_called_with( table_name, mock.ANY) assert inserted_data['PartitionKey'] == 'eastus' assert inserted_data['ip'] == '192.168.1.1' assert inserted_data['port'] == '1337' assert inserted_data['status'] == 'ready' assert inserted_data['lastHeartbeatTime']
def testProduction(self, mock_mailchimp_get_segment): # Set test values confirmedSubscribers = 500 # Set up Mailchimp responses mock_mailchimp_get_segment.return_value = { 'member_count': confirmedSubscribers } # Construct a mock HTTP request req = func.HttpRequest(method='GET', body=None, url='/api/getConfirmedSubscribers') # Call the function resp = main(req) jsonResponse = json.dumps({ 'frames': [{ 'icon': 'i29438', 'text': str(confirmedSubscribers) }] }) # Check the output self.assertEqual(resp.status_code, 200) self.assertEqual( resp.get_body(), jsonResponse.encode('ascii'), ) # Check basic calls to Mailchimp mock_mailchimp_get_segment.assert_called_once_with( app_config.MAILCHIMP_LIST_ID, '3577267')
def environ_to_request(self, environ: dict, route_params: dict) -> func.HttpRequest: # query parameters args = parse_qs(environ["QUERY_STRING"]) query = {} for key, value in args.items(): query[key] = ",".join(value) # headers headers = {} for key in environ: if key.startswith("HTTP_"): headers[key.replace("HTTP_", "").replace("_", "-").lower()] = environ[key] # content length try: request_body_size = int(environ.get('CONTENT_LENGTH', 0)) except (ValueError): request_body_size = 0 return func.HttpRequest( environ["REQUEST_METHOD"], environ["PATH_INFO"], headers=headers, route_params=route_params, params=query, body=environ['wsgi.input'].read(request_body_size))
def test_with_empty_body(self): """Test with empty POST body""" http_req = func.HttpRequest(method='post', url='http://somehost.com/somepath', body=self.empty_body.encode('ASCII')) with self.assertRaises(OfsMissingUrlError): get_url_from_req(http_req)
def test_negotiate_rejects_different_room_code(action: func.Out): player = model.Player("0", "player0", "ABCD", "SIGNING KEY") other_code = f"not {player.room_code}" json_request = { "roomCode": other_code, "playerDataToken": player.get_jwt() } with patch("__app__.utils.request.get_json") as get_json_mock: get_json_mock.return_value = json_request request = func.HttpRequest("POST", "/api/negotiate", body=json.dumps(json_request).encode()) with patch("__app__.datastore.environment.get_room_signing_key" ) as get_signing_key_mock: get_signing_key_mock.return_value = player.room_signing_key response = main(request, connection_info_json, action) get_json_mock.assert_called_once_with(request, ( "playerDataToken", "roomCode", )) get_signing_key_mock.assert_called_once_with(other_code) action.set.assert_not_called() common.validate_response_fields(response, status_code=HTTPStatus.FORBIDDEN, mimetype="text/plain")
async def test_issue_voip_token(mocker: MockerFixture) -> None: """Test case for issue_voip_token""" # values mock_token = "dummy_token" mock_acs_id = "dummy_id" mocker.patch('os.environ.get').return_value mocker.patch('issue_voip_token.CommunicationUser') mocker.patch('issue_voip_token.CommunicationIdentityClient') mock_request_body = { 'acs_id': mock_acs_id } mock_request = func.HttpRequest( 'POST', 'http://localhost:7071/api/issue_voip_token', headers={'Content-Type': 'application/json'}, params=None, body=json.dumps(mock_request_body).encode('utf-8') ) mock_response_body = { 'access_token': mock_token } mock_response = func.HttpResponse( body=json.dumps(mock_response_body).encode('utf-8'), status_code=200, headers={'Content-Type': 'application/json'} ) async with mocker.patch('issue_voip_token.CommunicationIdentityClient.from_connection_string').return_value as mock_communication_client: mock_communication_client.issue_token.return_value = mocker.Mock(token=mock_token) response = await main(mock_request) assert response.get_body() == mock_response.get_body()
def test_my_function(self): # Construct a mock HTTP request. mylist = { "PipelinePartType": "ADF", "JsonDefinition" : { "DataPipelineName" : "MyWaitADF" } } jsonparams = json.dumps(mylist,sort_keys=True,indent=4, separators=(',', ': ')) jsondict = json.loads(jsonparams) jsonparamsencoded = json.JSONEncoder().encode(jsondict) #req = func.HttpRequest( # method='GET', # body=jsonparamsencoded.encode(), # url='/api/HttpTrigger', # params={}) req = func.HttpRequest( method='POST', body=jsonparamsencoded.encode(), url='/api/HttpTrigger', params={"test":123}) # Call the function. resp = my_function(req) print(resp.get_body()) # Check the output. self.assertEqual( resp.get_body(), b'Hello Test', )
def test_with_invalid_institution_id(self): # Set an invalid institution_id string with 9 chars invalid_institution_id = "100000551" # Construct a mock HTTP request. req = func.HttpRequest( method="GET", body=None, url=f"/api/institutions/{invalid_institution_id}", params={"version": "1"}, route_params={"institution_id": invalid_institution_id}, ) # Call the main Azure Function entry point with the request. resp = main(req) # Check status code self.assertEqual(resp.status_code, 400) # Check content type headers = dict(resp.headers) self.assertEqual(headers["content-type"], "application/json") # Do some checking of the returned error message. error_msg = json.loads(resp.get_body().decode("utf-8")) self.assertEqual(error_msg["errors"][0]["error"], "Bad Request") self.assertEqual( error_msg["errors"][0]["error_values"][0]["Parameter Error"], "Invalid parameter passed", )
def test_external_port_timeout(): """ Test timeout connecting to a port """ dns_name = 'yahoo.com' port = '8443' req = func.HttpRequest(method='GET', body=b"", url='/api/', route_params={ 'scan': 'policy', 'view': 'external', 'target': dns_name, 'port': '8443' }) # Call the function resp = main(req) # Convert resp string to dict results = json.loads(resp) # Check the output to ensure the DNS name could not resolve assert results['Error Type'] == 'Connection Timeout' assert results[ "Message"] == f'TCP connection to {dns_name}:{port} timed-out'
def test_endpoint_for_non_existing_course(self): institution_id = "01135813" # Construct a mock HTTP request. req = func.HttpRequest( method="GET", body=None, url=f"/api/institutions/{institution_id}", params={"version": "1"}, route_params={"institution_id": institution_id}, ) # Call the function for the endpoint. resp = main(req) # Check status code self.assertEqual(resp.status_code, 404) # Check content type headers = dict(resp.headers) self.assertEqual(headers["content-type"], "application/json") # Do some checking of the returned error message. error_msg = json.loads(resp.get_body().decode("utf-8")) self.assertEqual(error_msg["errors"][0]["error"], "Not Found") self.assertEqual( error_msg["errors"][0]["error_values"][0]["institution"], "Institution was not found.", )
def test_endpoint_with_invalid_char_semicolon_version(self): institution_id = "10000055" version = ";" # semicolon is an invalid char for version # Construct a mock HTTP request. req = func.HttpRequest( method="GET", body=None, url=f"/api/institutions/{institution_id}", params={"version": version}, route_params={"institution_id": institution_id}, ) # Call the function for the endpoint. resp = main(req) # Check status code self.assertEqual(resp.status_code, 400) # Check content type headers = dict(resp.headers) self.assertEqual(headers["content-type"], "application/json") # Do some checking of the returned error message. error_msg = json.loads(resp.get_body().decode("utf-8")) self.assertEqual(error_msg["errors"][0]["error"], "Bad Request") self.assertEqual( error_msg["errors"][0]["error_values"][0]["Parameter Error"], "Invalid parameter passed", )
def test_endpoint_for_existing_course_with_tilde_in_course_id(self): # A course that exists in the HESA dataset institution_id = "10007850" course_id = "UUUL1-G104~USMA-AAM15" mode = "1" # Construct a mock HTTP request. req = func.HttpRequest( method="GET", body=None, url=f"/api/institutions/{institution_id}/courses/{course_id}/modes/{mode}", params={"version": "1"}, route_params={ "institution_id": institution_id, "course_id": course_id, "mode": mode, }, ) # Call the function for the endpoint. resp = main(req) # Check status code self.assertEqual(resp.status_code, 200) # Check content type headers = dict(resp.headers) self.assertEqual(headers["content-type"], "application/json") # Do some checking of the returned course. course = json.loads(resp.get_body().decode("utf-8")) self.assertEqual( course["course"]["institution"]["pub_ukprn"], f"{institution_id}" )
def test_negotiate_adds_player_to_group(action: func.Out): player = model.Player("0", "player0", "ABCD", "SIGNING KEY") json_request = { "roomCode": player.room_code, "playerDataToken": player.get_jwt() } with patch("__app__.utils.request.get_json") as get_json_mock: get_json_mock.return_value = json_request request = func.HttpRequest("POST", "/api/negotiate", body=json.dumps(json_request).encode()) with patch("__app__.datastore.environment.get_room_signing_key" ) as get_signing_key_mock: get_signing_key_mock.return_value = player.room_signing_key response = main(request, connection_info_json, action) get_json_mock.assert_called_once_with(request, ( "playerDataToken", "roomCode", )) get_signing_key_mock.assert_called_once_with(player.room_code) action.set.assert_called_once_with( json.dumps({ "userId": player.id, "groupName": player.room_code, "action": "add" })) common.validate_response_fields(response) assert common.get_json(response) == json.loads(connection_info_json)
def test_query_api_error_handling(): req = func.HttpRequest(method='GET', body=None, url='/api/tls', params={'nameserver': '8.8.8.8'}) resp = main(req) results = json.loads(resp) assert results['Error Type'] == "Missing required parameter"
def test_get_json(): json_body = {"field0": 0, "field1": 1} request = func.HttpRequest("POST", "/api/random", body=json.dumps(json_body).encode()) assert utils.request.get_json(request, ("field0", "field1")) == json_body
def test_with_empty_json_object(self): """Test with empty json object""" invalid_body = json.dumps(self.empty_json_object) http_req = func.HttpRequest(method='post', url='http://somehost.com/somepath', body=invalid_body.encode('ASCII')) with self.assertRaises(OfsMissingUrlError): get_url_from_req(http_req)
def create_http_request() -> func.HttpRequest: # Construct a mock HTTP request. req = func.HttpRequest( method="GET", body="", url="/api/sample", ) return req
def test_with_valid_json(self): """Test with valid json""" valid_body = json.dumps(self.valid_json) http_req = func.HttpRequest(method='post', url='http://somehost.com/somepath', body=valid_body.encode('ASCII')) url = get_url_from_req(http_req) self.assertEqual(url, self.test_url)
def test_users(): req = func.HttpRequest(method="GET", url="users/2", body=b"", route_params={"user_id": "2"}) result = users.main(req) assert result.status_code == 200 assert json.loads(result.get_body().decode())["user_id"] == "2"
def test_dependabot_completed_check_run(mocker: MockerFixture): with open("tests/check_run.json", "rb") as pr: req = func.HttpRequest(method="POST", url="https://testing.com", body=pr.read()) mocker.patch("github.PullRequest.PullRequest.merge") mocker.patch("github.PullRequest.PullRequest.create_review") result = main(req) assert result.status_code == 200 assert result.get_body().decode("utf8") == "Automatically merged PR"
def test_get_json_raises_MissingRequiredField(): json_body = {"field0": 0} request = func.HttpRequest("POST", "/api/random", body=json.dumps(json_body).encode()) with pytest.raises(utils.request.MissingRequiredField): utils.request.get_json(request, ("field0", "field1"))
def test_http_form_parse_urlencoded(self): data = b"foo=Hello+World&bar=baz" req = func.HttpRequest( method="POST", url='/foo', body=data, headers={'Content-Type': 'application/x-www-form-urlencoded'}, ) self.assertEqual(req.form["foo"], u"Hello World")
def http_call(funcToCall, name): # Construct a mock HTTP request. req = func.HttpRequest(method="GET", body=None, url="/api/HttpTrigger", params={"name": name}) # Call the function. return funcToCall(req)
def test_func_for_no_name(): """Construct a mock HTTP request.""" req = func.HttpRequest(method="POST", body=None, url=TRIGGER_URL, params={}) # Call the function. resp = main(req) # Check the output. assert resp.get_body() == NO_DATA_MSG # nosec
def test_main_get(self, mock_get_items): mock_get_items.return_value = [{'test': 'test'}] req = func.HttpRequest(method='GET', url='https://httpbin.org/get', body={}) main_result = main(req, func.Out[func.Document]) assert isinstance(main_result, func.HttpResponse) assert json.loads(main_result.get_body()) == [{'test': 'test'}] assert main_result.status_code == 200