Skip to content

Commit 67e1c0d

Browse files
malmelooWhyNotHugo
authored andcommitted
Make tests pass
1 parent 89a0163 commit 67e1c0d

File tree

2 files changed

+14
-9
lines changed

2 files changed

+14
-9
lines changed

tests/storage/test_http.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
from __future__ import annotations
22

33
import pytest
4-
from aiohttp import BasicAuth
54
from aioresponses import CallbackResult
65
from aioresponses import aioresponses
76

87
from tests import normalize_item
98
from vdirsyncer.exceptions import UserError
9+
from vdirsyncer.http import BasicAuthMethod, DigestAuthMethod
1010
from vdirsyncer.storage.http import HttpStorage
1111
from vdirsyncer.storage.http import prepare_auth
1212

@@ -91,16 +91,14 @@ def test_readonly_param(aio_connector):
9191
def test_prepare_auth():
9292
assert prepare_auth(None, "", "") is None
9393

94-
assert prepare_auth(None, "user", "pwd") == BasicAuth("user", "pwd")
95-
assert prepare_auth("basic", "user", "pwd") == BasicAuth("user", "pwd")
94+
assert prepare_auth(None, "user", "pwd") == BasicAuthMethod("user", "pwd")
95+
assert prepare_auth("basic", "user", "pwd") == BasicAuthMethod("user", "pwd")
9696

9797
with pytest.raises(ValueError) as excinfo:
9898
assert prepare_auth("basic", "", "pwd")
9999
assert "you need to specify username and password" in str(excinfo.value).lower()
100100

101-
from requests.auth import HTTPDigestAuth
102-
103-
assert isinstance(prepare_auth("digest", "user", "pwd"), HTTPDigestAuth)
101+
assert isinstance(prepare_auth("digest", "user", "pwd"), DigestAuthMethod)
104102

105103
with pytest.raises(ValueError) as excinfo:
106104
prepare_auth("ladida", "user", "pwd")

vdirsyncer/http.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,11 @@ def handle_401(self, response):
3131
def get_auth_header(self, method, url):
3232
raise NotImplementedError
3333

34+
def __eq__(self, other):
35+
if not isinstance(other, AuthMethod):
36+
return False
37+
return self.__class__ == other.__class__ and self.username == other.username and self.password == other.password
38+
3439

3540
class BasicAuthMethod(AuthMethod):
3641
def handle_401(self, _response):
@@ -131,7 +136,7 @@ async def request(
131136
method,
132137
url,
133138
session,
134-
auth,
139+
auth=None,
135140
latin1_fallback=True,
136141
**kwargs,
137142
):
@@ -174,10 +179,12 @@ async def request(
174179
headers = kwargs.pop("headers", {})
175180
num_401 = 0
176181
while num_401 < 2:
177-
headers["Authorization"] = auth.get_auth_header(method, url)
182+
if auth:
183+
headers["Authorization"] = auth.get_auth_header(method, url)
178184
response = await session.request(method, url, headers=headers, **kwargs)
179185

180-
if response.ok:
186+
if response.ok or not auth:
187+
# we don't need to do the 401-loop if we don't do auth in the first place
181188
break
182189

183190
if response.status == 401:

0 commit comments

Comments
 (0)