import os from . import test_dir from obsapi.sourceapi import ObsSourceApi prj = 'home:sbahling:obsapi:test' pkg = 'suse-hello-1.0' repo = 'SLE_12_SP3' arch = 'x86_64' sourceapi = ObsSourceApi(apiurl='https://api.opensuse.org') class RequestMatcher(): def __init__(self): self.put_data = None def match_put_data(self, request): return request.text == self.put_data def test_source_put_meta(requests_mock): content = b'' uri = '{}/source/{}/_meta'.format(sourceapi.apiurl, prj) with open(os.path.join(test_dir, 'obsapi_test_prj_meta.xml')) as f: xml = f.read().replace('__PRJ__', prj) matcher = RequestMatcher() matcher.put_data = xml requests_mock.put(uri, content=content, additional_matcher=matcher.match_put_data) response = sourceapi.put_meta(prj, meta=xml) assert response.content == content # GET /source/ # Result: directory listing as xml def test_source_get(requests_mock): uri = '{}/source/'.format(sourceapi.apiurl) text = '' requests_mock.get(uri, text=text) xml = sourceapi.get() assert xml == text # GET /source/ # Result: directory listing as xml def test_source_get_prj(requests_mock): uri = '{}/source/{}'.format(sourceapi.apiurl, prj) text = '' requests_mock.get(uri, text=text) xml = sourceapi.get(prj) assert xml == text # GET /source// # Result: directory listing as xml def test_source_get_pkg(requests_mock): uri = '{}/source/{}/{}'.format(sourceapi.apiurl, prj, pkg) text = '' requests_mock.get(uri, text=text) xml = sourceapi.get(prj, pkg) assert xml == text # GET /source// # Result: contents of a source file def test_source_get_file(requests_mock): fname = 'suse-hello-kmp-default.rpm' uri = '{}/source/{}/{}/{}'.format(sourceapi.apiurl, prj, pkg, fname) with open(os.path.join(test_dir, fname), 'rb') as f: content = f.read() requests_mock.get(uri, content=content) source_file = sourceapi.get(prj, pkg, fname) assert source_file == content # GET /source//_meta # Result: contents of the project meta file def test_source_get_meta_prj(requests_mock): uri = '{}/source/{}/_meta'.format(sourceapi.apiurl, prj) text = 'Project Meta File' requests_mock.get(uri, text=text) xml = sourceapi.get_meta(prj) assert xml == text # GET /source///_meta # Result: contents of the package meta file def test_source_get_meta_pkg(requests_mock): uri = '{}/source/{}/{}/_meta'.format(sourceapi.apiurl, prj, pkg) text = 'Package Meta File' requests_mock.get(uri, text=text) xml = sourceapi.get_meta(prj, pkg) assert xml == text # GET /source//_attribute # Result: list of attributes as xml def test_source_get_prj_attributes(requests_mock): uri = '{}/source/{}/_attribute'.format(sourceapi.apiurl, prj) text = '' requests_mock.get(uri, text=text) xml = sourceapi.get_attribute(prj) assert xml == text # GET /source//_attribute/ # Result: return single attribute in attributes xml container def test_source_get_prj_attribute(requests_mock): attribute = 'OBS:Screenshots' uri = '{}/source/{}/_attribute/{}'.format(sourceapi.apiurl, prj, attribute) text = '' requests_mock.get(uri, text=text) xml = sourceapi.get_attribute(prj, attribute=attribute) assert xml == text # GET /source///_attribute # Result: return package level attributes as xml def test_source_get_pkg_attributes(requests_mock): uri = '{}/source/{}/{}/_attribute'.format(sourceapi.apiurl, prj, pkg) text = '' requests_mock.get(uri, text=text) xml = sourceapi.get_attribute(prj, pkg) assert xml == text # GET /source//_attribute # Result: return project config file as text def test_source_get_prj_config(requests_mock): uri = '{}/source/{}/_config'.format(sourceapi.apiurl, prj) text = 'Project Config' requests_mock.get(uri, text=text) config = sourceapi.get_config(prj) assert config == text # GET /source//_pubkey # Result: return project pub key as text def test_source_get_prj_pubkey(requests_mock): uri = '{}/source/{}/_pubkey'.format(sourceapi.apiurl, prj) text = 'PubKey Contents' requests_mock.get(uri, text=text) pubkey = sourceapi.get_pubkey(prj) assert pubkey == text # GET /source///_history # Result: return package commit history as xml def test_source_get_history(requests_mock): uri = '{}/source/{}/{}/_history'.format(sourceapi.apiurl, prj, pkg) text = '' requests_mock.get(uri, text=text) xml = sourceapi.get_history(prj, pkg) assert xml == text