diff --git a/main.py b/main.py index 2eef402..6ba2179 100644 --- a/main.py +++ b/main.py @@ -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: @@ -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 @@ -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} ' @@ -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} ' diff --git a/tests/unit/test_main.py b/tests/unit/test_main.py index 0e99cce..710f762 100644 --- a/tests/unit/test_main.py +++ b/tests/unit/test_main.py @@ -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') @@ -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') @@ -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') @@ -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') @@ -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): @@ -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.""" @@ -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') @@ -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') @@ -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): @@ -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): @@ -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.""" @@ -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.')