2017-12-11 04:05:11 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-05-30 04:58:26 +00:00
|
|
|
require "spec_helper"
|
2013-08-27 20:34:16 +00:00
|
|
|
|
2017-04-01 04:51:21 +00:00
|
|
|
RSpec.describe PaperTrail do
|
2017-10-17 22:34:44 +00:00
|
|
|
describe "#config", versioning: true do
|
|
|
|
it "allows for config values to be set" do
|
2017-10-27 16:07:47 +00:00
|
|
|
expect(described_class.config.enabled).to eq(true)
|
|
|
|
described_class.config.enabled = false
|
|
|
|
expect(described_class.config.enabled).to eq(false)
|
2017-10-17 22:34:44 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it "accepts blocks and yield the config instance" do
|
2017-10-27 16:07:47 +00:00
|
|
|
expect(described_class.config.enabled).to eq(true)
|
|
|
|
described_class.config { |c| c.enabled = false }
|
|
|
|
expect(described_class.config.enabled).to eq(false)
|
2017-10-17 22:34:44 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "#configure" do
|
|
|
|
it "is an alias for the `config` method" do
|
2017-10-27 16:07:47 +00:00
|
|
|
expect(described_class.method(:configure)).to eq(
|
|
|
|
described_class.method(:config)
|
|
|
|
)
|
2017-10-17 22:34:44 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-04-01 05:19:54 +00:00
|
|
|
describe ".gem_version" do
|
|
|
|
it "returns a Gem::Version" do
|
|
|
|
v = described_class.gem_version
|
|
|
|
expect(v).to be_a(::Gem::Version)
|
|
|
|
expect(v.to_s).to eq(::PaperTrail::VERSION::STRING)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-08-22 04:27:16 +00:00
|
|
|
context "when enabled" do
|
|
|
|
it "affects all threads" do
|
2017-04-01 04:51:21 +00:00
|
|
|
Thread.new { described_class.enabled = false }.join
|
|
|
|
assert_equal false, described_class.enabled?
|
2016-08-22 04:27:16 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
after do
|
2017-04-01 04:51:21 +00:00
|
|
|
described_class.enabled = true
|
2016-08-22 04:27:16 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-03-05 22:07:32 +00:00
|
|
|
context "default" do
|
2017-04-01 05:50:13 +00:00
|
|
|
it "has versioning off by default" do
|
2017-04-01 04:51:21 +00:00
|
|
|
expect(described_class).not_to be_enabled
|
2013-08-27 20:34:16 +00:00
|
|
|
end
|
2015-12-19 23:47:13 +00:00
|
|
|
|
2017-04-01 05:50:13 +00:00
|
|
|
it "has versioning on in a `with_versioning` block" do
|
2017-04-01 04:51:21 +00:00
|
|
|
expect(described_class).not_to be_enabled
|
2013-10-29 16:07:54 +00:00
|
|
|
with_versioning do
|
2017-04-01 04:51:21 +00:00
|
|
|
expect(described_class).to be_enabled
|
2013-08-27 20:34:16 +00:00
|
|
|
end
|
2017-04-01 04:51:21 +00:00
|
|
|
expect(described_class).not_to be_enabled
|
2013-10-29 16:07:54 +00:00
|
|
|
end
|
2014-03-12 14:09:59 +00:00
|
|
|
|
|
|
|
context "error within `with_versioning` block" do
|
2017-04-01 05:50:13 +00:00
|
|
|
it "reverts the value of `PaperTrail.enabled?` to its previous state" do
|
2017-04-01 04:51:21 +00:00
|
|
|
expect(described_class).not_to be_enabled
|
2015-12-19 23:47:13 +00:00
|
|
|
expect { with_versioning { raise } }.to raise_error(RuntimeError)
|
2017-04-01 04:51:21 +00:00
|
|
|
expect(described_class).not_to be_enabled
|
2014-03-12 14:09:59 +00:00
|
|
|
end
|
|
|
|
end
|
2013-10-29 16:07:54 +00:00
|
|
|
end
|
|
|
|
|
2016-03-05 22:07:32 +00:00
|
|
|
context "`versioning: true`", versioning: true do
|
2017-04-01 05:50:13 +00:00
|
|
|
it "has versioning on by default" do
|
2017-04-01 04:51:21 +00:00
|
|
|
expect(described_class).to be_enabled
|
2013-10-29 16:07:54 +00:00
|
|
|
end
|
2015-12-19 23:47:13 +00:00
|
|
|
|
2017-04-01 05:50:13 +00:00
|
|
|
it "keeps versioning on after a with_versioning block" do
|
2017-04-01 04:51:21 +00:00
|
|
|
expect(described_class).to be_enabled
|
2013-10-29 16:07:54 +00:00
|
|
|
with_versioning do
|
2017-04-01 04:51:21 +00:00
|
|
|
expect(described_class).to be_enabled
|
2013-08-27 20:34:16 +00:00
|
|
|
end
|
2017-04-01 04:51:21 +00:00
|
|
|
expect(described_class).to be_enabled
|
2013-08-27 20:34:16 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-03-05 22:07:32 +00:00
|
|
|
context "`with_versioning` block at class level" do
|
2017-04-01 04:51:21 +00:00
|
|
|
it { expect(described_class).not_to be_enabled }
|
2014-03-11 14:48:06 +00:00
|
|
|
|
|
|
|
with_versioning do
|
2017-04-01 05:50:13 +00:00
|
|
|
it "has versioning on by default" do
|
2017-04-01 04:51:21 +00:00
|
|
|
expect(described_class).to be_enabled
|
2014-03-11 14:48:06 +00:00
|
|
|
end
|
|
|
|
end
|
2017-04-01 05:50:13 +00:00
|
|
|
it "does not leak the `enabled?` state into successive tests" do
|
2017-04-01 04:51:21 +00:00
|
|
|
expect(described_class).not_to be_enabled
|
2014-03-11 14:48:06 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-04-01 01:42:14 +00:00
|
|
|
describe ".version" do
|
2018-02-01 17:04:50 +00:00
|
|
|
it "returns the expected String" do
|
|
|
|
expect(described_class.version).to eq(described_class::VERSION::STRING)
|
2017-07-08 20:39:04 +00:00
|
|
|
end
|
2013-08-27 20:34:16 +00:00
|
|
|
end
|
2018-02-26 02:40:46 +00:00
|
|
|
|
|
|
|
describe "deprecated methods" do
|
|
|
|
before do
|
|
|
|
allow(ActiveSupport::Deprecation).to receive(:warn)
|
|
|
|
end
|
|
|
|
|
|
|
|
shared_examples "it delegates to request" do |method, args|
|
|
|
|
it do
|
|
|
|
arguments = args || [no_args]
|
|
|
|
allow(described_class.request).to receive(method)
|
|
|
|
described_class.public_send(method, *args)
|
|
|
|
expect(described_class.request).to have_received(method).with(*arguments)
|
|
|
|
expect(ActiveSupport::Deprecation).to have_received(:warn)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it_behaves_like "it delegates to request", :clear_transaction_id, nil
|
|
|
|
it_behaves_like "it delegates to request", :enabled_for_controller=, [true]
|
|
|
|
it_behaves_like "it delegates to request", :enabled_for_model, [Widget, true]
|
|
|
|
it_behaves_like "it delegates to request", :enabled_for_model?, [Widget]
|
|
|
|
it_behaves_like "it delegates to request", :whodunnit=, [:some_whodunnit]
|
|
|
|
it_behaves_like "it delegates to request", :whodunnit, nil
|
|
|
|
it_behaves_like "it delegates to request", :controller_info=, [:some_whodunnit]
|
|
|
|
it_behaves_like "it delegates to request", :controller_info, nil
|
|
|
|
it_behaves_like "it delegates to request", :transaction_id=, 123
|
|
|
|
it_behaves_like "it delegates to request", :transaction_id, nil
|
|
|
|
|
|
|
|
describe "whodunnit with block" do
|
|
|
|
it "delegates to request" do
|
|
|
|
allow(described_class.request).to receive(:with)
|
|
|
|
described_class.whodunnit(:some_whodunnit) { :some_block }
|
|
|
|
expect(ActiveSupport::Deprecation).to have_received(:warn)
|
|
|
|
expect(described_class.request).to have_received(:with) do |*args, &block|
|
|
|
|
expect(args).to eq([{ whodunnit: :some_whodunnit }])
|
|
|
|
expect(block.call).to eq :some_block
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "whodunnit with invalid arguments" do
|
|
|
|
it "raises an error" do
|
|
|
|
expect { described_class.whodunnit(:some_whodunnit) }.to raise_error(ArgumentError) do |e|
|
|
|
|
expect(e.message).to eq "Invalid arguments"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2013-08-27 20:34:16 +00:00
|
|
|
end
|