1 3
from django.test import TestCase
2 3
import logging
3

4
#from django.urls import reverse
5 3
from django.contrib.auth import get_user_model
6 3
from rest_framework.reverse import reverse
7 3
from rest_framework import status
8 3
from rest_framework.test import APITestCase
9

10 3
logger = logging.getLogger('cyborgbackup')
11 3
logger.setLevel(logging.CRITICAL)
12

13 3
class CyborgbackupApiTest(APITestCase):
14 3
    fixtures = ["settings.json", "tests.json"]
15 3
    user_login = 'admin@cyborg.local'
16 3
    user_pass = 'adminadmin'
17

18 3
    def test_page_not_found(self):
19 3
        response = self.client.get('/notFound', format='json')
20 3
        self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)
21

22 3
    def test_api_access_api(self):
23 3
        url = reverse('api:api_root_view')
24 3
        response = self.client.get(url, format='json')
25 3
        self.assertEqual(response.status_code, status.HTTP_200_OK)
26 3
        self.assertEqual(response.data['current_version'], '/api/v1/')
27

28
    # For the moment, index file are generated by nodeJs system
29
    # def test_ui_access_ui(self):
30
    #     url = reverse('ui:index')
31
    #     response = self.client.get(url, format='json')
32
    #     self.assertEqual(response.status_code, status.HTTP_200_OK)
33

34 3
    def test_api_access_swagger(self):
35 3
        url = reverse('api:swagger_view')
36 3
        response = self.client.get(url, format='json')
37 3
        self.assertEqual(response.status_code, status.HTTP_200_OK)
38

39 3
    def test_api_access_login(self):
40 3
        url = reverse('api:login')
41 3
        response = self.client.get(url, format='json')
42 3
        self.assertEqual(response.status_code, status.HTTP_200_OK)
43

44 3
    def test_api_access_logout(self):
45 3
        url = reverse('api:logout')
46 3
        response = self.client.get(url, format='json')
47 3
        self.assertEqual(response.status_code, status.HTTP_302_FOUND)
48

49 3
    def test_api_v1_access_root(self):
50 3
        url = reverse('api:api_v1_root_view', kwargs={'version': 'v1'})
51 3
        response = self.client.get(url, format='json')
52 3
        self.assertEqual(response.status_code, status.HTTP_200_OK)
53 3
        self.assertEqual(response.data['ping'], '/api/v1/ping/')
54

55 3
    def test_api_v1_access_ping(self):
56 3
        url = reverse('api:api_v1_ping_view', kwargs={'version': 'v1'})
57 3
        response = self.client.get(url, format='json')
58 3
        self.assertEqual(response.status_code, status.HTTP_200_OK)
59 3
        self.assertEqual(response.data['version'], '1.0')
60 3
        self.assertEqual(response.data['ping'], 'pong')
61

62 3
    def test_api_v1_access_config_without_auth(self):
63 3
        url = reverse('api:api_v1_config_view', kwargs={'version': 'v1'})
64 3
        response = self.client.get(url, format='json')
65 3
        self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED)
66

67 3
    def test_api_v1_access_config_with_auth(self):
68 3
        url = reverse('api:api_v1_config_view', kwargs={'version': 'v1'})
69 3
        user = get_user_model().objects.first()
70 3
        self.client.force_login(user)
71 3
        response = self.client.get(url, format='json')
72 3
        self.assertEqual(response.status_code, status.HTTP_200_OK)
73 3
        self.assertEqual(response.data['time_zone'], 'UTC')
74 3
        self.assertFalse(response.data['debug'])
75 3
        self.assertFalse(response.data['sql_debug'])
76 3
        self.assertEqual(response.data['version'], '1.0')
77

78 3
    def test_api_v1_access_me(self):
79 3
        url = reverse('api:user_me_list', kwargs={'version': 'v1'})
80 3
        self.client.login(username=self.user_login, password=self.user_pass)
81 3
        response = self.client.get(url, format='json')
82 3
        self.assertEqual(response.status_code, status.HTTP_200_OK)
83 3
        self.assertEqual(response.data['count'], 1)
