From afd5237ef7c1d46e2d8b3eebe890e20818424e18 Mon Sep 17 00:00:00 2001 From: DR695H Date: Mon, 12 Aug 2019 18:17:19 -0400 Subject: initial redo of the requests keywords for client certs Issue-ID: TEST-184 Change-Id: Ie35e9c64180bad574ba3e4fc77f8418e4ab27528 Signed-off-by: DR695H --- robotframework-onap/ONAPLibrary/BaseAAIKeywords.py | 33 +++++---- .../ONAPLibrary/BaseCLAMPKeywords.py | 48 +++++++++++++ robotframework-onap/ONAPLibrary/BaseSDCKeywords.py | 16 +++-- .../ONAPLibrary/BaseSDNCKeywords.py | 8 ++- robotframework-onap/ONAPLibrary/BaseSOKeywords.py | 11 +-- robotframework-onap/ONAPLibrary/CLAMP.py | 29 ++++++++ .../ONAPLibrary/CloudConfigSOKeywords.py | 6 +- robotframework-onap/ONAPLibrary/MUSICKeywords.py | 3 +- .../ONAPLibrary/PreloadSDNCKeywords.py | 2 +- .../ONAPLibrary/RequestSOKeywords.py | 4 +- .../ONAPLibrary/RequestsDecorators.py | 45 ++++++++++++ robotframework-onap/ONAPLibrary/RequestsHelper.py | 80 ++++++++++------------ robotframework-onap/ONAPLibrary/SNIROKeywords.py | 7 +- robotframework-onap/setup.py | 6 +- .../tests/ONAPLibrary/RequestsHelperTests.py | 50 ++++++++++++++ robotframework-onap/tests/runner.py | 2 + 16 files changed, 269 insertions(+), 81 deletions(-) create mode 100644 robotframework-onap/ONAPLibrary/BaseCLAMPKeywords.py create mode 100644 robotframework-onap/ONAPLibrary/CLAMP.py create mode 100644 robotframework-onap/ONAPLibrary/RequestsDecorators.py create mode 100644 robotframework-onap/tests/ONAPLibrary/RequestsHelperTests.py diff --git a/robotframework-onap/ONAPLibrary/BaseAAIKeywords.py b/robotframework-onap/ONAPLibrary/BaseAAIKeywords.py index 222c54b..fdc9e84 100644 --- a/robotframework-onap/ONAPLibrary/BaseAAIKeywords.py +++ b/robotframework-onap/ONAPLibrary/BaseAAIKeywords.py @@ -36,34 +36,38 @@ class BaseAAIKeywords(object): self.aai_endpoint = aai_server_protocol + '://' + aai_ip_addr + ':' + aai_server_port @keyword - def run_get_request(self, endpoint, data_path, accept="application/json", auth=None): + def run_get_request(self, endpoint, data_path, accept="application/json", auth=None, client_certs=None): """Runs an AAI get request""" - return self.reqs.get_request("aai", endpoint, data_path, sdc_user=None, accept=accept, auth=auth) + return self.reqs.get_request(alias="aai", endpoint=endpoint, data_path=data_path, accept=accept, auth=auth, + client_certs=client_certs) @keyword - def run_post_request(self, endpoint, data_path, data, accept="application/json", auth=None): + def run_post_request(self, endpoint, data_path, data, accept="application/json", auth=None, client_certs=None): """Runs an AAI post request""" - return self.reqs.post_request("aai", endpoint, data_path, data, sdc_user=None, files=None, - accept=accept, auth=auth) + return self.reqs.post_request(alias="aai", endpoint=endpoint, data_path=data_path, data=data, accept=accept, + auth=auth, client_certs=client_certs) @keyword - def run_put_request(self, endpoint, data_path, data, accept="application/json", auth=None): + def run_put_request(self, endpoint, data_path, data, accept="application/json", auth=None, client_certs=None): """Runs an AAI post request""" - return self.reqs.put_request("aai", endpoint, data_path, data, sdc_user=None, accept=accept, auth=auth) + return self.reqs.put_request(alias="aai", endpoint=endpoint, data_path=data_path, data=data, accept=accept, + auth=auth, client_certs=client_certs) @keyword - def run_delete_request(self, endpoint, data_path, resource_version, accept="application/json", auth=None): + def run_delete_request(self, endpoint, data_path, resource_version, accept="application/json", auth=None, + client_certs=None): """Runs an AAI delete request""" - return self.reqs.delete_request("aai", endpoint, data_path + '?resource-version=' + resource_version, - data=None, sdc_user=None, accept=accept, auth=auth) + return self.reqs.delete_request(alias="aai", endpoint=endpoint, accept=accept, auth=auth, + client_certs=client_certs, + data_path=data_path + '?resource-version=' + resource_version) @keyword - def wait_for_node_to_exist(self, search_node_type, key, uuid, auth=None): + def wait_for_node_to_exist(self, search_node_type, key, uuid, auth=None, client_certs=None): logger.info('Waiting for AAI traversal to complete...') for i in range(30): logger.trace("running iteration " + str(i)) time.sleep(1) - result = self.find_node(search_node_type, key, uuid, auth=auth) + result = self.find_node(search_node_type, key, uuid, auth=auth, client_certs=client_certs) if result: return result @@ -73,9 +77,10 @@ class BaseAAIKeywords(object): self.builtin.fail(error_message) @keyword - def find_node(self, search_node_type, key, node_uuid, auth=None): + def find_node(self, search_node_type, key, node_uuid, auth=None, client_certs=None): data_path = '/aai/v11/search/nodes-query?search-node-type={0}&filter={1}:EQUALS:{2}'.format( search_node_type, key, node_uuid) - resp = self.reqs.get_request("aai", self.aai_endpoint, data_path, accept="application/json", auth=auth) + resp = self.reqs.get_request("aai", self.aai_endpoint, data_path, accept="application/json", auth=auth, + client_certs=client_certs) response = resp.json() return 'result-data' in response diff --git a/robotframework-onap/ONAPLibrary/BaseCLAMPKeywords.py b/robotframework-onap/ONAPLibrary/BaseCLAMPKeywords.py new file mode 100644 index 0000000..fb67a99 --- /dev/null +++ b/robotframework-onap/ONAPLibrary/BaseCLAMPKeywords.py @@ -0,0 +1,48 @@ +# Copyright 2019 AT&T Intellectual Property. All rights reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +from robot.api.deco import keyword +from ONAPLibrary.RequestsHelper import RequestsHelper + + +class BaseCLAMPKeywords(object): + """The main interface for interacting with CLAMP. It handles low level stuff like managing the http request library + and required fields. """ + + def __init__(self): + super(BaseCLAMPKeywords, self).__init__() + self.reqs = RequestsHelper() + + @keyword + def run_get_request(self, endpoint, data_path, accept="application/json", auth=None): + """Runs an CLAMP get request""" + return self.reqs.get_request(alias="clamp", endpoint=endpoint, data_path=data_path, accept=accept, auth=auth) + + @keyword + def run_post_request(self, endpoint, data_path, data, accept="application/json", auth=None): + """Runs an CLAMP post request""" + return self.reqs.post_request(alias="clamp", endpoint=endpoint, data_path=data_path, data=data, accept=accept, + auth=auth) + + @keyword + def run_put_request(self, endpoint, data_path, data, accept="application/json", auth=None): + """Runs an CLAMP post request""" + return self.reqs.put_request(alias="clamp", endpoint=endpoint, data_path=data_path, data=data, accept=accept, + auth=auth) + + @keyword + def run_delete_request(self, endpoint, data_path, data, accept="application/json", auth=None): + """Runs an CLAMP delete request""" + return self.reqs.delete_request(alias="clamp", endpoint=endpoint, data_path=data_path, data=data, accept=accept, + auth=auth) diff --git a/robotframework-onap/ONAPLibrary/BaseSDCKeywords.py b/robotframework-onap/ONAPLibrary/BaseSDCKeywords.py index 6f71078..ea97f94 100644 --- a/robotframework-onap/ONAPLibrary/BaseSDCKeywords.py +++ b/robotframework-onap/ONAPLibrary/BaseSDCKeywords.py @@ -31,25 +31,29 @@ class BaseSDCKeywords(object): @keyword def run_get_request(self, endpoint, data_path, user, accept="application/json", auth=None): """Runs an SDC get request""" - return self.reqs.get_request("sdc", endpoint, data_path, sdc_user=user, accept=accept, auth=auth) + return self.reqs.get_request(alias="sdc", endpoint=endpoint, data_path=data_path, sdc_user=user, accept=accept, + auth=auth) @keyword def run_post_request(self, endpoint, data_path, data, user, accept="application/json", auth=None): """Runs an SDC post request""" - return self.reqs.post_request("sdc", endpoint, data_path, data, user, files=None, accept=accept, auth=auth) + return self.reqs.post_request(alias="sdc", endpoint=endpoint, data_path=data_path, data=data, sdc_user=user, + accept=accept, auth=auth) @keyword def run_post_files_request(self, endpoint, data_path, files, user, accept="application/json", auth=None): """Runs an SDC post files request""" - return self.reqs.post_request("sdc", endpoint, data_path, None, user, files=files, accept=accept, - content_type="multipart/form-data", auth=auth) + return self.reqs.post_request(alias="sdc", endpoint=endpoint, data_path=data_path, sdc_user=user, files=files, + accept=accept, content_type="multipart/form-data", auth=auth) @keyword def run_put_request(self, endpoint, data_path, data, user, accept="application/json", auth=None): """Runs an SDC post request""" - return self.reqs.put_request("sdc", endpoint, data_path, data, sdc_user=user, accept=accept, auth=auth) + return self.reqs.put_request(alias="sdc", endpoint=endpoint, data_path=data_path, data=data, sdc_user=user, + accept=accept, auth=auth) @keyword def run_delete_request(self, endpoint, data_path, data, user, accept="application/json", auth=None): """Runs an SDC delete request""" - return self.reqs.delete_request("sdc", endpoint, data_path, data, sdc_user=user, accept=accept, auth=auth) + return self.reqs.delete_request(alias="sdc", endpoint=endpoint, data_path=data_path, data=data, sdc_user=user, + accept=accept, auth=auth) diff --git a/robotframework-onap/ONAPLibrary/BaseSDNCKeywords.py b/robotframework-onap/ONAPLibrary/BaseSDNCKeywords.py index 14970ff..bbf1520 100644 --- a/robotframework-onap/ONAPLibrary/BaseSDNCKeywords.py +++ b/robotframework-onap/ONAPLibrary/BaseSDNCKeywords.py @@ -30,16 +30,18 @@ class BaseSDNCKeywords(object): @keyword def run_get_request(self, endpoint, data_path, accept="application/json", auth=None): """Runs an SDNC get request""" - resp = self.reqs.get_request("sdnc", endpoint, data_path, sdc_user=None, accept=accept, auth=auth) + resp = self.reqs.get_request(alias="sdnc", endpoint=endpoint, data_path=data_path, accept=accept, auth=auth) self.builtin.should_be_equal_as_strings(resp.status_code, "200") return resp @keyword def run_post_request(self, endpoint, data_path, data, accept="application/json", auth=None): """Runs an SDNC post request""" - return self.reqs.post_request("sdnc", endpoint, data_path, data, sdc_user=None, accept=accept, auth=auth) + return self.reqs.post_request(alias="sdnc", endpoint=endpoint, data_path=data_path, data=data, accept=accept, + auth=auth) @keyword def run_put_request(self, endpoint, data_path, data, accept="application/json", auth=None): """Runs an SDNC post request""" - return self.reqs.put_request("sdnc", endpoint, data_path, data, sdc_user=None, accept=accept, auth=auth) + return self.reqs.put_request(alias="sdnc", endpoint=endpoint, data_path=data_path, data=data, accept=accept, + auth=auth) diff --git a/robotframework-onap/ONAPLibrary/BaseSOKeywords.py b/robotframework-onap/ONAPLibrary/BaseSOKeywords.py index d86631b..d5401ea 100644 --- a/robotframework-onap/ONAPLibrary/BaseSOKeywords.py +++ b/robotframework-onap/ONAPLibrary/BaseSOKeywords.py @@ -30,21 +30,24 @@ class BaseSOKeywords(object): @keyword def run_get_request(self, endpoint, data_path, accept="application/json", auth=None): """Runs an SO get request""" - resp = self.reqs.get_request("so", endpoint, data_path, accept=accept, auth=auth) + resp = self.reqs.get_request(alias="so", endpoint=endpoint, data_path=data_path, accept=accept, auth=auth) self.builtin.should_be_equal_as_strings(resp.status_code, "200") return resp @keyword def run_post_request(self, endpoint, data_path, data, accept="application/json", auth=None): """Runs an SO post request""" - return self.reqs.post_request("so", endpoint, data_path, data, accept=accept, auth=auth) + return self.reqs.post_request(alias="so", endpoint=endpoint, data_path=data_path, data=data, accept=accept, + auth=auth) @keyword def run_put_request(self, endpoint, data_path, data, accept="application/json", auth=None): """Runs an SO post request""" - return self.reqs.put_request("so", endpoint, data_path, data, accept=accept, auth=auth) + return self.reqs.put_request(alias="so", endpoint=endpoint, data_path=data_path, data=data, accept=accept, + auth=auth) @keyword def run_delete_request(self, endpoint, data_path, data, accept="application/json", auth=None): """Runs an SO delete request""" - return self.reqs.delete_request("so", endpoint, data_path, data=data, accept=accept, auth=auth) + return self.reqs.delete_request(alias="so", endpoint=endpoint, data_path=data_path, data=data, accept=accept, + auth=auth) diff --git a/robotframework-onap/ONAPLibrary/CLAMP.py b/robotframework-onap/ONAPLibrary/CLAMP.py new file mode 100644 index 0000000..64361ba --- /dev/null +++ b/robotframework-onap/ONAPLibrary/CLAMP.py @@ -0,0 +1,29 @@ +# Copyright 2019 AT&T Intellectual Property. All rights reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +from ONAPLibrary.BaseCLAMPKeywords import BaseCLAMPKeywords +from ONAPLibrary.robotlibcore import HybridCore + + +class CLAMP(HybridCore): + """The main interface for interacting with CLAMP. It handles low level stuff like managing the http request + library and CLAMP required fields """ + + ROBOT_LIBRARY_SCOPE = "GLOBAL" + + def __init__(self): + self.keyword_implementors = [ + BaseCLAMPKeywords() + ] + HybridCore.__init__(self, self.keyword_implementors) diff --git a/robotframework-onap/ONAPLibrary/CloudConfigSOKeywords.py b/robotframework-onap/ONAPLibrary/CloudConfigSOKeywords.py index c40860f..fb8808d 100644 --- a/robotframework-onap/ONAPLibrary/CloudConfigSOKeywords.py +++ b/robotframework-onap/ONAPLibrary/CloudConfigSOKeywords.py @@ -38,7 +38,7 @@ class CloudConfigSOKeywords(object): """Creates a cloud configuration in SO, so it knows how to talk to an openstack cloud""" self.templating.create_environment("so", templates_folder) data = self.templating.apply_template("so", template, arguments) - resp = self.reqs.post_request("so", endpoint, data_path, data, auth=auth) + resp = self.reqs.post_request(alias="so", endpoint=endpoint, data_path=data_path, data=data, auth=auth) self.builtin.should_match_regexp(str(resp.status_code), "^(201|200)$") @keyword @@ -48,7 +48,7 @@ class CloudConfigSOKeywords(object): self.templating.create_environment("so", templates_folder) data = self.templating.apply_template("so", template, arguments) if get_resp.status_code == 404: - resp = self.reqs.post_request("so", endpoint, data_path, data, auth=auth) + resp = self.reqs.post_request(alias="so", endpoint=endpoint, data_path=data_path, data=data, auth=auth) else: - resp = self.reqs.put_request("so", endpoint, data_path, data, auth=auth) + resp = self.reqs.put_request(alias="so", endpoint=endpoint, data_path=data_path, data=data, auth=auth) self.builtin.should_match_regexp(str(resp.status_code), "^(201|200)$") diff --git a/robotframework-onap/ONAPLibrary/MUSICKeywords.py b/robotframework-onap/ONAPLibrary/MUSICKeywords.py index e942fea..2a9364a 100644 --- a/robotframework-onap/ONAPLibrary/MUSICKeywords.py +++ b/robotframework-onap/ONAPLibrary/MUSICKeywords.py @@ -30,8 +30,7 @@ class MUSICKeywords(object): @keyword def run_get_request(self, endpoint, data_path, accept="application/json", auth=None): """Runs an MUSIC get request""" - resp = self.reqs.get_request("music", endpoint, data_path, accept, auth) - return resp + return self.reqs.get_request(alias="music", endpoint=endpoint, data_path=data_path, accept=accept, auth=auth) def run_health_check(self, endpoint, health_check_path): """Runs MUSIC Health check""" diff --git a/robotframework-onap/ONAPLibrary/PreloadSDNCKeywords.py b/robotframework-onap/ONAPLibrary/PreloadSDNCKeywords.py index c42f06c..2ba224a 100644 --- a/robotframework-onap/ONAPLibrary/PreloadSDNCKeywords.py +++ b/robotframework-onap/ONAPLibrary/PreloadSDNCKeywords.py @@ -31,4 +31,4 @@ class PreloadSDNCKeywords(object): """Runs an SDNC request to preload certain data.""" self.templating.create_environment("sdnc", templates_folder) data = self.templating.apply_template("sdnc", template, preload_dictionary) - return self.reqs.post_request("sdnc", endpoint, data_path, data) + return self.reqs.post_request(alias="sdnc", endpoint=endpoint, data_path=data_path, data=data) diff --git a/robotframework-onap/ONAPLibrary/RequestSOKeywords.py b/robotframework-onap/ONAPLibrary/RequestSOKeywords.py index 9dd0977..671a306 100644 --- a/robotframework-onap/ONAPLibrary/RequestSOKeywords.py +++ b/robotframework-onap/ONAPLibrary/RequestSOKeywords.py @@ -37,7 +37,7 @@ class RequestSOKeywords(object): complete_states = ["COMPLETE"] # do this until it is done for i in range(tries): - resp = self.reqs.get_request("so", endpoint, data_path, auth=auth) + resp = self.reqs.get_request(alias="so", endpoint=endpoint, data_path=data_path, auth=auth) logger.info(resp.json()['request']['requestStatus']['requestState']) if resp.json()['request']['requestStatus']['requestState'] in fail_states: self.builtin.fail("Received failure response from so " + resp.text) @@ -51,7 +51,7 @@ class RequestSOKeywords(object): @keyword def run_create_request(self, endpoint, data_path, data, auth=None): """Runs an SO create request and returns the request id and instance id.""" - response = self.reqs.post_request("so", endpoint, data_path, data, auth=auth) + response = self.reqs.post_request(alias="so", endpoint=endpoint, data_path=data_path, data=data, auth=auth) logger.info("Creation request submitted to SO, got response") req_id = response.get('requestReferences', {}).get('requestId', '') diff --git a/robotframework-onap/ONAPLibrary/RequestsDecorators.py b/robotframework-onap/ONAPLibrary/RequestsDecorators.py new file mode 100644 index 0000000..1bb8b3b --- /dev/null +++ b/robotframework-onap/ONAPLibrary/RequestsDecorators.py @@ -0,0 +1,45 @@ +from robot.api import logger + +def log_wrapped(func): + def _log_wrapped(*args, **kwargs): + if 'endpoint' in kwargs: + endpoint = kwargs['endpoint'] + logger.info("Creating session " + endpoint) + resp = func(*args, **kwargs) + if 'alias' in kwargs: + alias = kwargs['alias'] + logger.info("Received response from [" + alias + "]: " + resp.text) + return resp + + return _log_wrapped + +def default_keywords(func): + def _default_keywords(*args, **kwargs): + dicts = _keyword_defaults(**kwargs) + return func(*args, **dicts) + + def _keyword_defaults(**kwargs): + if 'alias' not in kwargs: + raise ValueError('named attribute alias required', 'alias') + if 'endpoint' not in kwargs: + raise ValueError('named attribute required', 'endpoint') + if 'data_path' not in kwargs: + kwargs['data_path'] = None # default to whatever is in the session + if 'data' not in kwargs: + kwargs['data'] = None # default to empty body + if 'sdc_user' not in kwargs: + kwargs['sdc_user'] = None # default to no user + if 'accept' not in kwargs: + kwargs['accept'] = "application/json" # default to json + if 'content_type' not in kwargs: + kwargs['content_type'] = "application/json" # default to json + if 'auth' not in kwargs: + kwargs['auth'] = None # default to no basic auth + if 'client_certs' not in kwargs: + kwargs['client_certs'] = None # default to no client cert + if 'files' not in kwargs: + kwargs['files'] = None # default to no file + return kwargs + + return _default_keywords + diff --git a/robotframework-onap/ONAPLibrary/RequestsHelper.py b/robotframework-onap/ONAPLibrary/RequestsHelper.py index 2dba36e..7dc5903 100644 --- a/robotframework-onap/ONAPLibrary/RequestsHelper.py +++ b/robotframework-onap/ONAPLibrary/RequestsHelper.py @@ -14,10 +14,11 @@ from ONAPLibrary.UUIDKeywords import UUIDKeywords from RequestsLibrary import RequestsLibrary -from robot.api import logger import hashlib from ONAPLibrary.Base64Keywords import Base64Keywords from ONAPLibrary.HTTPKeywords import HTTPKeywords +from ONAPLibrary.RequestsDecorators import log_wrapped +from ONAPLibrary.RequestsDecorators import default_keywords class RequestsHelper(object): @@ -30,55 +31,41 @@ class RequestsHelper(object): self.requests = RequestsLibrary() self.http = HTTPKeywords() - def get_request(self, alias, endpoint, data_path, sdc_user=None, accept="application/json", auth=None, - client_certs=None): + @default_keywords + @log_wrapped + def get_request(self, **kwargs): """Runs a get request""" - self.http.disable_warnings() - logger.info("Creating session" + endpoint) - self._create_session(alias, endpoint, auth=auth, client_certs=client_certs) - headers = self._create_headers(sdc_user_id=sdc_user, accept=accept) - resp = self.requests.get_request(alias, data_path, headers=headers) - logger.info("Received response from [" + alias + "]: " + resp.text) - return resp + return self.requests.get_request(alias=kwargs['alias'], uri=kwargs['data_path'], + headers=self._perform_setup(**kwargs)) - def post_request(self, alias, endpoint, data_path, data, sdc_user=None, files=None, accept="application/json", - content_type="application/json", auth=None, client_certs=None): + @default_keywords + @log_wrapped + def post_request(self, **kwargs): """Runs a post request""" - self.http.disable_warnings() - logger.info("Creating session" + endpoint) - if data is not None: - md5 = hashlib.md5() - md5.update(data) - md5checksum = Base64Keywords().base64_encode(md5.hexdigest()) - else: - md5checksum = None - self._create_session(alias, endpoint, auth=auth, client_certs=client_certs) - headers = self._create_headers(sdc_user_id=sdc_user, accept=accept, content_type=content_type, md5=md5checksum) - resp = self.requests.post_request(alias, data_path, files=files, data=data, headers=headers) - logger.info("Received response from [" + alias + "]: " + resp.text) - return resp + kwargs['md5'] = self._format_md5(kwargs['data']) + return self.requests.post_request(alias=kwargs['alias'], uri=kwargs['data_path'], files=kwargs['files'], + data=kwargs['data'], headers=self._perform_setup(**kwargs)) - def put_request(self, alias, endpoint, data_path, data, sdc_user=None, accept="application/json", - auth=None, client_certs=None): + @default_keywords + @log_wrapped + def put_request(self, **kwargs): """Runs a put request""" - self.http.disable_warnings() - logger.info("Creating session" + endpoint) - self._create_session(alias, endpoint, auth=auth, client_certs=client_certs) - headers = self._create_headers(sdc_user_id=sdc_user, accept=accept) - resp = self.requests.put_request(alias, data_path, data=data, headers=headers) - logger.info("Received response from [" + alias + "]: " + resp.text) - return resp + return self.requests.put_request(alias=kwargs['alias'], uri=kwargs['data_path'], data=kwargs['data'], + headers=self._perform_setup(**kwargs)) - def delete_request(self, alias, endpoint, data_path, data=None, sdc_user=None, accept="application/json", - auth=None, client_certs=None): + @default_keywords + @log_wrapped + def delete_request(self, **kwargs): """Runs a delete request""" + return self.requests.delete_request(alias=kwargs['alias'], uri=kwargs['data_path'], data=kwargs['data'], + headers=self._perform_setup(**kwargs)) + + def _perform_setup(self, **kwargs): self.http.disable_warnings() - logger.info("Creating session" + endpoint) - self._create_session(alias, endpoint, auth=auth, client_certs=client_certs) - headers = self._create_headers(sdc_user_id=sdc_user, accept=accept) - resp = self.requests.delete_request(alias, data_path, data=data, headers=headers) - logger.info("Received response from [" + alias + "]: " + resp.text) - return resp + self._create_session(alias=kwargs['alias'], endpoint=kwargs['endpoint'], auth=kwargs['auth'], + client_certs=kwargs['client_certs']) + return self._create_headers(sdc_user_id=kwargs['sdc_user'], accept=kwargs['accept'], + content_type=kwargs['content_type'], md5=kwargs.get("md5", None)) def _create_session(self, alias, endpoint, auth=None, client_certs=None): if client_certs is not None: @@ -100,3 +87,12 @@ class RequestsHelper(object): if md5 is not None: headers["Content-MD5"] = md5 return headers + + @staticmethod + def _format_md5(md5_input): + if md5_input is not None: + md5 = hashlib.md5() + md5.update(md5_input) + return Base64Keywords().base64_encode(md5.hexdigest()) + else: + return None \ No newline at end of file diff --git a/robotframework-onap/ONAPLibrary/SNIROKeywords.py b/robotframework-onap/ONAPLibrary/SNIROKeywords.py index 34e371d..3a181dd 100644 --- a/robotframework-onap/ONAPLibrary/SNIROKeywords.py +++ b/robotframework-onap/ONAPLibrary/SNIROKeywords.py @@ -35,14 +35,15 @@ class SNIROKeywords(object): @keyword def run_sniro_get_request(self, endpoint, data_path, accept="application/json", auth=None): """Runs OOF-SNIRO Get request""" - resp = self.reqs.get_request("oof-sniro", endpoint, data_path, accept, auth) + resp = self.reqs.get_request(alias="oof-sniro", endpoint=endpoint, data_path=data_path, accept=accept, + auth=auth) self.builtin.should_be_equal_as_strings(resp.status_code, "200") return resp @keyword def reset_sniro(self, endpoint): logger.debug('Clearing SNIRO data') - resp = self.reqs.post_request("oof-sniro", endpoint, '/reset', None) + resp = self.reqs.post_request(alias="oof-sniro", endpoint=endpoint, data_path='/reset') self.builtin.should_be_equal_as_strings(resp.status_code, "200", 'Clearing SNIRO date failed.') @keyword @@ -60,6 +61,6 @@ class SNIROKeywords(object): base64_sniro_data = self.base64.base64_encode(sniro_data) replace_dict = {'base64_sniro_data': base64_sniro_data} sniro_request = self.templating.apply_template("sniro", template_sniro_request, replace_dict) - resp = self.reqs.post_request("oof-sniro", endpoint, '/', sniro_request) + resp = self.reqs.post_request(alias="oof-sniro", endpoint=endpoint, data_path='/', data=sniro_request) self.builtin.should_be_equal_as_strings(resp.status_code, "200", 'SNIRO preloading failed.') return True diff --git a/robotframework-onap/setup.py b/robotframework-onap/setup.py index 2718b40..34191b9 100644 --- a/robotframework-onap/setup.py +++ b/robotframework-onap/setup.py @@ -58,5 +58,9 @@ setup( 'Framework :: Robot Framework :: Library', 'License :: OSI Approved :: Apache Software License' ], - test_suite="tests.runner" + test_suite="tests.runner", + tests_require=[ + 'mock', + 'requests-mock' + ] ) diff --git a/robotframework-onap/tests/ONAPLibrary/RequestsHelperTests.py b/robotframework-onap/tests/ONAPLibrary/RequestsHelperTests.py new file mode 100644 index 0000000..29a0584 --- /dev/null +++ b/robotframework-onap/tests/ONAPLibrary/RequestsHelperTests.py @@ -0,0 +1,50 @@ +# Copyright 2019 AT&T Intellectual Property. All rights reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import requests_mock +from unittest import TestCase +from ONAPLibrary.RequestsHelper import RequestsHelper + + +class RequestsHelperTests(TestCase): + + def test_get(self): + with requests_mock.mock() as m: + rh = RequestsHelper() + m.get('http://test.com/', text='data') + resp = rh.get_request(alias="alias", endpoint="http://test.com", data_path="/", sdc_user="test123", + accept="application/json", auth={"user", "pass"}) + self.assertEqual("data", resp.text) + + def test_put(self): + with requests_mock.mock() as m: + rh = RequestsHelper() + m.put('http://test.com/', text='data') + resp = rh.put_request(alias="alias", endpoint="http://test.com", data="data", client_certs={"ca", "pem"}) + self.assertEqual("data", resp.text) + + def test_delete(self): + with requests_mock.mock() as m: + rh = RequestsHelper() + m.delete('http://test.com/', text='data') + resp = rh.delete_request(alias="alias", endpoint="http://test.com", data="data", client_certs={"ca", "pem"}) + self.assertEqual("data", resp.text) + + def test_post(self): + with requests_mock.mock() as m: + rh = RequestsHelper() + m.get('http://test.com/', text='data') + resp = rh.get_request(alias="alias", endpoint="http://test.com", data_path="/", sdc_user="test123", + accept="application/json", content_type="application/json", files="test/123") + self.assertEqual("data", resp.text) \ No newline at end of file diff --git a/robotframework-onap/tests/runner.py b/robotframework-onap/tests/runner.py index f9a57f5..a2fd5b7 100644 --- a/robotframework-onap/tests/runner.py +++ b/robotframework-onap/tests/runner.py @@ -8,6 +8,7 @@ from tests.ONAPLibrary.ProtobufKeywordsTest import ProtobufKeywordsTest from tests.ONAPLibrary.UUIDKeywordsTest import UUIDKeywordsTest from tests.ONAPLibrary.ServiceMappingKeywordsTests import ServiceMappingKeywordsTests from tests.ONAPLibrary.Base64KeywordsTests import Base64KeywordsTests +from tests.ONAPLibrary.RequestsHelperTests import RequestsHelperTests # initialize the test suite loader = TestLoader() @@ -19,6 +20,7 @@ suite.addTests(loader.loadTestsFromTestCase(SoUtilsTest)) suite.addTests(loader.loadTestsFromTestCase(UUIDKeywordsTest)) suite.addTests(loader.loadTestsFromTestCase(ServiceMappingKeywordsTests)) suite.addTests(loader.loadTestsFromTestCase(Base64KeywordsTests)) +suite.addTests(loader.loadTestsFromTestCase(RequestsHelperTests)) # initialize a runner, pass it your suite and run it runner = TextTestRunner(verbosity=3) -- cgit 1.2.3-korg