rescanner.rb 5.3 KB
Newer Older
J
Justin Collins 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
require 'tmpdir'
require 'brakeman/rescanner'

class Brakeman::Rescanner
  #For access to internals
  attr_reader :changes, :reindex
end

class RescannerTests < Test::Unit::TestCase
  include BrakemanTester::RescanTestHelper

  def test_no_change_no_warnings
    before_rescan_of []

    assert_fixed 0
    assert_new 0
    assert_equal false, rescan.warnings_changed?
  end

  def test_no_change
    before_rescan_of []

    assert rescan.any_warnings?
    assert_reindex :none
    assert_changes false
    assert_fixed 0
    assert_new 0
  end

  def test_irrelavent_new_file
    before_rescan_of "IRRELEVANT" do
      write_file "IRRELEVANT", "Nothing special here"
    end

    assert_reindex :none
    assert_changes false #No files were rescanned
    assert_new 0
    assert_fixed 0
  end

  def test_irrelevant_deleted_file
    before_rescan_of "README.rdoc" do
      remove "README.rdoc"
    end

    assert_reindex :none
    assert_changes false #No files were rescanned
    assert_new 0
    assert_fixed 0
  end

  def test_delete_template
    template = "app/views/users/show.html.erb"

    before_rescan_of template do
      remove template
    end

    assert_reindex :none #because deleted
    assert_changes
    assert_new 0
    assert_fixed 1
    assert_nil @original.templates[:"users/show"] #tracker is modified
  end

  def test_controller_remove_method
    controller = "app/controllers/removal_controller.rb"

    before_rescan_of controller do
      remove_method controller, :remove_this
    end

    assert_reindex :controllers, :templates 
    assert_changes
    assert_new 0
    assert_fixed 1
  end

  def test_controller_remove_method_for_line_numbers_only
    controller = "app/controllers/removal_controller.rb"

    before_rescan_of controller do
      remove_method controller, :change_lines
    end

    assert_reindex :controllers, :templates 
    assert_changes
    assert_new 0
    assert_fixed 0
  end

  def test_delete_controller
    controller = "app/controllers/removal_controller.rb"

    before_rescan_of controller do
      remove controller
    end

99
    assert_reindex :none
J
Justin Collins 已提交
100 101
    assert_changes
    assert_new 0
102
    assert_fixed 4
J
Justin Collins 已提交
103 104
  end

105 106 107 108 109 110 111 112 113 114 115 116 117
  def test_controller_escape_params
    controller = "app/controllers/users_controller.rb"

    before_rescan_of controller do
      replace controller, "@user_data = raw params[:user_data]", "@user_data = params[:user_data]"
    end

    assert_reindex :controllers, :templates
    assert_changes
    assert_new 0
    assert_fixed 1
  end

J
Justin Collins 已提交
118 119 120 121 122 123 124 125 126 127 128 129 130
  def test_template_add_line
    template = "app/views/users/show.html.erb"

    before_rescan_of template do
      append template, "<%= raw params[:bad] %>"
    end

    assert_reindex :templates
    assert_changes
    assert_new 1
    assert_fixed 0
  end

J
Justin Collins 已提交
131 132 133 134 135 136 137 138 139 140 141 142 143
  def test_partial_template_add_line
    template = "app/views/users/_form.html.erb"

    before_rescan_of template do
      append template, "<%= raw @user.thing %>"
    end

    assert_reindex :templates
    assert_changes
    assert_new 1
    assert_fixed 0
  end

J
Justin Collins 已提交
144 145 146 147 148 149 150 151 152
  def test_delete_model
    model = "app/models/user.rb"

    before_rescan_of model do
      remove model
    end

    assert_reindex :templates, :models, :controllers
    assert_changes
J
Justin Collins 已提交
153
    assert_new 7 #User is no longer a model, causing MORE warnings
154
    assert_fixed 7
J
Justin Collins 已提交
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
  end

  def test_add_method_to_model
    model = "app/models/user.rb"

    before_rescan_of model do
      add_method model, <<-'RUBY'
      def bad_sql input
        find(:all, :conditions => "x > #{input}")
      end
      RUBY
    end
      
    assert_reindex :models
    assert_changes
    assert_new 1
    assert_fixed 0
  end

  def test_change_config
175
    config = "config/environments/production.rb"
J
Justin Collins 已提交
176 177 178 179 180 181 182 183

    before_rescan_of config do
      replace config, "config.active_record.whitelist_attributes = true",
        "config.active_record.whitelist_attributes = false"
    end

    assert_reindex :none
    assert_changes 
184
    assert_new 1
J
Justin Collins 已提交
185 186
    assert_fixed 0
  end
187 188 189 190

  def test_remove_route
    routes = "config/routes.rb"

J
Justin Collins 已提交
191
    before_rescan_of routes, "rails3.2", :assume_all_routes => false do
192 193 194 195 196 197 198 199
      replace routes, "match 'implicit' => 'removal#implicit_render'", ""
    end

    assert_reindex :controllers, :templates
    assert_changes
    assert_new 0
    assert_fixed 1
  end
200

201 202 203 204 205 206 207 208 209 210 211 212 213
  def test_remove_initializer
    #Should probably remove initializer that actually affects something
    initializer = "config/initializers/wrap_parameters.rb"

    before_rescan_of initializer do
      remove initializer
    end

    assert_reindex :none
    assert_changes
    assert_new 0
    assert_fixed 0
  end
214 215 216 217 218 219 220 221 222 223 224 225 226

  def test_remove_mixin
    lib = 'lib/user_controller_mixin.rb'

    before_rescan_of lib do
      remove lib
    end

    assert_reindex :controllers, :templates
    assert_changes
    assert_new 0
    assert_fixed 1
  end
227 228 229 230 231 232 233 234 235 236 237 238 239

  def test_remove_route_from_mixin
    lib = 'lib/user_controller_mixin.rb'

    before_rescan_of lib do
      remove_method lib, :mixed_in
    end

    assert_reindex :controllers, :templates
    assert_changes
    assert_new 0
    assert_fixed 1
  end
240 241 242 243 244

  def test_gemfile_rails_version_change
    gemfile = "Gemfile.lock"

    before_rescan_of gemfile do
245
      replace gemfile, "rails (3.2.9.rc2)", "rails (3.2.6)"
246 247 248 249 250 251
    end

    #@original is actually modified
    assert @original.config[:rails_version], "3.2.6"
    assert_reindex :none
    assert_changes
J
Justin Collins 已提交
252 253
    assert_new 1
    assert_fixed 0
254
  end
J
Justin Collins 已提交
255
end