84 3
        self.assertEqual(response.data['results'][0]['type'], 'user')
85 3
        self.assertTrue(response.data['results'][0]['is_superuser'])
86 3
        self.assertEqual(response.data['results'][0]['email'], 'admin@cyborg.local')
87

88 3
    def test_api_v1_access_users(self):
89 3
        url = reverse('api:user_list', kwargs={'version': 'v1'})
90 3
        self.client.login(username=self.user_login, password=self.user_pass)
91 3
        response = self.client.get(url, format='json')
92 3
        self.assertEqual(response.status_code, status.HTTP_200_OK)
93 3
        self.assertEqual(response.data['count'], 1)
94

95 3
    def test_api_v1_access_settings(self):
96 3
        url = reverse('api:setting_list', kwargs={'version': 'v1'})
97 3
        self.client.login(username=self.user_login, password=self.user_pass)
98 3
        response = self.client.get(url, format='json')
99 3
        self.assertEqual(response.status_code, status.HTTP_200_OK)
100 3
        self.assertEqual(response.data['count'], 12)
101

102 3
    def test_api_v1_access_clients(self):
103 3
        url = reverse('api:client_list', kwargs={'version': 'v1'})
104 3
        self.client.login(username=self.user_login, password=self.user_pass)
105 3
        response = self.client.get(url, format='json')
106 3
        self.assertEqual(response.status_code, status.HTTP_200_OK)
107 3
        self.assertEqual(response.data['count'], 1)
108

109 3
    def test_api_v1_access_schedules(self):
110 3
        url = reverse('api:schedule_list', kwargs={'version': 'v1'})
111 3
        self.client.login(username=self.user_login, password=self.user_pass)
112 3
        response = self.client.get(url, format='json')
113 3
        self.assertEqual(response.status_code, status.HTTP_200_OK)
114 3
        self.assertEqual(response.data['count'], 1)
115

116 3
    def test_api_v1_access_repositories(self):
117 3
        url = reverse('api:repository_list', kwargs={'version': 'v1'})
118 3
        self.client.login(username=self.user_login, password=self.user_pass)
119 3
        response = self.client.get(url, format='json')
120 3
        self.assertEqual(response.status_code, status.HTTP_200_OK)
121 3
        self.assertEqual(response.data['count'], 1)
122

123 3
    def test_api_v1_access_policies(self):
124 3
        url = reverse('api:policy_list', kwargs={'version': 'v1'})
125 3
        self.client.login(username=self.user_login, password=self.user_pass)
126 3
        response = self.client.get(url, format='json')
127 3
        self.assertEqual(response.status_code, status.HTTP_200_OK)
128 3
        self.assertEqual(response.data['count'], 1)
129

130 3
    def test_api_v1_access_catalogs(self):
131 3
        url = reverse('api:catalog_list', kwargs={'version': 'v1'})
132 3
        self.client.login(username=self.user_login, password=self.user_pass)
133 3
        response = self.client.get(url, format='json')
134 3
        self.assertEqual(response.status_code, status.HTTP_200_OK)
135 3
        self.assertEqual(response.data['count'], 0)
136

137 3
    def test_api_v1_access_stats(self):
138 3
        url = reverse('api:stats', kwargs={'version': 'v1'})
139 3
        self.client.login(username=self.user_login, password=self.user_pass)
140 3
        response = self.client.get(url, format='json')
141 3
        self.assertEqual(response.status_code, status.HTTP_200_OK)
142 3
        self.assertEqual(response.data, [])
143

144 3
    def test_api_v1_get_schedule_1(self):
145 3
        url = reverse('api:schedule_detail', kwargs={'version': 'v1', 'pk': 1})
146 3
        self.client.login(username=self.user_login, password=self.user_pass)
147 3
        response = self.client.get(url, format='json')
148 3
        self.assertEqual(response.status_code, status.HTTP_200_OK)
149 3
        self.assertEqual(response.data['id'], 1)
150 3
        self.assertEqual(response.data['crontab'], "0 5 * * MON *")
151 3
        self.assertFalse(response.data['enabled'])
152

153 3
    def test_api_v1_access_schedules_create_schedule(self):
