Tighten pytest raises assertions

This commit is contained in:
mvdbeek
2023-01-21 12:54:05 +01:00
parent a79c3619f0
commit c431c6f0fc
2 changed files with 10 additions and 7 deletions
+2 -1
View File
@@ -1,4 +1,5 @@
import pytest
from pyparsing.exceptions import ParseException
from galaxy.util.bool_expressions import (
BooleanExpressionEvaluator,
@@ -61,7 +62,7 @@ def test_expression_evaluates_as_expected(expr: str, expected: bool, contained_e
@pytest.mark.parametrize("expr", INVALID_EXPRESSIONS_TESTS)
def test_invalid_expression_raises_exception(expr: str, contained_evaluator: BooleanExpressionEvaluator):
with pytest.raises(Exception):
with pytest.raises(ParseException):
contained_evaluator.evaluate_expression(expr)
+8 -6
View File
@@ -21,17 +21,19 @@ def test_sqlite_exploits():
connection = sqlite.connect(":memory:")
connection.execute("create TABLE FOO (foo1 text)")
__assert_has_n_rows(connection, "select * from FOO", 0)
__assert_query_errors(connection, "select * from FOOX")
__assert_query_errors(connection, "select * from FOOX", "no such table")
# Make sure sqlite query cannot execute multiple statements
__assert_query_errors(connection, "select * from FOO; select * from FOO")
__assert_query_errors(
connection, "select * from FOO; select * from FOO", "You can only execute one statement at a time."
)
# Make sure sqlite cannot select on PRAGMA results
__assert_query_errors(connection, "select * from (PRAGMA database_list)")
__assert_query_errors(connection, "select * from (PRAGMA database_list)", "no such table: PRAGMA")
__assert_has_n_rows(connection, "select * from FOO where foo1 in (SELECT foo1 from FOO)", 0)
# Ensure nested queries cannot modify database.
__assert_query_errors(connection, "select * from FOO where foo1 in (INSERT INTO FOO VALUES ('bar')")
__assert_query_errors(connection, "select * from FOO where foo1 in (INSERT INTO FOO VALUES ('bar')", "syntax error")
# Should access to the schema be disallowed?
# __assert_has_n_rows(connection, "select * from SQLITE_MASTER", 0)
@@ -44,8 +46,8 @@ def __assert_has_n_rows(connection, query, n):
assert count == n
def __assert_query_errors(connection, query):
with pytest.raises(Exception):
def __assert_query_errors(connection, query, match):
with pytest.raises(Exception, match=match):
connection.cursor().execute(query)