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

Commit ebb528a

Browse files
committed
Add tests
- Add auth renewal tests - Add client auth renewal tests
1 parent 6074fa3 commit ebb528a

File tree

3 files changed

+174
-1
lines changed

3 files changed

+174
-1
lines changed

tests/unit/auth_renewal_tests.py

Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
#!/usr/bin/env python
2+
# Copyright (c) 2016 IBM. All rights reserved.
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
"""
16+
Unit tests for the renewal of cookie auth
17+
18+
See configuration options for environment variables in unit_t_db_base
19+
module docstring.
20+
"""
21+
import unittest
22+
import os
23+
import requests
24+
import time
25+
26+
from cloudant._common_util import InfiniteSession
27+
28+
from .unit_t_db_base import UnitTestDbBase
29+
30+
@unittest.skipIf(os.environ.get('ADMIN_PARTY') == 'true', 'Skipping - Admin Party mode')
31+
class AuthRenewalTests(UnitTestDbBase):
32+
"""
33+
Auto renewal tests primarily testing the InfiniteSession functionality
34+
"""
35+
36+
def setUp(self):
37+
"""
38+
Override UnitTestDbBase.setUp() with no set up
39+
"""
40+
pass
41+
42+
def tearDown(self):
43+
"""
44+
Override UnitTestDbBase.tearDown() with no tear down
45+
"""
46+
pass
47+
48+
def test_client_db_doc_stack_success(self):
49+
"""
50+
Ensure that auto renewal of cookie auth happens as expected and applies
51+
to all references of r_session throughout the library.
52+
"""
53+
try:
54+
self.set_up_client(auto_connect=True, auto_renew=True)
55+
db = self.client._DATABASE_CLASS(self.client, self.dbname())
56+
db.create()
57+
db_2 = self.client._DATABASE_CLASS(self.client, self.dbname())
58+
doc = db.create_document({'_id': 'julia001', 'name': 'julia'})
59+
60+
auth_session = self.client.r_session.cookies.get('AuthSession')
61+
db_auth_session = db.r_session.cookies.get('AuthSession')
62+
db_2_auth_session = db_2.r_session.cookies.get('AuthSession')
63+
doc_auth_session = doc.r_session.cookies.get('AuthSession')
64+
65+
self.assertIsInstance(self.client.r_session, InfiniteSession)
66+
self.assertIsInstance(db.r_session, InfiniteSession)
67+
self.assertIsInstance(db_2.r_session, InfiniteSession)
68+
self.assertIsInstance(doc.r_session, InfiniteSession)
69+
self.assertIsNotNone(auth_session)
70+
self.assertTrue(
71+
auth_session ==
72+
db_auth_session ==
73+
db_2_auth_session ==
74+
doc_auth_session
75+
)
76+
self.assertTrue(db.exists())
77+
self.assertTrue(doc.exists())
78+
79+
# Will cause a 401 response to be handled internally
80+
self.client.r_session.cookies.clear()
81+
self.assertIsNone(self.client.r_session.cookies.get('AuthSession'))
82+
self.assertIsNone(db.r_session.cookies.get('AuthSession'))
83+
self.assertIsNone(db_2.r_session.cookies.get('AuthSession'))
84+
self.assertIsNone(doc.r_session.cookies.get('AuthSession'))
85+
86+
time.sleep(1) # Ensure a different cookie auth value
87+
88+
# 401 response handled by renew of cookie auth and retry of request
89+
db_2.create()
90+
91+
new_auth_session = self.client.r_session.cookies.get('AuthSession')
92+
new_db_auth_session = db.r_session.cookies.get('AuthSession')
93+
new_db_2_auth_session = db_2.r_session.cookies.get('AuthSession')
94+
new_doc_auth_session = doc.r_session.cookies.get('AuthSession')
95+
self.assertIsNotNone(new_auth_session)
96+
self.assertNotEqual(new_auth_session, auth_session)
97+
self.assertTrue(
98+
new_auth_session ==
99+
new_db_auth_session ==
100+
new_db_2_auth_session ==
101+
new_doc_auth_session
102+
)
103+
self.assertTrue(db.exists())
104+
self.assertTrue(doc.exists())
105+
finally:
106+
# Clean up
107+
self.client.delete_database(db.database_name)
108+
self.client.delete_database(db_2.database_name)
109+
self.client.disconnect()
110+
del self.client
111+
112+
def test_client_db_doc_stack_failure(self):
113+
"""
114+
Ensure that when the regular requests.Session is used that
115+
cookie auth renewal is not handled.
116+
"""
117+
try:
118+
self.set_up_client(auto_connect=True)
119+
db = self.client._DATABASE_CLASS(self.client, self.dbname())
120+
db.create()
121+
122+
self.assertIsInstance(self.client.r_session, requests.Session)
123+
self.assertIsInstance(db.r_session, requests.Session)
124+
125+
# Will cause a 401 response
126+
self.client.r_session.cookies.clear()
127+
128+
# 401 response expected to raised
129+
with self.assertRaises(requests.HTTPError) as cm:
130+
db.delete()
131+
self.assertEqual(cm.exception.response.status_code, 401)
132+
finally:
133+
# Manual reconnect
134+
self.client.disconnect()
135+
self.client.connect()
136+
# Clean up
137+
self.client.delete_database(db.database_name)
138+
self.client.disconnect()
139+
del self.client
140+
141+
142+
if __name__ == '__main__':
143+
unittest.main()

