1
0
Fork 0
mirror of https://github.com/paper-trail-gem/paper_trail.git synced 2022-11-09 11:33:19 -05:00
paper-trail-gem--paper_trail/test/functional/controller_test.rb

91 lines
3.3 KiB
Ruby
Raw Normal View History

require "test_helper"
2013-08-13 13:12:47 -04:00
class ControllerTest < ActionController::TestCase
tests WidgetsController
setup do
@request.env["REMOTE_ADDR"] = "127.0.0.1"
2013-08-13 13:12:47 -04:00
end
# Mimick what RequestStore will do outside of the test env, since it is
# middleware, and doesn't get executed in controller / request specs
2013-08-13 13:12:47 -04:00
teardown do
RequestStore.store[:paper_trail] = nil
2013-08-13 13:12:47 -04:00
end
test "disable on create" do
@request.env["HTTP_USER_AGENT"] = "Disable User-Agent"
post :create, params_wrapper(widget: { name: "Flugel" })
2013-08-13 13:12:47 -04:00
assert_equal 0, assigns(:widget).versions.length
end
test "disable on update" do
@request.env["HTTP_USER_AGENT"] = "Disable User-Agent"
post :create, params_wrapper(widget: { name: "Flugel" })
2013-08-13 13:12:47 -04:00
w = assigns(:widget)
assert_equal 0, w.versions.length
put :update, params_wrapper(id: w.id, widget: { name: "Bugle" })
2013-08-13 13:12:47 -04:00
widget = assigns(:widget)
assert_equal 0, widget.versions.length
end
test "disable on destroy" do
@request.env["HTTP_USER_AGENT"] = "Disable User-Agent"
post :create, params_wrapper(widget: { name: "Flugel" })
2013-08-13 13:12:47 -04:00
w = assigns(:widget)
assert_equal 0, w.versions.length
2016-02-15 21:44:10 -05:00
delete :destroy, params_wrapper(id: w.id)
assert_equal 0, PaperTrail::Version.with_item_keys("Widget", w.id).size
2013-08-13 13:12:47 -04:00
end
test "create" do
post :create, params_wrapper(widget: { name: "Flugel" })
2013-08-13 13:12:47 -04:00
widget = assigns(:widget)
assert_equal 1, widget.versions.length
assert_equal 153, widget.versions.last.whodunnit.to_i
assert_equal "127.0.0.1", widget.versions.last.ip
assert_equal "Rails Testing", widget.versions.last.user_agent
2013-08-13 13:12:47 -04:00
end
test "update" do
w = Widget.create name: "Duvel"
2013-08-13 13:12:47 -04:00
assert_equal 1, w.versions.length
put :update, params_wrapper(id: w.id, widget: { name: "Bugle" })
2013-08-13 13:12:47 -04:00
widget = assigns(:widget)
assert_equal 2, widget.versions.length
assert_equal 153, widget.versions.last.whodunnit.to_i
assert_equal "127.0.0.1", widget.versions.last.ip
assert_equal "Rails Testing", widget.versions.last.user_agent
2013-08-13 13:12:47 -04:00
end
test "destroy" do
w = Widget.create name: "Roundel"
2013-08-13 13:12:47 -04:00
assert_equal 1, w.versions.length
2016-02-15 21:44:10 -05:00
delete :destroy, params_wrapper(id: w.id)
2013-08-13 13:12:47 -04:00
widget = assigns(:widget)
assert_equal 2, widget.versions.length
assert_equal "127.0.0.1", widget.versions.last.ip
assert_equal "Rails Testing", widget.versions.last.user_agent
assert_equal 153, widget.versions.last.whodunnit.to_i
2013-08-13 13:12:47 -04:00
end
2016-02-15 18:13:45 -05:00
test "controller metadata methods should get evaluated if PT enabled for controller" do
@request.env["HTTP_USER_AGENT"] = "User-Agent"
post :create, params_wrapper(widget: { name: "Flugel" })
2013-08-13 13:12:47 -04:00
assert PaperTrail.enabled_for_controller?
assert_equal 153, PaperTrail.whodunnit
assert PaperTrail.controller_info.present?
assert PaperTrail.controller_info.keys.include?(:ip)
assert PaperTrail.controller_info.keys.include?(:user_agent)
end
2016-02-15 18:13:45 -05:00
test "controller metadata methods should not get evaluated if PT disabled for controller" do
@request.env["HTTP_USER_AGENT"] = "Disable User-Agent"
post :create, params_wrapper(widget: { name: "Flugel" })
2013-08-13 13:12:47 -04:00
assert_equal 0, assigns(:widget).versions.length
assert !PaperTrail.enabled_for_controller?
2013-08-13 13:12:47 -04:00
assert PaperTrail.whodunnit.nil?
assert PaperTrail.controller_info.nil?
end
end