예제 #1
0
def test_basic_auth(docker_env):
    gateway_ip, gateway_port = docker_env.get_host_ip_port('gateway')
    gateway_session = SSHSession(host=gateway_ip,
                                 port=gateway_port,
                                 username='******',
                                 password='******').open()
    rest_client = RestSshClient(gateway_session)

    uri = 'http://%s/authentication-required' % REMOTE_HOST_IP_PORT

    # wrong auth param format
    with pytest.raises(exception.RestClientError) as exc_info:
        rest_client.request('GET', uri, auth="invalid_auth_format")
    assert 'Invalid auth parameter' in str(exc_info.value)

    # authentication in error
    http_response = rest_client.request('GET',
                                        uri,
                                        auth=("wrong_user", "wrong_password"))
    assert http_response.status_code == 401
    assert 'You have to login with proper credentials' in http_response.text

    # authentication in success
    http_response = rest_client.request('GET', uri, auth=("admin", "secret"))
    assert http_response.status_code == 200
    assert http_response.text == "Authentication successful"
예제 #2
0
def test_request(docker_env):
    gateway_ip, gateway_port = docker_env.get_host_ip_port('gateway')
    gateway_session = SSHSession(host=gateway_ip,
                                 port=gateway_port,
                                 username='******',
                                 password='******').open()
    remotehost_ip, remotehost_port = docker_env.get_host_ip_port(
        'remotehost', private_port=5000)
    rest_client = RestSshClient(gateway_session)

    # check not properly formatted uri raise exception
    with pytest.raises(exception.RestClientError) as exc_info:
        rest_client.get('invalid_uri')
    assert 'returned exit status' in str(exc_info.value)

    # test header only
    http_response = rest_client.request(
        'GET',
        'http://%s:%s/' % (tests_util.get_host_ip(), remotehost_port),
        document_info_only=True)
    assert http_response.status_code == 200
    assert len(http_response.text) == 0

    # test uri parameters only
    parameters = {'param1': 'value1', 'param2': 'value2'}
    http_response = rest_client.request(
        'GET',
        'http://%s:%s/echo-parameters' %
        (tests_util.get_host_ip(), remotehost_port),
        params=parameters)
    assert http_response.status_code == 200
    # value is a list as each parameter can be specified multiple times with different values
    expected_body = {}
    for key, value in parameters.items():
        expected_body[key] = [value]
    assert http_response.json() == expected_body

    # test headers are properly handled
    http_response = rest_client.request(
        'GET',
        'http://%s:%s/echo-headers' %
        (tests_util.get_host_ip(), remotehost_port),
        headers={'My-Header': 'My-Value'})
    assert http_response.status_code == 200
    assert 'My-Header' in http_response.json()
    assert http_response.json()['My-Header'] == 'My-Value'