# Path: grafana_dashboards/client/connection.py
# class KerberosConnection(object):
#     def __init__(self, host):
#         logger.debug('Creating new kerberos connection with host=%s', host)
#         self._host = host
# 
#     def make_request(self, uri, body=None):
#         response = requests.post('{0}{1}'.format(self._host, uri), json=body, auth=HTTPKerberosAuth(), verify=False)
#         return response.json()
# 
# class BasicAuthConnection(BaseConnection):
#     def __init__(self, username, password, host, debug=0):
#         logger.debug('Creating new connection with username=%s host=%s', username, host)
# 
#         base64string = base64.encodestring(('%s:%s' % (username, password)).encode('utf-8')).replace(b'\n', b'')
# 
#         super(BasicAuthConnection, self).__init__(host, b'Basic ' + base64string, debug)
# 
# class BearerAuthConnection(BaseConnection):
#     def __init__(self, token, host, debug=0):
#         logger.debug('Creating new connection with token=%s host=%s', token[:5], host)
# 
#         super(BearerAuthConnection, self).__init__(host, 'Bearer %s' % token.strip(), debug)


    from urllib2 import Request
    from urllib.request import Request
from mock import MagicMock, patch
from requests_kerberos import HTTPKerberosAuth
from grafana_dashboards.client.connection import KerberosConnection, BasicAuthConnection, BearerAuthConnection
    assert request.get_method() == capture.value.get_method()
    assert request.data.encode('utf-8') == capture.value.data


def test_connection_with_token():
    connection = BearerAuthConnection('token', 'https://host')
    connection._opener = MagicMock()
    # noinspection PyProtectedMember
    connection._opener.open().read.return_value = '{"hello":"world"}'

    assert connection.make_request('/uri', {'it\'s': 'alive'}) == {'hello': 'world'}

    request = Request('https://host/uri',
                      '{"it\'s": "alive"}',
                      headers={
                          'Content-type': 'application/json',
                          'Accept': 'application/json',
                          'Authorization': 'Bearer token'
                      })
    capture = Capture()
    # noinspection PyProtectedMember
    connection._opener.open.assert_called_with(capture)
    assert request.get_full_url() == capture.value.get_full_url()
    assert request.header_items() == capture.value.header_items()
    assert request.get_method() == capture.value.get_method()
    assert request.data.encode('utf-8') == capture.value.data


@patch('requests.post')
def test_connection_with_kerberos(post):

# Path: grafana_dashboards/client/connection.py
# class KerberosConnection(object):
#     def __init__(self, host):
#         logger.debug('Creating new kerberos connection with host=%s', host)
#         self._host = host
# 
#     def make_request(self, uri, body=None):
#         response = requests.post('{0}{1}'.format(self._host, uri), json=body, auth=HTTPKerberosAuth(), verify=False)
#         return response.json()
# 
# class BasicAuthConnection(BaseConnection):
#     def __init__(self, username, password, host, debug=0):
#         logger.debug('Creating new connection with username=%s host=%s', username, host)
# 
#         base64string = base64.encodestring(('%s:%s' % (username, password)).encode('utf-8')).replace(b'\n', b'')
# 
#         super(BasicAuthConnection, self).__init__(host, b'Basic ' + base64string, debug)
# 
# class BearerAuthConnection(BaseConnection):
#     def __init__(self, token, host, debug=0):
#         logger.debug('Creating new connection with token=%s host=%s', token[:5], host)
# 
#         super(BearerAuthConnection, self).__init__(host, 'Bearer %s' % token.strip(), debug)


# Path: tests/grafana_dashboards/client/test_connection.py
    from urllib2 import Request
    from urllib.request import Request
from mock import MagicMock, patch
from requests_kerberos import HTTPKerberosAuth
from grafana_dashboards.client.connection import KerberosConnection, BasicAuthConnection, BearerAuthConnection

    assert request.get_method() == capture.value.get_method()
    assert request.data.encode('utf-8') == capture.value.data


def test_connection_with_token():
    connection = BearerAuthConnection('token', 'https://host')
    connection._opener = MagicMock()
    # noinspection PyProtectedMember
    connection._opener.open().read.return_value = '{"hello":"world"}'

    assert connection.make_request('/uri', {'it\'s': 'alive'}) == {'hello': 'world'}

    request = Request('https://host/uri',
                      '{"it\'s": "alive"}',
                      headers={
                          'Content-type': 'application/json',
                          'Accept': 'application/json',
                          'Authorization': 'Bearer token'
                      })
    capture = Capture()
    # noinspection PyProtectedMember
    connection._opener.open.assert_called_with(capture)
    assert request.get_full_url() == capture.value.get_full_url()
    assert request.header_items() == capture.value.header_items()
    assert request.get_method() == capture.value.get_method()
    assert request.data.encode('utf-8') == capture.value.data


@patch('requests.post')
def test_connection_with_kerberos(post):

    connection = KerberosConnection('https://host')