File: test_apigateway_validators.py

package info (click to toggle)
python-moto 5.1.18-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 116,520 kB
  • sloc: python: 636,725; javascript: 181; makefile: 39; sh: 3
file content (158 lines) | stat: -rw-r--r-- 4,508 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
import boto3
import pytest
from botocore.exceptions import ClientError

from moto import mock_aws

ID = "id"
NAME = "name"
VALIDATE_REQUEST_BODY = "validateRequestBody"
VALIDATE_REQUEST_PARAMETERS = "validateRequestParameters"
PARAM_NAME = "my-validator"
RESPONSE_METADATA = "ResponseMetadata"


@mock_aws
def test_create_request_validator():
    client = create_client()
    api_id = create_rest_api_id(client)
    response = create_validator(client, api_id)
    response.pop(RESPONSE_METADATA)
    response.pop(ID)
    assert response == {
        NAME: PARAM_NAME,
        VALIDATE_REQUEST_BODY: True,
        VALIDATE_REQUEST_PARAMETERS: True,
    }


@mock_aws
def test_get_request_validators():
    client = create_client()
    api_id = create_rest_api_id(client)
    response = client.get_request_validators(restApiId=api_id)

    validators = response["items"]
    assert len(validators) == 0

    response.pop(RESPONSE_METADATA)
    assert response == {"items": []}

    response = create_validator(client, api_id)
    validator_id1 = response[ID]
    response = create_validator(client, api_id)
    validator_id2 = response[ID]
    response = client.get_request_validators(restApiId=api_id)

    validators = response["items"]
    assert len(validators) == 2

    response.pop(RESPONSE_METADATA)
    assert response == {
        "items": [
            {
                ID: validator_id1,
                NAME: PARAM_NAME,
                VALIDATE_REQUEST_BODY: True,
                VALIDATE_REQUEST_PARAMETERS: True,
            },
            {
                ID: validator_id2,
                NAME: PARAM_NAME,
                VALIDATE_REQUEST_BODY: True,
                VALIDATE_REQUEST_PARAMETERS: True,
            },
        ]
    }


@mock_aws
def test_get_request_validator():
    client = create_client()
    api_id = create_rest_api_id(client)
    response = create_validator(client, api_id)
    validator_id = response[ID]
    response = client.get_request_validator(
        restApiId=api_id, requestValidatorId=validator_id
    )
    response.pop(RESPONSE_METADATA)
    assert response == {
        ID: validator_id,
        NAME: PARAM_NAME,
        VALIDATE_REQUEST_BODY: True,
        VALIDATE_REQUEST_PARAMETERS: True,
    }


@mock_aws
def test_delete_request_validator():
    client = create_client()
    api_id = create_rest_api_id(client)
    response = create_validator(client, api_id)
    # test get single validator by
    validator_id = response[ID]
    response = client.get_request_validator(
        restApiId=api_id, requestValidatorId=validator_id
    )

    response.pop(RESPONSE_METADATA)
    assert response == {
        ID: validator_id,
        NAME: PARAM_NAME,
        VALIDATE_REQUEST_BODY: True,
        VALIDATE_REQUEST_PARAMETERS: True,
    }

    # delete validator
    response = client.delete_request_validator(
        restApiId=api_id, requestValidatorId=validator_id
    )
    with pytest.raises(ClientError) as ex:
        client.get_request_validator(restApiId=api_id, requestValidatorId=validator_id)
    err = ex.value.response["Error"]
    assert err["Code"] == "BadRequestException"
    assert err["Message"] == "Invalid Request Validator Id specified"


@mock_aws
def test_update_request_validator():
    client = create_client()
    api_id = create_rest_api_id(client)
    response = create_validator(client, api_id)

    validator_id = response[ID]
    response = client.update_request_validator(
        restApiId=api_id,
        requestValidatorId=validator_id,
        patchOperations=[
            {"op": "replace", "path": "/name", "value": PARAM_NAME + PARAM_NAME},
            {"op": "replace", "path": "/validateRequestBody", "value": "False"},
            {"op": "replace", "path": "/validateRequestParameters", "value": "False"},
        ],
    )
    response.pop(RESPONSE_METADATA)
    assert response == {
        ID: validator_id,
        NAME: PARAM_NAME + PARAM_NAME,
        VALIDATE_REQUEST_BODY: False,
        VALIDATE_REQUEST_PARAMETERS: False,
    }


def create_validator(client, api_id):
    response = client.create_request_validator(
        restApiId=api_id,
        name=PARAM_NAME,
        validateRequestBody=True,
        validateRequestParameters=True,
    )
    return response


def create_client():
    return boto3.client("apigateway", region_name="us-west-2")


def create_rest_api_id(client):
    response = client.create_rest_api(name="my_api", description="this is my api")
    return response[ID]