Skip to content

Commit 4693f23

Browse files
committed
Add further tests
Signed-off-by: DL6ER <[email protected]>
1 parent a5e10fa commit 4693f23

8 files changed

Lines changed: 98 additions & 9 deletions

File tree

app/labeldesigner/label.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -417,8 +417,7 @@ def _draw_text(self, img = None, bboxes = [], text_offset = (0, 0)):
417417
anchor = "rt"
418418
# else: error
419419
else:
420-
logger.error(f"Unsupported alignment: {align}")
421-
return
420+
raise ValueError(f"Unsupported alignment: {align}")
422421

423422
if do_draw and 'font_inverted' in line and line['font_inverted']:
424423
# Draw a filled rectangle

app/labeldesigner/routes.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,8 @@ def print_label():
126126
printer = create_printer_from_request(request)
127127
label = create_label_from_request(request)
128128
print_count = int(request.values.get('print_count', 1))
129+
if print_count < 1:
130+
raise ValueError("print_count must be greater than 0")
129131
cut_once = int(request.values.get('cut_once', 0)) == 1
130132
except Exception as e:
131133
return_dict['message'] = str(e)
File renamed without changes.

tests/extra_fields_ignored.png

751 Bytes
Loading

tests/test_labeldesigner_api.py

Lines changed: 95 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ def test_generate_preview_inverted(client):
143143
assert response.content_type in ['image/png']
144144

145145
# Check image
146-
verify_image(response.data, 'tests/inverted.png')
146+
verify_image(response.data, 'tests/inverted_text.png')
147147

148148

149149
def test_generate_preview_rotated(client):
@@ -198,7 +198,7 @@ def test_generate_ean13(client):
198198
assert response.content_type in ['image/png']
199199

200200
# Check image
201-
verify_image(response.data, 'tests/ean13.png')
201+
verify_image(response.data, 'tests/barcode_ean13.png')
202202

203203

204204
def test_invalid_ean13(client):
@@ -256,7 +256,7 @@ def test_generate_qr(client):
256256
verify_image(response.data, 'tests/qr.png')
257257

258258

