Run all basic auth middleware tests.
()
| 132 | |
| 133 | |
| 134 | async def run_tests() -> None: |
| 135 | """Run all basic auth middleware tests.""" |
| 136 | # Create middleware instance |
| 137 | auth_middleware = BasicAuthMiddleware("user", "pass") |
| 138 | |
| 139 | # Use middleware in session |
| 140 | async with ClientSession(middlewares=(auth_middleware,)) as session: |
| 141 | # Test 1: Correct credentials endpoint |
| 142 | print("=== Test 1: Correct credentials ===") |
| 143 | async with session.get("http://localhost:8080/basic-auth/user/pass") as resp: |
| 144 | _LOGGER.info("Status: %s", resp.status) |
| 145 | |
| 146 | if resp.status == 200: |
| 147 | data = await resp.json() |
| 148 | _LOGGER.info("Response: %s", data) |
| 149 | print("Authentication successful!") |
| 150 | print(f"Authenticated: {data.get('authenticated')}") |
| 151 | print(f"User: {data.get('user')}") |
| 152 | else: |
| 153 | print("Authentication failed!") |
| 154 | print(f"Status: {resp.status}") |
| 155 | text = await resp.text() |
| 156 | print(f"Response: {text}") |
| 157 | |
| 158 | # Test 2: Wrong credentials endpoint |
| 159 | print("\n=== Test 2: Wrong credentials endpoint ===") |
| 160 | async with session.get("http://localhost:8080/basic-auth/other/secret") as resp: |
| 161 | if resp.status == 401: |
| 162 | print("Authentication failed as expected (wrong credentials)") |
| 163 | text = await resp.text() |
| 164 | print(f"Response: {text}") |
| 165 | else: |
| 166 | print(f"Unexpected status: {resp.status}") |
| 167 | |
| 168 | # Test 3: Protected resource |
| 169 | print("\n=== Test 3: Access protected resource ===") |
| 170 | async with session.get("http://localhost:8080/protected") as resp: |
| 171 | if resp.status == 200: |
| 172 | data = await resp.json() |
| 173 | print("Successfully accessed protected resource!") |
| 174 | print(f"Response: {data}") |
| 175 | else: |
| 176 | print(f"Failed to access protected resource: {resp.status}") |
| 177 | |
| 178 | |
| 179 | async def main() -> None: |
no test coverage detected