diff --git a/api/v2/models/schemas/schema_base.py b/api/v2/models/schemas/schema_base.py index d232103..cd9a2cd 100644 --- a/api/v2/models/schemas/schema_base.py +++ b/api/v2/models/schemas/schema_base.py @@ -1,20 +1,20 @@ from marshmallow import ValidationError -class SchemaBase(): +class SchemaBase: def validate_post(self, body): - if '_id' in body: - return False, 'Id must not be sent' + if "_id" in body: + return False, "Id must not be sent" - if 'updated_at' in body: - return False, 'Updated must not be sent' + if "updated_at" in body: + return False, "Updated must not be sent" - if 'deleted_at' in body: - return False, 'Deleted must not be sent' + if "deleted_at" in body: + return False, "Deleted must not be sent" try: self.load(body) except ValidationError as err: return False, err.messages - return True, 'OK' + return True, "OK" diff --git a/tests/v2/mocks/mock_service_orders.py b/tests/v2/mocks/mock_service_orders.py index 01cdbc2..0ad3b74 100644 --- a/tests/v2/mocks/mock_service_orders.py +++ b/tests/v2/mocks/mock_service_orders.py @@ -1,5 +1,11 @@ mock_service_orders = { 'service_order_with_id': { - '_id': '123' + '_id': 'xxxxx' + }, + 'service_order_with_updated': { + 'updated_at': 'xxxxx' + }, + 'service_order_with_deleted': { + 'deleted_at': 'xxxxx' } } diff --git a/tests/v2/models/__init__.py b/tests/v2/models/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/v2/models/schemas/__init__.py b/tests/v2/models/schemas/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/v2/models/schemas/test_service_orders_schema.py b/tests/v2/models/schemas/test_service_orders_schema.py index 4c231c2..be13604 100644 --- a/tests/v2/models/schemas/test_service_orders_schema.py +++ b/tests/v2/models/schemas/test_service_orders_schema.py @@ -12,21 +12,70 @@ def setUp(self): self.client = app.test_client() def test_body_with_id_returns_error(self): - erro_schema = [{'0': 'Id must not be sent'}] + erro_schema = (False, "Id must not be sent") when(ServiceOrderSchema).validate_post( - mock_service_orders['service_order_with_id'] + mock_service_orders["service_order_with_id"] ).thenReturn(erro_schema) - payload = json.dumps({'content': [mock_service_orders['service_order_with_id']]}) + payload = json.dumps( + {"content": [mock_service_orders["service_order_with_id"]]} + ) - # response = self.client.post( - # '/v2/service_orders', - # headers={"Content-Type": "application/json"}, - # data=payload) + response = self.client.post( + "/v2/service_orders", + headers={"Content-Type": "application/json"}, + data=payload, + ) - # error = response.json - # # self.assertEqual(response.status_code, HTTPStatus.BAD_REQUEST) - # self.assertIn('error', error) - # self.assertEqual(type(error['error']), list) - # self.assertIn('0', error['error'][0]) - # self.assertEqual(error['error'][0]['0'], 'Id must not be sent') + error = response.json + self.assertEqual(response.status_code, HTTPStatus.BAD_REQUEST) + self.assertIn("error", error) + self.assertEqual(type(error["error"]), list) + self.assertIn("0", error["error"][0]) + self.assertEqual(error["error"][0]["0"], "Id must not be sent") + + def test_body_with_updated_returns_error(self): + erro_schema = (False, "Updated must not be sent") + when(ServiceOrderSchema).validate_post( + mock_service_orders["service_order_with_updated"] + ).thenReturn(erro_schema) + + payload = json.dumps( + {"content": [mock_service_orders["service_order_with_updated"]]} + ) + + response = self.client.post( + "/v2/service_orders", + headers={"Content-Type": "application/json"}, + data=payload, + ) + + error = response.json + self.assertEqual(response.status_code, HTTPStatus.BAD_REQUEST) + self.assertIn("error", error) + self.assertEqual(type(error["error"]), list) + self.assertIn("0", error["error"][0]) + self.assertEqual(error["error"][0]["0"], "Updated must not be sent") + + def test_body_with_deleted_returns_error(self): + erro_schema = (False, "Deleted must not be sent") + when(ServiceOrderSchema).validate_post( + mock_service_orders["service_order_with_deleted"] + ).thenReturn(erro_schema) + + payload = json.dumps( + {"content": [mock_service_orders["service_order_with_deleted"]]} + ) + + response = self.client.post( + "/v2/service_orders", + headers={"Content-Type": "application/json"}, + data=payload, + ) + + error = response.json + self.assertEqual(response.status_code, HTTPStatus.BAD_REQUEST) + self.assertIn("error", error) + self.assertEqual(type(error["error"]), list) + self.assertIn("0", error["error"][0]) + self.assertEqual(error["error"][0]["0"], "Deleted must not be sent")