cdb3d7a86904b61af06677d29a87e9f00219576b
[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 import unittest
20
21 from requests.models import Response
22 from osdf.utils.interfaces import RestClient, get_rest_client
23 from unittest.mock import patch
24
25
26 m1 = Response()
27 m1._content = b'{"msg": "OK"}'
28 m1.status_code = 202
29 mock_good_response = m1
30
31 m2 = Response()
32 m2._content = b'{"msg": "Not-OK"}'
33 m2.status_code = 403
34 mock_bad_response = m2
35
36
37 class TestOsdfUtilsInterfaces(unittest.TestCase):
38     @patch('requests.request', return_value=mock_good_response)
39     def test_rc_request(self, mock_good_response):
40         rc = RestClient()
41         rc.add_headers({})
42         rc.request(req_id="testReq")
43
44     @patch('requests.request', return_value=mock_good_response)
45     def test_rc_request_v1(self, mock_good_response):
46         rc = RestClient()
47         rc.add_headers({})
48         rc.request(url="http://localhost", asjson=False, log_func=lambda x: None)
49         rc.request(url="http://localhost", raw_response=True)
50         rc.request(url="http://localhost", no_response=True)
51
52     @patch('requests.request', return_value=mock_bad_response)
53     def test_rc_request_v2(self, mock_bad_response):
54         rc = RestClient()
55         try:
56             rc.request()
57         except requests.RequestException:
58             return
59         raise Exception("Allows bad requests instead of raising exception")
60
61     def test_get_rest_client(self):
62         request_json = {"requestInfo": {"callbackUrl": "http://localhost"}}
63         service = "so"
64         get_rest_client(request_json, service)
65
66
67 if __name__ == "__main__":
68     unittest.main()
69