|
1 |
| -"""TODO: Tests for the rendering of SVG cards.""" |
| 1 | +import re |
| 2 | +from urllib.parse import urlencode |
| 3 | + |
| 4 | + |
| 5 | +def test_request_no_id(client): |
| 6 | + response = client.get("/") |
| 7 | + data = response.data.decode("utf-8") |
| 8 | + |
| 9 | + assert response.status_code == 400 |
| 10 | + assert "Required parameter 'id' is missing" in data |
| 11 | + |
| 12 | + |
| 13 | +def test_request_invalid_id(client): |
| 14 | + response = client.get("/?id=**********") |
| 15 | + data = response.data.decode("utf-8") |
| 16 | + |
| 17 | + assert response.status_code == 400 |
| 18 | + assert "id expects a video ID but got '**********'" in data |
| 19 | + |
| 20 | + |
| 21 | +def test_request_unknown_id(client): |
| 22 | + response = client.get("/?id=abc_123-456") |
| 23 | + data = response.data.decode("utf-8") |
| 24 | + |
| 25 | + assert response.status_code == 404 |
| 26 | + assert "Not Found" in data |
| 27 | + |
| 28 | + |
| 29 | +def test_request_valid_params(client): |
| 30 | + params = { |
| 31 | + "id": "dQw4w9WgXcQ", |
| 32 | + "title": "Rick Astley - Never Gonna Give You Up (Official Music Video)", |
| 33 | + "timestamp": "1256450400", |
| 34 | + "background_color": "#000000", |
| 35 | + "title_color": "#111111", |
| 36 | + "stats_color": "#222222", |
| 37 | + "width": "500", |
| 38 | + "duration": "211", |
| 39 | + } |
| 40 | + response = client.get(f"/?{urlencode(params)}") |
| 41 | + data = response.data.decode("utf-8") |
| 42 | + |
| 43 | + assert response.status_code == 200 |
| 44 | + |
| 45 | + # test views |
| 46 | + views_regex = re.compile(r"\d+(?:\.\d)?[KMBT]? views") |
| 47 | + assert views_regex.search(data) is not None |
| 48 | + |
| 49 | + # test width |
| 50 | + assert 'width="500"' in data |
| 51 | + |
| 52 | + # test background color |
| 53 | + assert 'fill="#000000"' in data |
| 54 | + |
| 55 | + # test title color |
| 56 | + assert 'fill="#111111"' in data |
| 57 | + |
| 58 | + # test stats color |
| 59 | + assert 'fill="#222222"' in data |
| 60 | + |
| 61 | + # test title |
| 62 | + assert "Rick Astley - Never Gonna Give You Up (Official Music Video)" in data |
| 63 | + |
| 64 | + # test duration |
| 65 | + assert "3:31" in data |
| 66 | + |
| 67 | + # test thumbnail |
| 68 | + thumbnail_regex = re.compile(r'href="data:image/jpeg;base64,[a-zA-Z0-9+/]+={0,2}"') |
| 69 | + assert thumbnail_regex.search(data) is not None |
| 70 | + |
| 71 | + # test timestamp |
| 72 | + timestamp_regex = re.compile(r"\d+ years ago") |
| 73 | + assert timestamp_regex.search(data) is not None |
0 commit comments