From 67e1c0ded58745cf6a8cadd8b9145c362d9bfbc3 Mon Sep 17 00:00:00 2001 From: "Mike A." Date: Tue, 10 Sep 2024 23:54:02 +0200 Subject: [PATCH] Make tests pass --- tests/storage/test_http.py | 10 ++++------ vdirsyncer/http.py | 13 ++++++++++--- 2 files changed, 14 insertions(+), 9 deletions(-) diff --git a/tests/storage/test_http.py b/tests/storage/test_http.py index 2e92d13..22c9ee9 100644 --- a/tests/storage/test_http.py +++ b/tests/storage/test_http.py @@ -1,12 +1,12 @@ from __future__ import annotations import pytest -from aiohttp import BasicAuth from aioresponses import CallbackResult from aioresponses import aioresponses from tests import normalize_item from vdirsyncer.exceptions import UserError +from vdirsyncer.http import BasicAuthMethod, DigestAuthMethod from vdirsyncer.storage.http import HttpStorage from vdirsyncer.storage.http import prepare_auth @@ -91,16 +91,14 @@ def test_readonly_param(aio_connector): def test_prepare_auth(): assert prepare_auth(None, "", "") is None - assert prepare_auth(None, "user", "pwd") == BasicAuth("user", "pwd") - assert prepare_auth("basic", "user", "pwd") == BasicAuth("user", "pwd") + assert prepare_auth(None, "user", "pwd") == BasicAuthMethod("user", "pwd") + assert prepare_auth("basic", "user", "pwd") == BasicAuthMethod("user", "pwd") with pytest.raises(ValueError) as excinfo: assert prepare_auth("basic", "", "pwd") assert "you need to specify username and password" in str(excinfo.value).lower() - from requests.auth import HTTPDigestAuth - - assert isinstance(prepare_auth("digest", "user", "pwd"), HTTPDigestAuth) + assert isinstance(prepare_auth("digest", "user", "pwd"), DigestAuthMethod) with pytest.raises(ValueError) as excinfo: prepare_auth("ladida", "user", "pwd") diff --git a/vdirsyncer/http.py b/vdirsyncer/http.py index 91c1ff0..16c60aa 100644 --- a/vdirsyncer/http.py +++ b/vdirsyncer/http.py @@ -31,6 +31,11 @@ class AuthMethod(ABC): def get_auth_header(self, method, url): raise NotImplementedError + def __eq__(self, other): + if not isinstance(other, AuthMethod): + return False + return self.__class__ == other.__class__ and self.username == other.username and self.password == other.password + class BasicAuthMethod(AuthMethod): def handle_401(self, _response): @@ -131,7 +136,7 @@ async def request( method, url, session, - auth, + auth=None, latin1_fallback=True, **kwargs, ): @@ -174,10 +179,12 @@ async def request( headers = kwargs.pop("headers", {}) num_401 = 0 while num_401 < 2: - headers["Authorization"] = auth.get_auth_header(method, url) + if auth: + headers["Authorization"] = auth.get_auth_header(method, url) response = await session.request(method, url, headers=headers, **kwargs) - if response.ok: + if response.ok or not auth: + # we don't need to do the 401-loop if we don't do auth in the first place break if response.status == 401: