Passed
Pull Request — master (#78)
by
unknown
05:53
created

TestMain.test_get_mw_case_1()   A

Complexity

Conditions 1

Size

Total Lines 9
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 1

Importance

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