Skip to content

Commit

Permalink
fix: strip values on insertion (#167)
Browse files Browse the repository at this point in the history
fixes: #156

migration was manually tested over a production database import
  • Loading branch information
alexgarel authored Mar 7, 2024
1 parent c0ee55b commit fbc1194
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 0 deletions.
4 changes: 4 additions & 0 deletions db/migrations/002-remove-trailing-spaces-in-values.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
-- Remove trailing spaces in values
-- depends: 000-init-db

UPDATE folksonomy SET v = TRIM(v), version=version+1 WHERE v LIKE '% ' OR v LIKE ' %';
1 change: 1 addition & 0 deletions folksonomy/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ async def get_current_user(token: str = Depends(oauth2_scheme)):
def sanitize_data(k, v):
"""Some sanitization of data"""
k = k.strip()
v = v.strip() if v else v
return k, v


Expand Down
2 changes: 2 additions & 0 deletions folksonomy/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ def key_check(cls, v):
def value_check(cls, v):
if not v:
raise ValueError('v cannot be empty')
# strip values
v = v.strip()
return v

@field_validator('version')
Expand Down
11 changes: 11 additions & 0 deletions tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -514,6 +514,17 @@ async def test_product_key_stripped_on_post(auth_tokens):
assert response.status_code == 200, f'getting stripped key should return 200, got {response.status_code} {response.text}'


@pytest.mark.asyncio
async def test_product_value_stripped_on_post(auth_tokens):
with TestClient(app) as client:
headers = {"Authorization": "Bearer foo__Utest-token"}
response = client.post("/product", headers=headers, json=
{"product": BARCODE_1, "version": 1, "k": "test_new", "v": " a test "})
assert response.status_code == 200, f'valid new entry should return 200, got {response.status_code} {response.text}'
# check created stripped
await check_tag(BARCODE_1, "test_new", v="a test", version=1)


def test_put_invalid(with_sample):
with TestClient(app) as client:
headers = {"Authorization": "Bearer foo__Utest-token"}
Expand Down

0 comments on commit fbc1194

Please sign in to comment.