| 1 |  |  | """kytos.utils.client tests.""" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | import tempfile | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | import unittest | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  | from unittest.mock import MagicMock, patch | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | from kytos.utils.client import CommonClient, NAppsClient, UsersClient | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | from kytos.utils.config import KytosConfig | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | class TestCommonClient(unittest.TestCase): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |     """Test the class CommonClient.""" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |     def setUp(self): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |         """Execute steps before each tests.""" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |         self.common_client = CommonClient() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |     @patch('requests.get') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |     def test_make_request(self, mock_requests_get): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |         """Test make_request method.""" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |         data = MagicMock() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |         self.common_client.make_request('endpoint', json=data) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |         mock_requests_get.assert_called_with('endpoint', json=data) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     @patch('requests.get') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     def test_make_request__package(self, mock_requests_get): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |         """Test make_request method with package.""" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |         data = MagicMock() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |         self.common_client.make_request('endpoint', package='any', json=data) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |         mock_requests_get.assert_called_with('endpoint', data=data, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |                                              files={'file': 'any'}) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  | # pylint: disable=protected-access | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  | class TestNAppsClient(unittest.TestCase): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |     """Test the class NAppsClient.""" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |     def setUp(self): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |         """Execute steps before each tests.""" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |         with tempfile.TemporaryDirectory() as tmp_dir: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |             config_file = '{}.kytosrc'.format(tmp_dir) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |             kytos_config = KytosConfig(config_file) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |             kytos_config.save_token('user', 'token') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |         self.napps_client = NAppsClient() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |         self.napps_client._config = kytos_config.config | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |         self.napps_client._config.set('kytos', 'api', 'endpoint') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |         self.napps_client._config.set('napps', 'api', 'endpoint') | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 50 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 51 |  |  |     @staticmethod | 
            
                                                                        
                            
            
                                    
            
            
                | 52 |  |  |     def _expected_response(status_code): | 
            
                                                                        
                            
            
                                    
            
            
                | 53 |  |  |         """Expected response mock.""" | 
            
                                                                        
                            
            
                                    
            
            
                | 54 |  |  |         response = MagicMock() | 
            
                                                                        
                            
            
                                    
            
            
                | 55 |  |  |         response.content = '{"napps": []}'.encode() | 
            
                                                                        
                            
            
                                    
            
            
                | 56 |  |  |         response.status_code = status_code | 
            
                                                                        
                            
            
                                    
            
            
                | 57 |  |  |         return response | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |     @patch('requests.get') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |     def test_get_napps(self, mock_request): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |         """Test get_napps method.""" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |         mock_request.return_value = self._expected_response(200) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |         self.napps_client.get_napps() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |         mock_request.assert_called_with('endpoint/napps/', json=[]) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |     @patch('requests.get') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |     def test_get_napp(self, mock_request): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |         """Test get_napp method.""" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |         mock_request.return_value = self._expected_response(200) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |         self.napps_client.get_napp('username', 'name') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |         endpoint = 'endpoint/napps/username/name/' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |         mock_request.assert_called_with(endpoint, json=[]) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |     @patch('requests.get') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |     def test_reload_napps__all(self, mock_request): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |         """Test reload_napps method to all napps.""" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |         mock_request.return_value = self._expected_response(200) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |         self.napps_client.reload_napps() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |         endpoint = 'endpoint/api/kytos/core/reload/all' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |         mock_request.assert_called_with(endpoint, json=[]) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |     @patch('requests.get') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |     def test_reload_napps__any(self, mock_request): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |         """Test reload_napps method to any napp.""" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |         mock_request.return_value = self._expected_response(200) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |         napps = [('user', 'napp', 'version')] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |         self.napps_client.reload_napps(napps) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |         endpoint = 'endpoint/api/kytos/core/reload/user/napp' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |         mock_request.assert_called_with(endpoint, json=[]) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |     @patch('requests.post') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |     @patch('requests.get') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |     @patch('configparser.ConfigParser.set') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |     @patch('configparser.ConfigParser.get', return_value='value') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |     @patch('configparser.ConfigParser.has_option', return_value=False) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |     @patch('kytos.utils.decorators.getpass', return_value='password') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |     @patch('builtins.input', return_value='username') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |     def test_upload_napp(self, *args): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |         """Test upload_napp method.""" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |         (_, _, _, _, _, mock_get, mock_post) = args | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |         mock_get.return_value = self._expected_response(201) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |         mock_post.return_value = self._expected_response(201) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |         metadata = MagicMock() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |         self.napps_client.upload_napp(metadata, 'package') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |         mock_post.assert_called_with('value/napps/', data=metadata, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |                                      files={'file': 'package'}) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |     @patch('requests.delete') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |     @patch('requests.get') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |     @patch('configparser.ConfigParser.set') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |     @patch('configparser.ConfigParser.get', return_value='value') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |     @patch('configparser.ConfigParser.has_option', return_value=False) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |     @patch('kytos.utils.decorators.getpass', return_value='password') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |     @patch('builtins.input', return_value='username') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |     def test_delete(self, *args): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |         """Test delete method.""" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |         (_, _, _, _, _, mock_get, mock_delete) = args | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |         mock_get.return_value = self._expected_response(201) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |         mock_delete.return_value = self._expected_response(201) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |         self.napps_client.delete('user', 'napp') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |         endpoint = 'value/napps/user/napp/' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |         token = self.napps_client._config.get('auth', 'token') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |         mock_delete.assert_called_with(endpoint, json={'token': token}) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  | class TestUsersClient(unittest.TestCase): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |     """Test the class UsersClient.""" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |     def setUp(self): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |         """Execute steps before each tests.""" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |         with tempfile.TemporaryDirectory() as tmp_dir: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |             config_file = '{}.kytosrc'.format(tmp_dir) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |             kytos_config = KytosConfig(config_file) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |         self.users_client = UsersClient() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |         self.users_client._config = kytos_config.config | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |         self.users_client._config.set('napps', 'api', 'endpoint') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |     @patch('requests.post') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |     def test_register(self, mock_request): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |         """Test register method.""" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  |         user_dict = MagicMock() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  |         self.users_client.register(user_dict) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  |  | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 157 |  |  |         mock_request.assert_called_with('endpoint/users/', json=user_dict) | 
            
                                                        
            
                                    
            
            
                | 158 |  |  |  |