154 3
        url = reverse('api:schedule_list', kwargs={'version': 'v1'})
155 3
        self.client.login(username=self.user_login, password=self.user_pass)
156 3
        data = {"name": "Test Create Schedule", "crontab": "1 1 1 1 * *"}
157 3
        response = self.client.post(url, data=data, format='json')
158 3
        self.assertEqual(response.status_code, status.HTTP_201_CREATED)
159 3
        self.assertEqual(response.data['crontab'], "1 1 1 1 * *")
160 3
        self.assertEqual(response.data['name'], "Test Create Schedule")
161 3
        self.assertTrue(response.data['enabled'])
162

163 3
    def test_api_v1_access_schedules_after_creation(self):
164 3
        url = reverse('api:schedule_list', kwargs={'version': 'v1'})
165 3
        self.client.login(username=self.user_login, password=self.user_pass)
166 3
        data = {"name": "Test List Schedule", "crontab": "1 1 1 1 * *"}
167 3
        response = self.client.post(url, data=data, format='json')
168 3
        self.assertEqual(response.status_code, status.HTTP_201_CREATED)
169

170 3
        url = reverse('api:schedule_list', kwargs={'version': 'v1'})
171 3
        self.client.login(username=self.user_login, password=self.user_pass)
172 3
        response = self.client.get(url, format='json')
173 3
        self.assertEqual(response.status_code, status.HTTP_200_OK)
174 3
        self.assertNotEqual(response.data['count'], 0)
175

176 3
    def test_api_v1_access_schedules_update_schedule(self):
177 3
        url = reverse('api:schedule_list', kwargs={'version': 'v1'})
178 3
        self.client.login(username=self.user_login, password=self.user_pass)
179 3
        data = {"name": "Test Update Schedule", "crontab": "2 2 2 2 * *"}
180 3
        response = self.client.post(url, data=data, format='json')
181 3
        self.assertEqual(response.status_code, status.HTTP_201_CREATED)
182 3
        id = response.data['id']
183 3
        url = response.data['url']
184

185 3
        url = reverse('api:schedule_detail', kwargs={'version': 'v1', 'pk': response.data['id']})
186 3
        self.client.login(username=self.user_login, password=self.user_pass)
187 3
        data = {"enabled": False}
188 3
        response = self.client.patch(url, data=data, format='json')
189 3
        self.assertEqual(response.status_code, status.HTTP_200_OK)
190 3
        self.assertEqual(response.data['id'], id)
191 3
        self.assertEqual(response.data['crontab'], "2 2 2 2 * *")
192 3
        self.assertEqual(response.data['name'], "Test Update Schedule")
193 3
        self.assertEqual(response.data['url'], url)
194 3
        self.assertFalse(response.data['enabled'])
195

196 3
    def test_api_v1_access_schedules_delete_schedule(self):
197 3
        url = reverse('api:schedule_list', kwargs={'version': 'v1'})
198 3
        self.client.login(username=self.user_login, password=self.user_pass)
199 3
        response = self.client.get(url, format='json')
200 3
        self.assertEqual(response.status_code, status.HTTP_200_OK)
201 3
        count_before_delete = response.data['count']
202

203 3
        url = reverse('api:schedule_list', kwargs={'version': 'v1'})
204 3
        self.client.login(username=self.user_login, password=self.user_pass)
205 3
        data = {"name": "Test Delete Schedule", "crontab": "1 1 1 1 * *"}
206 3
        response = self.client.post(url, data=data, format='json')
207 3
        self.assertEqual(response.status_code, status.HTTP_201_CREATED)
208

209 3
        url = reverse('api:schedule_detail', kwargs={'version': 'v1', 'pk': response.data['id']})
210 3
        self.client.login(username=self.user_login, password=self.user_pass)
211 3
        response = self.client.delete(url, format='json')
212 3
        self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT)
213

214 3
        url = reverse('api:schedule_list', kwargs={'version': 'v1'})
215 3
        self.client.login(username=self.user_login, password=self.user_pass)
216 3
        response = self.client.get(url, format='json')
217 3
        self.assertEqual(response.status_code, status.HTTP_200_OK)
