Passed
Push — master ( 0d5b5f...2f9122 )
by
unknown
01:39 queued 12s
created

build.tests.unit.test_main   A

Complexity

Total Complexity 33

Size/Duplication

Total Lines 752
Duplicated Lines 33.38 %

Test Coverage

Coverage 97.28%

Importance

Changes 0
Metric Value
eloc 592
dl 251
loc 752
ccs 357
cts 367
cp 0.9728
rs 9.76
c 0
b 0
f 0
wmc 33

32 Methods

Rating   Name   Duplication   Size   Complexity  
A TestMain.test_extend_case_5() 25 25 1
A TestMain.test_update_mw_case_6() 0 29 1
A TestMain.test_get_mw_case_3() 0 10 1
A TestMain.test_get_mw_case_4() 0 35 1
A TestMain.test_update_mw_case_7() 23 23 1
A TestMain.test_end_mw_case_1() 0 9 1
A TestMain.test_update_mw_case_5() 0 27 1
A TestMain.setup_method() 0 13 2
A TestMain.test_update_mw_case_2() 0 12 1
A TestMain.test_remove_mw_case_1() 0 11 1
A TestMain.test_get_mw_case_1() 0 9 1
A TestMain.test_end_mw_case_2() 21 21 1
A TestMain.test_update_mw_case_3() 0 31 1
A TestMain.test_create_mw_case_3() 22 22 1
A TestMain.test_extend_case_7() 0 15 1
A TestMain.test_create_mw_case_4() 23 23 1
A TestMain.test_remove_mw_case_3() 21 21 1
A TestMain.test_update_mw_case_4() 0 25 1
A TestMain.test_create_mw_case_2() 0 15 1
A TestMain.test_extend_case_6() 25 25 1
A TestMain.test_create_mw_case_6() 24 24 1
A TestMain.test_extend_case_2() 0 9 1
A TestMain.test_end_mw_case_3() 21 21 1
A TestMain.test_extend_case_4() 0 13 1
A TestMain.test_create_mw_case_1() 0 29 1
A TestMain.test_update_mw_case_1() 0 15 1
A TestMain.test_extend_case_3() 0 13 1
A TestMain.test_create_mw_case_5() 25 25 1
B TestMain.test_get_mw_case_2() 0 75 1
A TestMain.test_extend_case_1() 0 29 1
A TestMain.test_remove_mw_case_2() 0 20 1
A TestMain.test_end_mw_case_4() 21 21 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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