Spaces:
Running
Running
Download tests/test_deckbuilder_route.py from sahil-12kumar/IL_CMS_Tools: direct link, hf CLI and curl.
- Browser
- Download file 4.15 kB
-
https://huggingface.co/spaces/sahil-12kumar/IL_CMS_Tools/resolve/main/tests/test_deckbuilder_route.py
- Command line
-
hf download hf://spaces/sahil-12kumar/IL_CMS_Tools/tests/test_deckbuilder_route.py
-
curl -L -o test_deckbuilder_route.py https://huggingface.co/spaces/sahil-12kumar/IL_CMS_Tools/resolve/main/tests/test_deckbuilder_route.py
4.15 kB
| """Route tests for the Custom Deck builder (/deckbuilder, /deckbuilder/build).""" | |
| import importlib | |
| import io | |
| import json | |
| import sys | |
| import pytest | |
| def client_for(monkeypatch): | |
| def build(login=True): | |
| sys.modules.pop('app', None) | |
| app_mod = importlib.import_module('app') | |
| app_mod.app.config['TESTING'] = True | |
| c = app_mod.app.test_client() | |
| if login: | |
| with c.session_transaction() as s: | |
| s['app_authed'] = True | |
| return app_mod, c | |
| yield build | |
| sys.modules.pop('app', None) | |
| def png_bytes(): | |
| from PIL import Image | |
| buf = io.BytesIO() | |
| Image.new('RGB', (160, 90), 'white').save(buf, 'PNG') | |
| return buf.getvalue() | |
| def test_page_renders_when_logged_in(client_for): | |
| app_mod, c = client_for() | |
| r = c.get('/deckbuilder') | |
| assert r.status_code == 200 | |
| assert b'Custom PPT Builder' in r.data | |
| def test_page_requires_login(client_for): | |
| _, c = client_for(login=False) | |
| assert c.get('/deckbuilder').status_code == 302 # redirect to login | |
| def test_build_simple_deck(client_for): | |
| _, c = client_for() | |
| deck = {'title': 'Test Deck', 'text_style': 'auto', | |
| 'cover': {'enabled': True, 'title': 'Test Deck', 'subtitle': ''}, | |
| 'slides': [{'title': 'Slide one', 'body': ['Hello'], 'table': '', | |
| 'notes': '', 'images': []}]} | |
| r = c.post('/deckbuilder/build', | |
| data={'deck': json.dumps(deck)}, | |
| content_type='multipart/form-data') | |
| assert r.status_code == 200 | |
| assert r.data[:2] == b'PK' # valid .pptx (zip) | |
| assert 'presentationml.presentation' in (r.content_type or '') | |
| def test_build_with_image_and_caption(client_for, png_bytes): | |
| _, c = client_for() | |
| deck = {'title': 'Imaged', 'text_style': 'light-bg', | |
| 'cover': {'enabled': False}, | |
| 'slides': [{'title': '', 'body': [], 'table': '', 'notes': '', | |
| 'images': [{'caption': 'Radiograph'}]}]} | |
| r = c.post('/deckbuilder/build', | |
| data={'deck': json.dumps(deck), | |
| 'img_0_0.png': (io.BytesIO(png_bytes), 'img_0_0.png')}, | |
| content_type='multipart/form-data') | |
| assert r.status_code == 200 | |
| assert r.data[:2] == b'PK' | |
| def test_build_rejects_no_slides(client_for): | |
| _, c = client_for() | |
| r = c.post('/deckbuilder/build', | |
| data={'deck': json.dumps({'title': 'x', 'slides': []})}, | |
| content_type='multipart/form-data') | |
| assert r.status_code == 400 | |
| assert 'slide' in r.get_json()['error'].lower() | |
| def test_build_rejects_caption_without_file(client_for, png_bytes): | |
| _, c = client_for() | |
| deck = {'title': 'x', 'slides': [{'title': 's', 'body': [], 'table': '', | |
| 'notes': '', 'images': [{'caption': 'orphan'}]}]} | |
| r = c.post('/deckbuilder/build', | |
| data={'deck': json.dumps(deck)}, | |
| content_type='multipart/form-data') | |
| assert r.status_code == 400 | |
| assert 'caption but no uploaded file' in r.get_json()['error'] | |
| def test_build_rejects_garbage_image(client_for): | |
| _, c = client_for() | |
| deck = {'title': 'x', 'slides': [{'title': 's', 'body': [], 'table': '', | |
| 'notes': '', 'images': [{'caption': ''}]}]} | |
| r = c.post('/deckbuilder/build', | |
| data={'deck': json.dumps(deck), | |
| 'img_0_0.png': (io.BytesIO(b'not an image'), 'img_0_0.png')}, | |
| content_type='multipart/form-data') | |
| assert r.status_code == 400 | |
| assert 'not a readable image' in r.get_json()['error'] | |
| def test_build_rejects_wrong_image_field_name(client_for, png_bytes): | |
| _, c = client_for() | |
| deck = {'title': 'x', 'slides': [{'title': 's', 'images': []}]} | |
| r = c.post('/deckbuilder/build', | |
| data={'deck': json.dumps(deck), | |
| 'something_else.png': (io.BytesIO(png_bytes), 'something_else.png')}, | |
| content_type='multipart/form-data') | |
| assert r.status_code == 400 | |
| assert 'Unrecognised image field' in r.get_json()['error'] |