|
| 1 | +"""Provide some base configurations for tests.""" |
| 2 | +import phpypam |
| 3 | +import pytest |
| 4 | +import py.path |
| 5 | +import yaml |
| 6 | + |
| 7 | +from urllib.parse import urlparse, urlunparse |
| 8 | + |
| 9 | +TEST_CASES_PATH = py.path.local(__file__).realpath() / '..' / 'test_cases' |
| 10 | + |
| 11 | +with open('tests/vars/server.yml') as c: |
| 12 | + server = yaml.safe_load(c) |
| 13 | + |
| 14 | + |
| 15 | +@pytest.fixture(scope='module') |
| 16 | +def pi(*arg, **kwargs): |
| 17 | + """Create a phpypam.api object and return it. |
| 18 | +
|
| 19 | + :return: object phpypam |
| 20 | + :rtype: phpypam.api |
| 21 | + """ |
| 22 | + url = kwargs.pop('url', server['url']) |
| 23 | + app_id = kwargs.pop('app_id', server['app_id']) |
| 24 | + username = kwargs.pop('username', server['username']) |
| 25 | + password = kwargs.pop('password', server['password']) |
| 26 | + ssl_verify = kwargs.pop('ssl_verify', True) |
| 27 | + |
| 28 | + return phpypam.api( |
| 29 | + url=url, |
| 30 | + app_id=app_id, |
| 31 | + username=username, |
| 32 | + password=password, |
| 33 | + ssl_verify=ssl_verify, |
| 34 | + **kwargs |
| 35 | + ) |
| 36 | + |
| 37 | + |
| 38 | +def find_all_test_cases(): |
| 39 | + """Generate list of test cases. |
| 40 | +
|
| 41 | + :yield: generates each test case as list item |
| 42 | + :rtype: str |
| 43 | + """ |
| 44 | + for c in TEST_CASES_PATH.listdir(sort=True): |
| 45 | + c = c.basename |
| 46 | + if c.endswith('.py'): |
| 47 | + yield c.replace('.py', '') |
| 48 | + |
| 49 | + |
| 50 | +TEST_CASES = list(find_all_test_cases()) |
| 51 | + |
| 52 | + |
| 53 | +def pytest_addoption(parser): |
| 54 | + """Change command line options defaults. |
| 55 | +
|
| 56 | + We want run our tests only in three modes |
| 57 | + `live` - interact with an existing API |
| 58 | + `record` - interact with an existing API and record the interactions |
| 59 | + `replay` - replay previouly recorded interactions with API |
| 60 | +
|
| 61 | + :param parser: A parser object |
| 62 | + :type parser: object parser |
| 63 | + """ |
| 64 | + parser.addoption( |
| 65 | + "--vcrmode", |
| 66 | + action="store", |
| 67 | + default="replay", |
| 68 | + choices=["replay", "record", "live"], |
| 69 | + help="mode for vcr recording; one of ['replay', 'record', 'live']", |
| 70 | + ) |
| 71 | + |
| 72 | + |
| 73 | +@pytest.fixture |
| 74 | +def vcrmode(request): |
| 75 | + """Return vcrmode of a request. |
| 76 | +
|
| 77 | + :param request: A request object |
| 78 | + :type request: object request |
| 79 | + :return: vcrmode |
| 80 | + :rtype: str |
| 81 | + """ |
| 82 | + return request.config.getoption("vcrmode") |
| 83 | + |
| 84 | + |
| 85 | +def cassette_name(test_name=None): |
| 86 | + """Generate cassette_name.""" |
| 87 | + return 'tests/fixtures/{0}.yml'.format(test_name) |
| 88 | + |
| 89 | + |
| 90 | +FILTER_REQUEST_HEADERS = ['Authorization', 'Cookie', 'Token'] |
| 91 | +FILTER_RESPONSE_HEADERS = ['Apipie-Checksum', 'Date', 'ETag', 'Server', 'Set-Cookie', 'Via', 'X-Powered-By', 'X-Request-Id', 'X-Runtime'] |
| 92 | + |
| 93 | + |
| 94 | +def filter_response(response): |
| 95 | + """Filter headers before recording. |
| 96 | +
|
| 97 | + :param response: A response object where we want to filter the headers from. |
| 98 | + :type response: object response |
| 99 | + :return: response |
| 100 | + :rtype: object response |
| 101 | + """ |
| 102 | + for header in FILTER_RESPONSE_HEADERS: |
| 103 | + # headers should be case insensitive, but for some reason they weren't for me |
| 104 | + response['headers'].pop(header.lower(), None) |
| 105 | + response['headers'].pop(header, None) |
| 106 | + |
| 107 | + return response |
| 108 | + |
| 109 | + |
| 110 | +def filter_request_uri(request): |
| 111 | + """Filter uri before recording. |
| 112 | +
|
| 113 | + :param request: A request object where we want to filter the uri from. |
| 114 | + :type request: object request |
| 115 | + :return: request |
| 116 | + :rtype: object request |
| 117 | + """ |
| 118 | + request.uri = urlunparse(urlparse(request.uri)._replace(netloc="ipam.example.org")) |
| 119 | + return request |
0 commit comments