Test Failed
Pull Request — master (#75)
by Vinicius
05:05
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 %

Importance

Changes 0
Metric Value
cc 2
eloc 10
nop 1
dl 0
loc 11
rs 9.9
c 0
b 0
f 0
1
"""Tests for the main madule."""
2
3
from unittest.mock import patch, MagicMock
4
from datetime import datetime, timedelta
5
6
import pytz
7
8
from kytos.lib.helpers import get_controller_mock, get_test_client
9
from napps.kytos.maintenance.main import Main
10
from napps.kytos.maintenance.models import MaintenanceWindow as MW
11
from napps.kytos.maintenance.models import MaintenanceWindows
12
13
TIME_FMT = "%Y-%m-%dT%H:%M:%S%z"
14
15
16
class TestMain:
17
    """Test the Main class of this NApp."""
18
19
    # pylint: disable=too-many-public-methods
20
21
    def setup_method(self):
22
        """Initialize before tests are executed."""
23
        self.controller = get_controller_mock()
24
        self.scheduler = MagicMock()
25
        new_sched = 'napps.kytos.maintenance.models.Scheduler.new_scheduler'
26
        with patch(new_sched) as new_scheduler:
27
            new_scheduler.return_value = self.scheduler
28
            self.napp = Main(self.controller)
29
        self.api = get_test_client(self.controller, self.napp)
30
        self.maxDiff = None
31
        self.base_endpoint = "kytos/maintenance/v1"
32
33
    async def test_create_mw_case_1(self):
34
        """Test a successful case of the REST to create."""
35
        url = f'{self.base_endpoint}'
36
        start = datetime.now(pytz.utc) + timedelta(days=1)
37
        end = start + timedelta(hours=2)
38
        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
        response = await self.api.post(url, json=payload)
50
        assert response.status_code == 201
51
        current_data = response.json()
52
        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
        assert current_data == {'mw_id': '1234'}
63
64
    async def test_create_mw_case_2(self):
65
        """Test a fail case of the REST to create a maintenance window."""
66
        url = f'{self.base_endpoint}'
67
        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
        response = await self.api.post(url, json=payload)
76
        assert response.status_code == 400
77
        self.scheduler.add.assert_not_called()
78
79 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
        url = f'{self.base_endpoint}'
82
        start = datetime.now(pytz.utc) - timedelta(days=1)
83
        end = start + timedelta(hours=2)
84
        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
        response = await self.api.post(url, json=payload)
95
        current_data = response.json()
96
        assert response.status_code == 400
97
        assert current_data['description'] == \
98
                         'Start in the past not allowed'
99
        self.scheduler.add.assert_not_called()
100
101 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
        url = f'{self.base_endpoint}'
104
        start = datetime.now(pytz.utc) + timedelta(days=1)
105
        end = start - timedelta(hours=2)
106
        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
        response = await self.api.post(url, json=payload)
117
        current_data = response.json()
118
119
        assert response.status_code == 400
120
        assert current_data['description'] == \
121
                         'End before start not allowed'
122
        self.scheduler.add.assert_not_called()
123
124
    async def test_get_mw_case_1(self):
125
        """Test get all maintenance windows, empty list."""
126
        self.scheduler.list_maintenances.return_value = MaintenanceWindows.construct(__root__ = [])
127
        url = f'{self.base_endpoint}'
128
        response = await self.api.get(url)
129
        current_data = response.json()
130
        assert response.status_code == 200
131
        assert current_data == []
132
        self.scheduler.list_maintenances.assert_called_once()
133
134
    async def test_get_mw_case_2(self):
135
        """Test get all maintenance windows."""
136
        now = datetime.now(pytz.utc)
137
        start1 = datetime.now(pytz.utc) + timedelta(days=1)
138
        end1 = start1 + timedelta(hours=6)
139
        start2 = datetime.now(pytz.utc) + timedelta(hours=5)
140
        end2 = start2 + timedelta(hours=1, minutes=30)
141
        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
        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
        url = f'{self.base_endpoint}'
204
        response = await self.api.get(url)
205
        current_data = response.json()
206
        assert response.status_code == 200
207
        assert current_data == mw_dict
208
        self.scheduler.list_maintenances.assert_called_once()
209
210
    async def test_get_mw_case_3(self):
211
        """Test get non-existent id."""
212
        self.scheduler.get_maintenance.return_value = None
213
        url = f'{self.base_endpoint}/2345'
214
        response = await self.api.get(url)
215
        current_data = response.json()
216
        assert response.status_code == 404
217
        assert current_data['description'] == \
218
                         'Maintenance with id 2345 not found'
219
        self.scheduler.get_maintenance.assert_called_once_with('2345')
220
221
    async def test_get_mw_case_4(self):
222
        """Test get existent id."""
223
        now = datetime.now(pytz.utc)
224
        start2 = datetime.now(pytz.utc) + timedelta(hours=5)
225
        end2 = start2 + timedelta(hours=1, minutes=30)
226
        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
        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
        url = f'{self.base_endpoint}/4567'
251
        response = await self.api.get(url)
252
        current_data = response.json()
253
        assert response.status_code == 200
254
        assert current_data == mw_dict
255
        self.scheduler.get_maintenance.assert_called_once_with('4567')
256
257
    async def test_remove_mw_case_1(self):
258
        """Test remove non-existent id."""
259
        self.scheduler.get_maintenance.return_value = None
260
        url = f'{self.base_endpoint}/2345'
261
        response = await self.api.delete(url)
262
        current_data = response.json()
263
        assert response.status_code == 404
264
        assert current_data['description'] == \
265
                         'Maintenance with id 2345 not found'
266
        self.scheduler.get_maintenance.assert_called_once_with('2345')
267
        self.scheduler.remove.assert_not_called()
268
269
    async def test_remove_mw_case_2(self):
270
        """Test remove existent id."""
271
        start1 = datetime.now(pytz.utc) + timedelta(hours=1)
272
        end1 = start1 + timedelta(hours=6)
273
        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
        url = f'{self.base_endpoint}/1234'
282
        response = await self.api.delete(url)
283
        current_data = response.json()
284
        assert response.status_code == 200
285
        assert current_data == {'response': 'Maintenance with id 1234 ' \
286
                                                    'successfully removed'}
287
        self.scheduler.get_maintenance.assert_called_once_with('1234')
288
        self.scheduler.remove.assert_called_once_with('1234')
289
290 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
        start1 = datetime.now(pytz.utc) - timedelta(days=1)
293
        end1 = start1 + timedelta(hours=6)
294
        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
        url = f'{self.base_endpoint}/1234'
304
        response = await self.api.delete(url)
305
        current_data = response.json()
306
        assert response.status_code == 400
307
        assert current_data['description'] == \
308
                         'Deleting a running maintenance is not allowed'
309
        self.scheduler.get_maintenance.assert_called_once_with('1234')
310
        self.scheduler.remove.assert_not_called()
311
312
    async def test_update_mw_case_1(self):
313
        """Test update non-existent id."""
314
        start1 = datetime.now(pytz.utc) + timedelta(days=1)
315
        self.scheduler.get_maintenance.return_value = None
316
        payload = {
317
            "start": start1.strftime(TIME_FMT),
318
        }
319
        url = f'{self.base_endpoint}/2345'
320
        response = await self.api.patch(url, json=payload)
321
        current_data = response.json()
322
        assert response.status_code == 404
323
        assert current_data['description'] == \
324
                         'Maintenance with id 2345 not found'
325
        self.scheduler.update.assert_not_called()
326
327
    async def test_update_mw_case_2(self):
328
        """Test update no data."""
329
        start1 = datetime.now(pytz.utc) + timedelta(days=1)
330
        payload = {
331
            "start": start1.strftime(TIME_FMT),
332
        }
333
        url = f'{self.base_endpoint}/1234'
334
        response = await self.api.patch(url, json=payload)
335
        current_data = response.json()
336
        assert response.status_code == 400
337
        assert "field required" in current_data['description']
338
339
    async def test_update_mw_case_3(self):
340
        """Test successful update."""
341
        start1 = datetime.now(pytz.utc) + timedelta(days=1)
342
        end1 = start1 + timedelta(hours=6)
343
        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
        start_new = datetime.now(pytz.utc) + timedelta(days=1, hours=3)
352
        payload = {
353
            "start": start_new.strftime(TIME_FMT),
354
        }
355
        url = f'{self.base_endpoint}/1234'
356
        response = await self.api.patch(url, json=payload)
357
        current_data = response.json()
358
        assert current_data == \
359
                         {'response': 'Maintenance 1234 updated'}
360
        assert response.status_code == 200
361
        self.scheduler.get_maintenance.assert_called_once_with('1234')
362
        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
    async def test_update_mw_case_4(self):
374
        """Test successful update."""
375
        start1 = datetime.now(pytz.utc) + timedelta(days=1)
376
        end1 = start1 + timedelta(hours=6)
377
        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
        start_new = datetime.now(pytz.utc) - timedelta(days=1, hours=3)
386
        payload = {
387
            "start": start_new.strftime(TIME_FMT),
388
        }
389
        url = f'{self.base_endpoint}/1234'
390
        response = await self.api.patch(url, json=payload)
391
        current_data = response.json()
392
        assert response.status_code == 400
393
        assert current_data['description'] == \
394
                         'Start in the past not allowed'
395
        self.scheduler.get_maintenance.assert_called_once_with('1234')
396
        self.scheduler.update.assert_not_called()
397
398
    async def test_update_mw_case_5(self):
399
        """Test successful update."""
400
        start1 = datetime.now(pytz.utc) + timedelta(days=1)
401
        end1 = start1 + timedelta(hours=6)
402
        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
        start_new = datetime.now(pytz.utc) + timedelta(days=1, hours=3)
411
        end_new = start_new - timedelta(hours=5)
412
        payload = {
413
            "start": start_new.strftime(TIME_FMT),
414
            "end": end_new.strftime(TIME_FMT)
415
        }
416
        url = f'{self.base_endpoint}/1234'
417
        response = await self.api.patch(url, json=payload)
418
        current_data = response.json()
419
        assert response.status_code == 400
420
        assert current_data['description'] == \
421
                         'End before start not allowed'
422
        self.scheduler.get_maintenance.assert_called_once_with('1234')
423
        self.scheduler.update.assert_not_called()
424
425
    async def test_update_mw_case_6(self):
426
        """Test successful update."""
427
        start1 = datetime.now(pytz.utc) + timedelta(days=1)
428
        end1 = start1 + timedelta(hours=6)
429
        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
        start_new = datetime.now(pytz.utc) + timedelta(days=1, hours=3)
438
        payload = {
439
            "start": start_new.strftime(TIME_FMT),
440
            "switches": [],
441
            'interfaces': [],
442
            'links': [],
443
        }
444
445
        url = f'{self.base_endpoint}/1234'
446
        response = await self.api.patch(url, json=payload)
447
        current_data = response.json()
448
        assert response.status_code == 400
449
        assert current_data['description'] == \
450
                         'At least one item must be provided'
451
        self.scheduler.get_maintenance.assert_called_once_with('1234')
452
        self.scheduler.update.assert_not_called()
453
454 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
        start1 = datetime.now(pytz.utc) + timedelta(days=1)
457
        end1 = start1 + timedelta(hours=6)
458
        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
        payload = {
467
            'status': 'running',
468
        }
469
        url = f'{self.base_endpoint}/1234'
470
        response = await self.api.patch(url, json=payload)
471
        current_data = response.json()
472
        assert response.status_code == 400
473
        assert current_data['description'] == \
474
                         'Updating a maintenance status is not allowed'
475
        self.scheduler.update.assert_not_called()
476
477
    async def test_end_mw_case_1(self):
478
        """Test method that finishes the maintenance now."""
479
        self.scheduler.get_maintenance.return_value = None
480
        url = f'{self.base_endpoint}/2345/end'
481
        response = await self.api.patch(url)
482
        current_data = response.json()
483
        assert response.status_code == 404
484
        assert current_data['description'] == \
485
                         'Maintenance with id 2345 not found'
486
487 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
        start1 = datetime.now(pytz.utc) - timedelta(hours=1)
490
        end1 = start1 + timedelta(hours=6)
491
        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
        url = f'{self.base_endpoint}/1234/end'
501
        response = await self.api.patch(url)
502
        current_data = response.json()
503
        self.scheduler.get.asssert_called_once_with('1234')
504
        self.scheduler.end_maintenance_early.assert_called_once_with('1234')
505
        assert response.status_code == 200
506
        assert current_data == \
507
                         {'response': 'Maintenance window 1234 finished'}
508
509 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
        start1 = datetime.now(pytz.utc) + timedelta(hours=1)
512
        end1 = start1 + timedelta(hours=6)
513
        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
        url = f'{self.base_endpoint}/1234/end'
523
        response = await self.api.patch(url)
524
        current_data = response.json()
525
        self.scheduler.get.asssert_called_once_with('1234')
526
        self.scheduler.end_maintenance_early.assert_not_called()
527
        assert response.status_code == 400
528
        assert current_data['description'] == \
529
                         'Maintenance window 1234 has not yet started'
530
531 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
        start1 = datetime.now(pytz.utc) - timedelta(hours=5)
534
        end1 = start1 + timedelta(hours=4)
535
        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
        url = f'{self.base_endpoint}/1234/end'
545
        response = await self.api.patch(url)
546
        current_data = response.json()
547
        self.scheduler.get.asssert_called_once_with('1234')
548
        self.scheduler.end_maintenance_early.assert_not_called()
549
        assert response.status_code == 400
550
        assert current_data['description'] == \
551
                         'Maintenance window 1234 has already finished'
552
553
    async def test_extend_case_1(self):
554
        """Test successful extension."""
555
        start1 = datetime.now(pytz.utc) - timedelta(hours=3)
556
        end1 = start1 + timedelta(hours=4)
557
        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
        url = f'{self.base_endpoint}/1234/extend'
567
        payload = {
568
            'minutes': 45
569
        }
570
        response = await self.api.patch(url, json=payload)
571
        assert response.status_code == 200
572
        self.scheduler.get_maintenance.called_with('1234')
573
        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
    async def test_extend_case_2(self):
584
        """Test no payload error."""
585
        url = f'{self.base_endpoint}/1234/extend'
586
        response = await self.api.patch(url)
587
        assert response.status_code == 400
588
        current_data = response.json()
589
        assert 'Invalid json' in current_data['description']
590
        self.scheduler.update.assert_not_called()
591
592
    async def test_extend_case_3(self):
593
        """Test payload without minutes."""
594
        url = f'{self.base_endpoint}/1234/extend'
595
        payload = {
596
            'seconds': 240
597
        }
598
        response = await self.api.patch(url, json=payload)
599
        assert response.status_code == 400
600
        current_data = response.json()
601
        assert current_data['description'] == \
602
                         'Minutes of extension must be sent'
603
        self.scheduler.update.assert_not_called()
604
605
    async def test_extend_case_4(self):
606
        """Test no integer extension minutes."""
607
        url = f'{self.base_endpoint}/1234/extend'
608
        payload = {
609
            'minutes': '240'
610
        }
611
        response = await self.api.patch(url, json=payload)
612
        assert response.status_code == 400
613
        current_data = response.json()
614
        assert current_data['description'] == \
615
                         'Minutes of extension must be integer'
616
        self.scheduler.update.assert_not_called()
617
618 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
        start1 = datetime.now(pytz.utc) + timedelta(hours=3)
621
        end1 = start1 + timedelta(hours=4)
622
        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
        url = f'{self.base_endpoint}/1234/extend'
632
        payload = {
633
            'minutes': 240
634
        }
635
        response = await self.api.patch(url, json=payload)
636
        assert response.status_code == 400
637
        current_data = response.json()
638
        assert current_data['description'] == \
639
                         'Maintenance window 1234 has not yet started'
640
        self.scheduler.get_maintenance.assert_called_once_with('1234')
641
        self.scheduler.update.assert_not_called()
642
643 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
        start1 = datetime.now(pytz.utc) - timedelta(hours=3)
646
        end1 = start1 + timedelta(hours=2)
647
        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
        url = f'{self.base_endpoint}/1234/extend'
657
        payload = {
658
            'minutes': 240
659
        }
660
        response = await self.api.patch(url, json=payload)
661
        assert response.status_code == 400
662
        current_data = response.json()
663
        assert current_data['description'] == \
664
                         'Maintenance window 1234 has already finished'
665
        self.scheduler.get_maintenance.assert_called_once_with('1234')
666
        self.scheduler.update.assert_not_called()
667
668
    async def test_extend_case_7(self):
669
        """Test no maintenace found."""
670
        self.scheduler.get_maintenance.return_value = None
671
        url = f'{self.base_endpoint}/1235/extend'
672
        payload = {
673
            'minutes': 240
674
        }
675
        response = await self.api.patch(url, json=payload)
676
        assert response.status_code == 404
677
        current_data = response.json()
678
        assert current_data['description'] == \
679
                         'Maintenance with id 1235 not found'
680
        self.scheduler.get_maintenance.assert_called_once_with('1235')
681
        self.scheduler.update.assert_not_called()
682