2016-03-05 17:07:32 -05:00
|
|
|
require "test_helper"
|
2013-08-13 13:12:47 -04:00
|
|
|
|
|
|
|
class ControllerTest < ActionController::TestCase
|
|
|
|
tests WidgetsController
|
|
|
|
|
|
|
|
setup do
|
2016-03-05 17:07:32 -05:00
|
|
|
@request.env["REMOTE_ADDR"] = "127.0.0.1"
|
2013-08-13 13:12:47 -04:00
|
|
|
end
|
|
|
|
|
2015-02-03 16:28:43 -05:00
|
|
|
# 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
|
2015-02-03 16:28:43 -05:00
|
|
|
RequestStore.store[:paper_trail] = nil
|
2013-08-13 13:12:47 -04:00
|
|
|
end
|
|
|
|
|
2016-03-05 17:07:32 -05:00
|
|
|
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
|
|
|
|
|
2016-03-05 17:07:32 -05:00
|
|
|
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
|
2016-03-05 17:07:32 -05:00
|
|
|
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
|
|
|
|
|
2016-03-05 17:07:32 -05:00
|
|
|
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)
|
2016-03-05 17:07:32 -05:00
|
|
|
assert_equal 0, PaperTrail::Version.with_item_keys("Widget", w.id).size
|
2013-08-13 13:12:47 -04:00
|
|
|
end
|
|
|
|
|
2016-03-05 17:07:32 -05:00
|
|
|
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
|
2016-03-05 17:07:32 -05:00
|
|
|
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
|
|
|
|
|
2016-03-05 17:07:32 -05:00
|
|
|
test "update" do
|
|
|
|
w = Widget.create name: "Duvel"
|
2013-08-13 13:12:47 -04:00
|
|
|
assert_equal 1, w.versions.length
|
2016-03-05 17:07:32 -05:00
|
|
|
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
|
2016-03-05 17:07:32 -05:00
|
|
|
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
|
|
|
|
|
2016-03-05 17:07:32 -05:00
|
|
|
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)
|
2014-03-15 13:50:19 -04:00
|
|
|
assert_equal 2, widget.versions.length
|
2016-03-05 17:07:32 -05:00
|
|
|
assert_equal "127.0.0.1", widget.versions.last.ip
|
|
|
|
assert_equal "Rails Testing", widget.versions.last.user_agent
|
2014-03-15 13:50:19 -04:00
|
|
|
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
|
2016-03-05 17:07:32 -05:00
|
|
|
@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
|
2016-03-05 17:07:32 -05:00
|
|
|
@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
|
2015-02-03 16:28:43 -05:00
|
|
|
assert !PaperTrail.enabled_for_controller?
|
2013-08-13 13:12:47 -04:00
|
|
|
assert PaperTrail.whodunnit.nil?
|
|
|
|
assert PaperTrail.controller_info.nil?
|
|
|
|
end
|
|
|
|
end
|