Repository : http://git.fedorahosted.org/cgit/copr.git
On branch : master
commit 919e6e78fe7fac339748456378f1ad0c96831a25 Author: Valentin Gologuzov vgologuz@redhat.com Date: Mon Nov 24 15:34:29 2014 +0100
[frontend] fixed paramater validation for API hanlde `create_new_copr`
frontend/coprs_frontend/coprs/forms.py | 5 +- frontend/coprs_frontend/coprs/logic/users_logic.py | 10 ++ .../coprs/views/api_ns/api_general.py | 5 +- frontend/coprs_frontend/coprs/views/misc.py | 10 +- frontend/coprs_frontend/tests/test_helpers.py | 1 - .../test_views/test_api_ns/test_api_general.py | 100 ++++++++++++++++++++ frontend/run_tests.sh | 6 + 7 files changed, 129 insertions(+), 8 deletions(-)
diff --git a/frontend/coprs_frontend/coprs/forms.py b/frontend/coprs_frontend/coprs/forms.py index 3a69f1f..538ff92 100644 --- a/frontend/coprs_frontend/coprs/forms.py +++ b/frontend/coprs_frontend/coprs/forms.py @@ -125,7 +125,10 @@ class CoprFormFactory(object): validators=[UrlListValidator()], filters=[StringListFilter()])
- auto_createrepo = wtforms.BooleanField(default=True) + # FIXME: false_values used for API, json API shouldn't not be + # validated in the same way as plain POST requests + auto_createrepo = wtforms.BooleanField( + default=True, false_values=["", "false", False])
@property def selected_chroots(self): diff --git a/frontend/coprs_frontend/coprs/logic/users_logic.py b/frontend/coprs_frontend/coprs/logic/users_logic.py index 4c2638b..3d494e8 100644 --- a/frontend/coprs_frontend/coprs/logic/users_logic.py +++ b/frontend/coprs_frontend/coprs/logic/users_logic.py @@ -1,9 +1,19 @@ from coprs import exceptions
+from coprs.models import User +
class UsersLogic(object):
@classmethod + def get(cls, username): + return User.query.filter(User.username == username) + + @classmethod + def get_by_api_login(cls, login): + return User.query.filter(User.api_login == login) + + @classmethod def raise_if_cant_update_copr(cls, user, copr, message): """ Raise InsufficientRightsException if given user cant update diff --git a/frontend/coprs_frontend/coprs/views/api_ns/api_general.py b/frontend/coprs_frontend/coprs/views/api_ns/api_general.py index 0ca9780..cae3495 100644 --- a/frontend/coprs_frontend/coprs/views/api_ns/api_general.py +++ b/frontend/coprs_frontend/coprs/views/api_ns/api_general.py @@ -80,6 +80,9 @@ def api_new_copr(username):
elif form.validate_on_submit(): infos = [] + auto_createrepo = form.auto_createrepo.data + if "auto_createrepo" not in flask.request.data: + auto_createrepo = True try: copr = coprs_logic.CoprsLogic.add( name=form.name.data.strip(), @@ -89,7 +92,7 @@ def api_new_copr(username): description=form.description.data, instructions=form.instructions.data, check_for_duplicates=True, - auto_createrepo=form.auto_createrepo.data + auto_createrepo=auto_createrepo ) infos.append("New project was successfully created.")
diff --git a/frontend/coprs_frontend/coprs/views/misc.py b/frontend/coprs_frontend/coprs/views/misc.py index f244b47..7d6138a 100644 --- a/frontend/coprs_frontend/coprs/views/misc.py +++ b/frontend/coprs_frontend/coprs/views/misc.py @@ -10,6 +10,7 @@ from coprs import db from coprs import helpers from coprs import models from coprs import oid +from coprs.logic.users_logic import UsersLogic
def fed_openidize_name(name): @@ -189,16 +190,15 @@ def api_login_required(f): @functools.wraps(f) def decorated_function(*args, **kwargs): token = None - username = None + apt_login = None if "Authorization" in flask.request.headers: base64string = flask.request.headers["Authorization"] base64string = base64string.split()[1].strip() userstring = base64.b64decode(base64string) - (username, token) = userstring.split(":") + (apt_login, token) = userstring.split(":") token_auth = False - if token and username: - user = models.User.query.filter( - models.User.api_login == username).first() + if token and apt_login: + user = UsersLogic.get_by_api_login(apt_login).first() if (user and user.api_token == token and user.api_token_expiration >= datetime.date.today()):
diff --git a/frontend/coprs_frontend/tests/test_helpers.py b/frontend/coprs_frontend/tests/test_helpers.py index 4e41748..65ffe9e 100644 --- a/frontend/coprs_frontend/tests/test_helpers.py +++ b/frontend/coprs_frontend/tests/test_helpers.py @@ -80,7 +80,6 @@ class TestHelpers(CoprsTestCase): https_url = "https://example.com/repo"
orig = app.config["ENFORCE_PROTOCOL_FOR_BACKEND_URL"] - print("Orig: {}".format(orig)) try: app.config["ENFORCE_PROTOCOL_FOR_BACKEND_URL"] = "https" assert fix_protocol_for_backend(https_url) == https_url diff --git a/frontend/coprs_frontend/tests/test_views/test_api_ns/test_api_general.py b/frontend/coprs_frontend/tests/test_views/test_api_ns/test_api_general.py new file mode 100644 index 0000000..1829acc --- /dev/null +++ b/frontend/coprs_frontend/tests/test_views/test_api_ns/test_api_general.py @@ -0,0 +1,100 @@ +import base64 +import json + +import pytest +import sqlalchemy + +from coprs.logic.users_logic import UsersLogic +from coprs.logic.coprs_logic import CoprsLogic +from tests.coprs_test_case import CoprsTestCase, TransactionDecorator + + +class TestCreateCopr(CoprsTestCase): + copr_name = "copr_api_1" + instructions = "1234" + description = "567890" + repos = "http://example.com/repo" # TODO: better response on on http:// entries + initial_pkgs = "http://example.com/pkg" + + def post_api_with_auth(self, url, content): + userstring = "{}:{}".format(self.u1.api_login, self.u1.api_token) + base64string_user = base64.b64encode(userstring) + base64string = "Basic " + base64string_user + + return self.tc.post( + url, + content_type="application/json", + data=content, + headers={ + "Authorization": base64string + } + ) + + @TransactionDecorator("u1") + def test_api_create_copr_ok_minimal(self, f_users, f_mock_chroots, f_db): + self.db.session.add_all([self.u1, self.mc1]) + self.tc.post("/api/new/") + + content = { + "name": self.copr_name, + self.mc1.name: "y", + # "repos": repos, + # "initial_pkgs": initial_pkgs, + # "description": description, + # "instructions": instructions + } + content_encoded = json.dumps(content) + + with pytest.raises(sqlalchemy.orm.exc.NoResultFound): + CoprsLogic.get(None, self.u1.name, self.copr_name).one() + + r = self.post_api_with_auth( + "/api/coprs/{}/new/".format(self.u1.name), + content_encoded + ) + response = json.loads(r.data) + assert "New project was successfully created" in response["message"] + + copr = self.models.Copr.query.filter(self.models.Copr.name == self.copr_name).one() + assert copr.name == self.copr_name + assert [self.mc1.name] == [c.name for c in copr.active_chroots] + assert copr.repos == '' + assert copr.owner.id == self.u1.id + assert copr.auto_createrepo + + + @TransactionDecorator("u1") + def test_api_create_copr_ok_all(self, f_users, f_mock_chroots, f_db): + self.db.session.add_all([self.u1, self.mc1]) + self.tc.post("/api/new/") + + content = { + "name": self.copr_name, + self.mc1.name: "y", + "repos": self.repos, + "initial_pkgs": self.initial_pkgs, + "description": self.description, + "instructions": self.instructions, + "auto_createrepo": False + } + content_encoded = json.dumps(content) + + with pytest.raises(sqlalchemy.orm.exc.NoResultFound): + CoprsLogic.get(None, self.u1.name, self.copr_name).one() + + r = self.post_api_with_auth( + "/api/coprs/{}/new/".format(self.u1.name), + content_encoded + ) + response = json.loads(r.data) + assert "New project was successfully created" in response["message"] + + copr = self.models.Copr.query.filter(self.models.Copr.name == self.copr_name).one() + assert copr.name == self.copr_name + assert [self.mc1.name] == [c.name for c in copr.active_chroots] + assert copr.repos == self.repos + assert copr.owner.id == self.u1.id + assert copr.description == self.description + assert copr.instructions == self.instructions + + assert not copr.auto_createrepo diff --git a/frontend/run_tests.sh b/frontend/run_tests.sh new file mode 100755 index 0000000..41e390e --- /dev/null +++ b/frontend/run_tests.sh @@ -0,0 +1,6 @@ +#!/bin/sh + +cd coprs_frontend +COPR_CONFIG="$(pwd)/config/copr_unit_test.conf" python -m pytest tests \ + --cov-report term-missing --cov coprs $@ +
copr-commits@lists.fedorahosted.org