Coverage for appr/tests/test_apiserver.py: 24%

# Coverage for appr/tests/test_apiserver.py : 24%

## 349 statements 83 run 266 missing 0 excluded

Hot-keys on this page

r m x p toggle line displays

j k next/prev highlighted chunk

0 (zero) top of page

1 (one) first highlighted chunk

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 | from __future__ import absolute_import, division, print_function import json import os import urllib import pytest import requests from flask import request import appr from appr.client import DEFAULT_PREFIX @pytest.mark.api @pytest.mark.integration class TestServer: from appr.models.kv.filesystem.db import ApprDB DB_CLASS = ApprDB @pytest.fixture() def db(self): return self.DB_CLASS @property def token(self): return None def headers(self): d = {'Content-Type': 'application/json'} if self.token: d['Authorization'] = self.token return d class Client(object): def __init__(self, client, headers=None): self.client = client self.headers = headers def _request(self, method, path, params, body): if params: path = path + "?" + urllib.urlencode(params) return getattr(self.client, method)(path, data=json.dumps(body), headers=self.headers) def get(self, path, params=None, body=None): return self._request('get', path, params, body) def delete(self, path, params=None, body=None): return self._request('delete', path, params, body) def post(self, path, params=None, body=None): return self._request('post', path, params, body) def json(self, res): return res.json def content(self, res): return res.data def _url_for(self, path): return DEFAULT_PREFIX + "/" + self.api_prefix + path @property def api_prefix(self): return os.getenv("APPR_API_PREFIX", "") def test_version(self, client): url = self._url_for("version") res = self.Client(client, self.headers()).get(url) assert res.status_code == 200 assert self.json(res) == {"appr-api": appr.__version__} def test_search_package_match(self, db_with_data1, client): url = self._url_for("api/v1/packages/search") res = self.Client(client, self.headers()).get(url, params={'q': 'rocketchat'}) assert res.status_code == 200 assert self.json(res) == ['titi/rocketchat'] def test_search_package_no_match(self, db_with_data1, client): url = self._url_for("api/v1/packages/search") res = self.Client(client, self.headers()).get(url, params={'q': 'toto'}) assert res.status_code == 200 assert self.json(res) == [] def test_list_search_package_match(self, db_with_data1, client): url = self._url_for("api/v1/packages") res = self.Client(client, self.headers()).get(url, params={'query': 'rocketchat'}) assert res.status_code == 200 assert len(self.json(res)) == 1 assert self.json(res)[0]['name'] == 'titi/rocketchat' def test_list_search_package_no_match(self, db_with_data1, client): url = self._url_for("api/v1/packages") res = self.Client(client, self.headers()).get(url, params={'query': 'toto'}) assert res.status_code == 200 assert self.json(res) == [] # @TODO check content def test_pull_package(self, db_with_data1, client): package = "titi/rocketchat" release = "1.0.1" media_type = "kpm" url = self._url_for("api/v1/packages/%s/%s/%s/pull" % (package, release, media_type)) res = self.Client(client, self.headers()).get(url) assert res.status_code == 200 def test_pull_package_no_release(self, db_with_data1, client): package = "titi/rocketchat" release = "1.0.3" media_type = "kpm" url = self._url_for("api/v1/packages/%s/%s/%s/pull" % (package, release, media_type)) res = self.Client(client, self.headers()).get(url) assert res.status_code == 404 def test_pull_package_default_media_type(self, db_with_data1, client): package = "titi/rocketchat" release = "1.0.1" media_type = "-" url = self._url_for("api/v1/packages/%s/%s/%s/pull" % (package, release, media_type)) res = self.Client(client, self.headers()).get(url, params={'format': 'json'}) assert res.status_code == 200 assert self.json(res)['media_type'] == "kpm" def test_pull_package_bad_release(self, db_with_data1, client): package = "titi/rocketchat" release = "abc" media_type = "kpm" url = self._url_for("api/v1/packages/%s/%s/%s/pull" % (package, release, media_type)) res = self.Client(client, self.headers()).get(url) assert res.status_code == 422 def test_pull_package_json(self, db_with_data1, client): package = "titi/rocketchat" release = "1.0.1" media_type = "kpm" url = self._url_for("api/v1/packages/%s/%s/%s/pull" % (package, release, media_type)) res = self.Client(client, self.headers()).get(url, params={'format': 'json'}) assert res.status_code == 200 p = db_with_data1.Package.get(package, '1.0.1', 'kpm') blob = db_with_data1.Blob.get(p.package, p.digest) assert self.json(res)['blob'] == blob.b64blob def test_push_package(self, newdb, package_b64blob, client): package = "titi/rocketchat" url = self._url_for("api/v1/packages/%s" % package) res = self.Client(client, self.headers()).post(url, body={ 'release': '2.4.1', 'media_type': 'kpm', 'blob': package_b64blob}) assert res.status_code == 200 p = newdb.Package.get(package, '2.4.1', 'kpm') blob = newdb.Blob.get(p.package, p.digest) assert blob.b64blob == package_b64blob def test_push_package_bad_release(self, newdb, package_b64blob, client): package = "titi/rocketchat" url = self._url_for("api/v1/packages/%s" % package) res = self.Client(client, self.headers()).post(url, body={ 'package': package, 'release': 'anc', 'media_type': 'kpm', 'blob': package_b64blob}) assert res.status_code == 422 def test_push_package_already_exists(self, db_with_data1, package_b64blob, client): package = "titi/rocketchat" url = self._url_for("api/v1/packages/%s" % package) res = self.Client(client, self.headers()).post(url, body={ 'package': package, 'release': '1.0.1', 'media_type': 'kpm', 'blob': package_b64blob}) assert res.status_code == 409 def test_push_package_already_exists_force(self, db_with_data1, package_b64blob, client): package = "titi/rocketchat" url = self._url_for("api/v1/packages/%s" % package) res = self.Client(client, self.headers()).post(url, body={ 'package': package, 'release': '1.0.1', 'force': 'true', 'media_type': 'kpm', 'blob': package_b64blob}) assert res.status_code == 200 def test_get_blob(self, db_with_data1, client): package = "titi/rocketchat" p = db_with_data1.Package.get(package, "default", "kpm") blob = db_with_data1.Blob.get(package, p.digest) url = self._url_for("api/v1/packages/%s/blobs/sha256/%s" % (package, p.digest)) res = self.Client(client, self.headers()).get(url) assert res.status_code == 200 assert self.content(res) == blob.blob def test_get_absent_blob(self, newdb, client): package = "a/b" digest = "12345" url = self._url_for("api/v1/packages/%s/blobs/sha256/%s" % (package, digest)) res = self.Client(client, self.headers()).get(url) assert res.status_code == 404 def test_list_packages(self, db_with_data1, client): url = self._url_for("api/v1/packages/") res = self.Client(client, self.headers()).get(url) assert sorted(self.json(res)) == sorted(db_with_data1.Package.all()) def test_list_packages_filter_namespace(self, db_with_data1, client): url = self._url_for("api/v1/packages/?namespace=ant31") res = self.Client(client, self.headers()).get(url) assert sorted(self.json(res)) == sorted(db_with_data1.Package.all('ant31')) url = self._url_for("api/v1/packages/?namespace=titi") res = self.Client(client, self.headers()).get(url) assert sorted(self.json(res)) == sorted(db_with_data1.Package.all('titi')) def test_list_empty(self, newdb, client): url = self._url_for("api/v1/packages") res = self.Client(client, self.headers()).get(url) assert self.json(res) == [] def test_delete_package(self, db_with_data1, client): package = "titi/rocketchat" release = "1.0.1" media_type = "kpm" url = self._url_for("api/v1/packages/%s/%s/%s" % (package, release, media_type)) res = self.Client(client, self.headers()).get(url) assert res.status_code == 200 res = self.Client(client, self.headers()).delete(url) assert res.status_code == 200 res = self.Client(client, self.headers()).get(url) assert res.status_code == 404 def test_delete_absent_package(self, newdb, client): package = "titi/rocketchat" release = "1.0.1" media_type = "kpm" url = self._url_for("api/v1/packages/%s/%s/%s" % (package, release, media_type)) res = self.Client(client, self.headers()).delete(url) assert res.status_code == 404 def test_show_package(self, db_with_data1, client): package = "titi/rocketchat" release = "1.0.1" media_type = "kpm" url = self._url_for("api/v1/packages/%s/%s/%s" % (package, release, media_type)) res = self.Client(client, self.headers()).get(url) assert res.status_code == 200 p = db_with_data1.Package.get(package, "1.0.1", "kpm") assert self.json(res)['content']['digest'] == p.digest def test_show_package_releases(self, db_with_data1, client): package = "titi/rocketchat" url = self._url_for("api/v1/packages/%s" % (package)) res = self.Client(client, self.headers()).get(url) assert res.status_code == 200 assert len(self.json(res)) == 4 def test_show_package_manifests(self, db_with_data1, client): package = "titi/rocketchat" release = "1.0.1" url = self._url_for("api/v1/packages/%s/%s" % (package, release)) res = self.Client(client, self.headers()).get(url) assert res.status_code == 200 p = db_with_data1.Package.get(package, "1.0.1", "kpm") assert len(self.json(res)) == 1 assert self.json(res)[0]['content']['digest'] == p.digest def test_show_package_absent(self, newdb, client): package = "titi/rocketchat" release = "1.0.1" media_type = "kpm" url = self._url_for("api/v1/packages/%s/%s/%s" % (package, release, media_type)) res = self.Client(client, self.headers()).get(url) assert res.status_code == 404 def test_show_package_bad_release(self, db_with_data1, client): package = "titi/rocketchat" release = "abc" media_type = "kpm" url = self._url_for("api/v1/packages/%s/%s/%s" % (package, release, media_type)) res = self.Client(client, self.headers()).get(url) assert res.status_code == 422 def test_show_package_media_type(self, db_with_data1, client): package = "titi/rocketchat" package = "titi/rocketchat" release = "0.0.1" media_type = "helm" url = self._url_for("api/v1/packages/%s/%s/%s" % (package, release, media_type)) res = self.Client(client, self.headers()).get(url) assert res.status_code == 200 p = db_with_data1.Package.get(package, "0.0.1", "helm") assert self.json(res)['content']['digest'] == p.digest def test_show_channel(self, db_with_data1, client): package = "titi/rocketchat" channel = 'stable' url = self._url_for("api/v1/packages/%s/channels/%s" % (package, channel)) res = self.Client(client, self.headers()).get(url) assert res.status_code == 200 assert self.json(res) == db_with_data1.Channel.get(channel, package).to_dict() def test_show_channel_absent_package(self, newdb, client): package = "titi/rocketchat" channel = 'no' url = self._url_for("api/v1/packages/%s/channels/%s" % (package, channel)) res = self.Client(client, self.headers()).get(url) assert res.status_code == 404 def test_list_channels(self, db_with_data1, client): package = "titi/rocketchat" url = self._url_for("api/v1/packages/%s/channels" % (package)) res = self.Client(client, self.headers()).get(url) assert res.status_code == 200 assert sorted(self.json(res)) == sorted([ c.to_dict() for c in db_with_data1.Channel.all(package)]) def test_list_channels_404(self, newdb, client): package = "titi/no" url = self._url_for("api/v1/packages/%s/channels" % (package)) res = self.Client(client, self.headers()).get(url) assert res.status_code == 404 def test_add_channel_release(self, db_with_data1, client): package = "titi/rocketchat" channel = 'default' release = '1.0.1' url = self._url_for("api/v1/packages/%s/channels/%s" % (package, channel)) res = self.Client(client, self.headers()).get(url) assert res.status_code == 404 url = self._url_for("api/v1/packages/%s/channels/%s/%s" % (package, channel, release)) res = self.Client(client, self.headers()).post(url) assert res.status_code == 200 assert self.json(res)['releases'] == [release] def test_add_channel_release_new_chan(self, db_with_data1, client): package = "titi/rocketchat" channel = 'newchan' release = '1.0.1' chanurl = self._url_for("api/v1/packages/%s/channels/%s" % (package, channel)) res = self.Client(client, self.headers()).get(chanurl) assert res.status_code == 404 url = self._url_for("api/v1/packages/%s/channels/%s/%s" % (package, channel, release)) res = self.Client(client, self.headers()).post(url) assert res.status_code == 200 chanurl = self._url_for("api/v1/packages/%s/channels/%s" % (package, channel)) res = self.Client(client, self.headers()).get(chanurl) assert res.status_code == 200 assert self.json(res)['releases'] == [release] def test_add_channel_release_absent_release(self, db_with_data1, client): package = "titi/rocketchat" channel = 'default' release = '1.0.2' url = self._url_for("api/v1/packages/%s/channels/%s/%s" % (package, channel, release)) res = self.Client(client, self.headers()).post(url) assert res.status_code == 404 def test_delete_channel_release(self, db_with_data1, client): package = "titi/rocketchat" channel = 'stable' release = '1.0.1' url = self._url_for("api/v1/packages/%s/channels/%s" % (package, channel)) res = self.Client(client, self.headers()).get(url) assert res.status_code == 200 assert release in self.json(res)['releases'] url = self._url_for("api/v1/packages/%s/channels/%s/%s" % (package, channel, release)) res = self.Client(client, self.headers()).delete(url) assert res.status_code == 200 def test_delete_channel_release_absent_release(self, db_with_data1, client): package = "titi/rocketchat" channel = 'stable' release = '1.0.2' url = self._url_for("api/v1/packages/%s/channels/%s" % (package, channel)) res = self.Client(client, self.headers()).get(url) assert res.status_code == 200 assert release not in self.json(res)['releases'] url = self._url_for("api/v1/packages/%s/channels/%s/%s" % (package, channel, release)) res = self.Client(client, self.headers()).delete(url) assert res.status_code == 404 def test_delete_channel(self, db_with_data1, client): package = "titi/rocketchat" channel = 'dev' p = db_with_data1.Package.get(package, "1.0.1", "kpm") assert channel in p.channels(db_with_data1.Channel) chanurl = self._url_for("api/v1/packages/%s/channels/%s" % (package, channel)) res = self.Client(client, self.headers()).delete(chanurl) assert res.status_code == 200 p = db_with_data1.Package.get(package, "1.0.1", "kpm") assert channel not in p.channels(db_with_data1.Channel) def test_delete_absent_channel(self, db_with_data1, client): package = "titi/rocketchat" channel = 'no' chanurl = self._url_for("api/v1/packages/%s/channels/%s" % (package, channel)) res = self.Client(client, self.headers()).delete(chanurl) assert res.status_code == 404 BaseTestServer = TestServer @pytest.mark.usefixtures('live_server') class LiveTestServer(BaseTestServer): class Client(object): def __init__(self, client, headers): self.client = requests self.headers = headers def _request(self, method, path, params, body): return getattr(self.client, method)(path, params=params, data=json.dumps(body), headers=self.headers) def get(self, path, params=None, body=None): return self._request('get', path, params, body) def delete(self, path, params=None, body=None): return self._request('delete', path, params, body) def post(self, path, params=None, body=None): return self._request('post', path, params, body) def content(self, res): return res.content def _url_for(self, path): return request.url_root + self.api_prefix + path @pytest.fixture(autouse=True) def client(self, client): return requests def json(self, res): return res.json() def get_server_class(): if os.getenv("APPR_TEST_LIVESERVER", "false") == 'true': return LiveTestServer else: return BaseTestServer ServerTest = get_server_class()