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

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

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

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

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

22 6
    def test_api_access_api(self):
23 6
        url = reverse('api:api_root_view')
24 6
        response = self.client.get(url, format='json')
25 6
        self.assertEqual(response.status_code, status.HTTP_200_OK)
26 6
        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 6
    def test_api_access_swagger(self):
35 6
        url = reverse('api:swagger_view')
36 6
        response = self.client.get(url, format='json')
37 6
        self.assertEqual(response.status_code, status.HTTP_200_OK)
38

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

220 6
        self.assertEqual(count_before_delete, count_after_delete)
221

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

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

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

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

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

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

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

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

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

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

300 6
        self.assertEqual(count_before_delete, count_after_delete)
301

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

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

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

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

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

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

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

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

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

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

378 6
        self.assertEqual(count_before_delete, count_after_delete)
379

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

487 6
        self.assertEqual(count_before_delete, count_after_delete)

Read our documentation on viewing source code .

Loading