Skip to content

armgit / sure

Public Imported from GitHub
0

The personal finance app for everyone (by everyone)

Ruby
4.1 KB
Raw Blame
require "test_helper"

# A matches_regex condition is validated when it is saved, so an unsafe pattern
# never reaches a rule that runs on every sync.
class Rule::ConditionRegexTest < ActiveSupport::TestCase
  setup do
    @family = families(:empty)
  end

  test "a rule with a valid regex condition saves" do
    rule = build_rule(value: '^amzn\s+mktp')

    assert_difference -> { @family.rules.count }, 1 do
      assert rule.save, rule.errors.full_messages.to_sentence
    end
  end

  {
    "a back-reference" => [ '(a*)*\1c', :regex_backreference ],
    "an unbalanced group" => [ "(", :regex_invalid ],
    "a pattern Postgres calls too complex" => [ "^(a{1,255}){1,255}(b)", :regex_invalid ],
    "an over-long pattern" => [ "a" * 201, :regex_too_long ]
  }.each do |description, (pattern, kind)|
    # Postgres needs a few milliseconds of CPU to reject the too-complex pattern,
    # and on a loaded machine the probe's time limit could fire first. A
    # statement_timeout of 0 lifts the limit so each case gets Postgres' verdict;
    # Rule::SafeRegexTest covers the limit.
    test "a rule with #{description} is rejected and nothing is saved" do
      rule = build_rule(value: pattern)

      assert_no_difference [ -> { @family.rules.count }, -> { Rule::Condition.count } ] do
        stub_const(Rule::SafeRegex, :PROBE_TIMEOUT_MS, 0) { assert_not rule.save }
      end

      assert rule.conditions.first.errors.of_kind?(:value, kind), rule.conditions.first.errors.details.inspect
    end
  end

  # The database driver will not send a string holding a null byte or malformed
  # UTF-8 (ArgumentError), so probing one would raise out of validation. Such a
  # value is DatabaseTextValidator's to report, once, and is never probed.
  {
    "a null byte" => "ab\u0000c",
    "a lone null byte" => "\u0000",
    "malformed UTF-8" => "ab\xFFc".dup.force_encoding(Encoding::UTF_8)
  }.each do |description, pattern|
    test "a regex with #{description} is rejected as invalid text without a probe, and nothing is saved" do
      rule = build_rule(value: pattern)
      Rule::SafeRegex.expects(:error_for).never

      assert_no_difference [ -> { @family.rules.count }, -> { Rule::Condition.count } ] do
        assert_not rule.save
      end

      assert_equal [ { error: :invalid } ], rule.conditions.first.errors.details[:value]
      assert_equal [ "Value is invalid" ], rule.conditions.first.errors.full_messages
    end
  end

  test "the same pattern is accepted under the contains operator" do
    rule = build_rule(value: "(", operator: "like")

    assert rule.save, rule.errors.full_messages.to_sentence
  end

  test "a sub-condition inside a compound condition is validated too" do
    rule = @family.rules.build(resource_type: "transaction", actions: [ Rule::Action.new(action_type: "exclude_transaction") ])
    compound = rule.conditions.build(condition_type: "compound", operator: "or")
    compound.sub_conditions.build(condition_type: "transaction_name", operator: "matches_regex", value: "(")

    assert_no_difference -> { @family.rules.count } do
      assert_not rule.save
    end
  end

  test "the validation does not run for other operators" do
    Rule::SafeRegex.expects(:error_for).never

    assert build_rule(value: "(", operator: "like").save
  end

  test "an unchanged regex condition is not probed again when its rule is saved" do
    rule = build_rule(value: "^amzn")
    rule.save!

    Rule::SafeRegex.expects(:error_for).never
    rule.update!(active: true)
    # Saving the rule leaves an unchanged condition unvalidated by itself, so the
    # condition is also saved directly: its own validations always run.
    rule.conditions.first.save!

    Rule::SafeRegex.expects(:error_for).with("^changed").returns(nil).once
    rule.conditions.first.update!(value: "^changed")
  end

  private
    def build_rule(value:, operator: "matches_regex")
      @family.rules.build(
        resource_type: "transaction",
        conditions: [ Rule::Condition.new(condition_type: "transaction_name", operator: operator, value: value) ],
        actions: [ Rule::Action.new(action_type: "exclude_transaction") ]
      )
    end
end