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 # -------------------------------------------------------------------------
19 from flask import Flask
22 from osdf.adapters.aaf import aaf_authentication as auth
23 from osdf.utils.interfaces import RestClient
25 BASE_DIR = os.path.dirname(__file__)
28 class TestAafAuthentication():
30 def test_authenticate(self):
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"}]}
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')
46 def test_auth_cache(self):
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"}]}
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')
63 def test_authenticate_fail(self):
67 def mock_aaf_response(*args, **kwargs):
68 return {"perm": [{"instance": "menu_ecd", "action": "*", "type": "org.onap.oof.controller.dev.menu"}]}
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')
74 def test_authenticate_uri_mismatch(self):
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"}]}
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')
90 def test_authenticate_fail1(self):
94 def mock_aaf_response(*args, **kwargs):
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')
101 def test_authenticate_fail3(self):
102 app = Flask(__name__)
105 def mock_aaf_response2(*args, **kwargs):
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')
112 def test_authenticate_except(self):
113 app = Flask(__name__)
116 def mock_aaf_response2(*args, **kwargs):
117 raise Exception('This is the exception you expect to handle')
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')