How to use the cloudant._client_session.IAMSession function in cloudant

To help you get started, we’ve selected a few cloudant examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github cloudant / python-cloudant / tests / unit / iam_auth_tests.py View on Github external
def test_iam_logout(self):
        iam = IAMSession(MOCK_API_KEY, 'http://127.0.0.1:5984')
        # add a valid cookie to jar
        iam.cookies.set_cookie(self._mock_cookie())
        self.assertEqual(len(iam.cookies.keys()), 1)
        iam.logout()
        self.assertEqual(len(iam.cookies.keys()), 0)
github cloudant / python-cloudant / tests / unit / iam_auth_tests.py View on Github external
def test_iam_get_access_token(self, m_req):
        m_response = mock.MagicMock()
        mock_token_response_text = mock.PropertyMock(return_value=MOCK_IAM_TOKEN_RESPONSE)
        type(m_response).text = mock_token_response_text
        m_req.return_value = m_response

        iam = IAMSession(MOCK_API_KEY, 'http://127.0.0.1:5984')
        access_token = iam._get_access_token()

        m_req.assert_called_once_with(
            'POST',
            iam._token_url,
            auth=None,
            headers={'Accepts': 'application/json'},
            data={
                'grant_type': 'urn:ibm:params:oauth:grant-type:apikey',
                'response_type': 'cloud_iam',
                'apikey': MOCK_API_KEY
            }
        )

        self.assertEqual(access_token, MOCK_ACCESS_TOKEN)
        self.assertTrue(m_response.raise_for_status.called)
github cloudant / python-cloudant / tests / unit / iam_auth_tests.py View on Github external
def test_iam_get_access_token_with_iam_client_id_and_secret(self, m_req):
        m_response = mock.MagicMock()
        mock_token_response_text = mock.PropertyMock(return_value=MOCK_IAM_TOKEN_RESPONSE)
        type(m_response).text = mock_token_response_text
        m_req.return_value = m_response

        iam_client_id = 'foo'
        iam_client_secret = 'bar'

        iam = IAMSession(MOCK_API_KEY, 'http://127.0.0.1:5984',
                         client_id=iam_client_id,
                         client_secret=iam_client_secret)
        access_token = iam._get_access_token()

        m_req.assert_called_once_with(
            'POST',
            iam._token_url,
            auth=(iam_client_id, iam_client_secret),
            headers={'Accepts': 'application/json'},
            data={
                'grant_type': 'urn:ibm:params:oauth:grant-type:apikey',
                'response_type': 'cloud_iam',
                'apikey': MOCK_API_KEY
            }
        )
github cloudant / python-cloudant / tests / unit / iam_auth_tests.py View on Github external
def test_iam_get_session_info(self, m_get):
        m_info = '{"ok": true, "info": {"authentication_db": "_users"}}'

        m_response = mock.MagicMock()
        type(m_response).text = mock.PropertyMock(return_value=m_info)
        m_get.return_value = m_response

        iam = IAMSession(MOCK_API_KEY, 'http://127.0.0.1:5984')
        info = iam.info()

        m_get.assert_called_once_with(iam._session_url)

        self.assertEqual(info, json.loads(m_info))
        self.assertTrue(m_response.raise_for_status.called)
github cloudant / python-cloudant / tests / unit / iam_auth_tests.py View on Github external
def test_iam_renew_cookie_on_401_failure(self, m_req, m_login):
        # mock 401
        m_response_bad = mock.MagicMock()
        type(m_response_bad).status_code = mock.PropertyMock(return_value=401)

        m_req.return_value = m_response_bad

        iam = IAMSession(MOCK_API_KEY, 'http://127.0.0.1:5984', auto_renew=True)
        iam.login()
        self.assertEqual(m_login.call_count, 1)

        # add a valid cookie to jar
        iam.cookies.set_cookie(self._mock_cookie())

        resp = iam.request('GET', 'http://127.0.0.1:5984/mydb1')
        self.assertEqual(resp.status_code, 401)
        self.assertEqual(m_login.call_count, 2)
        self.assertEqual(m_req.call_count, 2)

        resp = iam.request('GET', 'http://127.0.0.1:5984/mydb1')
        self.assertEqual(resp.status_code, 401)
        self.assertEqual(m_login.call_count, 3)
        self.assertEqual(m_req.call_count, 4)