218 3
        count_after_delete = response.data['count']
219

220 3
        self.assertEqual(count_before_delete, count_after_delete)
221

222 3
    def test_api_v1_get_repository_1(self):
223 3
        url = reverse('api:repository_detail', kwargs={'version': 'v1', 'pk': 1})
224 3
        self.client.login(username=self.user_login, password=self.user_pass)
225 3
        response = self.client.get(url, format='json')
226 3
        self.assertEqual(response.status_code, status.HTTP_200_OK)
227 3
        self.assertEqual(response.data['id'], 1)
228 3
        self.assertEqual(response.data['name'], "Demo Repository")
229 3
        self.assertFalse(response.data['enabled'])
230 3
        self.assertEqual(response.data['path'], "/tmp/repository")
231 3
        self.assertEqual(response.data['repository_key'], "0123456789abcdef")
232

233 3
    def test_api_v1_access_repositories_create_repository(self):
234 3
        url = reverse('api:repository_list', kwargs={'version': 'v1'})
235 3
        self.client.login(username=self.user_login, password=self.user_pass)
236 3
        data = {"name": "Test Create Repository", "path": "/dev/null", "repository_key": "abcedf02"}
237 3
        response = self.client.post(url, data=data, format='json')
238 3
        self.assertEqual(response.status_code, status.HTTP_201_CREATED)
239 3
        self.assertEqual(response.data['name'], "Test Create Repository")
240 3
        self.assertEqual(response.data['path'], "/dev/null")
241 3
        self.assertTrue(response.data['enabled'])
242

243 3
    def test_api_v1_access_repositories_after_creation(self):
244 3
        url = reverse('api:repository_list', kwargs={'version': 'v1'})
245 3
        self.client.login(username=self.user_login, password=self.user_pass)
246 3
        data = {"name": "Test List Repository", "path": "/dev/log", "repository_key": "abcedf03"}
247 3
        response = self.client.post(url, data=data, format='json')
248 3
        self.assertEqual(response.status_code, status.HTTP_201_CREATED)
249

250 3
        url = reverse('api:repository_list', kwargs={'version': 'v1'})
251 3
        self.client.login(username=self.user_login, password=self.user_pass)
252 3
        response = self.client.get(url, format='json')
253 3
        self.assertEqual(response.status_code, status.HTTP_200_OK)
254 3
        self.assertNotEqual(response.data['count'], 0)
255

256 3
    def test_api_v1_access_repositories_update_repository(self):
257 3
        url = reverse('api:repository_list', kwargs={'version': 'v1'})
258 3
        self.client.login(username=self.user_login, password=self.user_pass)
259 3
        data = {"name": "Test Update Repository", "path": "/dev/log", "repository_key": "abcedf04"}
260 3
        response = self.client.post(url, data=data, format='json')
261 3
        self.assertEqual(response.status_code, status.HTTP_201_CREATED)
262 3
        id = response.data['id']
263 3
        url = response.data['url']
264

265 3
        url = reverse('api:repository_detail', kwargs={'version': 'v1', 'pk': response.data['id']})
266 3
        self.client.login(username=self.user_login, password=self.user_pass)
267 3
        data = {"enabled": False, "path": "/dev/null"}
268 3
        response = self.client.patch(url, data=data, format='json')
269 3
        self.assertEqual(response.status_code, status.HTTP_200_OK)
270 3
        self.assertEqual(response.data['id'], id)
271 3
        self.assertEqual(response.data['path'], "/dev/null")
272 3
        self.assertEqual(response.data['name'], "Test Update Repository")
273 3
        self.assertEqual(response.data['url'], url)
274 3
        self.assertFalse(response.data['enabled'])
275

276 3
    def test_api_v1_access_repositories_delete_repository(self):
277 3
        url = reverse('api:repository_list', kwargs={'version': 'v1'})
278 3
        self.client.login(username=self.user_login, password=self.user_pass)
279 3
        response = self.client.get(url, format='json')
280 3
        self.assertEqual(response.status_code, status.HTTP_200_OK)
281 3
        count_before_delete = response.data['count']
282

283 3
        url = reverse('api:repository_list', kwargs={'version': 'v1'})
