Skip to content

Commit

Permalink
[@adrilene/@denisousa] Issue #73 - Testes de validação.
Browse files Browse the repository at this point in the history
  • Loading branch information
Adrilene committed Jun 24, 2020
1 parent c9900f6 commit 0af3728
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 22 deletions.
16 changes: 8 additions & 8 deletions api/v2/models/schemas/schema_base.py
Original file line number Diff line number Diff line change
@@ -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"
8 changes: 7 additions & 1 deletion tests/v2/mocks/mock_service_orders.py
Original file line number Diff line number Diff line change
@@ -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'
}
}
Empty file added tests/v2/models/__init__.py
Empty file.
Empty file.
75 changes: 62 additions & 13 deletions tests/v2/models/schemas/test_service_orders_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")

0 comments on commit 0af3728

Please sign in to comment.