2015-11-18 04:59:35 +00:00
|
|
|
require 'rails_helper'
|
2015-05-07 02:29:39 +00:00
|
|
|
|
2015-11-18 04:59:35 +00:00
|
|
|
# The `json_versions` table tests postgres' `json` data type. So, that
|
|
|
|
# table is only created when testing against postgres and ActiveRecord >= 4.
|
|
|
|
if JsonVersion.table_exists?
|
2015-05-07 18:56:28 +00:00
|
|
|
|
|
|
|
describe JsonVersion, :type => :model do
|
|
|
|
it "should include the `VersionConcern` module to get base functionality" do
|
|
|
|
expect(JsonVersion).to include(PaperTrail::VersionConcern)
|
|
|
|
end
|
2015-05-07 02:29:39 +00:00
|
|
|
|
|
|
|
describe "Methods" do
|
|
|
|
describe "Class" do
|
|
|
|
describe '#where_object' do
|
|
|
|
it { expect(JsonVersion).to respond_to(:where_object) }
|
|
|
|
|
2016-01-18 19:21:27 +00:00
|
|
|
it "escapes values" do
|
|
|
|
f = Fruit.create(:name => 'Bobby')
|
|
|
|
expect(
|
|
|
|
f.
|
|
|
|
versions.
|
|
|
|
where_object(:name => "Robert'; DROP TABLE Students;--").
|
|
|
|
count
|
|
|
|
).to eq(0)
|
|
|
|
end
|
|
|
|
|
2015-05-07 02:29:39 +00:00
|
|
|
context "invalid arguments" do
|
|
|
|
it "should raise an error" do
|
|
|
|
expect { JsonVersion.where_object(:foo) }.to raise_error(ArgumentError)
|
|
|
|
expect { JsonVersion.where_object([]) }.to raise_error(ArgumentError)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "valid arguments", :versioning => true do
|
|
|
|
let(:fruit_names) { %w(apple orange lemon banana lime coconut strawberry blueberry) }
|
|
|
|
let(:fruit) { Fruit.new }
|
|
|
|
let(:name) { 'pomegranate' }
|
2015-12-20 00:01:58 +00:00
|
|
|
let(:color) { FFaker::Color.name }
|
2015-05-07 02:29:39 +00:00
|
|
|
|
|
|
|
before do
|
|
|
|
fruit.update_attributes!(:name => name)
|
|
|
|
fruit.update_attributes!(:name => fruit_names.sample, :color => color)
|
2015-12-20 00:01:58 +00:00
|
|
|
fruit.update_attributes!(:name => fruit_names.sample, :color => FFaker::Color.name)
|
2015-05-07 02:29:39 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it "should be able to locate versions according to their `object` contents" do
|
|
|
|
expect(JsonVersion.where_object(:name => name)).to eq([fruit.versions[1]])
|
|
|
|
expect(JsonVersion.where_object(:color => color)).to eq([fruit.versions[2]])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#where_object_changes' do
|
|
|
|
it { expect(JsonVersion).to respond_to(:where_object_changes) }
|
|
|
|
|
2016-01-18 19:21:27 +00:00
|
|
|
it "escapes values" do
|
|
|
|
f = Fruit.create(:name => 'Bobby')
|
|
|
|
expect(
|
|
|
|
f.
|
|
|
|
versions.
|
|
|
|
where_object_changes(:name => "Robert'; DROP TABLE Students;--").
|
|
|
|
count
|
|
|
|
).to eq(0)
|
|
|
|
end
|
|
|
|
|
2015-05-07 02:29:39 +00:00
|
|
|
context "invalid arguments" do
|
|
|
|
it "should raise an error" do
|
|
|
|
expect { JsonVersion.where_object_changes(:foo) }.to raise_error(ArgumentError)
|
|
|
|
expect { JsonVersion.where_object_changes([]) }.to raise_error(ArgumentError)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "valid arguments", :versioning => true do
|
2015-12-20 00:58:49 +00:00
|
|
|
let(:color) { %w[red green] }
|
2015-12-20 02:16:25 +00:00
|
|
|
let(:fruit) { Fruit.create!(name: name[0]) }
|
|
|
|
let(:name) { %w[banana kiwi mango] }
|
2015-05-07 02:29:39 +00:00
|
|
|
|
|
|
|
before do
|
2015-12-20 02:16:25 +00:00
|
|
|
fruit.update_attributes!(name: name[1], color: color[0])
|
|
|
|
fruit.update_attributes!(name: name[2], color: color[1])
|
2015-05-07 02:29:39 +00:00
|
|
|
end
|
|
|
|
|
2015-12-20 00:58:49 +00:00
|
|
|
it "finds versions according to their `object_changes` contents" do
|
|
|
|
expect(
|
2015-12-20 02:16:25 +00:00
|
|
|
fruit.versions.where_object_changes(name: name[0])
|
2015-12-20 00:58:49 +00:00
|
|
|
).to match_array(fruit.versions[0..1])
|
|
|
|
expect(
|
|
|
|
fruit.versions.where_object_changes(color: color[0])
|
|
|
|
).to match_array(fruit.versions[1..2])
|
2015-05-07 02:29:39 +00:00
|
|
|
end
|
|
|
|
|
2015-12-20 00:58:49 +00:00
|
|
|
it "finds versions with multiple attributes changed" do
|
|
|
|
expect(
|
2015-12-20 02:16:25 +00:00
|
|
|
fruit.versions.where_object_changes(color: color[0], name: name[0])
|
2015-12-20 00:58:49 +00:00
|
|
|
).to match_array([fruit.versions[1]])
|
2015-05-07 02:29:39 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|