259-
def image_test(client, image_path: str = "tests/demo_image.jpg", rotated: bool = False, fit: bool = False, text: bool = False, image_mode: str = "grayscale"):
259+
def image_test(client, image_path: str = "tests/_demo_image.jpg", rotated: bool = False, fit: bool = False, text: bool = False, image_mode: str = "grayscale"):
260260
data = EXAMPLE_FORMDATA.copy()
261261
my_file = FileStorage(
262262
stream=open(image_path, "rb"),
@@ -506,6 +506,7 @@ def test_corrupted_image_upload(client):
506506
assert 'message' in data
507507
assert data['message'] == 'Truncated File Read'
508508

509+
509510
def test_empty_label(client):
510511
data = EXAMPLE_FORMDATA.copy()
511512
data['text'] = json.dumps([])
@@ -597,10 +598,6 @@ def test_file_size_limits(client):
597598
assert response.status_code == 413
598599

599600

600-
# Even more edge-case and robustness tests
601-
import time
602-
from flask import Response
603-
604601
def test_extra_fields_ignored(client):
605602
data = EXAMPLE_FORMDATA.copy()
606603
data['text'] = json.dumps([
@@ -685,6 +682,97 @@ def test_missing_optional_fields(client):
685682
# Check image
686683
verify_image(response.data, 'tests/missing_optional_fields.png')
687684

685+
686+
# Further advanced test cases
687+
import unicodedata
688+
689+
def test_invalid_json_structure(client):
690+
data = EXAMPLE_FORMDATA.copy()
691+
# JSON string, but not a list
692+
data['text'] = json.dumps({'foo': 'bar'})
693+
response = client.post('/labeldesigner/api/preview', data=data)
694+
assert response.status_code == 400
695+
696+
697+
@pytest.mark.parametrize('size', [0, -1, -100])
698+
def test_negative_zero_font_size(client, size):
699+
data = EXAMPLE_FORMDATA.copy()
700+
data['text'] = json.dumps([
701+
{'font_family': 'DejaVu Sans', 'font_style': 'Regular', 'text': 'Bad size', 'font_size': str(size), 'align': 'center'}
702+
])
703+
response = client.post('/labeldesigner/api/preview', data=data)
704+
assert response.status_code == 400
705+
706+
707+
@pytest.mark.parametrize('size', [0, -1, -100])
708+
def test_negative_zero_label_size(client, size):
709+
data = EXAMPLE_FORMDATA.copy()
710+
data['label_size'] = size
711+
data['text'] = json.dumps([
712+
{'font_family': 'DejaVu Sans', 'font_style': 'Regular', 'text': 'Bad label', 'font_size': '12', 'align': 'center'}
713+
])
714+
response = client.post('/labeldesigner/api/preview', data=data)
715+
assert response.status_code == 400
716+
717+
718+
def test_invalid_alignment(client):
719+
data = EXAMPLE_FORMDATA.copy()
720+
data['text'] = json.dumps([
721+
{'font_family': 'DejaVu Sans', 'font_style': 'Regular', 'text': 'Bad align', 'font_size': '12', 'align': 'diagonal'}
722+
])
723+
response = client.post('/labeldesigner/api/preview', data=data)
724+
assert response.status_code == 400
725+
726+
727+
def test_non_string_text_value(client):
728+
data = EXAMPLE_FORMDATA.copy()
729+
data['text'] = json.dumps([
730+
{'font_family': 'DejaVu Sans', 'font_style': 'Regular', 'text': 12345, 'font_size': '12', 'align': 'center'}
731+
])
732+
response = client.post('/labeldesigner/api/preview', data=data)
733+
assert response.status_code == 400
734+
735+
736+
def test_conflicting_fields(client):
737+
data = EXAMPLE_FORMDATA.copy()
738+
data['print_type'] = 'qrcode_text'
739+
data['barcode_type'] = 'QR'
740+
data['image'] = FileStorage(stream=io.BytesIO(b'img'), filename='img.jpg', content_type='image/jpeg')
741+
data['image_mode'] = 'grayscale'
742+
data['text'] = json.dumps([
743+
{'font_family': 'DejaVu Sans', 'font_style': 'Regular', 'text': 'Conflict', 'font_size': '12', 'align': 'center'}
744+
])
745+
response = client.post('/labeldesigner/api/preview', data=data)
746+
assert response.status_code == 400
747+
748+
749+
def test_unicode_normalization(client):
750+
data = EXAMPLE_FORMDATA.copy()
751+
s1 = 'Café'
752+
s2 = unicodedata.normalize('NFD', s1)
753+
data['text'] = json.dumps([
754+
{'font_family': 'DejaVu Sans', 'font_style': 'Regular', 'text': s1, 'font_size': '24', 'align': 'center'},
755+
{'font_family': 'DejaVu Sans', 'font_style': 'Regular', 'text': s2, 'font_size': '24', 'align': 'center'}
756+
])
757+
response = client.post('/labeldesigner/api/preview', data=data)
758+
assert response.status_code == 200
759+
assert response.content_type in ['image/png']
760+
761+
# Check image
762+
verify_image(response.data, 'tests/unicode_normalization.png')
763+
764+
765+
def test_head_request(client):
766+
response = client.head('/labeldesigner/api/preview')
767+
assert response.status_code == 405
768+
769+
770+
def test_malformed_multipart(client):
771+
# Simulate a malformed multipart by sending a bad content-type
772+
data = '--bad-boundary\r\nContent-Disposition: form-data; name="text"\r\n\r\nfoo\r\n--bad-boundary--\r\n'
773+
response = client.post('/labeldesigner/api/preview', data=data, headers={'Content-Type': 'multipart/form-data; boundary=bad-boundary'})
774+
assert response.status_code == 400
775+
688776
# We cannot test the print functionality without a physical printer
689777
# def test_print_label(client):
690778
# data = {

tests/unicode_normalization.png

2.98 KB
Loading

0 commit comments

Comments
 (0)