Skip to content

armgit / sure

Public Imported from GitHub
0

The personal finance app for everyone (by everyone)

Ruby

Fix flaky SafeRegex 'too complex' test (#5360)

David Gil78c0d4fa209e
diff --git a/test/models/rule/safe_regex_test.rb b/test/models/rule/safe_regex_test.rb
index 4d5245014c..618eb31f28 100644
--- a/test/models/rule/safe_regex_test.rb
+++ b/test/models/rule/safe_regex_test.rb
@@ -43,10 +43,21 @@ class Rule::SafeRegexTest < ActiveSupport::TestCase
assert_equal :invalid, Rule::SafeRegex.error_for("[a-")
end
+ # Postgres needs a few milliseconds of CPU to give up on this pattern, so under
+ # a loaded suite the probe's time limit could fire first. The limit is lifted
+ # here: this test is about Postgres' verdict, the next one about the limit.
test "a pattern Postgres calls too complex is refused" do
+ Rule::SafeRegex.stubs(:with_timeout).yields
+
assert_equal :invalid, Rule::SafeRegex.error_for("^(a{1,255}){1,255}(b)")
end
+ test "a pattern whose probe runs out of time is refused as too complex" do
+ Rule::SafeRegex.stubs(:with_timeout).raises(Rule::SafeRegex::TimeoutError)
+
+ assert_equal :too_complex, Rule::SafeRegex.error_for("abc")
+ end
+
test "a database error that is not a regex error is not reported as an invalid pattern" do
ActiveRecord::Base.connection.stubs(:select_value).raises(ActiveRecord::StatementInvalid.new("connection lost"))