action_tracker_test.rb 21.9 KB
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 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619
require 'test_helper'

ActiveRecord::Base.establish_connection({
  :adapter => "sqlite3",
  :database => ":memory:"
})

ActiveRecord::Schema.define do
  create_table :some_table, :force => true do |t|
    t.column :some_column, :string
  end
  create_table :other_table, :force => true do |t|
    t.column :other_column, :string
    t.column :another_column, :integer
  end
  create_table :action_tracker do |t|
    t.belongs_to :user, :polymorphic => true
    t.belongs_to :target, :polymorphic => true
    t.text :params
    t.string :verb
    t.timestamps
  end
end

class SomeModel < ActiveRecord::Base
  self.table_name = :some_table
  acts_as_trackable
end

class OtherModel < ActiveRecord::Base
  self.table_name = :other_table
  acts_as_trackable
end

class ThingsController < ActionController::Base

  def index
    params[:foo] = params[:foo].to_i + 1
    render :text => "test"
  end

  def test
    render :text => "test"
  end

  def rescue_action(e)
    raise e
  end

end

ActionController::Routing::Routes.draw { |map| map.resources :things, :collection => { :test => :get } }

class ActionTrackerTest < ActiveSupport::TestCase

  def setup
    ActionTrackerConfig.current_user = proc{ SomeModel.first || SomeModel.create! }
    ActionTracker::Record.delete_all
    ActionTrackerConfig.verbs = { :some_verb => { :description => "Did something" } }
    @request = ActionController::TestRequest.new
    @response = ActionController::TestResponse.new
    @controller = ThingsController.new
  end

  def test_index
    get :index
    assert_response :success
  end

  def test_track_actions_after_runs_after_action
    @controller = create_controller { track_actions_after :some_verb }
    assert_difference 'ActionTracker::Record.count' do
      get :index, :foo => "4"
    end
    assert_equal 5, ActionTracker::Record.first.params[:foo]
  end

  def test_track_actions_after_runs_before_action
    @controller = create_controller { track_actions_before :some_verb }
    assert_difference 'ActionTracker::Record.count' do
      get :index, :foo => "4"
    end
    assert_equal "4", ActionTracker::Record.first.params[:foo]
  end

  def test_track_actions_default_is_after
    ActionTrackerConfig.default_filter_time = :after
    @controller = create_controller { track_actions :some_verb }
    assert_difference 'ActionTracker::Record.count' do
      get :index, :foo => "4"
    end
    assert_equal 5, ActionTracker::Record.first.params[:foo]
  end

  def test_track_actions_default_is_before
    ActionTrackerConfig.default_filter_time = :before
    @controller = create_controller { track_actions :some_verb }
    assert_difference 'ActionTracker::Record.count' do
      get :index, :foo => "4"
    end
    assert_equal "4", ActionTracker::Record.first.params[:foo]
  end

  def test_track_actions_executes_block
    @controller = create_controller do
      track_actions :some_verb do
        throw :some_symbol
      end
    end
    assert_difference 'ActionTracker::Record.count' do
      assert_throws :some_symbol do
        get :index, :foo => "4"
      end
    end
    assert_equal "4", ActionTracker::Record.first.params[:foo]
  end

  def test_pass_keep_params_as_symbol
    @controller = create_controller { track_actions_before :some_verb, :keep_params => [:foo] }
    assert_difference 'ActionTracker::Record.count' do
      get :index, :foo => 5
    end
    assert_equal({ "foo" => "5" }, ActionTracker::Record.first.params)
  end

  def test_pass_keep_params_as_string
    @controller = create_controller { track_actions_before :some_verb, "keep_params" => [:foo, :bar] }
    assert_difference 'ActionTracker::Record.count' do
      get :index, :foo => 5, :bar => 10
    end
    assert_equal({ "foo" => "5", "bar" => "10" }, ActionTracker::Record.first.params)
  end

  def test_pass_keep_params_none
    @controller = create_controller { track_actions_before :some_verb, :keep_params => :none }
    assert_difference 'ActionTracker::Record.count' do
      get :index, :foo => 5
    end
    assert_equal({}, ActionTracker::Record.first.params)
    ActionTracker::Record.delete_all
		@controller = create_controller { track_actions_before :some_verb, :keep_params => "none" }
    assert_difference 'ActionTracker::Record.count' do
      get :index, :foo => 5
    end
    assert_equal({}, ActionTracker::Record.first.params)
  end

  def test_pass_keep_params_all
    @controller = create_controller { track_actions_before :some_verb, :keep_params => :all }
    assert_difference 'ActionTracker::Record.count' do
      get :index, :foo => 5
    end
    assert_equal({"action"=>"index", "foo"=>"5", "controller"=>"things"}, ActionTracker::Record.first.params)
    ActionTracker::Record.delete_all
    @controller = create_controller { track_actions_before :some_verb, :keep_params => "all" }
    assert_difference 'ActionTracker::Record.count' do
      get :index, :foo => 5
    end
    assert_equal({"action"=>"index", "foo"=>"5", "controller"=>"things"}, ActionTracker::Record.first.params)
	end

  def test_keep_params_not_set_should_store_all_params
    @controller = create_controller { track_actions_before :some_verb }
    assert_difference 'ActionTracker::Record.count' do
      get :index, :foo => 5
    end
    assert_equal({"action"=>"index", "foo"=>"5", "controller"=>"things"}, ActionTracker::Record.first.params)
  end

  def test_execute_if_some_condition_is_true
    @controller = create_controller { track_actions_before :some_verb, :if => Proc.new { 2 < 1 } }
    assert_no_difference 'ActionTracker::Record.count' do
      get :index, :foo => 5
    end
    @controller = create_controller { track_actions_before :some_verb, :if => Proc.new { 2 > 1 } }
    assert_difference 'ActionTracker::Record.count' do
      get :index, :foo => 5
    end
  end

  def test_execute_unless_some_condition_is_true
    @controller = create_controller { track_actions_before :some_verb, :unless => Proc.new { 2 < 1 } }
    assert_difference 'ActionTracker::Record.count' do
      get :index, :foo => 5
    end
    @controller = create_controller { track_actions_before :some_verb, :unless => Proc.new { 2 > 1 } }
    assert_no_difference 'ActionTracker::Record.count' do
      get :index, :foo => 5
    end
  end

  def test_execute_for_all_actions
    @controller = create_controller { track_actions_before :some_verb }
    assert_difference 'ActionTracker::Record.count' do
      get :index
    end
    assert_difference 'ActionTracker::Record.count' do
      get :test
    end
  end

  def test_execute_only_for_some_action
    @controller = create_controller { track_actions_before :some_verb, :only => [:index] }
    assert_difference 'ActionTracker::Record.count' do
      get :index
    end
    assert_no_difference 'ActionTracker::Record.count' do
      get :test
    end
  end

  def test_execute_except_for_some_action
    @controller = create_controller { track_actions_before :some_verb, :except => [:index] }
    assert_no_difference 'ActionTracker::Record.count' do
      get :index
    end
    assert_difference 'ActionTracker::Record.count' do
      get :test
    end
  end

  def test_store_user
    @controller = create_controller do
			track_actions_before :some_verb
		end
    ActionTrackerConfig.current_user = proc{ SomeModel.create! :some_column => "test" }

    assert_difference 'ActionTracker::Record.count' do
      get :test
    end
		assert_equal "test", ActionTracker::Record.last.user.some_column
  end

  def test_should_update_when_verb_is_updatable_and_no_timeout
    ActionTrackerConfig.verbs = { :some_verb => { :description => "Did something", :type => :updatable } }
    ActionTrackerConfig.timeout = 5.minutes
    @controller = create_controller { track_actions_before :some_verb }
    assert ActionTrackerConfig.verb_type(:some_verb) == :updatable
		assert_difference 'ActionTracker::Record.count' do
      get :test
    end
    t = ActionTracker::Record.last
    t.updated_at = t.updated_at.ago(2.minutes)
    t.send :update_without_callbacks
	  assert_no_difference 'ActionTracker::Record.count' do
      get :test
    end
  end

  def test_should_create_when_verb_is_updatable_and_timeout
    ActionTrackerConfig.verbs = { :some_verb => { :description => "Did something", :type => :updatable } }
    ActionTrackerConfig.timeout = 5.minutes
    @controller = create_controller { track_actions_before :some_verb }
    assert ActionTrackerConfig.verb_type(:some_verb) == :updatable
		assert_difference 'ActionTracker::Record.count' do
      get :test
    end
    t = ActionTracker::Record.last
    t.updated_at = t.updated_at.ago(6.minutes)
    t.send :update_without_callbacks
	  assert_difference 'ActionTracker::Record.count' do
      get :test
    end
  end

  def test_should_update_when_verb_is_groupable_and_no_timeout
    ActionTrackerConfig.verbs = { :some_verb => { :description => "Did something", :type => :groupable } }
    ActionTrackerConfig.timeout = 5.minutes
    @controller = create_controller { track_actions_before :some_verb, :keep_params => [:foo] }
    assert ActionTrackerConfig.verb_type(:some_verb) == :groupable
		assert_difference 'ActionTracker::Record.count' do
      get :test, :foo => "bar"
    end
    t = ActionTracker::Record.last
    t.updated_at = t.updated_at.ago(2.minutes)
    t.send :update_without_callbacks
	  assert_no_difference 'ActionTracker::Record.count' do
      get :test, :foo => "test"
    end
  end

  def test_should_create_when_verb_is_groupable_and_timeout
    ActionTrackerConfig.verbs = { :some_verb => { :description => "Did something", :type => :groupable } }
    ActionTrackerConfig.timeout = 5.minutes
    @controller = create_controller { track_actions_before :some_verb, :keep_params => [:foo] }
    assert ActionTrackerConfig.verb_type(:some_verb) == :groupable
		assert_difference 'ActionTracker::Record.count' do
      get :test, :foo => "bar"
    end
    t = ActionTracker::Record.last
    t.created_at = t.updated_at.ago(6.minutes)
    t.send :update_without_callbacks
	  assert_difference 'ActionTracker::Record.count' do
      get :test, :foo => "test"
    end
  end

  def test_should_create_when_verb_is_single
    ActionTrackerConfig.verbs = { :some_verb => { :description => "Did something", :type => :single } }
    @controller = create_controller { track_actions_before :some_verb }
    assert ActionTrackerConfig.verb_type(:some_verb) == :single
		assert_difference 'ActionTracker::Record.count' do
      get :test
    end
	  assert_difference 'ActionTracker::Record.count' do
      get :test
    end
  end

  def test_should_act_as_trackable
    m = SomeModel.create!
    assert m.respond_to?(:tracked_actions)
    assert_kind_of Array, m.tracked_actions
    @controller = create_controller { track_actions_before :some_verb }
    @controller.stubs(:current_user).returns(m)
    get :index
    sleep 2
    get :test
    assert ActionTracker::Record.last.updated_at > ActionTracker::Record.first.updated_at
    assert_equal [ActionTracker::Record.last, ActionTracker::Record.first], m.reload.tracked_actions
  end

  def test_should_get_time_spent_doing_something
    ActionTrackerConfig.verbs = { :some_verb => { :type => :updatable }, :other_verb => { :type => :updatable } }
    m = SomeModel.create!
    @controller = create_controller do
      track_actions :some_verb
    end
    @controller.stubs(:current_user).returns(m)
    get :index
    t1 = ActionTracker::Record.last
    t1.updated_at = t1.updated_at.ago(4.hours)
    t1.created_at = t1.updated_at.ago(3.hours)
    t1.send :update_without_callbacks
    get :test
    t2 = ActionTracker::Record.last
    t2.updated_at = t2.updated_at.ago(3.hours)
    t2.created_at = t2.updated_at.ago(2.hours)
    t2.send :update_without_callbacks
    assert_equal 5.hours, m.time_spent_doing(:some_verb)
    assert_equal 3.hours, m.time_spent_doing(:some_verb, :id => t1.id)
    assert_equal 0.0, m.time_spent_doing(:other_verb)
    assert_equal 0.0, m.time_spent_doing(:other_verb, :verb => :some_verb)
  end

  def test_helper_describe_action_tracker_object
    ActionTrackerConfig.verbs = { :some_verb => { :description => "Hey, {{link_to 'click here', :controller => :things}} {{ta.user.some_column}}!" } }
    view = ActionView::Base.new
    view.controller = @controller
    @request.env["HTTP_REFERER"] = "http://test.com"
    get :index
    user = SomeModel.create! :some_column => "test"
    t = ActionTracker::Record.create! :verb => :some_verb, :user => user
    assert_equal 'Hey, <a href="/things">click here</a> test!', view.describe(t)
  end

  def test_helper_describe_non_action_tracker_object
    view = ActionView::Base.new
    view.controller = @controller
    @request.env["HTTP_REFERER"] = "http://test.com"
    get :index
    assert_equal "", view.describe("Something")
  end

  def test_track_actions_store_user
    ActionTrackerConfig.verbs = { :test => { :description => "Some" } }
    model = create_model do
      track_actions :test, :after_create
    end
    @controller = create_controller_for_model(model)
    assert_difference 'ActionTracker::Record.count' do
      get :test
    end
    assert_kind_of SomeModel, ActionTracker::Record.last.user
		assert_equal "test", ActionTracker::Record.last.user.some_column
  end

  def test_track_actions_store_some_params
    ActionTrackerConfig.verbs = { :test => { :description => "Some" } }
    model = create_model do
      track_actions :test, :after_create, :keep_params => [:other_column]
    end
    @controller = create_controller_for_model(model, :other_column => "foo", :another_column => 2)
    assert_difference 'ActionTracker::Record.count' do
      get :test
    end
		assert_equal "foo", ActionTracker::Record.last.params["other_column"]
		assert_nil ActionTracker::Record.last.params["another_column"]
  end

  def test_replace_dots_by_underline_in_param_name
    ActionTrackerConfig.verbs = { :test => { :description => "Some" } }
    model = create_model do
      track_actions :test, :after_create, :keep_params => ["other_column.size", :another_column]
    end
    @controller = create_controller_for_model(model, :other_column => "foo", :another_column => 5)
    assert_difference 'ActionTracker::Record.count' do
      get :test
    end
		assert_equal 3, ActionTracker::Record.last.params["other_column_size"]
		assert_equal 5, ActionTracker::Record.last.params["another_column"]
  end

  def test_track_actions_store_all_params
    ActionTrackerConfig.verbs = { :test => { :description => "Some" } }
    model = create_model do
      track_actions :test, :after_create, :keep_params => :all
    end
    @controller = create_controller_for_model(model, :other_column => "foo", :another_column => 2)
    assert_difference 'ActionTracker::Record.count' do
      get :test
    end
		assert_equal "foo", ActionTracker::Record.last.params["other_column"]
		assert_equal 2, ActionTracker::Record.last.params["another_column"]
  end

  def test_track_actions_store_all_params_by_default
    ActionTrackerConfig.verbs = { :test => { :description => "Some" } }
    model = create_model do
      track_actions :test, :after_create
    end
    @controller = create_controller_for_model(model, :other_column => "foo", :another_column => 2)
    assert_difference 'ActionTracker::Record.count' do
      get :test
    end
		assert_equal "foo", ActionTracker::Record.last.params["other_column"]
		assert_equal 2, ActionTracker::Record.last.params["another_column"]
  end

  def test_track_actions_store_no_params
    ActionTrackerConfig.verbs = { :test => { :description => "Some" } }
    model = create_model do
      track_actions :test, :after_create, :keep_params => :none
    end
    @controller = create_controller_for_model(model, :other_column => "foo", :another_column => 2)
    assert_difference 'ActionTracker::Record.count' do
      get :test
    end
		assert_nil ActionTracker::Record.last.params["other_column"]
		assert_nil ActionTracker::Record.last.params["another_column"]
  end

  def test_track_actions_with_options
    ActionTrackerConfig.verbs = { :test => { :description => "Some" } }
    model = create_model { track_actions :test, :after_create, :keep_params => :all, :if => Proc.new { 2 > 1 } }
    @controller = create_controller_for_model(model)
    assert_difference('ActionTracker::Record.count') { get :test }

    model = create_model { track_actions :test, :after_create, :keep_params => :all, :if => Proc.new { 2 < 1 } }
    @controller = create_controller_for_model(model)
    assert_no_difference('ActionTracker::Record.count') { get :test }

    model = create_model { track_actions :test, :after_create, :keep_params => :all, :unless => Proc.new { 2 > 1 } }
    @controller = create_controller_for_model(model)
    assert_no_difference('ActionTracker::Record.count') { get :test }

    model = create_model { track_actions :test, :after_create, :keep_params => :all, :unless => Proc.new { 2 < 1 } }
    @controller = create_controller_for_model(model)
    assert_difference('ActionTracker::Record.count') { get :test }
  end

  def test_track_actions_post_processing_as_symbol
    ActionTrackerConfig.verbs = { :test => { :description => "Some" } }
    model = create_model do
      track_actions :test, :after_create, :post_processing => Proc.new { |ta| OtherModel.create!(:other_column => ta.verb) }
    end
    @controller = create_controller_for_model(model, :another_column => 2)
    assert_difference 'ActionTracker::Record.count' do
      assert_difference('OtherModel.count', 2) do
        get :test
      end
    end
		assert_equal "test", OtherModel.last.other_column
  end

  def test_track_actions_post_processing_as_string
    ActionTrackerConfig.verbs = { :test => { :description => "Some" } }
    model = create_model do
      track_actions :test, :after_create, "post_processing" => Proc.new { |ta| OtherModel.create!(:other_column => ta.verb) }
    end
    @controller = create_controller_for_model(model, :another_column => 2)
    assert_difference 'ActionTracker::Record.count' do
      assert_difference('OtherModel.count', 2) do
        get :test
      end
    end
    assert_equal "test", OtherModel.last.other_column
  end

  def test_track_actions_custom_user_as_symbol
    ActionTrackerConfig.verbs = { :test => { :description => "Some" } }
    model = create_model do
      track_actions :test, :after_create, :custom_user => :test_custom_user
      def test_custom_user
        OtherModel.create!
      end
    end
    ActionTrackerConfig.current_user = proc{ SomeModel.create! }
    @controller = create_controller_for_model(model, :another_column => 2)
    assert_difference('ActionTracker::Record.count') { get :test }
		assert_kind_of OtherModel, ActionTracker::Record.last.user
  end

  def test_track_actions_custom_user_as_string
    ActionTrackerConfig.verbs = { :test => { :description => "Some" } }
    model = create_model do
      track_actions :test, :after_create, "custom_user" => :test_custom_user
      def test_custom_user
        OtherModel.create!
      end
    end
    ActionTrackerConfig.current_user = proc{ SomeModel.create! }
    @controller = create_controller_for_model(model, :another_column => 2)
    assert_difference('ActionTracker::Record.count') { get :test }
		assert_kind_of OtherModel, ActionTracker::Record.last.user
  end

  def test_track_actions_custom_user_is_nil_by_default
    ActionTrackerConfig.verbs = { :test => { :description => "Some" } }
    model = create_model do
      track_actions :test, :after_create
      def test_custom_user
        OtherModel.create!
      end
    end
    ActionTrackerConfig.current_user = proc{ SomeModel.create! }
    @controller = create_controller_for_model(model, :another_column => 2)
    assert_difference('ActionTracker::Record.count') { get :test }
		assert_kind_of SomeModel, ActionTracker::Record.last.user
  end

  def test_track_actions_custom_target_as_symbol
    ActionTrackerConfig.verbs = { :test => { :description => "Some" } }
    model = create_model do
      track_actions :test, :after_create, :custom_target => :test_custom_target
      def test_custom_target
        SomeModel.create!
      end
    end
    @controller = create_controller_for_model(model, :another_column => 2)
    assert_difference('ActionTracker::Record.count') { get :test }
		assert_kind_of SomeModel, ActionTracker::Record.last.target
  end

  def test_track_actions_custom_target_as_string
    ActionTrackerConfig.verbs = { :test => { :description => "Some" } }
    model = create_model do
      track_actions :test, :after_create, "custom_target" => :test_custom_target
      def test_custom_target
        SomeModel.create!
      end
    end
    @controller = create_controller_for_model(model, :another_column => 2)
    assert_difference('ActionTracker::Record.count') { get :test }
		assert_kind_of SomeModel, ActionTracker::Record.last.target
  end

  def test_acts_as_trackable_with_options
    ActionTrackerConfig.verbs = { :test => { :description => "Some" } }
    @@action = create_model do
      track_actions :test, :after_create
    end
    @@user = create_model do
      acts_as_trackable :after_add => Proc.new { |x, y| raise 'I was called' }
    end
    @controller = create_controller do
      def test
        @@action.create!
        render :text => "test"
      end
			def current_user
				@@user.create!
			end
		end
    assert_raise(RuntimeError, 'I was called') do
      get :test
    end
  end

  def test_track_actions_save_target
    ActionTrackerConfig.verbs = { :test => { :description => "Some" } }
    model = create_model do
      track_actions :test, :after_create
    end
    @controller = create_controller_for_model(model)
    assert_difference 'ActionTracker::Record.count' do
      get :test
    end
    assert_kind_of model.base_class, ActionTracker::Record.last.target
  end

  private

	def create_controller(&block)
    klass = Class.new(ThingsController)
    klass.module_eval &block
    klass.stubs(:controller_path).returns('things')
    klass.new
  end

  def create_model(&block)
    klass = Class.new(OtherModel)
    klass.module_eval &block
    klass
  end

  def create_controller_for_model(model, attributes = {})
    @@model, @@attributes = model, attributes
    create_controller do
      def test
        @@model.create! @@attributes
        render :text => "test"
      end

		end
    ActionTrackerConfig.current_user = proc{ SomeModel.create! :some_column => "test" }
  end

end