Enabled CVS for the OSDF placement API
[optf/osdf.git] / test / test_aaf_authentication.py
1 # -------------------------------------------------------------------------
2 #   Copyright (c) 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 os
19 from flask import Flask
20 from mock import mock
21
22 from osdf.adapters.aaf import aaf_authentication as auth
23 from osdf.utils.interfaces import RestClient
24
25 BASE_DIR = os.path.dirname(__file__)
26
27
28 class TestAafAuthentication():
29
30     def test_authenticate(self):
31         app = Flask(__name__)
32         auth.clear_cache()
33
34         def mock_aaf_response(*args, **kwargs):
35             return {"perm": [{"instance": "menu_ecd", "action": "*", "type": "org.onap.oof.controller.dev.menu"},
36                              {"instance": "*", "action": "*", "type": "org.onap.osdf.access"},
37                              {"instance": "aaf", "action": "request", "type": "org.onap.osdf.certman"},
38                              {"instance": "*", "action": "*", "type": "org.onap.osdf.dev.access"},
39                              {"instance": ":*:*", "action": "*", "type": "org.onap.osdf.dev.k8"},
40                              {"instance": ":*:*", "action": "*", "type": "org.onap.osdf.ist.k8"}]}
41
42         with app.test_request_context(path='/api/oof/v1/placement'):
43             with mock.patch.object(RestClient, 'request', side_effect=mock_aaf_response):
44                 assert auth.authenticate('user', 'password')
45
46     def test_auth_cache(self):
47         app = Flask(__name__)
48         auth.clear_cache()
49
50         def mock_aaf_response(*args, **kwargs):
51             return {"perm": [{"instance": "menu_ecd", "action": "*", "type": "org.onap.oof.controller.dev.menu"},
52                              {"instance": "*", "action": "*", "type": "org.onap.osdf.access"},
53                              {"instance": "aaf", "action": "request", "type": "org.onap.osdf.certman"},
54                              {"instance": "*", "action": "*", "type": "org.onap.osdf.dev.access"},
55                              {"instance": ":*:*", "action": "*", "type": "org.onap.osdf.dev.k8"},
56                              {"instance": ":*:*", "action": "*", "type": "org.onap.osdf.ist.k8"}]}
57
58         with app.test_request_context(path='/api/oof/v1/placement'):
59             with mock.patch.object(RestClient, 'request', side_effect=mock_aaf_response):
60                 assert auth.authenticate('user', 'password')
61                 assert auth.authenticate('user', 'password')
62
63     def test_authenticate_fail(self):
64         app = Flask(__name__)
65         auth.clear_cache()
66
67         def mock_aaf_response(*args, **kwargs):
68             return {"perm": [{"instance": "menu_ecd", "action": "*", "type": "org.onap.oof.controller.dev.menu"}]}
69
70         with app.test_request_context(path='/api/oof/v1/placement'):
71             with mock.patch.object(RestClient, 'request', side_effect=mock_aaf_response):
72                 assert not auth.authenticate('user1', 'password1')
73
74     def test_authenticate_uri_mismatch(self):
75         app = Flask(__name__)
76         auth.clear_cache()
77
78         def mock_aaf_response(*args, **kwargs):
79             return {"perm": [{"instance": "menu_ecd", "action": "*", "type": "org.onap.oof.controller.dev.menu"},
80                              {"instance": "*", "action": "*", "type": "org.onap.osdf.access"},
81                              {"instance": "aaf", "action": "request", "type": "org.onap.osdf.certman"},
82                              {"instance": "*", "action": "*", "type": "org.onap.osdf.dev.access"},
83                              {"instance": ":*:*", "action": "*", "type": "org.onap.osdf.dev.k8"},
84                              {"instance": ":*:*", "action": "*", "type": "org.onap.osdf.ist.k8"}]}
85
86         with app.test_request_context(path='/sniro/wrong/uri'):
87             with mock.patch.object(RestClient, 'request', side_effect=mock_aaf_response):
88                 assert not auth.authenticate('user', 'password')
89
90     def test_authenticate_fail1(self):
91         app = Flask(__name__)
92         auth.clear_cache()
93
94         def mock_aaf_response(*args, **kwargs):
95             return {}
96
97         with app.test_request_context(path='/api/oof/v1/placement'):
98             with mock.patch.object(RestClient, 'request', side_effect=mock_aaf_response):
99                 assert not auth.authenticate('user2', 'password2')
100
101     def test_authenticate_fail3(self):
102         app = Flask(__name__)
103         auth.clear_cache()
104
105         def mock_aaf_response2(*args, **kwargs):
106             return {}
107
108         with app.test_request_context(path='/api/oof/v1/placement'):
109             with mock.patch.object(RestClient, 'request', side_effect=mock_aaf_response2):
110                 assert not auth.authenticate('user3', 'password3')
111
112     def test_authenticate_except(self):
113         app = Flask(__name__)
114         auth.clear_cache()
115
116         def mock_aaf_response2(*args, **kwargs):
117             raise Exception('This is the exception you expect to handle')
118
119         with app.test_request_context(path='/api/oof/v1/placement'):
120             with mock.patch.object(RestClient, 'request', side_effect=mock_aaf_response2):
121                 assert not auth.authenticate('user3', 'password3')