def test_connect_will_prompt_password_when_forced(): mock_app = MagicMock(name='PeekApp') mock_app.config.as_bool = MagicMock(return_value=False) connect(mock_app, **{ 'username': '******', 'password': '******', 'force_prompt': True, }) mock_app.input.assert_called()
def test_connect_will_fail_when_password_is_not_provided_and_prompt_is_not_allowed(): mock_app = MagicMock(name='PeekApp') mock_app.config.as_bool = MagicMock(return_value=False) with pytest.raises(PeekError) as e: connect(mock_app, **{ 'username': '******', 'no_prompt': True, }) assert 'Password is not found and password prompt is disabled' in str(e)
def test_connect_will_use_key_ring_when_configured(): mock_keyring = MagicMock(return_value='password') with patch('peek.connection._keyring', mock_keyring): mock_app = MagicMock(name='PeekApp') mock_app.config.as_bool = MagicMock(return_value=True) connect(mock_app, **{ 'username': '******', }) mock_keyring.assert_has_calls( [call('peek/localhost:9200/userpass', 'foo'), call('peek/localhost:9200/userpass', 'foo', 'password')])
def test_connect_will_not_prompt_password_when_password_is_found_in_env(): mock_app = MagicMock(name='PeekApp') mock_app.config.as_bool = MagicMock(return_value=False) client = connect(mock_app, **{ 'username': '******', 'name': 'my-connection' }) mock_app.input.assert_not_called() assert str(client) == 'my-connection'
def test_connect_will_prompt_password_when_no_password_is_found(): mock_app = MagicMock(name='PeekApp') mock_app.config.as_bool = MagicMock(return_value=False) client = connect(mock_app, **{ 'username': '******', 'hosts': 'localhost:9201', 'use_ssl': True, }) mock_app.input.assert_called() assert str(client) == 'foo @ https://localhost:9201'
def test_connect_has_second_priority_for_token(): mock_app = MagicMock(name='PeekApp') mock_app.config.as_bool = MagicMock(return_value=False) client = connect(mock_app, **{ 'token': 'some-token', 'username': '******', 'password': '******', }) assert str(client) == 'T-some-token @ http://localhost:9200' assert client.info()['auth'].startswith('Token some-token')
def test_connect_has_highest_priority_for_api_key(): mock_app = MagicMock(name='PeekApp') mock_app.config.as_bool = MagicMock(return_value=False) client = connect(mock_app, **{ 'api_key': 'id:key', 'token': 'some-token', 'username': '******', 'password': '******', }) assert str(client) == 'K-id @ http://localhost:9200' assert client.info()['auth'].startswith('ApiKey id')
def test_es_client_to_and_from_dict(): mock_app = MagicMock(name='PeekApp') mock_app.config.as_bool = MagicMock(return_value=False) client = connect(mock_app, **{ 'username': '******', 'password': '******', 'hosts': 'example.com:9200', 'use_ssl': True, }) d = client.to_dict() assert d['password'] is None with patch.dict(os.environ, {'PEEK_PASSWORD': '******'}): assert client.to_dict() == EsClient.from_dict(mock_app, d).to_dict()
def test_connect_will_prefer_cloud_id(): mock_app = MagicMock(name='PeekApp') mock_app.config.as_bool = MagicMock(return_value=False) mock_es = MagicMock MockEs = MagicMock(return_value=mock_es) with patch('peek.connection.Elasticsearch', MockEs): client = connect(mock_app, **{ 'username': '******', 'password': '******', 'cloud_id': 'my-cloud-id', 'hosts': 'example.com:9200', }) assert str(client) == 'foo @ my-cloud-id' assert client.hosts is None
def test_refreshing_es_client_to_and_from_dict(): mock_app = MagicMock(name='PeekApp') mock_app.config.as_bool = MagicMock(return_value=False) parent = connect(mock_app, **{ 'username': '******', 'password': '******', 'hosts': 'example.com:9200', 'use_ssl': True, }) client = RefreshingEsClient( parent=parent, username='******', access_token='access_token', refresh_token='refresh_token', expires_in=42, name='my-refreshing-client' ) assert client.to_dict() == RefreshingEsClient.from_dict(client.to_dict()).to_dict()
def _init_es_client_manager(self): if self.cli_ns.zero_connection: self._repopulate_clients(EsClientManager()) return options = {} keys = [ 'name', 'hosts', 'cloud_id', 'username', 'password', 'api_key', 'token', 'use_ssl', 'verify_certs', 'assert_hostname', 'ca_certs', 'client_cert', 'client_key', 'force_prompt', 'no_prompt', ] for k in keys: v = getattr(self.cli_ns, k, None) if v is not None: options[k] = v if self._should_auto_load_session(options): _logger.info('Auto-loading connection state') data = self.history.load_session(AUTO_SAVE_NAME) if data is not None: self._repopulate_clients( EsClientManager.from_dict(self, json.loads(data))) return es_client_manager = EsClientManager() es_client_manager.add(connect(self, **options)) self._repopulate_clients(es_client_manager)
def test_connect_default(): mock_app = MagicMock(name='PeekApp') client = connect(mock_app) mock_app.input.assert_not_called() assert str(client) == 'http://localhost:9200'