2017-12-10 23:05:11 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-06-10 01:28:48 -04:00
|
|
|
require "spec_helper"
|
|
|
|
|
|
|
|
RSpec.describe WidgetsController, type: :controller, versioning: true do
|
|
|
|
before { request.env["REMOTE_ADDR"] = "127.0.0.1" }
|
2018-08-14 01:29:08 -04:00
|
|
|
|
2017-06-10 01:28:48 -04:00
|
|
|
after { RequestStore.store[:paper_trail] = nil }
|
|
|
|
|
|
|
|
describe "#create" do
|
|
|
|
context "PT enabled" do
|
|
|
|
it "stores information like IP address in version" do
|
2020-03-02 21:58:33 -05:00
|
|
|
post(:create, params: { widget: { name: "Flugel" } })
|
2017-06-10 01:28:48 -04:00
|
|
|
widget = assigns(:widget)
|
|
|
|
expect(widget.versions.length).to(eq(1))
|
|
|
|
expect(widget.versions.last.whodunnit.to_i).to(eq(153))
|
|
|
|
expect(widget.versions.last.ip).to(eq("127.0.0.1"))
|
|
|
|
expect(widget.versions.last.user_agent).to(eq("Rails Testing"))
|
|
|
|
end
|
|
|
|
|
|
|
|
it "controller metadata methods should get evaluated" do
|
|
|
|
request.env["HTTP_USER_AGENT"] = "User-Agent"
|
2020-03-02 21:58:33 -05:00
|
|
|
post :create, params: { widget: { name: "Flugel" } }
|
2018-03-23 21:58:45 -04:00
|
|
|
expect(PaperTrail.request.enabled?).to eq(true)
|
2018-02-01 12:04:50 -05:00
|
|
|
expect(PaperTrail.request.whodunnit).to(eq(153))
|
|
|
|
expect(PaperTrail.request.controller_info.present?).to(eq(true))
|
2018-05-14 21:54:12 -04:00
|
|
|
expect(PaperTrail.request.controller_info.key?(:ip)).to(eq(true))
|
|
|
|
expect(PaperTrail.request.controller_info.key?(:user_agent)).to(eq(true))
|
2017-06-10 01:28:48 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "PT disabled" do
|
|
|
|
it "does not save a version, and metadata is not set" do
|
|
|
|
request.env["HTTP_USER_AGENT"] = "Disable User-Agent"
|
2020-03-02 21:58:33 -05:00
|
|
|
post :create, params: { widget: { name: "Flugel" } }
|
2017-06-10 01:28:48 -04:00
|
|
|
expect(assigns(:widget).versions.length).to(eq(0))
|
2018-03-23 21:58:45 -04:00
|
|
|
expect(PaperTrail.request.enabled?).to eq(false)
|
2018-02-01 12:04:50 -05:00
|
|
|
expect(PaperTrail.request.whodunnit).to be_nil
|
|
|
|
expect(PaperTrail.request.controller_info).to eq({})
|
2017-06-10 01:28:48 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "#destroy" do
|
|
|
|
it "can be disabled" do
|
|
|
|
request.env["HTTP_USER_AGENT"] = "Disable User-Agent"
|
2020-03-02 21:58:33 -05:00
|
|
|
post(:create, params: { widget: { name: "Flugel" } })
|
2017-06-10 01:28:48 -04:00
|
|
|
w = assigns(:widget)
|
|
|
|
expect(w.versions.length).to(eq(0))
|
2020-03-02 21:58:33 -05:00
|
|
|
delete(:destroy, params: { id: w.id })
|
2017-06-10 01:28:48 -04:00
|
|
|
expect(PaperTrail::Version.with_item_keys("Widget", w.id).size).to(eq(0))
|
|
|
|
end
|
|
|
|
|
|
|
|
it "stores information like IP address in version" do
|
|
|
|
w = Widget.create(name: "Roundel")
|
|
|
|
expect(w.versions.length).to(eq(1))
|
2020-03-02 21:58:33 -05:00
|
|
|
delete(:destroy, params: { id: w.id })
|
2017-06-10 01:28:48 -04:00
|
|
|
widget = assigns(:widget)
|
|
|
|
expect(widget.versions.length).to(eq(2))
|
|
|
|
expect(widget.versions.last.ip).to(eq("127.0.0.1"))
|
|
|
|
expect(widget.versions.last.user_agent).to(eq("Rails Testing"))
|
|
|
|
expect(widget.versions.last.whodunnit.to_i).to(eq(153))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "#update" do
|
|
|
|
it "stores information like IP address in version" do
|
|
|
|
w = Widget.create(name: "Duvel")
|
|
|
|
expect(w.versions.length).to(eq(1))
|
2020-03-02 21:58:33 -05:00
|
|
|
put(:update, params: { id: w.id, widget: { name: "Bugle" } })
|
2017-06-10 01:28:48 -04:00
|
|
|
widget = assigns(:widget)
|
|
|
|
expect(widget.versions.length).to(eq(2))
|
|
|
|
expect(widget.versions.last.whodunnit.to_i).to(eq(153))
|
|
|
|
expect(widget.versions.last.ip).to(eq("127.0.0.1"))
|
|
|
|
expect(widget.versions.last.user_agent).to(eq("Rails Testing"))
|
|
|
|
end
|
|
|
|
|
|
|
|
it "can be disabled" do
|
|
|
|
request.env["HTTP_USER_AGENT"] = "Disable User-Agent"
|
2020-03-02 21:58:33 -05:00
|
|
|
post(:create, params: { widget: { name: "Flugel" } })
|
2017-06-10 01:28:48 -04:00
|
|
|
w = assigns(:widget)
|
|
|
|
expect(w.versions.length).to(eq(0))
|
2020-03-02 21:58:33 -05:00
|
|
|
put(:update, params: { id: w.id, widget: { name: "Bugle" } })
|
2017-06-10 01:28:48 -04:00
|
|
|
widget = assigns(:widget)
|
|
|
|
expect(widget.versions.length).to(eq(0))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|