Esempio n. 1
0
def test_check_response_status_code_add_info(client_err, expected_description):
    info_key = generate_random_string()
    info_value = generate_random_string()
    actual_msg = http_helpers.check_response_status_code(
        expected_description,
        client_err,
        additional_info={info_key: info_value})
    assert_.is_in(info_key, actual_msg)
    assert_.is_in(info_value, actual_msg)
Esempio n. 2
0
def _headers_to_test():
    return [
        {'Content-Type': 'application/json'},
        {'Content-Type': 'text/html', generate_random_string(): generate_random_string()},
        {
            'Content-Type': 'application/json',
            'HEADER 1': 'Value',
            generate_random_string(): generate_random_string()
        }
    ]
Esempio n. 3
0
def test_skip_headers(headers_to_test):
    key_to_skip = generate_random_string()
    headers_to_test[key_to_skip] = generate_random_string()

    curl = _request_curl_with_defaults(
        kwargs={'headers': headers_to_test}, skip_headers=[key_to_skip]
    )

    assert key_to_skip not in curl
    assert headers_to_test[key_to_skip] not in curl
Esempio n. 4
0
def test_override_headers(headers_to_test):
    value_to_find = generate_random_string()
    key_to_override = generate_random_string()
    value_to_not_find = generate_random_string()

    headers_to_test[key_to_override] = value_to_not_find
    curl = _request_curl_with_defaults(
        kwargs={'headers': headers_to_test}, override_headers={key_to_override: value_to_find}
    )

    assert value_to_find in curl
    assert value_to_not_find not in curl
def _make_request(log_dir,
                  session,
                  request_item,
                  log_message=None,
                  url_prefix=None,
                  **kwargs):
    '''Make a request and return the log contents, info about the request, and the response.'''
    log_file = _setup_logging(log_dir)
    method = request_item.method.lower()
    url = request_item.url

    if url_prefix:
        url = urljoin(url_prefix, url)

    if log_message:
        session.log(log_message)

    session_method = getattr(session, method)
    outgoing_text = generate_random_string()
    response = session_method(url, data=outgoing_text, **kwargs)

    log_contents = get_file_contents(log_file)
    fields = {
        'request URL': url,
        'outgoing text': outgoing_text,
        'status_code': str(response.status_code),
        'response text': request_item.text,
    }

    return log_contents, fields, response
Esempio n. 6
0
def test_exclude_params(test_param):
    # The GET method is not valid for testing the exclusion of a parameter, as it doesn't
    # actually appear in the curl, so there is no way to tell if it was purposefully excluded.
    valid_exclusion_test_methods = _methods_to_test(exclude='GET')
    excluded_value = generate_random_string()
    data_to_test = {
        'headers': {'kwargs': {'headers': {excluded_value: excluded_value}}},
        'data': {'kwargs': {'data': {excluded_value: excluded_value}}},
        'method': {'method': choice(valid_exclusion_test_methods)},
        'url': {'url': '{}{}'.format(DEFAULT_URL, excluded_value)},
        'command': {'command': excluded_value}
    }

    kwargs = {'exclude_params': [test_param]}
    kwargs.update(data_to_test[test_param])
    curl = _request_curl_with_defaults(**kwargs)

    # the param 'method' is its own special snowflake. It can not be set to a truly random value,
    # and must be one of the valid test methods.
    excluded_value = data_to_test['method'].get(test_param, excluded_value)
    assert excluded_value not in curl
    def log_request(self, *args, **kwargs):
        pass

    def log_response(self, *args, **kwargs):
        pass


MOCK_SCHEME = 'file'  # Only some schemes work because urljoin is finicky
MOCK_BASE = '{}://test.com/'.format(MOCK_SCHEME)

RequestItem = namedtuple('RequestItem', 'url method status_code text')
REQUESTS_TO_TEST = [
    RequestItem(url='ok',
                method='GET',
                status_code=200,
                text=generate_random_string()),
    RequestItem(url='accepted',
                method='PUT',
                status_code=201,
                text=generate_random_string()),
    RequestItem(url='created',
                method='POST',
                status_code=202,
                text=generate_random_string()),
]

TOKEN = generate_random_string(prefix='token-', size=25)
USERNAME = generate_random_string(prefix='username-', size=25)
PASSWORD = generate_random_string(prefix='password', size=25)

OVERRIDE_TOKEN = generate_random_string(prefix='other-token-', size=25)
Esempio n. 8
0
def test_invalid_json_with_message(bad_json):
    random_text = generate_random_string()
    with pytest.raises(AssertionError) as e:
        http_helpers.safe_json_from(bad_json, description=random_text)
    assert random_text in str(e.value)
Esempio n. 9
0
def single_item_random_dict():
    return {generate_random_string(): generate_random_string()}
Esempio n. 10
0

@pytest.mark.parametrize('test_request,test_resp', product(requests_to_test(), responses_to_test()))
def test_request_and_response_are_logged(log_dir, test_request, test_resp, **kwargs):
    log_contents = _setup_log_and_get_contents(log_dir, test_request, test_resp, **kwargs)

    _verify_request(test_request, log_contents)
    _verify_response(test_resp, log_contents, resp_text=test_resp.text)

    return log_contents


@pytest.mark.parametrize(
    'logger_name,test_request,test_resp',
    product(
        [generate_random_string(), generate_random_string()],
        requests_to_test(),
        responses_to_test()
    )
)
def test_log_can_be_passed(log_dir, logger_name, test_request, test_resp):
    log_contents = test_request_and_response_are_logged(
        log_dir, test_request, test_resp, logger=logging.getLogger(logger_name)
    )
    msg = 'Log info:\n\n{}\n\n Did not contain logger name: {}'.format(log_contents, logger_name)
    assert logger_name in log_contents, msg


@pytest.mark.parametrize(
    'test_param,test_request,test_resp',
    product(['url', 'method'], requests_to_test(), responses_to_test())
Esempio n. 11
0
def _urls_to_test():
    # Joining the url with a random string to provide unique urls.  The number of urls to test
    # is simply an arbitrary number.
    return [DEFAULT_URL] + [''.join([DEFAULT_URL, generate_random_string()]) for _ in range(2)]
Esempio n. 12
0

@pytest.mark.parametrize('headers_to_test', _headers_to_test())
def test_skip_headers(headers_to_test):
    key_to_skip = generate_random_string()
    headers_to_test[key_to_skip] = generate_random_string()

    curl = _request_curl_with_defaults(
        kwargs={'headers': headers_to_test}, skip_headers=[key_to_skip]
    )

    assert key_to_skip not in curl
    assert headers_to_test[key_to_skip] not in curl


@pytest.mark.parametrize('command_to_test', [generate_random_string(), generate_random_string()])
def test_command_is_used(command_to_test):
    curl = _request_curl_with_defaults(command=command_to_test)

    msg = 'command {} did not start with {}'.format(curl, command_to_test)
    assert curl.startswith(command_to_test), msg


@pytest.mark.parametrize('test_param', ['command', 'method', 'headers', 'data', 'url'])
def test_exclude_params(test_param):
    # The GET method is not valid for testing the exclusion of a parameter, as it doesn't
    # actually appear in the curl, so there is no way to tell if it was purposefully excluded.
    valid_exclusion_test_methods = _methods_to_test(exclude='GET')
    excluded_value = generate_random_string()
    data_to_test = {
        'headers': {'kwargs': {'headers': {excluded_value: excluded_value}}},