Remove ca-cert from docker image
[optf/osdf.git] / test / osdf / utils / test_interfaces.py
1 # -------------------------------------------------------------------------
2 #   Copyright (c) 2017-2018 AT&T Intellectual Property
3 #
4 #   Licensed under the Apache License, Version 2.0 (the "License");
5 #   you may not use this file except in compliance with the License.
6 #   You may obtain a copy of the License at
7 #
8 #       http://www.apache.org/licenses/LICENSE-2.0
9 #
10 #   Unless required by applicable law or agreed to in writing, software
11 #   distributed under the License is distributed on an "AS IS" BASIS,
12 #   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 #   See the License for the specific language governing permissions and
14 #   limitations under the License.
15 #
16 # -------------------------------------------------------------------------
17 #
18 import requests
19 from requests.models import Response
20 import unittest
21 from unittest.mock import patch
22
23 from osdf.utils.interfaces import get_rest_client
24 from osdf.utils.interfaces import RestClient
25
26
27 m1 = Response()
28 m1._content = b'{"msg": "OK"}'
29 m1.status_code = 202
30 mock_good_response = m1
31
32 m2 = Response()
33 m2._content = b'{"msg": "Not-OK"}'
34 m2.status_code = 403
35 mock_bad_response = m2
36
37
38 class TestOsdfUtilsInterfaces(unittest.TestCase):
39     @patch('requests.request', return_value=mock_good_response)
40     def test_rc_request(self, mock_good_response):
41         rc = RestClient()
42         rc.add_headers({})
43         rc.request(url="http://localhost", req_id="testReq")
44
45     @patch('requests.request', return_value=mock_good_response)
46     def test_rc_request_v1(self, mock_good_response):
47         rc = RestClient()
48         rc.add_headers({})
49         rc.request(url="http://localhost", asjson=False, log_func=lambda x: None)
50         rc.request(url="http://localhost", raw_response=True)
51         rc.request(url="http://localhost", no_response=True)
52
53     @patch('requests.request', return_value=mock_bad_response)
54     def test_rc_request_v2(self, mock_bad_response):
55         rc = RestClient()
56         try:
57             rc.request(url="http://localhost")
58         except requests.RequestException:
59             return
60         raise Exception("Allows bad requests instead of raising exception")
61
62     def test_get_rest_client(self):
63         request_json = {"requestInfo": {"callbackUrl": "http://localhost"}}
64         service = "so"
65         get_rest_client(request_json, service)
66
67
68 if __name__ == "__main__":
69     unittest.main()