1 # -------------------------------------------------------------------------
2 # Copyright (c) 2018 AT&T Intellectual Property
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
8 # http://www.apache.org/licenses/LICENSE-2.0
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.
16 # -------------------------------------------------------------------------
20 from flask import Flask
23 from osdf.adapters.aaf import aaf_authentication as auth
24 from osdf.utils.interfaces import RestClient
26 BASE_DIR = os.path.dirname(__file__)
29 class TestAafAuthentication():
31 def test_authenticate(self):
35 def mock_aaf_response(*args, **kwargs):
36 return {"perm": [{"instance": "menu_ecd", "action": "*", "type": "org.onap.oof.controller.dev.menu"},
37 {"instance": "*", "action": "read", "type": "org.onap.oof.access"},
38 {"instance": "aaf", "action": "request", "type": "org.onap.osdf.certman"},
39 {"instance": "*", "action": "*", "type": "org.onap.osdf.dev.access"},
40 {"instance": ":*:*", "action": "*", "type": "org.onap.osdf.dev.k8"},
41 {"instance": ":*:*", "action": "*", "type": "org.onap.osdf.ist.k8"}]}
43 with app.test_request_context(path='/api/oof/v1/placement'):
44 with mock.patch.object(RestClient, 'request', side_effect=mock_aaf_response):
45 assert auth.authenticate('user', 'password')
47 def test_auth_cache(self):
51 def mock_aaf_response(*args, **kwargs):
52 return {"perm": [{"instance": "menu_ecd", "action": "*", "type": "org.onap.osdf.controller.dev.menu"},
53 {"instance": "*", "action": "read", "type": "org.onap.oof.access"},
54 {"instance": "aaf", "action": "request", "type": "org.onap.osdf.certman"},
55 {"instance": "*", "action": "*", "type": "org.onap.osdf.dev.access"},
56 {"instance": ":*:*", "action": "*", "type": "org.onap.osdf.dev.k8"},
57 {"instance": ":*:*", "action": "*", "type": "org.onap.osdf.ist.k8"}]}
59 with app.test_request_context(path='/api/oof/v1/placement'):
60 with mock.patch.object(RestClient, 'request', side_effect=mock_aaf_response):
61 assert auth.authenticate('user', 'password')
62 assert auth.authenticate('user', 'password')
64 def test_authenticate_fail(self):
68 def mock_aaf_response(*args, **kwargs):
69 return {"perm": [{"instance": "menu_ecd", "action": "*", "type": "org.onap.oof.controller.dev.menu"}]}
71 with app.test_request_context(path='/api/oof/v1/placement'):
72 with mock.patch.object(RestClient, 'request', side_effect=mock_aaf_response):
73 assert not auth.authenticate('user1', 'password1')
75 def test_authenticate_uri_mismatch(self):
79 def mock_aaf_response(*args, **kwargs):
80 return {"perm": [{"instance": "menu_ecd", "action": "*", "type": "org.onap.oof.controller.dev.menu"},
81 {"instance": "*", "action": "*", "type": "org.onap.oof.access"},
82 {"instance": "aaf", "action": "request", "type": "org.onap.osdf.certman"},
83 {"instance": "*", "action": "*", "type": "org.onap.osdf.dev.access"},
84 {"instance": ":*:*", "action": "*", "type": "org.onap.osdf.dev.k8"},
85 {"instance": ":*:*", "action": "*", "type": "org.onap.osdf.ist.k8"}]}
87 with app.test_request_context(path='/sniro/wrong/uri'):
88 with mock.patch.object(RestClient, 'request', side_effect=mock_aaf_response):
89 assert not auth.authenticate('user', 'password')
91 def test_authenticate_fail1(self):
95 def mock_aaf_response(*args, **kwargs):
98 with app.test_request_context(path='/api/oof/v1/placement'):
99 with mock.patch.object(RestClient, 'request', side_effect=mock_aaf_response):
100 assert not auth.authenticate('user2', 'password2')
102 def test_authenticate_fail3(self):
103 app = Flask(__name__)
106 def mock_aaf_response2(*args, **kwargs):
109 with app.test_request_context(path='/api/oof/v1/placement'):
110 with mock.patch.object(RestClient, 'request', side_effect=mock_aaf_response2):
111 assert not auth.authenticate('user3', 'password3')
113 def test_authenticate_except(self):
114 app = Flask(__name__)
117 def mock_aaf_response2(*args, **kwargs):
118 raise Exception('This is the exception you expect to handle')
120 with app.test_request_context(path='/api/oof/v1/placement'):
121 with mock.patch.object(RestClient, 'request', side_effect=mock_aaf_response2):
122 assert not auth.authenticate('user3', 'password3')