284 3
        self.client.login(username=self.user_login, password=self.user_pass)
285 3
        data = {"name": "Test Delete Repository", "path": "/dev/none", "repository_key": "abcedf05"}
286 3
        response = self.client.post(url, data=data, format='json')
287 3
        self.assertEqual(response.status_code, status.HTTP_201_CREATED)
288

289 3
        url = reverse('api:repository_detail', kwargs={'version': 'v1', 'pk': response.data['id']})
290 3
        self.client.login(username=self.user_login, password=self.user_pass)
291 3
        response = self.client.delete(url, format='json')
292 3
        self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT)
293

294 3
        url = reverse('api:repository_list', kwargs={'version': 'v1'})
295 3
        self.client.login(username=self.user_login, password=self.user_pass)
296 3
        response = self.client.get(url, format='json')
297 3
        self.assertEqual(response.status_code, status.HTTP_200_OK)
298 3
        count_after_delete = response.data['count']
299

300 3
        self.assertEqual(count_before_delete, count_after_delete)
301

302 3
    def test_api_v1_get_client_1(self):
303 3
        url = reverse('api:client_detail', kwargs={'version': 'v1', 'pk': 1})
304 3
        self.client.login(username=self.user_login, password=self.user_pass)
305 3
        response = self.client.get(url, format='json')
306 3
        self.assertEqual(response.status_code, status.HTTP_200_OK)
307 3
        self.assertEqual(response.data['id'], 1)
308 3
        self.assertEqual(response.data['hostname'], "localhost")
309 3
        self.assertFalse(response.data['enabled'])
310 3
        self.assertEqual(response.data['ip'], "")
311 3
        self.assertFalse(response.data['ready'])
312

313 3
    def test_api_v1_access_clients_create_client(self):
314 3
        url = reverse('api:client_list', kwargs={'version': 'v1'})
315 3
        self.client.login(username=self.user_login, password=self.user_pass)
316 3
        data = {"hostname": "localhost.localdomain"}
317 3
        response = self.client.post(url, data=data, format='json')
318 3
        self.assertEqual(response.status_code, status.HTTP_201_CREATED)
319 3
        self.assertEqual(response.data['hostname'], "localhost.localdomain")
320 3
        self.assertTrue(response.data['enabled'])
321

322 3
    def test_api_v1_access_clients_after_creation(self):
323 3
        url = reverse('api:client_list', kwargs={'version': 'v1'})
324 3
        self.client.login(username=self.user_login, password=self.user_pass)
325 3
        data = {"hostname": "localhost.contoso"}
326 3
        response = self.client.post(url, data=data, format='json')
327 3
        self.assertEqual(response.status_code, status.HTTP_201_CREATED)
328

329 3
        url = reverse('api:client_list', kwargs={'version': 'v1'})
330 3
        self.client.login(username=self.user_login, password=self.user_pass)
331 3
        response = self.client.get(url, format='json')
332 3
        self.assertEqual(response.status_code, status.HTTP_200_OK)
333 3
        self.assertNotEqual(response.data['count'], 0)
334

335 3
    def test_api_v1_access_clients_update_client(self):
336 3
        url = reverse('api:client_list', kwargs={'version': 'v1'})
337 3
        self.client.login(username=self.user_login, password=self.user_pass)
338 3
        data = {"hostname": "localhost.example"}
339 3
        response = self.client.post(url, data=data, format='json')
340 3
        self.assertEqual(response.status_code, status.HTTP_201_CREATED)
341 3
        id = response.data['id']
342 3
        url = response.data['url']
343

344 3
        url = reverse('api:client_detail', kwargs={'version': 'v1', 'pk': response.data['id']})
345 3
        self.client.login(username=self.user_login, password=self.user_pass)
346 3
        data = {"enabled": False}
347 3
        response = self.client.patch(url, data=data, format='json')
348 3
        self.assertEqual(response.status_code, status.HTTP_200_OK)
349 3
        self.assertEqual(response.data['id'], id)
350 3
        self.assertEqual(response.data['hostname'], "localhost.example")
351 3
        self.assertEqual(response.data['url'], url)
