diff options
| author | Scott Bahling <sbahling@suse.com> | 2020-01-28 18:42:04 +0100 |
|---|---|---|
| committer | Scott Bahling <sbahling@suse.com> | 2020-01-28 18:43:15 +0100 |
| commit | 4a59fed2f76cf1e02888e02d8066f0e0c22630d2 (patch) | |
| tree | 1c00daafbcbb4a540b3a66d4f74649cff24cf0b4 /test/test_obs_source_api.py | |
| parent | f2213c9523e96964a17430ca788c5fc880b96612 (diff) | |
| download | obsapi-4a59fed2f76cf1e02888e02d8066f0e0c22630d2.tar.gz obsapi-4a59fed2f76cf1e02888e02d8066f0e0c22630d2.tar.xz obsapi-4a59fed2f76cf1e02888e02d8066f0e0c22630d2.zip | |
Use requests_mock for unit tests
Diffstat (limited to 'test/test_obs_source_api.py')
| -rw-r--r-- | test/test_obs_source_api.py | 160 |
1 files changed, 118 insertions, 42 deletions
diff --git a/test/test_obs_source_api.py b/test/test_obs_source_api.py index 47df73f..abe47e1 100644 --- a/test/test_obs_source_api.py +++ b/test/test_obs_source_api.py @@ -1,5 +1,5 @@ -from obsapi.sourceapi import ObsSourceApi import os +from obsapi.sourceapi import ObsSourceApi prj = 'home:sbahling:obsapi:test' @@ -9,71 +9,147 @@ arch = 'x86_64' sourceapi = ObsSourceApi(apiurl='https://api.opensuse.org') -modpath = os.path.dirname(os.path.realpath(__file__)) +test_dir = os.path.dirname(os.path.realpath(__file__)) -def test_source_put_meta(): - prj = 'home:sbahling:obsapi:t1' - with open(os.path.join(modpath, 'obsapi_test_prj_meta.xml')) as f: - xml = f.read().replace('__PRJ__', prj) - response = sourceapi.put_meta(prj, xml) - assert '<status code="ok">' in str(response.content) +class RequestMatcher(): + def __init__(self): + self.put_data = None + def match_put_data(self, request): + return request.text == self.put_data -def test_source_get_prj(): - xml = sourceapi.get(prj) - assert '<entry name="suse-hello-1.0"/>' in xml +def test_source_put_meta(requests_mock): + content = b'<status code="ok"></status>' + 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, xml) + assert response.content == content -def test_source_get_pkg(): - xml = sourceapi.get(prj, pkg) - assert '<directory name="suse-hello-1.0"' in xml +# GET /source/ +# Result: directory listing as xml +def test_source_get(requests_mock): + uri = '{}/source/'.format(sourceapi.apiurl) + text = '<directory></directory>' + requests_mock.get(uri, text=text) + xml = sourceapi.get() + assert xml == text -def test_source_get_file(): - source_file = sourceapi.get(prj, pkg, 'COPYING') - assert 'GNU GENERAL PUBLIC LICENSE' in source_file + +# GET /source/<project> +# Result: directory listing as xml +def test_source_get_prj(requests_mock): + uri = '{}/source/{}'.format(sourceapi.apiurl, prj) + text = '<directory></directory>' + requests_mock.get(uri, text=text) + xml = sourceapi.get(prj) + assert xml == text -def test_source_get_meta_prj(): +# GET /source/<project>/<package> +# Result: directory listing as xml +def test_source_get_pkg(requests_mock): + uri = '{}/source/{}/{}'.format(sourceapi.apiurl, prj, pkg) + text = '<directory></directory>' + requests_mock.get(uri, text=text) + xml = sourceapi.get(prj, pkg) + assert xml == text + + +# GET /source/<project>/<package></filename> +# 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/<project>/_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 '<project name="{}">'.format(prj) in xml + assert xml == text -def test_source_get_meta_pkg(): +# GET /source/<project>/<package>/_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 '<package name="{}" project="{}">'.format(pkg, prj) in xml + assert xml == text -def test_source_get_prj_attributes(): +# GET /source/<project>/_attribute +# Result: list of attributes as xml +def test_source_get_prj_attributes(requests_mock): + uri = '{}/source/{}/_attribute'.format(sourceapi.apiurl, prj) + text = '<attributes></attributes>' + requests_mock.get(uri, text=text) xml = sourceapi.get_attribute(prj) - assert '<attributes>' in xml - - -def test_source_get_prj_attribute(): - xml = sourceapi.get_attribute(prj, attribute='OBS:Screenshots') - assert '<attributes>' in xml - assert 'name="Screenshots"' in xml - assert 'namespace="OBS"' in xml - - -def test_source_get_pkg_attributes(): + assert xml == text + + +# GET /source/<project>/_attribute/<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 = '<attributes></attributes>' + requests_mock.get(uri, text=text) + xml = sourceapi.get_attribute(prj, attribute=attribute) + assert xml == text + + +# GET /source/<project>/<package>/_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 = '<attributes></attributes>' + requests_mock.get(uri, text=text) xml = sourceapi.get_attribute(prj, pkg) - assert '<attributes>' in xml - assert 'name="Screenshots"' in xml - assert 'namespace="OBS"' in xml + assert xml == text -def test_source_get_prj_config(): +# GET /source/<project>/_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 'Macros:' in config + assert config == text -def test_source_get_prj_pubkey(): +# GET /source/<project>/_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 '-BEGIN PGP PUBLIC KEY BLOCK-' in pubkey + assert pubkey == text -def test_source_get_history(): +# GET /source/<project>/<package>/_history +# Result: return package commit history as xml +def test_source_get_history(requests_mock): + uri = '{}/source/{}/{}/_history'.format(sourceapi.apiurl, prj, pkg) + text = '<revisionlist></revisionlist>' + requests_mock.get(uri, text=text) xml = sourceapi.get_history(prj, pkg) - assert '<revisionlist>' in xml + assert xml == text |
