Skip to content

Commit

Permalink
Merge pull request #31 from Hvv007/fields_validatian
Browse files Browse the repository at this point in the history
add name field validation and test for it
  • Loading branch information
artamaney authored Jan 13, 2024
2 parents 2885b35 + 3ba9d44 commit 14e1a16
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
5 changes: 5 additions & 0 deletions overhave/admin/views/testing_users.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from re import fullmatch
from typing import cast

from flask_admin.form import JSONField
Expand Down Expand Up @@ -50,6 +51,8 @@ class TestUserView(ModelViewConfigured):

_feature_type: FeatureTypeName | None = None

_allowed_symbols = r"[a-zA-Z_]*"

def on_form_prefill(self, form: Form, id) -> None: # type: ignore # noqa: A002
if not isinstance(form._obj, db.TestUser):
return
Expand Down Expand Up @@ -78,6 +81,8 @@ def _validate_json(model: db.TestUser) -> None:
raise ValidationError(f"Could not convert specified data into {parser.__name__} model!")

def on_model_change(self, form: Form, model: db.TestUser, is_created: bool) -> None:
if model.key is not None and not fullmatch(self._allowed_symbols, model.key):
raise ValidationError("Key should contain only characters, digits or underscore!")
self._feature_type = cast(FeatureTypeName, model.feature_type.name)
self._validate_json(model)
if is_created:
Expand Down
13 changes: 13 additions & 0 deletions tests/unit/admin/test_testing_users.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,19 @@ def test_incorrect_model_raises_error(
with pytest.raises(ValidationError):
test_testing_user_view.on_model_change(form=form_mock, model=db_test_user, is_created=test_is_created)

@pytest.mark.parametrize("user_role", [db.Role.user, db.Role.admin], indirect=True)
@pytest.mark.parametrize("test_is_created", [False, True])
def test_key_with_spaces_for_model_raises_errors(
self,
test_testing_user_view: TestUserView,
current_user_mock: mock.MagicMock,
form_mock: mock.MagicMock,
test_is_created: bool,
) -> None:
db_test_user = db.TestUser(key="kek kek")
with pytest.raises(ValidationError):
test_testing_user_view.on_model_change(form=form_mock, model=db_test_user, is_created=test_is_created)

@pytest.mark.parametrize("user_role", [db.Role.admin, db.Role.user], indirect=True)
def test_on_form_prefill_set_feature_type(
self,
Expand Down

0 comments on commit 14e1a16

Please sign in to comment.