Skip to content

Commit

Permalink
Fix return codes
Browse files Browse the repository at this point in the history
Fix some return codes on update, delete and end maintenance.
Fix kytos#44
  • Loading branch information
ajoaoff committed Jun 8, 2021
1 parent f59836a commit 20013af
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 38 deletions.
29 changes: 12 additions & 17 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ def create_mw(self):
if not data:
raise UnsupportedMediaType('The request does not have a json.')
try:
maintenance = MW.from_dict(data, self.controller)
maintenance = MW.from_dict(data, self.controller)
except ValueError as err:
raise BadRequest(f'{err}')
if maintenance is None:
Expand All @@ -87,19 +87,17 @@ def update_mw(self, mw_id):
"""Update a maintenance window."""
data = request.get_json()
if not data:
return jsonify("Bad request: The request do not have a json."), 415
raise UnsupportedMediaType('The request does not have a json.')
try:
maintenance = self.maintenances[mw_id]
except KeyError:
return jsonify({'response': f'Maintenance with id {mw_id} not '
f'found'}), 404
raise NotFound(f'Maintenance with id {mw_id} not found')
if maintenance.status == Status.RUNNING:
return jsonify({'response': 'Updating a running maintenance is '
'not allowed'}), 400
raise BadRequest('Updating a running maintenance is not allowed')
try:
maintenance.update(data)
except ValueError as error:
return jsonify(f'{error}'), 400
raise BadRequest(f'{error}')
self.scheduler.remove(maintenance)
self.scheduler.add(maintenance)
return jsonify({'response': f'Maintenance {mw_id} updated'}), 201
Expand All @@ -110,11 +108,9 @@ def remove_mw(self, mw_id):
try:
maintenance = self.maintenances[mw_id]
except KeyError:
return jsonify({'response': f'Maintenance with id {mw_id} not '
f'found'}), 404
raise NotFound(f'Maintenance with id {mw_id} not found')
if maintenance.status == Status.RUNNING:
return jsonify({'response': 'Deleting a running maintenance is '
'not allowed'}), 400
raise BadRequest('Deleting a running maintenance is not allowed')
self.scheduler.remove(maintenance)
del self.maintenances[mw_id]
return jsonify({'response': f'Maintenance with id {mw_id} '
Expand All @@ -126,15 +122,14 @@ def end_mw(self, mw_id):
try:
maintenance = self.maintenances[mw_id]
except KeyError:
return jsonify({'response': f'Maintenance with id '
f'{mw_id} not found'}), 404
raise NotFound(f'Maintenance with id {mw_id} not found')
now = datetime.datetime.now(pytz.utc)
if now < maintenance.start:
return jsonify({'response': f'Maintenance window {mw_id} has not '
f'yet started.'}), 400
raise BadRequest(f'Maintenance window {mw_id} has not yet '
'started.')
if now > maintenance.end:
return jsonify({'response': f'Maintenance window {mw_id} has '
f'already finished.'}), 400
raise BadRequest(f'Maintenance window {mw_id} has already '
'finished.')
self.scheduler.remove(maintenance)
maintenance.end_mw()
return jsonify({'response': f'Maintenance window {mw_id} '
Expand Down
45 changes: 24 additions & 21 deletions tests/unit/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,8 @@ def test_create_mw_case_2(self, from_dict_mock, sched_add_mock):
content_type='application/json')
current_data = json.loads(response.data)
self.assertEqual(response.status_code, 400)
self.assertEqual(current_data, 'One or more items are invalid')
self.assertEqual(current_data['description'],
'One or more items are invalid')
sched_add_mock.assert_not_called()

@patch('napps.kytos.maintenance.models.Scheduler.add')
Expand Down Expand Up @@ -130,7 +131,8 @@ def test_create_mw_case_3(self, from_dict_mock, sched_add_mock):
content_type='application/json')
current_data = json.loads(response.data)
self.assertEqual(response.status_code, 400)
self.assertEqual(current_data, 'Start in the past not allowed')
self.assertEqual(current_data['description'],
'Start in the past not allowed')
sched_add_mock.assert_not_called()

@patch('napps.kytos.maintenance.models.Scheduler.add')
Expand Down Expand Up @@ -165,7 +167,8 @@ def test_create_mw_case_4(self, from_dict_mock, sched_add_mock):
content_type='application/json')
current_data = json.loads(response.data)
self.assertEqual(response.status_code, 400)
self.assertEqual(current_data, 'End before start not allowed')
self.assertEqual(current_data['description'],
'End before start not allowed')
sched_add_mock.assert_not_called()

@patch('napps.kytos.maintenance.models.MaintenanceWindow.as_dict')
Expand Down Expand Up @@ -238,8 +241,8 @@ def test_get_mw_case_3(self, mw_as_dict_mock):
response = self.api.get(url)
current_data = json.loads(response.data)
self.assertEqual(response.status_code, 404)
self.assertEqual(current_data, {'response': 'Maintenance with id 2345 '
'not found'})
self.assertEqual(current_data['description'],
'Maintenance with id 2345 not found')
mw_as_dict_mock.assert_not_called()

@patch('napps.kytos.maintenance.models.MaintenanceWindow.as_dict')
Expand Down Expand Up @@ -291,8 +294,8 @@ def test_remove_mw_case_1(self):
response = self.api.delete(url)
current_data = json.loads(response.data)
self.assertEqual(response.status_code, 404)
self.assertEqual(current_data, {'response': 'Maintenance with id 2345 '
'not found'})
self.assertEqual(current_data['description'],
'Maintenance with id 2345 not found')

@patch('napps.kytos.maintenance.models.Scheduler.remove')
def test_remove_mw_case_2(self, sched_remove_mock):
Expand Down Expand Up @@ -361,8 +364,8 @@ def test_update_mw_case_1(self):
content_type='application/json')
current_data = json.loads(response.data)
self.assertEqual(response.status_code, 404)
self.assertEqual(current_data, {'response': 'Maintenance with id 2345 '
'not found'})
self.assertEqual(current_data['description'],
'Maintenance with id 2345 not found')

