fix md5 use to call correct object
[testsuite/python-testing-utils.git] / robotframework-onap / ONAPLibrary / BaseSDCKeywords.py
1 # Copyright 2019 AT&T Intellectual Property. All rights reserved.
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 #         http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14 from RequestsLibrary import RequestsLibrary
15 from robot.api import logger
16 from robot.api.deco import keyword
17 from robot.libraries.BuiltIn import BuiltIn
18
19 import hashlib
20
21 from ONAPLibrary.Base64Keywords import Base64Keywords
22 from ONAPLibrary.UUIDKeywords import UUIDKeywords
23
24
25 class BaseSDCKeywords(object):
26     """The main interface for interacting with SDC. It handles low level stuff like managing the http request library
27     and required fields. """
28
29     def __init__(self):
30         super(BaseSDCKeywords, self).__init__()
31         self.application_id = "robot-ete"
32         self.uuid = UUIDKeywords()
33         self.builtin = BuiltIn()
34
35     @keyword
36     def run_get_request(self, endpoint, data_path, user, accept="application/json", auth=None):
37         """Runs an SDC get request"""
38         resp = self.get_request(endpoint, data_path, user, accept, auth)
39         self.builtin.should_be_equal_as_strings(resp.status_code, "200")
40         return resp
41
42     @keyword
43     def run_post_request(self, endpoint, data_path, data, user, accept="application/json", auth=None):
44         """Runs an SDC post request"""
45         return self.post_request(endpoint, data_path, data, user, files=None, accept=accept, auth=auth)
46
47     @keyword
48     def run_post_files_request(self, endpoint, data_path, files, user, accept="application/json", auth=None):
49         """Runs an SDC post files request"""
50         return self.post_request(endpoint, data_path, files, user, files=None, accept=accept,
51                                  content_type="multipart/form-data", auth=auth)
52
53     @keyword
54     def run_put_request(self, endpoint, data_path, data, user, accept="application/json", auth=None):
55         """Runs an SDC post request"""
56         return self.put_request(endpoint, data_path, data, user, accept, auth)
57
58     def get_request(self, endpoint, data_path, user, accept="application/json", auth=None):
59         """Runs an SDC get request"""
60         logger.info("Creating session" + endpoint)
61         RequestsLibrary().create_session("sdc", endpoint, auth=auth)
62         resp = RequestsLibrary().get_request("sdc", data_path, headers=self.create_headers(user, accept))
63         logger.info("Received response from sdc " + resp.text)
64         return resp
65
66     def create_headers(self, user=None, accept="application/json", content_type="application/json", md5=None):
67         """Create the headers that are used by sdc"""
68         uuid = self.uuid.generate_uuid4()
69         headers = {
70             "Accept": accept,
71             "Content-Type": content_type,
72             "X-TransactionId": self.application_id + "-" + uuid,
73             "X-FromAppId": self.application_id
74         }
75         if not user:
76             headers["USER_ID"] = user
77         if not md5:
78             headers["Content-MD5"] = md5
79         return headers
80
81     def post_request(self, endpoint, data_path, data, user, files=None, accept="application/json",
82                      content_type="application/json", auth=None):
83         """Runs an SDC post request"""
84         logger.info("Creating session" + endpoint)
85         md5 = hashlib.md5()
86         md5.update(data)
87         md5checksum = Base64Keywords().base64_encode(md5.hexdigest())
88         RequestsLibrary().create_session("sdc", endpoint, auth=auth)
89         headers = self.create_headers(user, accept=accept, content_type=content_type, md5=md5checksum)
90         resp = RequestsLibrary().post_request("sdc", data_path, files=files, data=data, headers=headers)
91
92         logger.info("Received response from sdc " + resp.text)
93         return resp
94
95     def put_request(self, endpoint, data_path, data, user, accept="application/json", auth=None):
96         """Runs an SDC post request"""
97         logger.info("Creating session" + endpoint)
98         RequestsLibrary().create_session("sdc", endpoint, auth=auth)
99         resp = RequestsLibrary().put_request("sdc", data_path, data=data, headers=self.create_headers(user, accept))
100         logger.info("Received response from sdc " + resp.text)
101         return resp
102
103     def delete_request(self, endpoint, data_path, data, user, accept="application/json", auth=None):
104         """Runs an SDC post request"""
105         logger.info("Creating session" + endpoint)
106         RequestsLibrary().create_session("sdc", endpoint, auth=auth)
107         resp = RequestsLibrary().delete_request("sdc", data_path, data=data, headers=self.create_headers(user, accept))
108         logger.info("Received response from sdc " + resp.text)
109         return resp