Add unit test cases for aaf auth api
[optf/osdf.git] / test / test_aaf_authentication.py
1 import os
2 from flask import Flask
3 from mock import mock
4
5 from osdf.adapters.aaf import aaf_authentication as auth
6 from osdf.utils.interfaces import RestClient
7
8 BASE_DIR = os.path.dirname(__file__)
9
10
11 class TestAafAuthentication():
12
13     def test_authenticate(self):
14         app = Flask(__name__)
15         auth.clear_cache()
16
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"}]}
24
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')
28
29     def test_auth_cache(self):
30         app = Flask(__name__)
31         auth.clear_cache()
32
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"}]}
40
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')
45
46     def test_authenticate_fail(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
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')
56
57     def test_authenticate_uri_mismatch(self):
58         app = Flask(__name__)
59         auth.clear_cache()
60
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"}]}
68
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')
72
73     def test_authenticate_fail1(self):
74         app = Flask(__name__)
75         auth.clear_cache()
76
77         def mock_aaf_response(*args, **kwargs):
78             return {}
79
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')
83
84     def test_authenticate_fail3(self):
85         app = Flask(__name__)
86         auth.clear_cache()
87
88         def mock_aaf_response2(*args, **kwargs):
89             return {}
90
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')
94
95     def test_authenticate_except(self):
96         app = Flask(__name__)
97         auth.clear_cache()
98
99         def mock_aaf_response2(*args, **kwargs):
100             raise Exception('This is the exception you expect to handle')
101
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')