github cloudant / python-cloudant / tests / unit / iam_auth_tests.py View on Github external
def test_iam_set_credentials(self):
        iam = IAMSession(MOCK_API_KEY, 'http://127.0.0.1:5984')
        self.assertEquals(iam._api_key, MOCK_API_KEY)

        new_api_key = 'some_new_api_key'
        iam.set_credentials(None, new_api_key)

        self.assertEquals(iam._api_key, new_api_key)
github cloudant / python-cloudant / tests / unit / iam_auth_tests.py View on Github external
def test_iam_first_request(self, m_req, m_login):
        # mock 200
        m_response_ok = mock.MagicMock()
        type(m_response_ok).status_code = mock.PropertyMock(return_value=200)
        type(m_response_ok).text = mock.PropertyMock(return_value='{"ok": true}')

        m_req.return_value = m_response_ok

        iam = IAMSession(MOCK_API_KEY, 'http://127.0.0.1:5984', auto_renew=True)
        iam.login()

        self.assertEqual(m_login.call_count, 1)
        self.assertEqual(m_req.call_count, 0)

        # add a valid cookie to jar
        iam.cookies.set_cookie(self._mock_cookie())

        resp = iam.request('GET', 'http://127.0.0.1:5984/mydb1')

        self.assertEqual(m_login.call_count, 1)
        self.assertEqual(m_req.call_count, 1)
        self.assertEqual(resp.status_code, 200)
github cloudant / python-cloudant / tests / unit / iam_auth_tests.py View on Github external
def test_iam_renew_cookie_on_401_success(self, m_req, m_login):
        # mock 200
        m_response_ok = mock.MagicMock()
        type(m_response_ok).status_code = mock.PropertyMock(return_value=200)
        type(m_response_ok).text = mock.PropertyMock(return_value='{"ok": true}')
        # mock 401
        m_response_bad = mock.MagicMock()
        type(m_response_bad).status_code = mock.PropertyMock(return_value=401)

        m_req.side_effect = [m_response_bad, m_response_ok, m_response_ok]

        iam = IAMSession(MOCK_API_KEY, 'http://127.0.0.1:5984', auto_renew=True)
        iam.login()
        self.assertEqual(m_login.call_count, 1)

        # add a valid cookie to jar
        iam.cookies.set_cookie(self._mock_cookie())

        resp = iam.request('GET', 'http://127.0.0.1:5984/mydb1')
        self.assertEqual(resp.status_code, 200)
        self.assertEqual(m_login.call_count, 2)
        self.assertEqual(m_req.call_count, 2)

        resp = iam.request('GET', 'http://127.0.0.1:5984/mydb1')
        self.assertEqual(resp.status_code, 200)
        self.assertEqual(m_login.call_count, 2)
        self.assertEqual(m_req.call_count, 3)
github cloudant / python-cloudant / tests / unit / iam_auth_tests.py View on Github external
def test_iam_renew_cookie_disabled(self, m_req, m_login):
        # mock 401
        m_response_bad = mock.MagicMock()
        type(m_response_bad).status_code = mock.PropertyMock(return_value=401)

        m_req.return_value = m_response_bad

        iam = IAMSession(MOCK_API_KEY, 'http://127.0.0.1:5984', auto_renew=False)
        iam.login()
        self.assertEqual(m_login.call_count, 1)

        resp = iam.request('GET', 'http://127.0.0.1:5984/mydb1')
        self.assertEqual(resp.status_code, 401)
        self.assertEqual(m_login.call_count, 1)  # no attempt to renew
        self.assertEqual(m_req.call_count, 1)

        resp = iam.request('GET', 'http://127.0.0.1:5984/mydb1')
        self.assertEqual(resp.status_code, 401)
        self.assertEqual(m_login.call_count, 1)  # no attempt to renew
        self.assertEqual(m_req.call_count, 2)
github cloudant / python-cloudant / tests / unit / iam_auth_tests.py View on Github external
def test_iam_login(self, m_token, m_req):
        m_token.return_value = MOCK_ACCESS_TOKEN
        m_response = mock.MagicMock()
        m_req.return_value = m_response

        iam = IAMSession(MOCK_API_KEY, 'http://127.0.0.1:5984')
        iam.login()

        m_req.assert_called_once_with(
            'POST',
            iam._session_url,
            headers={'Content-Type': 'application/json'},
            data=json.dumps({'access_token': MOCK_ACCESS_TOKEN})
        )

        self.assertEqual(m_token.call_count, 1)
        self.assertTrue(m_response.raise_for_status.called)