|
| 1 | +"""tests/test_search.py |
| 2 | +
|
| 3 | +Tests for the search functionality, focusing on correct escaping of SQL LIKE |
| 4 | +special characters (%, _, \\) in search queries. |
| 5 | +""" |
| 6 | + |
| 7 | +import pytest |
| 8 | + |
| 9 | +from web.utils.helpers import escape_like |
| 10 | + |
| 11 | + |
| 12 | +# --------------------------------------------------------------------------- # |
| 13 | +# escape_like helper # |
| 14 | +# --------------------------------------------------------------------------- # |
| 15 | + |
| 16 | + |
| 17 | +class TestEscapeLike: |
| 18 | + def test_plain_string_unchanged(self): |
| 19 | + """Strings with no special characters are left as-is.""" |
| 20 | + assert escape_like("hello world") == "hello world" |
| 21 | + |
| 22 | + def test_percent_escaped(self): |
| 23 | + """% is escaped to \\%.""" |
| 24 | + assert escape_like("%") == "\\%" |
| 25 | + assert escape_like("100%") == "100\\%" |
| 26 | + assert escape_like("%complete%") == "\\%complete\\%" |
| 27 | + |
| 28 | + def test_underscore_escaped(self): |
| 29 | + """_ is escaped to \\_.""" |
| 30 | + assert escape_like("_") == "\\_" |
| 31 | + assert escape_like("game_1") == "game\\_1" |
| 32 | + |
| 33 | + def test_backslash_escaped(self): |
| 34 | + r"""\ is escaped to \\.""" |
| 35 | + assert escape_like("a\\b") == "a\\\\b" |
| 36 | + |
| 37 | + def test_combined_special_chars(self): |
| 38 | + """Multiple special characters are all escaped.""" |
| 39 | + assert escape_like("50% off_sale") == "50\\% off\\_sale" |
| 40 | + |
| 41 | + |
| 42 | +# --------------------------------------------------------------------------- # |
| 43 | +# Library search – special characters # |
| 44 | +# --------------------------------------------------------------------------- # |
| 45 | + |
| 46 | + |
| 47 | +@pytest.fixture |
| 48 | +def client_with_special_games(db_conn, client): |
| 49 | + """Insert games with special characters in their names.""" |
| 50 | + db_conn.execute( |
| 51 | + "INSERT INTO games (name, store, store_id) VALUES (?, ?, ?)", |
| 52 | + ("100% Orange Juice", "steam", "282870"), |
| 53 | + ) |
| 54 | + db_conn.execute( |
| 55 | + "INSERT INTO games (name, store, store_id) VALUES (?, ?, ?)", |
| 56 | + ("Pro_game", "steam", "99999"), |
| 57 | + ) |
| 58 | + db_conn.execute( |
| 59 | + "INSERT INTO games (name, store, store_id) VALUES (?, ?, ?)", |
| 60 | + ("Normal Game", "steam", "11111"), |
| 61 | + ) |
| 62 | + db_conn.commit() |
| 63 | + # client fixture already sets up the DB override; just return it |
| 64 | + return client |
| 65 | + |
| 66 | + |
| 67 | +class TestLibrarySearchSpecialChars: |
| 68 | + def test_percent_search_returns_only_matching_game(self, client_with_special_games): |
| 69 | + """Searching for % should match games that contain % in their name, not all games.""" |
| 70 | + resp = client_with_special_games.get("/library?search=%25") # URL-encoded % |
| 71 | + assert resp.status_code == 200 |
| 72 | + text = resp.text |
| 73 | + # The game with % in its name should appear |
| 74 | + assert "100% Orange Juice" in text |
| 75 | + # Normal games should NOT appear |
| 76 | + assert "Normal Game" not in text |
| 77 | + |
| 78 | + def test_underscore_search_returns_only_matching_game(self, client_with_special_games): |
| 79 | + """Searching for _ should not act as a wildcard; only exact matches are returned.""" |
| 80 | + resp = client_with_special_games.get("/library?search=Pro_game") |
| 81 | + assert resp.status_code == 200 |
| 82 | + text = resp.text |
| 83 | + assert "Pro_game" in text |
| 84 | + # Normal Game has no underscore – should not match |
| 85 | + assert "Normal Game" not in text |
| 86 | + |
| 87 | + def test_plain_search_still_works(self, client_with_special_games): |
| 88 | + """Regular search without special chars continues to work.""" |
| 89 | + resp = client_with_special_games.get("/library?search=Normal") |
| 90 | + assert resp.status_code == 200 |
| 91 | + assert "Normal Game" in resp.text |
| 92 | + assert "100% Orange Juice" not in resp.text |
| 93 | + |
| 94 | + def test_search_too_long_returns_422(self, client_with_special_games): |
| 95 | + """Search strings longer than 200 chars are rejected with HTTP 422.""" |
| 96 | + long_search = "a" * 201 |
| 97 | + resp = client_with_special_games.get(f"/library?search={long_search}") |
| 98 | + assert resp.status_code == 422 |
| 99 | + |
| 100 | + def test_search_exactly_200_chars_is_accepted(self, client_with_special_games): |
| 101 | + """Search strings of exactly 200 chars are accepted.""" |
| 102 | + search = "a" * 200 |
| 103 | + resp = client_with_special_games.get(f"/library?search={search}") |
| 104 | + assert resp.status_code == 200 |
0 commit comments