2 from flask import Flask
5 from osdf.adapters.aaf import aaf_authentication as auth
6 from osdf.utils.interfaces import RestClient
8 BASE_DIR = os.path.dirname(__file__)
11 class TestAafAuthentication():
13 def test_authenticate(self):
17 def mock_aaf_response(*args, **kwargs):
18 return {"perm": [{"instance": "menu_ecd", "action": "*", "type": "org.onap.oof.controller.dev.menu"},
19 {"instance": "*", "action": "*", "type": "org.onap.osdf.access"},
20 {"instance": "aaf", "action": "request", "type": "org.onap.osdf.certman"},
21 {"instance": "*", "action": "*", "type": "org.onap.osdf.dev.access"},
22 {"instance": ":*:*", "action": "*", "type": "org.onap.osdf.dev.k8"},
23 {"instance": ":*:*", "action": "*", "type": "org.onap.osdf.ist.k8"}]}
25 with app.test_request_context(path='/api/oof/v1/placement'):
26 with mock.patch.object(RestClient, 'request', side_effect=mock_aaf_response):
27 assert auth.authenticate('user', 'password')
29 def test_auth_cache(self):
33 def mock_aaf_response(*args, **kwargs):
34 return {"perm": [{"instance": "menu_ecd", "action": "*", "type": "org.onap.oof.controller.dev.menu"},
35 {"instance": "*", "action": "*", "type": "org.onap.osdf.access"},
36 {"instance": "aaf", "action": "request", "type": "org.onap.osdf.certman"},
37 {"instance": "*", "action": "*", "type": "org.onap.osdf.dev.access"},
38 {"instance": ":*:*", "action": "*", "type": "org.onap.osdf.dev.k8"},
39 {"instance": ":*:*", "action": "*", "type": "org.onap.osdf.ist.k8"}]}
41 with app.test_request_context(path='/api/oof/v1/placement'):
42 with mock.patch.object(RestClient, 'request', side_effect=mock_aaf_response):
43 assert auth.authenticate('user', 'password')
44 assert auth.authenticate('user', 'password')
46 def test_authenticate_fail(self):
50 def mock_aaf_response(*args, **kwargs):
51 return {"perm": [{"instance": "menu_ecd", "action": "*", "type": "org.onap.oof.controller.dev.menu"}]}
53 with app.test_request_context(path='/api/oof/v1/placement'):
54 with mock.patch.object(RestClient, 'request', side_effect=mock_aaf_response):
55 assert not auth.authenticate('user1', 'password1')
57 def test_authenticate_uri_mismatch(self):
61 def mock_aaf_response(*args, **kwargs):
62 return {"perm": [{"instance": "menu_ecd", "action": "*", "type": "org.onap.oof.controller.dev.menu"},
63 {"instance": "*", "action": "*", "type": "org.onap.osdf.access"},
64 {"instance": "aaf", "action": "request", "type": "org.onap.osdf.certman"},
65 {"instance": "*", "action": "*", "type": "org.onap.osdf.dev.access"},
66 {"instance": ":*:*", "action": "*", "type": "org.onap.osdf.dev.k8"},
67 {"instance": ":*:*", "action": "*", "type": "org.onap.osdf.ist.k8"}]}
69 with app.test_request_context(path='/sniro/wrong/uri'):
70 with mock.patch.object(RestClient, 'request', side_effect=mock_aaf_response):
71 assert not auth.authenticate('user', 'password')
73 def test_authenticate_fail1(self):
77 def mock_aaf_response(*args, **kwargs):
80 with app.test_request_context(path='/api/oof/v1/placement'):
81 with mock.patch.object(RestClient, 'request', side_effect=mock_aaf_response):
82 assert not auth.authenticate('user2', 'password2')
84 def test_authenticate_fail3(self):
88 def mock_aaf_response2(*args, **kwargs):
91 with app.test_request_context(path='/api/oof/v1/placement'):
92 with mock.patch.object(RestClient, 'request', side_effect=mock_aaf_response2):
93 assert not auth.authenticate('user3', 'password3')
95 def test_authenticate_except(self):
99 def mock_aaf_response2(*args, **kwargs):
100 raise Exception('This is the exception you expect to handle')
102 with app.test_request_context(path='/api/oof/v1/placement'):
103 with mock.patch.object(RestClient, 'request', side_effect=mock_aaf_response2):
104 assert not auth.authenticate('user3', 'password3')