def test_update_mw_case_2(self):
"""Test update no data."""
Expand All @@ -386,8 +389,8 @@ def test_update_mw_case_2(self):
content_type='text/plain')
current_data = json.loads(response.data)
self.assertEqual(response.status_code, 415)
self.assertEqual(current_data,
'Bad request: The request do not have a json.')
self.assertEqual(current_data['description'],
'The request does not have a json.')

@patch('napps.kytos.maintenance.models.Scheduler.add')
@patch('napps.kytos.maintenance.models.Scheduler.remove')
Expand Down Expand Up @@ -448,7 +451,8 @@ def test_update_mw_case_4(self, mw_update_mock):
content_type='application/json')
current_data = json.loads(response.data)
self.assertEqual(response.status_code, 400)
self.assertEqual(current_data, 'Start in the past not allowed.')
self.assertEqual(current_data['description'],
'Start in the past not allowed.')
mw_update_mock.assert_called_once_with(payload)

@patch('napps.kytos.maintenance.models.MaintenanceWindow.update')
Expand Down Expand Up @@ -479,7 +483,8 @@ def test_update_mw_case_5(self, mw_update_mock):
content_type='application/json')
current_data = json.loads(response.data)
self.assertEqual(response.status_code, 400)
self.assertEqual(current_data, 'End before start not allowed.')
self.assertEqual(current_data['description'],
'End before start not allowed.')
mw_update_mock.assert_called_once_with(payload)

def test_end_mw_case_1(self):
Expand All @@ -500,8 +505,8 @@ def test_end_mw_case_1(self):
response = self.api.patch(url)
current_data = json.loads(response.data)
self.assertEqual(response.status_code, 404)
self.assertEqual(current_data,
{'response': 'Maintenance with id 2345 not found'})
self.assertEqual(current_data['description'],
'Maintenance with id 2345 not found')

@patch('napps.kytos.maintenance.models.MaintenanceWindow.end_mw')
def test_end_mw_case_2(self, end_mw_mock):
Expand Down Expand Up @@ -544,9 +549,8 @@ def test_end_mw_case_3(self):
response = self.api.patch(url)
current_data = json.loads(response.data)
self.assertEqual(response.status_code, 400)
self.assertEqual(current_data,
{'response': 'Maintenance window 1234 has not yet '
'started.'})
self.assertEqual(current_data['description'],
'Maintenance window 1234 has not yet started.')

def test_end_mw_case_4(self):
"""Test method that finishes the maintenance now."""
Expand All @@ -566,6 +570,5 @@ def test_end_mw_case_4(self):
response = self.api.patch(url)
current_data = json.loads(response.data)
self.assertEqual(response.status_code, 400)
self.assertEqual(current_data,
{'response': 'Maintenance window 1234 has already '
'finished.'})
self.assertEqual(current_data['description'],
'Maintenance window 1234 has already finished.')

0 comments on commit 20013af

Please sign in to comment.