Skip to content
This repository was archived by the owner on Aug 30, 2024. It is now read-only.

Commit 73e268f

Browse files
committed
Don't authenticate basic access requests if credentials are missing
1 parent 1847844 commit 73e268f

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

src/cloudant/client_session.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,12 @@ def request(self, method, url, **kwargs):
120120
Overrides ``requests.Session.request`` to provide basic access
121121
authentication.
122122
"""
123+
auth = None
124+
if self._username is not None and self._password is not None:
125+
auth = (self._username, self._password)
126+
123127
return super(BasicSession, self).request(
124-
method, url, auth=(self._username, self._password), **kwargs)
128+
method, url, auth=auth, **kwargs)
125129

126130

127131
class CookieSession(ClientSession):

tests/unit/client_tests.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
from cloudant import cloudant, cloudant_bluemix, couchdb, couchdb_admin_party
3636
from cloudant.client import Cloudant, CouchDB
3737
from cloudant.client_session import BasicSession, CookieSession
38+
from cloudant.database import CloudantDatabase
3839
from cloudant.error import CloudantArgumentError, CloudantClientException
3940
from cloudant.feed import Feed, InfiniteFeed
4041

@@ -246,6 +247,31 @@ def test_session_basic(self, m_req):
246247

247248
self.assertEquals(all_dbs, ['animaldb'])
248249

250+
@mock.patch('cloudant.client_session.Session.request')
251+
def test_session_basic_with_no_credentials(self, m_req):
252+
"""
253+
Test using basic access authentication with no credentials.
254+
"""
255+
m_response_ok = mock.MagicMock()
256+
type(m_response_ok).status_code = mock.PropertyMock(return_value=200)
257+
m_req.return_value = m_response_ok
258+
259+
client = Cloudant(None, None, url=self.url, use_basic_auth=True)
260+
client.connect()
261+
self.assertIsInstance(client.r_session, BasicSession)
262+
263+
db = client['animaldb']
264+
265+
m_req.assert_called_once_with(
266+
'HEAD',
267+
self.url + '/animaldb',
268+
allow_redirects=False,
269+
auth=None, # ensure no authentication specified
270+
timeout=None
271+
)
272+
273+
self.assertIsInstance(db, CloudantDatabase)
274+
249275
@mock.patch('cloudant.client_session.Session.request')
250276
def test_change_credentials_basic(self, m_req):
251277
"""

0 commit comments

Comments
 (0)