osdf rearchitecture into apps and libs
[optf/osdf.git] / test / mainapp / test_osdfapp.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
19 import unittest
20
21 from osdf.apps import baseapp
22 from osdf.operation.exceptions import BusinessException
23 from requests import Request, RequestException
24 from schematics.exceptions import DataError
25 from unittest import mock, TestCase
26 from unittest.mock import patch
27
28
29 class TestOSDFApp(TestCase):
30
31     def setUp(self):
32         self.patcher_g = patch('osdf.apps.baseapp.g', return_value={'request_id':'DUMMY-REQ'})
33         self.Mock_g = self.patcher_g.start()
34         # self.patcher2 = patch('package.module.Class2')
35         # self.MockClass2 = self.patcher2.start()
36
37     def tearDown(self):
38         patch.stopall()
39
40     def dummy_request_exception(self):
41         e = RequestException("Web Request Exception Description")
42         e.response = mock.MagicMock()
43         e.request = Request(method="GET", url="SOME-URL")
44         e.response.status_code = 400
45         e.response.content = "Some request exception occurred"
46         # request().raise_for_status.side_effect = e
47         return e
48  
49     def test_handle_business_exception(self):
50         e = BusinessException("Business Exception Description")
51         resp = baseapp.handle_business_exception(e)
52         assert resp.status_code == 400
53
54     def test_handle_request_exception(self):
55         e = self.dummy_request_exception()
56         resp = baseapp.handle_request_exception(e)
57         assert resp.status_code == 400
58
59     def test_handle_data_error(self):
60         e = DataError({"A1": "A1 Data Error"})
61         resp = baseapp.handle_data_error(e)
62         assert resp.status_code == 400
63
64     def test_internal_failure(self):
65         e = Exception("An Internal Error")
66         resp = baseapp.internal_failure(e)
67         assert resp.status_code == 500
68
69     def test_get_options_default(self):
70         opts = baseapp.get_options(["PROG"])  # ensure nothing breaks
71
72
73 if __name__ == "__main__":
74     unittest.main()
75