352 3
        self.assertFalse(response.data['enabled'])
353

354 3
    def test_api_v1_access_clients_delete_client(self):
355 3
        url = reverse('api:client_list', kwargs={'version': 'v1'})
356 3
        self.client.login(username=self.user_login, password=self.user_pass)
357 3
        response = self.client.get(url, format='json')
358 3
        self.assertEqual(response.status_code, status.HTTP_200_OK)
359 3
        count_before_delete = response.data['count']
360

361 3
        url = reverse('api:client_list', kwargs={'version': 'v1'})
362 3
        self.client.login(username=self.user_login, password=self.user_pass)
363 3
        data = {"hostname": "localhost.test"}
364 3
        response = self.client.post(url, data=data, format='json')
365 3
        self.assertEqual(response.status_code, status.HTTP_201_CREATED)
366

367 3
        url = reverse('api:client_detail', kwargs={'version': 'v1', 'pk': response.data['id']})
368 3
        self.client.login(username=self.user_login, password=self.user_pass)
369 3
        response = self.client.delete(url, format='json')
370 3
        self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT)
371

372 3
        url = reverse('api:client_list', kwargs={'version': 'v1'})
373 3
        self.client.login(username=self.user_login, password=self.user_pass)
374 3
        response = self.client.get(url, format='json')
375 3
        self.assertEqual(response.status_code, status.HTTP_200_OK)
376 3
        count_after_delete = response.data['count']
377

378 3
        self.assertEqual(count_before_delete, count_after_delete)
379

380 3
    def test_api_v1_get_policy_1(self):
381 3
        url = reverse('api:policy_detail', kwargs={'version': 'v1', 'pk': 1})
382 3
        self.client.login(username=self.user_login, password=self.user_pass)
383 3
        response = self.client.get(url, format='json')
384 3
        self.assertEqual(response.status_code, status.HTTP_200_OK)
385 3
        self.assertEqual(response.data['id'], 1)
386 3
        self.assertEqual(response.data['name'], "Demo Policy")
387 3
        self.assertFalse(response.data['enabled'])
388 3
        self.assertEqual(response.data['policy_type'], "rootfs")
389

390 3
    def test_api_v1_get_policy_vmmmodule(self):
391 3
        url = reverse('api:policy_vmmodule', kwargs={'version': 'v1'})
392 3
        self.client.login(username=self.user_login, password=self.user_pass)
393 3
        response = self.client.get(url, format='json')
394 3
        self.assertEqual(response.status_code, status.HTTP_200_OK)
395 3
        self.assertEqual(response.data, [])
396

397 3
    def test_api_v1_get_policy_calendar_1(self):
398 3
        url = reverse('api:policy_calendar', kwargs={'version': 'v1', 'pk': 1})
399 3
        self.client.login(username=self.user_login, password=self.user_pass)
400 3
        response = self.client.get(url, format='json')
401

402 3
        import datetime
403 3
        import tzcron
404 3
        import dateutil
405 3
        import pytz
406

407 3
        now = datetime.datetime.now(pytz.utc)
408 3
        start_month = now.replace(day=1, hour=0, minute=0, second=0, microsecond=0)
409 3
        year = now.year
410 3
        if start_month.month == 12:
411 3
            year += 1
412 3
        relative_month = dateutil.relativedelta.relativedelta(months=1)
413 3
        end_month = datetime.datetime(year, (start_month + relative_month).month, 1) - datetime.timedelta(days=1)
414 3
        end_month = end_month.replace(hour=23, minute=59, second=50, tzinfo=pytz.utc)
415 3
        schedule = tzcron.Schedule("0 5 * * MON *", pytz.utc, start_month, end_month)
416

417 3
        expectedCalendar = [s.isoformat() for s in schedule]
418 3
        self.assertEqual(response.status_code, status.HTTP_200_OK)
419 3
        self.assertEqual(response.data, expectedCalendar)
420

421 3
    def test_api_v1_access_policies_create_policy(self):
422 3
        url = reverse('api:policy_list', kwargs={'version': 'v1'})
423 3
        self.client.login(username=self.user_login, password=self.user_pass)
