|
| 1 | +# Xandikos |
| 2 | +# Copyright (C) 2016-2017 Jelmer Vernooij <[email protected]>, et al. |
| 3 | +# |
| 4 | +# This program is free software; you can redistribute it and/or |
| 5 | +# modify it under the terms of the GNU General Public License |
| 6 | +# as published by the Free Software Foundation; version 3 |
| 7 | +# of the License or (at your option) any later version of |
| 8 | +# the License. |
| 9 | +# |
| 10 | +# This program is distributed in the hope that it will be useful, |
| 11 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | +# GNU General Public License for more details. |
| 14 | +# |
| 15 | +# You should have received a copy of the GNU General Public License |
| 16 | +# along with this program; if not, write to the Free Software |
| 17 | +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, |
| 18 | +# MA 02110-1301, USA. |
| 19 | + |
| 20 | +"""Tests for authentication handling in xandikos.""" |
| 21 | + |
| 22 | +import asyncio |
| 23 | +import tempfile |
| 24 | +import shutil |
| 25 | +import unittest |
| 26 | +from unittest.mock import MagicMock, AsyncMock |
| 27 | +from wsgiref.util import setup_testing_defaults |
| 28 | + |
| 29 | +from xandikos.webdav import WebDAVApp |
| 30 | +from xandikos.web import MultiUserXandikosBackend |
| 31 | + |
| 32 | + |
| 33 | +class MockBackend: |
| 34 | + """Mock backend for testing.""" |
| 35 | + |
| 36 | + def __init__(self): |
| 37 | + self.set_principal_calls = [] |
| 38 | + self.resources = {} |
| 39 | + |
| 40 | + def set_principal(self, user): |
| 41 | + self.set_principal_calls.append(user) |
| 42 | + |
| 43 | + def get_resource(self, path): |
| 44 | + return self.resources.get(path) |
| 45 | + |
| 46 | + |
| 47 | +class AuthenticationTests(unittest.TestCase): |
| 48 | + """Tests for authentication header handling.""" |
| 49 | + |
| 50 | + def setUp(self): |
| 51 | + self.backend = MockBackend() |
| 52 | + self.app = WebDAVApp(self.backend) |
| 53 | + self.loop = asyncio.new_event_loop() |
| 54 | + asyncio.set_event_loop(self.loop) |
| 55 | + |
| 56 | + def tearDown(self): |
| 57 | + self.loop.close() |
| 58 | + |
| 59 | + def test_wsgi_x_remote_user_header(self): |
| 60 | + """Test that HTTP_X_REMOTE_USER is handled in WSGI.""" |
| 61 | + environ = { |
| 62 | + "REQUEST_METHOD": "OPTIONS", |
| 63 | + "PATH_INFO": "/", |
| 64 | + "HTTP_X_REMOTE_USER": "testuser", |
| 65 | + } |
| 66 | + setup_testing_defaults(environ) |
| 67 | + |
| 68 | + # Mock the resource |
| 69 | + mock_resource = MagicMock() |
| 70 | + mock_resource.resource_types = [] |
| 71 | + self.backend.resources["/"] = mock_resource |
| 72 | + |
| 73 | + # Mock start_response |
| 74 | + responses = [] |
| 75 | + |
| 76 | + def start_response(status, headers): |
| 77 | + responses.append((status, headers)) |
| 78 | + return lambda x: None |
| 79 | + |
| 80 | + # Call the WSGI handler |
| 81 | + list(self.app.handle_wsgi_request(environ, start_response)) |
| 82 | + |
| 83 | + # Check that we got a response |
| 84 | + self.assertTrue(len(responses) > 0) |
| 85 | + |
| 86 | + # Check that set_principal was called with the user |
| 87 | + self.assertEqual(["testuser"], self.backend.set_principal_calls) |
| 88 | + |
| 89 | + # Check that REMOTE_USER was set in environ for the request |
| 90 | + # (The environ is recreated in handle_wsgi_request, so we can't check it directly) |
| 91 | + |
| 92 | + def test_wsgi_no_remote_user(self): |
| 93 | + """Test WSGI without authentication header.""" |
| 94 | + environ = { |
| 95 | + "REQUEST_METHOD": "OPTIONS", |
| 96 | + "PATH_INFO": "/", |
| 97 | + } |
| 98 | + setup_testing_defaults(environ) |
| 99 | + |
| 100 | + # Mock the resource |
| 101 | + mock_resource = MagicMock() |
| 102 | + mock_resource.resource_types = [] |
| 103 | + self.backend.resources["/"] = mock_resource |
| 104 | + |
| 105 | + # Mock start_response |
| 106 | + responses = [] |
| 107 | + |
| 108 | + def start_response(status, headers): |
| 109 | + responses.append((status, headers)) |
| 110 | + return lambda x: None |
| 111 | + |
| 112 | + # Call the WSGI handler |
| 113 | + self.app.handle_wsgi_request(environ, start_response) |
| 114 | + |
| 115 | + # Check that set_principal was NOT called |
| 116 | + self.assertEqual([], self.backend.set_principal_calls) |
| 117 | + |
| 118 | + def test_aiohttp_x_remote_user_header(self): |
| 119 | + """Test that X-Remote-User header is handled in aiohttp.""" |
| 120 | + # Create a mock aiohttp request |
| 121 | + mock_request = AsyncMock() |
| 122 | + mock_headers = MagicMock() |
| 123 | + mock_headers.get.side_effect = ( |
| 124 | + lambda k, d=None: "aiohttpuser" if k == "X-Remote-User" else d |
| 125 | + ) |
| 126 | + mock_headers.__getitem__.side_effect = ( |
| 127 | + lambda k: "aiohttpuser" if k == "X-Remote-User" else None |
| 128 | + ) |
| 129 | + mock_request.headers = mock_headers |
| 130 | + mock_request.method = "OPTIONS" |
| 131 | + mock_request.path = "/" |
| 132 | + mock_request.url = "http://example.com/" |
| 133 | + mock_request.raw_path = "/" |
| 134 | + mock_request.match_info = {"path_info": "/"} |
| 135 | + mock_request.content_type = "text/plain" |
| 136 | + mock_request.content_length = 0 |
| 137 | + mock_request.can_read_body = False |
| 138 | + |
| 139 | + # Mock the resource |
| 140 | + mock_resource = MagicMock() |
| 141 | + mock_resource.resource_types = [] |
| 142 | + self.backend.resources["/"] = mock_resource |
| 143 | + |
| 144 | + # Call the aiohttp handler |
| 145 | + self.loop.run_until_complete(self.app.aiohttp_handler(mock_request, "/")) |
| 146 | + |
| 147 | + # Check that set_principal was called with the user |
| 148 | + self.assertEqual(["aiohttpuser"], self.backend.set_principal_calls) |
| 149 | + |
| 150 | + def test_aiohttp_no_remote_user(self): |
| 151 | + """Test aiohttp without authentication header.""" |
| 152 | + # Create a mock aiohttp request |
| 153 | + mock_request = AsyncMock() |
| 154 | + mock_headers = MagicMock() |
| 155 | + mock_headers.get.return_value = None |
| 156 | + mock_request.headers = mock_headers |
| 157 | + mock_request.method = "OPTIONS" |
| 158 | + mock_request.path = "/" |
| 159 | + mock_request.url = "http://example.com/" |
| 160 | + mock_request.raw_path = "/" |
| 161 | + mock_request.match_info = {"path_info": "/"} |
| 162 | + mock_request.content_type = "text/plain" |
| 163 | + mock_request.content_length = 0 |
| 164 | + mock_request.can_read_body = False |
| 165 | + |
| 166 | + # Mock the resource |
| 167 | + mock_resource = MagicMock() |
| 168 | + mock_resource.resource_types = [] |
| 169 | + self.backend.resources["/"] = mock_resource |
| 170 | + |
| 171 | + # Call the aiohttp handler |
| 172 | + self.loop.run_until_complete(self.app.aiohttp_handler(mock_request, "/")) |
| 173 | + |
| 174 | + # Check that set_principal was NOT called |
| 175 | + self.assertEqual([], self.backend.set_principal_calls) |
| 176 | + |
| 177 | + |
| 178 | +class IntegrationTests(unittest.TestCase): |
| 179 | + """Integration tests with real backends.""" |
| 180 | + |
| 181 | + def setUp(self): |
| 182 | + self.d = tempfile.mkdtemp() |
| 183 | + self.loop = asyncio.new_event_loop() |
| 184 | + asyncio.set_event_loop(self.loop) |
| 185 | + |
| 186 | + def tearDown(self): |
| 187 | + shutil.rmtree(self.d) |
| 188 | + self.loop.close() |
| 189 | + |
| 190 | + def test_multiuser_backend_with_aiohttp_auth(self): |
| 191 | + """Test MultiUserXandikosBackend with aiohttp authentication.""" |
| 192 | + backend = MultiUserXandikosBackend(self.d) |
| 193 | + app = WebDAVApp(backend) |
| 194 | + |
| 195 | + # Create a mock aiohttp request with auth |
| 196 | + mock_request = AsyncMock() |
| 197 | + mock_headers = MagicMock() |
| 198 | + mock_headers.get.side_effect = ( |
| 199 | + lambda k, d=None: "alice" if k == "X-Remote-User" else d |
| 200 | + ) |
| 201 | + mock_headers.__getitem__.side_effect = ( |
| 202 | + lambda k: "alice" if k == "X-Remote-User" else None |
| 203 | + ) |
| 204 | + mock_request.headers = mock_headers |
| 205 | + mock_request.method = "PROPFIND" |
| 206 | + mock_request.path = "/alice/" |
| 207 | + mock_request.url = "http://example.com/alice/" |
| 208 | + mock_request.raw_path = "/alice/" |
| 209 | + mock_request.match_info = {"path_info": "/alice/"} |
| 210 | + mock_request.content_type = "application/xml" |
| 211 | + mock_request.content_length = 0 |
| 212 | + mock_request.can_read_body = False |
| 213 | + |
| 214 | + # Call the aiohttp handler |
| 215 | + self.loop.run_until_complete(app.aiohttp_handler(mock_request, "/")) |
| 216 | + |
| 217 | + # Check that the principal was created |
| 218 | + resource = backend.get_resource("/alice/") |
| 219 | + self.assertIsNotNone(resource) |
| 220 | + # _mark_as_principal normalizes the path, removing trailing slashes |
| 221 | + self.assertIn("/alice", backend._user_principals) |
0 commit comments