tests/unit/client_tests.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
from cloudant.client import Cloudant, CouchDB
3333
from cloudant.error import CloudantException, CloudantArgumentError
3434
from cloudant.feed import Feed, InfiniteFeed
35+
from cloudant._common_util import InfiniteSession
3536

3637
from .unit_t_db_base import UnitTestDbBase
3738
from .. import bytes_, str_
@@ -120,6 +121,33 @@ def test_multiple_connect(self):
120121
self.client.disconnect()
121122
self.assertIsNone(self.client.r_session)
122123

124+
def test_auto_renew_enabled(self):
125+
"""
126+
Test that InfiniteSession is used when auto_renew is enabled.
127+
"""
128+
try:
129+
self.set_up_client(auto_renew=True)
130+
self.client.connect()
131+
if os.environ.get('ADMIN_PARTY') == 'true':
132+
self.assertIsInstance(self.client.r_session, requests.Session)
133+
else:
134+
self.assertIsInstance(self.client.r_session, InfiniteSession)
135+
finally:
136+
self.client.disconnect()
137+
138+
def test_auto_renew_enabled_with_auto_connect(self):
139+
"""
140+
Test that InfiniteSession is used when auto_renew is enabled along with
141+
an auto_connect.
142+
"""
143+
try:
144+
self.set_up_client(auto_connect=True, auto_renew=True)
145+
if os.environ.get('ADMIN_PARTY') == 'true':
146+
self.assertIsInstance(self.client.r_session, requests.Session)
147+
else:
148+
self.assertIsInstance(self.client.r_session, InfiniteSession)
149+
finally:
150+
self.client.disconnect()
123151

124152
def test_session(self):
125153
"""

tests/unit/unit_t_db_base.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ def setUp(self):
135135
"""
136136
self.set_up_client()
137137

138-
def set_up_client(self, auto_connect=False, encoder=None):
138+
def set_up_client(self, auto_connect=False, auto_renew=False, encoder=None):
139139
if os.environ.get('RUN_CLOUDANT_TESTS') is None:
140140
admin_party = False
141141
if (os.environ.get('ADMIN_PARTY') and
@@ -150,6 +150,7 @@ def set_up_client(self, auto_connect=False, encoder=None):
150150
admin_party,
151151
url=self.url,
152152
connect=auto_connect,
153+
auto_renew=auto_renew,
153154
encoder=encoder
154155
)
155156
else:
@@ -165,6 +166,7 @@ def set_up_client(self, auto_connect=False, encoder=None):
165166
url=self.url,
166167
x_cloudant_user=self.account,
167168
connect=auto_connect,
169+
auto_renew=auto_renew,
168170
encoder=encoder
169171
)
170172

0 commit comments

Comments
 (0)