424 3
        data = {"name": "Test Create Policy", "policy_type": "config", "schedule": 1, "repository": 1, "clients": [1]}
425 3
        response = self.client.post(url, data=data, format='json')
426 3
        self.assertEqual(response.status_code, status.HTTP_201_CREATED)
427 3
        self.assertEqual(response.data['name'], "Test Create Policy")
428 3
        self.assertEqual(response.data['policy_type'], "config")
429 3
        self.assertTrue(response.data['enabled'])
430

431 3
    def test_api_v1_access_policies_after_policy(self):
432 3
        url = reverse('api:policy_list', kwargs={'version': 'v1'})
433 3
        self.client.login(username=self.user_login, password=self.user_pass)
434 3
        data = {"name": "Test List Policy", "policy_type": "config", "schedule": 1, "repository": 1, "clients": [1]}
435 3
        response = self.client.post(url, data=data, format='json')
436 3
        self.assertEqual(response.status_code, status.HTTP_201_CREATED)
437

438 3
        url = reverse('api:policy_list', kwargs={'version': 'v1'})
439 3
        self.client.login(username=self.user_login, password=self.user_pass)
440 3
        response = self.client.get(url, format='json')
441 3
        self.assertEqual(response.status_code, status.HTTP_200_OK)
442 3
        self.assertNotEqual(response.data['count'], 0)
443

444 3
    def test_api_v1_access_policies_update_policy(self):
445 3
        url = reverse('api:policy_list', kwargs={'version': 'v1'})
446 3
        self.client.login(username=self.user_login, password=self.user_pass)
447 3
        data = {"name": "Test Update Policy", "policy_type": "config", "schedule": 1, "repository": 1, "clients": [1]}
448 3
        response = self.client.post(url, data=data, format='json')
449 3
        self.assertEqual(response.status_code, status.HTTP_201_CREATED)
450 3
        id = response.data['id']
451 3
        url = response.data['url']
452

453 3
        url = reverse('api:policy_detail', kwargs={'version': 'v1', 'pk': response.data['id']})
454 3
        self.client.login(username=self.user_login, password=self.user_pass)
455 3
        data = {"enabled": False}
456 3
        response = self.client.patch(url, data=data, format='json')
457 3
        self.assertEqual(response.status_code, status.HTTP_200_OK)
458 3
        self.assertEqual(response.data['id'], id)
459 3
        self.assertEqual(response.data['name'], "Test Update Policy")
460 3
        self.assertEqual(response.data['url'], url)
461 3
        self.assertFalse(response.data['enabled'])
462

463 3
    def test_api_v1_access_policies_delete_policy(self):
464 3
        url = reverse('api:policy_list', kwargs={'version': 'v1'})
465 3
        self.client.login(username=self.user_login, password=self.user_pass)
466 3
        response = self.client.get(url, format='json')
467 3
        self.assertEqual(response.status_code, status.HTTP_200_OK)
468 3
        count_before_delete = response.data['count']
469

470 3
        url = reverse('api:policy_list', kwargs={'version': 'v1'})
471 3
        self.client.login(username=self.user_login, password=self.user_pass)
472 3
        data = {"name": "Test Delete Policy", "policy_type": "config", "schedule": 1, "repository": 1, "clients": [1]}
473 3
        response = self.client.post(url, data=data, format='json')
474 3
        self.assertEqual(response.status_code, status.HTTP_201_CREATED)
475

476 3
        url = reverse('api:policy_detail', kwargs={'version': 'v1', 'pk': response.data['id']})
477 3
        self.client.login(username=self.user_login, password=self.user_pass)
478 3
        response = self.client.delete(url, format='json')
479 3
        self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT)
480

481 3
        url = reverse('api:policy_list', kwargs={'version': 'v1'})
482 3
        self.client.login(username=self.user_login, password=self.user_pass)
483 3
        response = self.client.get(url, format='json')
484 3
        self.assertEqual(response.status_code, status.HTTP_200_OK)
485 3
        count_after_delete = response.data['count']
486

487 3
        self.assertEqual(count_before_delete, count_after_delete)

Read our documentation on viewing source code .

Loading