From 2fd4aaead3010c9e491dbb130d8205d2d46d9667 Mon Sep 17 00:00:00 2001 From: Markus Unterwaditzer Date: Fri, 15 Aug 2014 18:29:11 +0200 Subject: [PATCH] Add tests for prepare_auth --- tests/storage/test_http.py | 37 ++++++++++++++++++++++++++++++++++++- 1 file changed, 36 insertions(+), 1 deletion(-) diff --git a/tests/storage/test_http.py b/tests/storage/test_http.py index 90fa320..648f98a 100644 --- a/tests/storage/test_http.py +++ b/tests/storage/test_http.py @@ -13,7 +13,7 @@ from requests import Response from tests import normalize_item -from vdirsyncer.storage.http import HttpStorage +from vdirsyncer.storage.http import HttpStorage, prepare_auth def test_list(monkeypatch): @@ -84,3 +84,38 @@ def test_readonly_param(): a = HttpStorage(url=url, read_only=True).read_only b = HttpStorage(url=url, read_only=None).read_only assert a is b is True + + +def test_prepare_auth(): + assert prepare_auth(None, '', '') is None + + assert prepare_auth('basic', 'user', 'pwd') == ('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) + + with pytest.raises(ValueError) as excinfo: + prepare_auth('ladida', 'user', 'pwd') + + assert 'unknown authentication method' in str(excinfo.value).lower() + + +@pytest.mark.parametrize('auth', (None, 'guess')) +def test_prepare_auth_guess(monkeypatch, auth): + import requests_toolbelt + + assert isinstance(prepare_auth(auth, 'user', 'pwd'), + requests_toolbelt.GuessAuth) + + if hasattr(requests_toolbelt, 'GuessAuth'): + monkeypatch.delattr(requests_toolbelt, 'GuessAuth') + + with pytest.raises(RuntimeError) as excinfo: + prepare_auth(auth, 'user', 'pwd') + + assert 'requests_toolbelt is too old' in str(excinfo.value).lower()