update link to upper-constraints.txt
[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
20 from flask import Flask
21 from mock import mock
22
23 from osdf.adapters.aaf import aaf_authentication as auth
24 from osdf.utils.interfaces import RestClient
25
26 BASE_DIR = os.path.dirname(__file__)
27
28
29 class TestAafAuthentication():
30
31     def test_authenticate(self):
32         app = Flask(__name__)
33         auth.clear_cache()
34
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"}]}
42
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')
46
47     def test_auth_cache(self):
48         app = Flask(__name__)
49         auth.clear_cache()
50
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"}]}
58
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')
63
64     def test_authenticate_fail(self):
65         app = Flask(__name__)
66         auth.clear_cache()
67
68         def mock_aaf_response(*args, **kwargs):
69             return {"perm": [{"instance": "menu_ecd", "action": "*", "type": "org.onap.oof.controller.dev.menu"}]}
70
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')
74
75     def test_authenticate_uri_mismatch(self):
76         app = Flask(__name__)
77         auth.clear_cache()
78
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"}]}
86
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')
90
91     def test_authenticate_fail1(self):
92         app = Flask(__name__)
93         auth.clear_cache()
94
95         def mock_aaf_response(*args, **kwargs):
96             return {}
97
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')
101
102     def test_authenticate_fail3(self):
103         app = Flask(__name__)
104         auth.clear_cache()
105
106         def mock_aaf_response2(*args, **kwargs):
107             return {}
108
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')
112
113     def test_authenticate_except(self):
114         app = Flask(__name__)
115         auth.clear_cache()
116
117         def mock_aaf_response2(*args, **kwargs):
118             raise Exception('This is the exception you expect to handle')
119
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')