|
| 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() |
0 commit comments