|
1 | 1 | # pylint:disable=too-many-lines |
| 2 | +from http import HTTPStatus |
| 3 | +import json |
| 4 | +from json import JSONDecodeError |
2 | 5 | import unittest |
3 | 6 | from unittest.mock import patch, MagicMock |
4 | | -from json import JSONDecodeError |
| 7 | +from uuid import uuid4 |
| 8 | + |
5 | 9 | from botocore.exceptions import ClientError |
6 | 10 | from schema import Schema, And, Optional |
| 11 | + |
7 | 12 | from aws_lambda_decorators.classes import ExceptionHandler, Parameter, SSMParameter, ValidatedParameter |
8 | 13 | from aws_lambda_decorators.decorators import extract, extract_from_event, extract_from_context, handle_exceptions, \ |
9 | 14 | log, response_body_as_json, extract_from_ssm, validate, handle_all_exceptions, cors |
@@ -1496,3 +1501,76 @@ def handler(event, context, c=None): # noqa |
1496 | 1501 |
|
1497 | 1502 | response = handler(dictionary, None) |
1498 | 1503 | self.assertEqual(None, response) |
| 1504 | + |
| 1505 | + def test_extract_from_event_missing_parameter_path(self): |
| 1506 | + event = { |
| 1507 | + "body": "{}" |
| 1508 | + } |
| 1509 | + |
| 1510 | + @extract_from_event(parameters=[Parameter(path="body[json]/optional/value", default="Hello")]) |
| 1511 | + def handler(event, context, **kwargs): # noqa |
| 1512 | + return { |
| 1513 | + "statusCode": HTTPStatus.OK, |
| 1514 | + "body": json.dumps(kwargs) |
| 1515 | + } |
| 1516 | + |
| 1517 | + expected_body = json.dumps({ |
| 1518 | + "value": "Hello" |
| 1519 | + }) |
| 1520 | + |
| 1521 | + response = handler(event, None) |
| 1522 | + |
| 1523 | + self.assertEqual(HTTPStatus.OK, response["statusCode"]) |
| 1524 | + self.assertEqual(expected_body, response["body"]) |
| 1525 | + |
| 1526 | + |
| 1527 | +class IsolatedDecoderTests(unittest.TestCase): |
| 1528 | + # Tests have been named so they run in a specific order |
| 1529 | + |
| 1530 | + ID_PATTERN = "^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$" |
| 1531 | + |
| 1532 | + PARAMETERS = [ |
| 1533 | + Parameter(path="pathParameters/test_id", validators=[Mandatory, RegexValidator(ID_PATTERN)]), |
| 1534 | + Parameter(path="body[json]/name", validators=[MaxLength(255)]) |
| 1535 | + ] |
| 1536 | + |
| 1537 | + def test_01_extract_from_event_400(self): |
| 1538 | + event = { |
| 1539 | + "pathParameters": {} |
| 1540 | + } |
| 1541 | + |
| 1542 | + @extract_from_event(parameters=self.PARAMETERS, group_errors=True, allow_none_defaults=False) |
| 1543 | + def handler(event, context, **kwargs): # noqa |
| 1544 | + return kwargs |
| 1545 | + |
| 1546 | + response = handler(event, None) |
| 1547 | + self.assertEqual(HTTPStatus.BAD_REQUEST, response["statusCode"]) |
| 1548 | + |
| 1549 | + def test_02_extract_from_event_200(self): |
| 1550 | + test_id = str(uuid4()) |
| 1551 | + |
| 1552 | + event = { |
| 1553 | + "pathParameters": { |
| 1554 | + "test_id": test_id |
| 1555 | + }, |
| 1556 | + "body": json.dumps({ |
| 1557 | + "name": "Gird" |
| 1558 | + }) |
| 1559 | + } |
| 1560 | + |
| 1561 | + @extract_from_event(parameters=self.PARAMETERS, group_errors=True, allow_none_defaults=False) |
| 1562 | + def handler(event, context, **kwargs): # noqa |
| 1563 | + return { |
| 1564 | + "statusCode": HTTPStatus.OK, |
| 1565 | + "body": json.dumps(kwargs) |
| 1566 | + } |
| 1567 | + |
| 1568 | + expected_body = json.dumps({ |
| 1569 | + "test_id": test_id, |
| 1570 | + "name": "Gird" |
| 1571 | + }) |
| 1572 | + |
| 1573 | + response = handler(event, None) |
| 1574 | + |
| 1575 | + self.assertEqual(HTTPStatus.OK, response["statusCode"]) |
| 1576 | + self.assertEqual(expected_body, response["body"]) |
0 commit comments