|
| 1 | +# Copyright 2022, Google LLC |
| 2 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | +# you may not use this file except in compliance with the License. |
| 4 | +# You may obtain a copy of the License at |
| 5 | +# |
| 6 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | +# |
| 8 | +# Unless required by applicable law or agreed to in writing, software |
| 9 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | +# See the License for the specific language governing permissions and |
| 12 | +# limitations under the License. |
| 13 | + |
| 14 | +"""Test configure optional or required form param.""" |
| 15 | + |
| 16 | +import flask |
| 17 | +import pytest |
| 18 | + |
| 19 | +from webhook_configure_optional_or_required_form_parameters import ( |
| 20 | + configure_optional_form_param, |
| 21 | +) |
| 22 | + |
| 23 | + |
| 24 | +@pytest.fixture(name="app", scope="module") |
| 25 | +def fixture_app(): |
| 26 | + """Flask fixture to pass a flask.Request to the test function.""" |
| 27 | + return flask.Flask(__name__) |
| 28 | + |
| 29 | + |
| 30 | +@pytest.mark.parametrize( |
| 31 | + "value,form_parameter,required,state,text", |
| 32 | + [ |
| 33 | + (10, None, True, "INVALID", "10 isn't going to work for me. Please try again!"), |
| 34 | + (17, 17, False, "VALID", "17 is too many, but it's okay. Let's move on."), |
| 35 | + (25, None, True, "INVALID", "25 isn't going to work for me. Please try again!"), |
| 36 | + ], |
| 37 | +) |
| 38 | +def test_validate_parameter(value, form_parameter, required, state, text, app): |
| 39 | + """Test for configure optional or required form param.""" |
| 40 | + |
| 41 | + request = {"pageInfo": {"formInfo": {"parameterInfo": [{"value": value}]}}} |
| 42 | + |
| 43 | + with app.test_request_context(json=request): |
| 44 | + res = configure_optional_form_param(flask.request) |
| 45 | + assert res["sessionInfo"]["parameterInfo"]["formParameter"] == form_parameter |
| 46 | + assert ( |
| 47 | + res["pageInfo"]["formInfo"]["parameterInfo"][0]["displayName"] |
| 48 | + == form_parameter |
| 49 | + ) |
| 50 | + assert res["pageInfo"]["formInfo"]["parameterInfo"][0]["required"] == required |
| 51 | + assert res["pageInfo"]["formInfo"]["parameterInfo"][0]["state"] == state |
| 52 | + assert res["fulfillment_response"]["messages"][0]["text"]["text"][0] == text |
0 commit comments