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