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 May 17, 2021
1 parent 17106ff commit b4e1884
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 33 deletions.
21 changes: 9 additions & 12 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,16 +87,15 @@ 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')
try:
maintenance.update(data)
except ValueError as error:
return jsonify(f'{error}'), 400
raise BadRequest(f'{error}')
return jsonify({'response': f'Maintenance {mw_id} updated'}), 201

@rest('/<mw_id>', methods=['DELETE'])
Expand All @@ -105,8 +104,7 @@ 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')
self.scheduler.remove(maintenance)
del self.maintenances[mw_id]
return jsonify({'response': f'Maintenance with id {mw_id} '
Expand All @@ -118,15 +116,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 @@ -94,7 +94,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 @@ -129,7 +130,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 @@ -164,7 +166,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 @@ -237,8 +240,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 @@ -290,8 +293,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 @@ -339,8 +342,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 @@ -364,8 +367,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.MaintenanceWindow.update')
def test_update_mw_case_3(self, mw_update_mock):
Expand Down Expand Up @@ -421,7 +424,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 @@ -452,7 +456,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 @@ -473,8 +478,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 @@ -517,9 +522,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 @@ -539,6 +543,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 b4e1884

Please sign in to comment.