|
| 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 | +"""Test validate form webhook request session ID snippet.""" |
| 14 | + |
| 15 | +import flask |
| 16 | +import pytest |
| 17 | + |
| 18 | +from webhook_log_session_info import log_session_id_for_troubleshooting |
| 19 | + |
| 20 | + |
| 21 | +@pytest.fixture(name="app", scope="module") |
| 22 | +def fixture_app(): |
| 23 | + """Flask fixture to pass a flask.Request to the test function""" |
| 24 | + return flask.Flask(__name__) |
| 25 | + |
| 26 | + |
| 27 | +@pytest.fixture |
| 28 | +def session_id(): |
| 29 | + return "d0bdaa0c-0d00-0000-b0eb-b00b0db000b0" |
| 30 | + |
| 31 | + |
| 32 | +@pytest.fixture |
| 33 | +def session_prefix(): |
| 34 | + agent_id = "000000f0-f000-00b0-0000-af00d0e00000" |
| 35 | + return f"projects/test_project/locations/us-central1/agents/{agent_id}" |
| 36 | + |
| 37 | + |
| 38 | +@pytest.fixture |
| 39 | +def session(session_prefix, session_id): |
| 40 | + """Session string without environment path""" |
| 41 | + return f"{session_prefix}/sessions/{session_id}" |
| 42 | + |
| 43 | + |
| 44 | +@pytest.fixture |
| 45 | +def env_session(session_prefix, session_id): |
| 46 | + """Session string with environment path""" |
| 47 | + environment = "0d0000f0-0aac-0d0c-0a00-b00b0000a000" |
| 48 | + return f"{session_prefix}/environments/{environment}/sessions/{session_id}" |
| 49 | + |
| 50 | + |
| 51 | +def test_logging_session_id(app, session, session_id): |
| 52 | + """Parameterized test for regular session string.""" |
| 53 | + request = {"sessionInfo": {"session": session}} |
| 54 | + with app.test_request_context(json=request): |
| 55 | + res = log_session_id_for_troubleshooting(flask.request) |
| 56 | + assert session_id in str(res) |
| 57 | + |
| 58 | + |
| 59 | +def test_logging_session_id_with_env_path(app, env_session, session_id): |
| 60 | + """Parameterized test for session string with environment path.""" |
| 61 | + request = {"sessionInfo": {"session": env_session}} |
| 62 | + with app.test_request_context(json=request): |
| 63 | + res = log_session_id_for_troubleshooting(flask.request) |
| 64 | + assert session_id in str(res) |
0 commit comments