diff --git a/tests/unit/integrations/gitea/middlewares/check_enabled/test_GiteaCheckEnabledMiddleware.py b/tests/unit/integrations/gitea/middlewares/check_enabled/test_GiteaCheckEnabledMiddleware.py new file mode 100644 index 0000000..8ee6fc1 --- /dev/null +++ b/tests/unit/integrations/gitea/middlewares/check_enabled/test_GiteaCheckEnabledMiddleware.py @@ -0,0 +1,225 @@ +# Copyright 2026 jCloud + +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at + +# http://www.apache.org/licenses/LICENSE-2.0 + +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +from src.jcloud_deployment_server.integrations.gitea.middlewares.check_enabled import GiteaCheckEnabledMiddleware +from tests.utils.make_request import call_next, make_request +from jcloud_config_parser.ini import INIConfiguration +import pytest + +@pytest.mark.asyncio +@pytest.mark.parametrize('path,enabled,should_fail', [ + ('/gitea', 'false', True), + ('/gitea', '', True), + ('/gitea', 'f', True), + ('/gitea', 'n', True), + ('/gitea', '42', True), + ('/gitea', 'abcdefg', True), + + ('/gitea/', 'false', True), + ('/gitea/', '', True), + ('/gitea/', 'f', True), + ('/gitea/', 'n', True), + ('/gitea/', '42', True), + ('/gitea/', 'abcdefg', True), + + ('/gitea/42', 'false', True), + ('/gitea/42', '', True), + ('/gitea/42', 'f', True), + ('/gitea/42', 'n', True), + ('/gitea/42', '42', True), + ('/gitea/42', 'abcdefg', True), + + ('/gitea/ ', 'false', True), + ('/gitea/ ', '', True), + ('/gitea/ ', 'f', True), + ('/gitea/ ', 'n', True), + ('/gitea/ ', '42', True), + ('/gitea/ ', 'abcdefg', True), + + + ('/gitea/a', 'false', True), + ('/gitea/a', '', True), + ('/gitea/a', 'f', True), + ('/gitea/a', 'n', True), + ('/gitea/a', '42', True), + ('/gitea/a', 'abcdefg', True), + + ('/gitea/a/b', 'false', True), + ('/gitea/a/b', '', True), + ('/gitea/a/b', 'f', True), + ('/gitea/a/b', 'n', True), + ('/gitea/a/b', '42', True), + ('/gitea/a/b', 'abcdefg', True), + + ('/gitea/a/', 'false', True), + ('/gitea/a/', '', True), + ('/gitea/a/', 'f', True), + ('/gitea/a/', 'n', True), + ('/gitea/a/', '42', True), + ('/gitea/a/', 'abcdefg', True), + + ('/gitea/a/b/', 'false', True), + ('/gitea/a/b/', '', True), + ('/gitea/a/b/', 'f', True), + ('/gitea/a/b/', 'n', True), + ('/gitea/a/b/', '42', True), + ('/gitea/a/b/', 'abcdefg', True), + + + ('/gite', 'false', False), + ('/gite', '', False), + ('/gite', 'f', False), + ('/gite', 'n', False), + ('/gite', '42', False), + ('/gite', 'abcdefg', False), + + ('/g', 'false', False), + ('/g', '', False), + ('/g', 'f', False), + ('/g', 'n', False), + ('/g', '42', False), + ('/g', 'abcdefg', False), + + ('/a', 'false', False), + ('/a', '', False), + ('/a', 'f', False), + ('/a', 'n', False), + ('/a', '42', False), + ('/a', 'abcdefg', False), + + ('/a/', 'false', False), + ('/a/', '', False), + ('/a/', 'f', False), + ('/a/', 'n', False), + ('/a/', '42', False), + ('/a/', 'abcdefg', False), + + ('/a/b', 'false', False), + ('/a/b', '', False), + ('/a/b', 'f', False), + ('/a/b', 'n', False), + ('/a/b', '42', False), + ('/a/b', 'abcdefg', False), + + ('/a/b/', 'false', False), + ('/a/b/', '', False), + ('/a/b/', 'f', False), + ('/a/b/', 'n', False), + ('/a/b/', '42', False), + ('/a/b/', 'abcdefg', False), + + ('/', 'false', False), + ('/', '', False), + ('/', 'f', False), + ('/', 'n', False), + ('/', '42', False), + ('/', 'abcdefg', False), + + + + ('/gitea', 'true', False), + ('/gitea', 'yes', False), + ('/gitea', 't', False), + ('/gitea', 'y', False), + + ('/gitea/', 'true', False), + ('/gitea/', 'yes', False), + ('/gitea/', 't', False), + ('/gitea/', 'y', False), + + ('/gitea/42', 'true', False), + ('/gitea/42', 'yes', False), + ('/gitea/42', 't', False), + ('/gitea/42', 'y', False), + + ('/gitea/ ', 'true', False), + ('/gitea/ ', 'yes', False), + ('/gitea/ ', 't', False), + ('/gitea/ ', 'y', False), + + + ('/gitea/a', 'true', False), + ('/gitea/a', 'yes', False), + ('/gitea/a', 't', False), + ('/gitea/a', 'y', False), + + ('/gitea/a/b', 'true', False), + ('/gitea/a/b', 'yes', False), + ('/gitea/a/b', 't', False), + ('/gitea/a/b', 'y', False), + + ('/gitea/a/', 'true', False), + ('/gitea/a/', 'yes', False), + ('/gitea/a/', 't', False), + ('/gitea/a/', 'y', False), + + ('/gitea/a/b/', 'true', False), + ('/gitea/a/b/', 'yes', False), + ('/gitea/a/b/', 't', False), + ('/gitea/a/b/', 'y', False), + + + ('/gite', 'true', False), + ('/gite', 'yes', False), + ('/gite', 't', False), + ('/gite', 'y', False), + + ('/g', 'true', False), + ('/g', 'yes', False), + ('/g', 't', False), + ('/g', 'y', False), + + ('/a', 'true', False), + ('/a', 'yes', False), + ('/a', 't', False), + ('/a', 'y', False), + + ('/a/', 'true', False), + ('/a/', 'yes', False), + ('/a/', 't', False), + ('/a/', 'y', False), + + ('/a/b', 'true', False), + ('/a/b', 'yes', False), + ('/a/b', 't', False), + ('/a/b', 'y', False), + + ('/a/b/', 'true', False), + ('/a/b/', 'yes', False), + ('/a/b/', 't', False), + ('/a/b/', 'y', False), + + ('/', 'true', False), + ('/', 'yes', False), + ('/', 't', False), + ('/', 'y', False), +]) +async def test_GiteaCheckEnabledMiddleware(path, enabled, should_fail): + configuration = INIConfiguration() + gitea_config = INIConfiguration() + gitea_config.enabled = enabled + configuration.gitea = gitea_config + + middleware = GiteaCheckEnabledMiddleware(None, configuration) + + req = make_request( + b'', + path + ) + + res = await middleware.dispatch(req, call_next) + if should_fail: + assert res.status_code == 403 + else: + assert hasattr(res, 'called') \ No newline at end of file