import pytest from fastapi import Security from fastapi.exceptions import FastAPIError @pytest.mark.parametrize("parameter_name", ["scopes", "oauth_scopes"]) @pytest.mark.filterwarnings("ignore::DeprecationWarning") def test_pass_single_str(parameter_name: str): """ Test passing single string instead of list of strings to `scopes` or `oauth_scopes`. """ with pytest.raises(FastAPIError) as exc_info: Security(dependency=lambda: None, **{parameter_name: "admin"}) assert str(exc_info.value) == ( f"Invalid value for the '{parameter_name}' parameter in Security(). " "Expected a sequence of strings (e.g., ['admin', 'user']), but received a single string. " "Wrap it in a list: oauth_scopes=['your_scope'] instead of oauth_scopes='your_scope'." ) @pytest.mark.parametrize("value", ["function", "request"]) @pytest.mark.filterwarnings("ignore::DeprecationWarning") def test_pass_scope_as_scopes(value: str): """ Test passing `scopes="function"` instead of `scope="function"` to `Security`. """ with pytest.raises(FastAPIError) as exc_info: Security(dependency=lambda: None, scopes=value) assert str(exc_info.value) == ( "Invalid value for the 'scopes' parameter in Security(). " "Expected a sequence of strings (e.g., ['admin', 'user']), but received a single string. " f'Did you mean to use scope="{value}" to specify when the exit code of dependencies with yield should run? ' )