def test_init_with_lang_family(fixture_dir, requests_mock, tmpdir): with fixture_dir.join('dummy.json').open() as read_file: data = json.load(read_file) requests_mock.get('https://api.example.com/apidoc/v1.tlh.json', json=data) apypie.Api(uri='https://api.example.com', apidoc_cache_dir=tmpdir.strpath, language='tlh_EN')
def test_init_bad_cachedir(tmpdir): bad_cachedir = tmpdir.join('bad') bad_cachedir.ensure(file=True) api = apypie.Api(uri='https://api.example.com', apidoc_cache_dir=bad_cachedir.strpath) with pytest.raises(OSError): api.apidoc
def test_init_auth(apidoc_cache_dir, username, password, expected): api = apypie.Api(uri='https://api.example.com', apidoc_cache_dir=apidoc_cache_dir.strpath, username=username, password=password) assert api._session.auth == expected
def test_init_language(apidoc_cache_dir): api = apypie.Api(uri='https://api.example.com', apidoc_cache_dir=apidoc_cache_dir.strpath, language='tlh') assert api.language == 'tlh' assert api._session.headers['Accept-Language'] == 'tlh'
def foreman_api(fixture_dir, requests_mock, tmpdir): with fixture_dir.join('foreman.json').open() as read_file: data = json.load(read_file) requests_mock.get('https://foreman.example.com/apidoc/v2.json', json=data) return apypie.Api(uri='https://foreman.example.com', api_version=2, apidoc_cache_dir=tmpdir.strpath)
def test_init_bad_response(requests_mock, tmpdir): requests_mock.get('https://api.example.com/apidoc/v1.json', status_code=404) with pytest.raises(apypie.exceptions.DocLoadingError) as excinfo: apypie.Api(uri='https://api.example.com', apidoc_cache_dir=tmpdir.strpath) assert "Could not load data from https://api.example.com" in str( excinfo.value)
def api(user, base_url): return apypie.Api( uri=base_url, username=user.username, password=user.password, api_version=2, verify_ssl=False, )
def test_init_with_xdg_cachedir(fixture_dir, requests_mock, tmp_xdg_cache_home): with fixture_dir.join('dummy.json').open() as read_file: data = json.load(read_file) requests_mock.get('https://api.example.com/apidoc/v1.json', json=data) api = apypie.Api(uri='https://api.example.com') assert api.apidoc assert tmp_xdg_cache_home.join('apypie', 'https___api.example.com', 'v1', 'default.json').check(file=1)
def api(fixture_dir, requests_mock, tmpdir): with fixture_dir.join('dummy.json').open() as read_file: data = json.load(read_file) requests_mock.get('https://api.example.com/apidoc/v1.json', json=data) api = apypie.Api(uri='https://api.example.com', apidoc_cache_dir=tmpdir.strpath) # explicitly trigger loading of the apidoc # our tests often mock Api.http_call, which breaks apidoc loading api.apidoc return api
def test_init_with_xdg_cachedir(fixture_dir, requests_mock, tmpdir): with fixture_dir.join('dummy.json').open() as read_file: data = json.load(read_file) requests_mock.get('https://api.example.com/apidoc/v1.json', json=data) old_environ = os.environ.copy() os.environ['XDG_CACHE_HOME'] = tmpdir.strpath apypie.Api(uri='https://api.example.com') os.environ = old_environ assert tmpdir.join('apypie/https___api.example.com/v1/default.json').check( file=1)
def connect(self, ping=True): self.foremanapi = apypie.Api( uri=self._foremanapi_server_url, username=self._foremanapi_username, password=self._foremanapi_password, api_version=2, verify_ssl=self._foremanapi_validate_certs, ) if ping: self.ping()
def connect(self): self.foremanapi = apypie.Api( uri=self._foremanapi_server_url, username=self._foremanapi_username, password=self._foremanapi_password, api_version=2, verify_ssl=self._foremanapi_validate_certs, ) self.ping() self._patch_location_api() self._patch_subnet_rex_api()
def test_http_call_get_headers_lang(apidoc_cache_dir, requests_mock): headers = {'X-Apypie-Test': 'Awesome'} expected_headers = { 'Accept': 'application/json;version=1', 'Accept-Language': 'tlh', } expected_headers.update(headers) requests_mock.get('https://api.example.com/', request_headers=expected_headers, text='{}') api = apypie.Api(uri='https://api.example.com', apidoc_cache_dir=apidoc_cache_dir.strpath, language='tlh') api.http_call('get', '/', headers=headers)
def test_validate_cache_path_traversal(fixture_dir, requests_mock, tmp_xdg_cache_home): with fixture_dir.join('dummy.json').open() as read_file: data = json.load(read_file) requests_mock.get('https://api.example.com/apidoc/v1.json', json=data) api = apypie.Api(uri='https://api.example.com') assert api.apidoc assert tmp_xdg_cache_home.join('apypie', 'https___api.example.com', 'v1', 'default.json').check(file=1) api.validate_cache('../help/testcache') assert tmp_xdg_cache_home.join('apypie', 'https___api.example.com', 'v1', 'default.json').check(exists=0) assert api.apidoc assert tmp_xdg_cache_home.join('apypie', 'https___api.example.com', 'v1', 'testcache.json').check(file=1)
def test_http_call_get_headers_auth(apidoc_cache_dir, requests_mock): headers = {'X-Apypie-Test': 'Awesome'} expected_headers = { 'Accept': 'application/json;version=1', 'Authorization': 'Basic dXNlcjpwYXNz', } expected_headers.update(headers) requests_mock.get('https://api.example.com/', request_headers=expected_headers, text='{}') api = apypie.Api(uri='https://api.example.com', apidoc_cache_dir=apidoc_cache_dir.strpath, username='******', password='******') api.http_call('get', '/', headers=headers)
def test_init_with_bad_cache(fixture_dir, requests_mock, tmp_xdg_cache_home): with fixture_dir.join('dummy.json').open() as read_file: data = json.load(read_file) requests_mock.get('https://api.example.com/apidoc/v1.json', json=data) json_path = tmp_xdg_cache_home.join('apypie', 'https___api.example.com', 'v1', 'deadc0ffee.json') json_path.ensure(file=True) json_path.write('BAD JSON') api = apypie.Api(uri='https://api.example.com') api.apidoc assert json_path.check(file=1) assert tmp_xdg_cache_home.join('apypie', 'https___api.example.com', 'v1', 'default.json').check(exists=0)
def test_validate_cache_path_traversal(fixture_dir, requests_mock, tmpdir): with fixture_dir.join('dummy.json').open() as read_file: data = json.load(read_file) requests_mock.get('https://api.example.com/apidoc/v1.json', json=data) old_environ = os.environ.copy() os.environ['XDG_CACHE_HOME'] = tmpdir.strpath api = apypie.Api(uri='https://api.example.com') os.environ = old_environ assert api.apidoc assert tmpdir.join('apypie/https___api.example.com/v1/default.json').check( file=1) api.validate_cache('../help/testcache') assert tmpdir.join('apypie/https___api.example.com/v1/default.json').check( exists=0) assert api.apidoc assert tmpdir.join( 'apypie/https___api.example.com/v1/testcache.json').check(file=1)
def test_http_call_get_headers_cache(fixture_dir, requests_mock, tmp_xdg_cache_home): response_headers = {'Apipie-Checksum': 'c0ffeec0ffee'} with fixture_dir.join('dummy.json').open() as read_file: data = json.load(read_file) requests_mock.get('https://api.example.com/apidoc/v1.json', headers=response_headers, json=data) api = apypie.Api(uri='https://api.example.com') api.apidoc print(api.apidoc_cache_name) assert tmp_xdg_cache_home.join('apypie', 'https___api.example.com', 'v1', 'default.json').check(exists=0) assert tmp_xdg_cache_home.join('apypie', 'https___api.example.com', 'v1', 'c0ffeec0ffee.json').check(file=1)
def test_http_call_get_headers_cache(fixture_dir, requests_mock, tmpdir): response_headers = {'Apipie-Checksum': 'c0ffeec0ffee'} with fixture_dir.join('dummy.json').open() as read_file: data = json.load(read_file) requests_mock.get('https://api.example.com/apidoc/v1.json', headers=response_headers, json=data) old_environ = os.environ.copy() os.environ['XDG_CACHE_HOME'] = tmpdir.strpath api = apypie.Api(uri='https://api.example.com') api.apidoc print(api.apidoc_cache_name) os.environ = old_environ assert tmpdir.join('apypie/https___api.example.com/v1/default.json').check( exists=0) assert tmpdir.join( 'apypie/https___api.example.com/v1/c0ffeec0ffee.json').check(file=1)
def api(fixture_dir, requests_mock, tmpdir): with fixture_dir.join('dummy.json').open() as read_file: data = json.load(read_file) requests_mock.get('https://api.example.com/apidoc/v1.json', json=data) return apypie.Api(uri='https://api.example.com', apidoc_cache_dir=tmpdir.strpath)