Passed
Pull Request — master (#75)
by Vinicius
02:38
created

build.tests.unit.test_main.TestMain.setup_method()   A

Complexity

Conditions 2

Size

Total Lines 11
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 10
nop 1
dl 0
loc 11
ccs 10
cts 10
cp 1
crap 2
rs 9.9
c 0
b 0
f 0
1
"""Tests for the main madule."""
2
3 1
from unittest.mock import patch, MagicMock
4 1
from datetime import datetime, timedelta
5
6 1
import pytz
7
8 1
from kytos.lib.helpers import get_controller_mock, get_test_client
9 1
from napps.kytos.maintenance.main import Main
10 1
from napps.kytos.maintenance.models import MaintenanceWindow as MW
11 1
from napps.kytos.maintenance.models import MaintenanceWindows
12
13 1
TIME_FMT = "%Y-%m-%dT%H:%M:%S%z"
14
15
16 1
class TestMain:
17
    """Test the Main class of this NApp."""
18
19
    # pylint: disable=too-many-public-methods
20
21 1
    def setup_method(self):
22
        """Initialize before tests are executed."""
23 1
        self.controller = get_controller_mock()
24 1
        self.scheduler = MagicMock()
25 1
        new_sched = 'napps.kytos.maintenance.models.Scheduler.new_scheduler'
26 1
        with patch(new_sched) as new_scheduler:
27 1
            new_scheduler.return_value = self.scheduler
28 1
            self.napp = Main(self.controller)
29 1
        self.api = get_test_client(self.controller, self.napp)
30 1
        self.maxDiff = None
31 1
        self.base_endpoint = "kytos/maintenance/v1"
32
33 1
    async def test_create_mw_case_1(self):
34
        """Test a successful case of the REST to create."""
35 1
        url = f'{self.base_endpoint}'
36 1
        start = datetime.now(pytz.utc) + timedelta(days=1)
37 1
        end = start + timedelta(hours=2)
38 1
        payload = {
39
            'id': '1234',
40
            "start": start.strftime(TIME_FMT),
41
            "end": end.strftime(TIME_FMT),
42
            "switches": [
43
                "00:00:00:00:00:00:02",
44
            ],
45
            'interfaces': [
46
                "00:00:00:00:00:00:00:03:3",
47
            ],
48
        }
49 1
        response = await self.api.post(url, json=payload)
50 1
        assert response.status_code == 201
51 1
        current_data = response.json()
52 1
        self.scheduler.add.assert_called_once_with(
53
            MW.construct(
54
                id='1234',
55
                start=start.replace(microsecond=0),
56
                end=end.replace(microsecond=0),
57
                switches=['00:00:00:00:00:00:02'],
58
                interfaces=['00:00:00:00:00:00:00:03:3']
59
            ),
60
            force=False
61
        )
62 1
        assert current_data == {'mw_id': '1234'}
63
64 1
    async def test_create_mw_case_2(self):
65
        """Test a fail case of the REST to create a maintenance window."""
66 1
        url = f'{self.base_endpoint}'
67 1
        payload = {
68
            "switches": [
69
                "00:00:00:00:00:00:02",
70
            ],
71
            'interfaces': [
72
                "00:00:00:00:00:00:00:03:3",
73
            ],
74
        }
75 1
        response = await self.api.post(url, json=payload)
76 1
        assert response.status_code == 400
77 1
        self.scheduler.add.assert_not_called()
78
79 1 View Code Duplication
    async def test_create_mw_case_3(self):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
80
        """Test a fail case of the REST to create a maintenance window."""
81 1
        url = f'{self.base_endpoint}'
82 1
        start = datetime.now(pytz.utc) - timedelta(days=1)
83 1
        end = start + timedelta(hours=2)
84 1
        payload = {
85
            "start": start.strftime(TIME_FMT),
86
            "end": end.strftime(TIME_FMT),
87
            "switches": [
88
                "00:00:00:00:00:00:02",
89
            ],
90
            'interfaces': [
91
                "00:00:00:00:00:00:00:03:3",
92
            ],
93
        }
94 1
        response = await self.api.post(url, json=payload)
95 1
        current_data = response.json()
96 1
        assert response.status_code == 400
97 1
        assert current_data['description'] == \
98
                         'Start in the past not allowed'
99 1
        self.scheduler.add.assert_not_called()
100
101 1 View Code Duplication
    async def test_create_mw_case_4(self):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
102
        """Test a fail case of the REST to create a maintenance window."""
103 1
        url = f'{self.base_endpoint}'
104 1
        start = datetime.now(pytz.utc) + timedelta(days=1)
105 1
        end = start - timedelta(hours=2)
106 1
        payload = {
107
            "start": start.strftime(TIME_FMT),
108
            "end": end.strftime(TIME_FMT),
109
            "switches": [
110
                "00:00:00:00:00:00:02",
111
            ],
112
            'interfaces': [
113
                "00:00:00:00:00:00:00:03:3",
114
            ],
115
        }
116 1
        response = await self.api.post(url, json=payload)
117 1
        current_data = response.json()
118
119 1
        assert response.status_code == 400
120 1
        assert current_data['description'] == \
121
                         'End before start not allowed'
122 1
        self.scheduler.add.assert_not_called()
123
124 1
    async def test_get_mw_case_1(self):
125
        """Test get all maintenance windows, empty list."""
126 1
        self.scheduler.list_maintenances.return_value = MaintenanceWindows.construct(__root__ = [])
127 1
        url = f'{self.base_endpoint}'
128 1
        response = await self.api.get(url)
129 1
        current_data = response.json()
130 1
        assert response.status_code == 200
131 1
        assert current_data == []
132 1
        self.scheduler.list_maintenances.assert_called_once()
133
134 1
    async def test_get_mw_case_2(self):
135
        """Test get all maintenance windows."""
136 1
        now = datetime.now(pytz.utc)
137 1
        start1 = datetime.now(pytz.utc) + timedelta(days=1)
138 1
        end1 = start1 + timedelta(hours=6)
139 1
        start2 = datetime.now(pytz.utc) + timedelta(hours=5)
140 1
        end2 = start2 + timedelta(hours=1, minutes=30)
141 1
        self.scheduler.list_maintenances.return_value = MaintenanceWindows.construct(
142
            __root__ = [
143
            MW.construct(
144
                id = '1234',
145
                start = start1.replace(microsecond=0),
146
                end = end1.replace(microsecond=0),
147
                switches = [
148
                    '00:00:00:00:00:00:12:23'
149
                ],
150
                description = '',
151
                links = [],
152
                interfaces = [],
153
                status = 'pending',
154
                updated_at = now.replace(microsecond=0),
155
                inserted_at = now.replace(microsecond=0),
156
            ),
157
            MW.construct(
158
                id = '4567',
159
                start = start2.replace(microsecond=0),
160
                end = end2.replace(microsecond=0),
161
                switches = [
162
                    '12:34:56:78:90:ab:cd:ef'
163
                ],
164
                description = '',
165
                links = [],
166
                interfaces = [],
167
                status = 'pending',
168
                updated_at = now.replace(microsecond=0),
169
                inserted_at = now.replace(microsecond=0),
170
            ),
171
        ])
172 1
        mw_dict = [
173
            {
174
                'id': '1234',
175
                'start': start1.strftime(TIME_FMT),
176
                'end': end1.strftime(TIME_FMT),
177
                'switches': [
178
                    '00:00:00:00:00:00:12:23'
179
                ],
180
                'description': '',
181
                'links': [],
182
                'interfaces': [],
183
                'status': 'pending',
184
                'updated_at': now.strftime(TIME_FMT),
185
                'inserted_at': now.strftime(TIME_FMT),
186
            },
187
            {
188
                'id': '4567',
189
                'start': start2.strftime(TIME_FMT),
190
                'end': end2.strftime(TIME_FMT),
191
                'switches': [
192
                    '12:34:56:78:90:ab:cd:ef'
193
                ],
194
                'description': '',
195
                'links': [],
196
                'interfaces': [],
197
                'status': 'pending',
198
                'updated_at': now.strftime(TIME_FMT),
199
                'inserted_at': now.strftime(TIME_FMT),
200
            }
201
        ]
202
203 1
        url = f'{self.base_endpoint}'
204 1
        response = await self.api.get(url)
205 1
        current_data = response.json()
206 1
        assert response.status_code == 200
207 1
        assert current_data == mw_dict
208 1
        self.scheduler.list_maintenances.assert_called_once()
209
210 1
    async def test_get_mw_case_3(self):
211
        """Test get non-existent id."""
212 1
        self.scheduler.get_maintenance.return_value = None
213 1
        url = f'{self.base_endpoint}/2345'
214 1
        response = await self.api.get(url)
215 1
        current_data = response.json()
216 1
        assert response.status_code == 404
217 1
        assert current_data['description'] == \
218
                         'Maintenance with id 2345 not found'
219 1
        self.scheduler.get_maintenance.assert_called_once_with('2345')
220
221 1
    async def test_get_mw_case_4(self):
222
        """Test get existent id."""
223 1
        now = datetime.now(pytz.utc)
224 1
        start2 = datetime.now(pytz.utc) + timedelta(hours=5)
225 1
        end2 = start2 + timedelta(hours=1, minutes=30)
226 1
        self.scheduler.get_maintenance.return_value = MW.construct(
227
            id = '4567',
228
            start = start2.replace(microsecond=0),
229
            end = end2.replace(microsecond=0),
230
            switches = [
231
                '12:34:56:78:90:ab:cd:ef'
232
            ],
233
            updated_at = now.replace(microsecond=0),
234
            inserted_at = now.replace(microsecond=0),
235
        )
236 1
        mw_dict = {
237
            'id': '4567',
238
            'start': start2.strftime(TIME_FMT),
239
            'end': end2.strftime(TIME_FMT),
240
            'switches': [
241
                '12:34:56:78:90:ab:cd:ef'
242
            ],
243
            'description': '',
244
            'links': [],
245
            'interfaces': [],
246
            'status': 'pending',
247
            'updated_at': now.strftime(TIME_FMT),
248
            'inserted_at': now.strftime(TIME_FMT),
249
        }
250 1
        url = f'{self.base_endpoint}/4567'
251 1
        response = await self.api.get(url)
252 1
        current_data = response.json()
253 1
        assert response.status_code == 200
254 1
        assert current_data == mw_dict
255 1
        self.scheduler.get_maintenance.assert_called_once_with('4567')
256
257 1
    async def test_remove_mw_case_1(self):
258
        """Test remove non-existent id."""
259 1
        self.scheduler.get_maintenance.return_value = None
260 1
        url = f'{self.base_endpoint}/2345'
261 1
        response = await self.api.delete(url)
262 1
        current_data = response.json()
263 1
        assert response.status_code == 404
264 1
        assert current_data['description'] == \
265
                         'Maintenance with id 2345 not found'
266 1
        self.scheduler.get_maintenance.assert_called_once_with('2345')
267 1
        self.scheduler.remove.assert_not_called()
268
269 1
    async def test_remove_mw_case_2(self):
270
        """Test remove existent id."""
271 1
        start1 = datetime.now(pytz.utc) + timedelta(hours=1)
272 1
        end1 = start1 + timedelta(hours=6)
273 1
        self.scheduler.get_maintenance.return_value = MW.construct(
274
            id = '1234',
275
            start = start1.replace(microsecond=0),
276
            end = end1.replace(microsecond=0),
277
            switches = [
278
                '00:00:00:00:00:00:12:23'
279
            ],
280
        )
281 1
        url = f'{self.base_endpoint}/1234'
282 1
        response = await self.api.delete(url)
283 1
        current_data = response.json()
284 1
        assert response.status_code == 200
285 1
        assert current_data == {'response': 'Maintenance with id 1234 ' \
286
                                                    'successfully removed'}
287 1
        self.scheduler.get_maintenance.assert_called_once_with('1234')
288 1
        self.scheduler.remove.assert_called_once_with('1234')
289
290 1 View Code Duplication
    async def test_remove_mw_case_3(self):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
291
        """Test remove existent id."""
292 1
        start1 = datetime.now(pytz.utc) - timedelta(days=1)
293 1
        end1 = start1 + timedelta(hours=6)
294 1
        self.scheduler.get_maintenance.return_value = MW.construct(
295
            id = '1234',
296
            start = start1.replace(microsecond=0),
297
            end = end1.replace(microsecond=0),
298
            switches = [
299
                '00:00:00:00:00:00:12:23'
300
            ],
301
            status = 'running',
302
        )
303 1
        url = f'{self.base_endpoint}/1234'
304 1
        response = await self.api.delete(url)
305 1
        current_data = response.json()
306 1
        assert response.status_code == 400
307 1
        assert current_data['description'] == \
308
                         'Deleting a running maintenance is not allowed'
309 1
        self.scheduler.get_maintenance.assert_called_once_with('1234')
310 1
        self.scheduler.remove.assert_not_called()
311
312 1
    async def test_update_mw_case_1(self):
313
        """Test update non-existent id."""
314 1
        start1 = datetime.now(pytz.utc) + timedelta(days=1)
315 1
        self.scheduler.get_maintenance.return_value = None
316 1
        payload = {
317
            "start": start1.strftime(TIME_FMT),
318
        }
319 1
        url = f'{self.base_endpoint}/2345'
320 1
        response = await self.api.patch(url, json=payload)
321 1
        current_data = response.json()
322 1
        assert response.status_code == 404
323 1
        assert current_data['description'] == \
324
                         'Maintenance with id 2345 not found'
325 1
        self.scheduler.update.assert_not_called()
326
327 1
    async def test_update_mw_case_2(self):
328
        """Test update no data."""
329 1
        start1 = datetime.now(pytz.utc) + timedelta(days=1)
330 1
        payload = {
331
            "start": start1.strftime(TIME_FMT),
332
        }
333 1
        url = f'{self.base_endpoint}/1234'
334 1
        response = await self.api.patch(url, json=payload)
335 1
        current_data = response.json()
336 1
        assert response.status_code == 400
337 1
        assert "field required" in current_data['description']
338
339 1
    async def test_update_mw_case_3(self):
340
        """Test successful update."""
341 1
        start1 = datetime.now(pytz.utc) + timedelta(days=1)
342 1
        end1 = start1 + timedelta(hours=6)
343 1
        self.scheduler.get_maintenance.return_value = MW.construct(
344
            id = '1234',
345
            start = start1.replace(microsecond=0),
346
            end = end1.replace(microsecond=0),
347
            switches = [
348
                '00:00:00:00:00:00:12:23'
349
            ],
350
        )
351 1
        start_new = datetime.now(pytz.utc) + timedelta(days=1, hours=3)
352 1
        payload = {
353
            "start": start_new.strftime(TIME_FMT),
354
        }
355 1
        url = f'{self.base_endpoint}/1234'
356 1
        response = await self.api.patch(url, json=payload)
357 1
        current_data = response.json()
358 1
        assert current_data == \
359
                         {'response': 'Maintenance 1234 updated'}
360 1
        assert response.status_code == 200
361 1
        self.scheduler.get_maintenance.assert_called_once_with('1234')
362 1
        self.scheduler.update.assert_called_once_with(
363
            MW.construct(
364
                id = '1234',
365
                start = start_new.replace(microsecond=0),
366
                end = end1.replace(microsecond=0),
367
                switches = [
368
                    '00:00:00:00:00:00:12:23'
369
                ],
370
            )
371
        )
372
373 1
    async def test_update_mw_case_4(self):
374
        """Test successful update."""
375 1
        start1 = datetime.now(pytz.utc) + timedelta(days=1)
376 1
        end1 = start1 + timedelta(hours=6)
377 1
        self.scheduler.get_maintenance.return_value = MW.construct(
378
            id = '1234',
379
            start = start1.replace(microsecond=0),
380
            end = end1.replace(microsecond=0),
381
            switches = [
382
                '00:00:00:00:00:00:12:23'
383
            ],
384
        )
385 1
        start_new = datetime.now(pytz.utc) - timedelta(days=1, hours=3)
386 1
        payload = {
387
            "start": start_new.strftime(TIME_FMT),
388
        }
389 1
        url = f'{self.base_endpoint}/1234'
390 1
        response = await self.api.patch(url, json=payload)
391 1
        current_data = response.json()
392 1
        assert response.status_code == 400
393 1
        assert current_data['description'] == \
394
                         'Start in the past not allowed'
395 1
        self.scheduler.get_maintenance.assert_called_once_with('1234')
396 1
        self.scheduler.update.assert_not_called()
397
398 1
    async def test_update_mw_case_5(self):
399
        """Test successful update."""
400 1
        start1 = datetime.now(pytz.utc) + timedelta(days=1)
401 1
        end1 = start1 + timedelta(hours=6)
402 1
        self.scheduler.get_maintenance.return_value = MW.construct(
403
            id = '1234',
404
            start = start1.replace(microsecond=0),
405
            end = end1.replace(microsecond=0),
406
            switches = [
407
                '00:00:00:00:00:00:12:23'
408
            ],
409
        )
410 1
        start_new = datetime.now(pytz.utc) + timedelta(days=1, hours=3)
411 1
        end_new = start_new - timedelta(hours=5)
412 1
        payload = {
413
            "start": start_new.strftime(TIME_FMT),
414
            "end": end_new.strftime(TIME_FMT)
415
        }
416 1
        url = f'{self.base_endpoint}/1234'
417 1
        response = await self.api.patch(url, json=payload)
418 1
        current_data = response.json()
419 1
        assert response.status_code == 400
420 1
        assert current_data['description'] == \
421
                         'End before start not allowed'
422 1
        self.scheduler.get_maintenance.assert_called_once_with('1234')
423 1
        self.scheduler.update.assert_not_called()
424
425 1
    async def test_update_mw_case_6(self):
426
        """Test successful update."""
427 1
        start1 = datetime.now(pytz.utc) + timedelta(days=1)
428 1
        end1 = start1 + timedelta(hours=6)
429 1
        self.scheduler.get_maintenance.return_value = MW.construct(
430
            id = '1234',
431
            start = start1.replace(microsecond=0),
432
            end = end1.replace(microsecond=0),
433
            switches = [
434
                '00:00:00:00:00:00:12:23'
435
            ],
436
        )
437 1
        start_new = datetime.now(pytz.utc) + timedelta(days=1, hours=3)
438 1
        payload = {
439
            "start": start_new.strftime(TIME_FMT),
440
            "switches": [],
441
            'interfaces': [],
442
            'links': [],
443
        }
444
445 1
        url = f'{self.base_endpoint}/1234'
446 1
        response = await self.api.patch(url, json=payload)
447 1
        current_data = response.json()
448 1
        assert response.status_code == 400
449 1
        assert current_data['description'] == \
450
                         'At least one item must be provided'
451 1
        self.scheduler.get_maintenance.assert_called_once_with('1234')
452 1
        self.scheduler.update.assert_not_called()
453
454 1 View Code Duplication
    async def test_update_mw_case_7(self):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
455
        """Test successful update."""
456 1
        start1 = datetime.now(pytz.utc) + timedelta(days=1)
457 1
        end1 = start1 + timedelta(hours=6)
458 1
        self.scheduler.get_maintenance.return_value = MW.construct(
459
            id = '1234',
460
            start = start1.replace(microsecond=0),
461
            end = end1.replace(microsecond=0),
462
            switches = [
463
                '00:00:00:00:00:00:12:23'
464
            ],
465
        )
466 1
        payload = {
467
            'status': 'running',
468
        }
469 1
        url = f'{self.base_endpoint}/1234'
470 1
        response = await self.api.patch(url, json=payload)
471 1
        current_data = response.json()
472 1
        assert response.status_code == 400
473 1
        assert current_data['description'] == \
474
                         'Updating a maintenance status is not allowed'
475 1
        self.scheduler.update.assert_not_called()
476
477 1
    async def test_end_mw_case_1(self):
478
        """Test method that finishes the maintenance now."""
479 1
        self.scheduler.get_maintenance.return_value = None
480 1
        url = f'{self.base_endpoint}/2345/end'
481 1
        response = await self.api.patch(url)
482 1
        current_data = response.json()
483 1
        assert response.status_code == 404
484 1
        assert current_data['description'] == \
485
                         'Maintenance with id 2345 not found'
486
487 1 View Code Duplication
    async def test_end_mw_case_2(self):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
488
        """Test method that finishes the maintenance now."""
489 1
        start1 = datetime.now(pytz.utc) - timedelta(hours=1)
490 1
        end1 = start1 + timedelta(hours=6)
491 1
        self.scheduler.get_maintenance.return_value = MW.construct(
492
            id = '1234',
493
            start = start1.replace(microsecond=0),
494
            end = end1.replace(microsecond=0),
495
            switches = [
496
                '00:00:00:00:00:00:12:23'
497
            ],
498
            status = 'running',
499
        )
500 1
        url = f'{self.base_endpoint}/1234/end'
501 1
        response = await self.api.patch(url)
502 1
        current_data = response.json()
503 1
        self.scheduler.get.asssert_called_once_with('1234')
504 1
        self.scheduler.end_maintenance_early.assert_called_once_with('1234')
505 1
        assert response.status_code == 200
506 1
        assert current_data == \
507
                         {'response': 'Maintenance window 1234 finished'}
508
509 1 View Code Duplication
    async def test_end_mw_case_3(self):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
510
        """Test method that finishes the maintenance now."""
511 1
        start1 = datetime.now(pytz.utc) + timedelta(hours=1)
512 1
        end1 = start1 + timedelta(hours=6)
513 1
        self.scheduler.get_maintenance.return_value = MW.construct(
514
            id = '1234',
515
            start = start1.replace(microsecond=0),
516
            end = end1.replace(microsecond=0),
517
            switches = [
518
                '00:00:00:00:00:00:12:23'
519
            ],
520
            status = 'pending',
521
        )
522 1
        url = f'{self.base_endpoint}/1234/end'
523 1
        response = await self.api.patch(url)
524 1
        current_data = response.json()
525 1
        self.scheduler.get.asssert_called_once_with('1234')
526 1
        self.scheduler.end_maintenance_early.assert_not_called()
527 1
        assert response.status_code == 400
528 1
        assert current_data['description'] == \
529
                         'Maintenance window 1234 has not yet started'
530
531 1 View Code Duplication
    async def test_end_mw_case_4(self):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
532
        """Test method that finishes the maintenance now."""
533 1
        start1 = datetime.now(pytz.utc) - timedelta(hours=5)
534 1
        end1 = start1 + timedelta(hours=4)
535 1
        self.scheduler.get_maintenance.return_value = MW.construct(
536
            id = '1234',
537
            start = start1.replace(microsecond=0),
538
            end = end1.replace(microsecond=0),
539
            switches = [
540
                '00:00:00:00:00:00:12:23'
541
            ],
542
            status = 'finished',
543
        )
544 1
        url = f'{self.base_endpoint}/1234/end'
545 1
        response = await self.api.patch(url)
546 1
        current_data = response.json()
547 1
        self.scheduler.get.asssert_called_once_with('1234')
548 1
        self.scheduler.end_maintenance_early.assert_not_called()
549 1
        assert response.status_code == 400
550 1
        assert current_data['description'] == \
551
                         'Maintenance window 1234 has already finished'
552
553 1
    async def test_extend_case_1(self):
554
        """Test successful extension."""
555 1
        start1 = datetime.now(pytz.utc) - timedelta(hours=3)
556 1
        end1 = start1 + timedelta(hours=4)
557 1
        self.scheduler.get_maintenance.return_value = MW.construct(
558
            id = '1234',
559
            start = start1.replace(microsecond=0),
560
            end = end1.replace(microsecond=0),
561
            switches = [
562
                '00:00:00:00:00:00:12:23'
563
            ],
564
            status = 'running',
565
        )
566 1
        url = f'{self.base_endpoint}/1234/extend'
567 1
        payload = {
568
            'minutes': 45
569
        }
570 1
        response = await self.api.patch(url, json=payload)
571 1
        assert response.status_code == 200
572 1
        self.scheduler.get_maintenance.called_with('1234')
573 1
        self.scheduler.update.assert_called_with(MW.construct(
574
            id = '1234',
575
            start = start1.replace(microsecond=0),
576
            end = end1.replace(microsecond=0) + timedelta(minutes=45),
577
            switches = [
578
                '00:00:00:00:00:00:12:23'
579
            ],
580
            status = 'running',
581
        ))
582
583 1
    async def test_extend_case_2(self):
584
        """Test no payload error."""
585 1
        url = f'{self.base_endpoint}/1234/extend'
586 1
        response = await self.api.patch(url)
587 1
        assert response.status_code == 400
588 1
        current_data = response.json()
589 1
        assert 'Invalid json' in current_data['description']
590 1
        self.scheduler.update.assert_not_called()
591
592 1
    async def test_extend_case_3(self):
593
        """Test payload without minutes."""
594 1
        url = f'{self.base_endpoint}/1234/extend'
595 1
        payload = {
596
            'seconds': 240
597
        }
598 1
        response = await self.api.patch(url, json=payload)
599 1
        assert response.status_code == 400
600 1
        current_data = response.json()
601 1
        assert current_data['description'] == \
602
                         'Minutes of extension must be sent'
603 1
        self.scheduler.update.assert_not_called()
604
605 1
    async def test_extend_case_4(self):
606
        """Test no integer extension minutes."""
607 1
        url = f'{self.base_endpoint}/1234/extend'
608 1
        payload = {
609
            'minutes': '240'
610
        }
611 1
        response = await self.api.patch(url, json=payload)
612 1
        assert response.status_code == 400
613 1
        current_data = response.json()
614 1
        assert current_data['description'] == \
615
                         'Minutes of extension must be integer'
616 1
        self.scheduler.update.assert_not_called()
617
618 1 View Code Duplication
    async def test_extend_case_5(self):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
619
        """Test maintenance did not start."""
620 1
        start1 = datetime.now(pytz.utc) + timedelta(hours=3)
621 1
        end1 = start1 + timedelta(hours=4)
622 1
        self.scheduler.get_maintenance.return_value = MW.construct(
623
            id = '1234',
624
            start = start1.replace(microsecond=0),
625
            end = end1.replace(microsecond=0),
626
            switches = [
627
                '00:00:00:00:00:00:12:23'
628
            ],
629
            status = 'pending',
630
        )
631 1
        url = f'{self.base_endpoint}/1234/extend'
632 1
        payload = {
633
            'minutes': 240
634
        }
635 1
        response = await self.api.patch(url, json=payload)
636 1
        assert response.status_code == 400
637 1
        current_data = response.json()
638 1
        assert current_data['description'] == \
639
                         'Maintenance window 1234 has not yet started'
640 1
        self.scheduler.get_maintenance.assert_called_once_with('1234')
641 1
        self.scheduler.update.assert_not_called()
642
643 1 View Code Duplication
    async def test_extend_case_6(self):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
644
        """Test maintenance already finished."""
645 1
        start1 = datetime.now(pytz.utc) - timedelta(hours=3)
646 1
        end1 = start1 + timedelta(hours=2)
647 1
        self.scheduler.get_maintenance.return_value = MW.construct(
648
            id = '1234',
649
            start = start1.replace(microsecond=0),
650
            end = end1.replace(microsecond=0),
651
            switches = [
652
                '00:00:00:00:00:00:12:23'
653
            ],
654
            status = 'finished',
655
        )
656 1
        url = f'{self.base_endpoint}/1234/extend'
657 1
        payload = {
658
            'minutes': 240
659
        }
660 1
        response = await self.api.patch(url, json=payload)
661 1
        assert response.status_code == 400
662 1
        current_data = response.json()
663 1
        assert current_data['description'] == \
664
                         'Maintenance window 1234 has already finished'
665 1
        self.scheduler.get_maintenance.assert_called_once_with('1234')
666 1
        self.scheduler.update.assert_not_called()
667
668 1
    async def test_extend_case_7(self):
669
        """Test no maintenace found."""
670 1
        self.scheduler.get_maintenance.return_value = None
671 1
        url = f'{self.base_endpoint}/1235/extend'
672 1
        payload = {
673
            'minutes': 240
674
        }
675 1
        response = await self.api.patch(url, json=payload)
676 1
        assert response.status_code == 404
677 1
        current_data = response.json()
678 1
        assert current_data['description'] == \
679
                         'Maintenance with id 1235 not found'
680 1
        self.scheduler.get_maintenance.assert_called_once_with('1235')
681
        self.scheduler.update.assert_not_called()
682