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