mirror of
https://github.com/galaxyproject/galaxy.git
synced 2026-09-24 16:30:27 +08:00
Tighten pytest raises assertions
This commit is contained in:
@@ -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)
|
||||
|
||||
|
||||
|
||||
@@ -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)
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user