2012-03-12 23:09:21 +00:00
|
|
|
require 'spec_helper'
|
2011-11-10 13:16:57 +00:00
|
|
|
|
|
|
|
# we use the posts controller as it uses the impressionsist module. any such controller would do.
|
|
|
|
describe DummyController do
|
|
|
|
before do
|
2011-11-27 19:39:29 +00:00
|
|
|
@impression_count = Impression.all.size
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "impressionist filter uniqueness" do
|
2020-07-20 13:35:41 +00:00
|
|
|
it "ignore uniqueness if not requested" do
|
2014-02-22 00:00:15 +00:00
|
|
|
controller.impressionist_subapp_filter
|
|
|
|
controller.impressionist_subapp_filter
|
2020-07-20 13:35:41 +00:00
|
|
|
|
|
|
|
expect(Impression.count).to eq(@impression_count + 2)
|
2011-11-10 13:16:57 +00:00
|
|
|
end
|
2011-11-27 19:39:29 +00:00
|
|
|
|
2020-07-20 13:35:41 +00:00
|
|
|
it "recognize unique session" do
|
|
|
|
allow(controller).to receive(:session_hash).and_return(request.session_options[:id])
|
|
|
|
|
2014-02-22 00:00:15 +00:00
|
|
|
controller.impressionist_subapp_filter(unique: [:session_hash])
|
|
|
|
controller.impressionist_subapp_filter(unique: [:session_hash])
|
2020-07-20 13:35:41 +00:00
|
|
|
|
|
|
|
expect(Impression.count).to eq(@impression_count + 1)
|
2011-11-10 13:16:57 +00:00
|
|
|
end
|
2011-11-27 19:39:29 +00:00
|
|
|
|
2020-07-20 13:35:41 +00:00
|
|
|
it "recognize unique ip" do
|
|
|
|
allow(controller).to receive(:remote_ip).and_return("1.2.3.4")
|
|
|
|
|
2014-02-22 00:00:15 +00:00
|
|
|
controller.impressionist_subapp_filter(unique: [:ip_address])
|
|
|
|
controller.impressionist_subapp_filter(unique: [:ip_address])
|
2020-07-20 13:35:41 +00:00
|
|
|
|
|
|
|
expect(Impression.count).to equal(@impression_count + 1)
|
2011-11-10 13:16:57 +00:00
|
|
|
end
|
2011-11-27 19:39:29 +00:00
|
|
|
|
2020-07-20 13:35:41 +00:00
|
|
|
it "recognize unique request" do
|
2014-02-22 00:00:15 +00:00
|
|
|
controller.impressionist_subapp_filter(unique: [:request_hash])
|
|
|
|
controller.impressionist_subapp_filter(unique: [:request_hash])
|
2020-07-20 13:35:41 +00:00
|
|
|
|
|
|
|
expect(Impression.count).to equal(@impression_count + 1)
|
2011-11-10 13:16:57 +00:00
|
|
|
end
|
2011-11-27 19:39:29 +00:00
|
|
|
|
2020-07-20 13:35:41 +00:00
|
|
|
it "recognize unique action" do
|
|
|
|
allow(controller).to receive(:action_name).and_return("test_action")
|
|
|
|
|
2014-02-22 00:00:15 +00:00
|
|
|
controller.impressionist_subapp_filter(unique: [:action_name])
|
|
|
|
controller.impressionist_subapp_filter(unique: [:action_name])
|
2020-07-20 13:35:41 +00:00
|
|
|
|
|
|
|
expect(Impression.count).to equal(@impression_count + 1)
|
2011-11-10 13:16:57 +00:00
|
|
|
end
|
2011-11-27 19:39:29 +00:00
|
|
|
|
2020-07-20 13:35:41 +00:00
|
|
|
it "recognize unique controller" do
|
|
|
|
allow(controller).to receive(:controller_name).and_return("post")
|
|
|
|
|
2014-02-22 00:00:15 +00:00
|
|
|
controller.impressionist_subapp_filter(unique: [:controller_name])
|
|
|
|
controller.impressionist_subapp_filter(unique: [:controller_name])
|
2020-07-20 13:35:41 +00:00
|
|
|
expect(Impression.count).to equal(@impression_count + 1)
|
2011-11-10 13:16:57 +00:00
|
|
|
end
|
2011-11-27 19:39:29 +00:00
|
|
|
|
2020-07-20 13:35:41 +00:00
|
|
|
it "recognize unique user" do
|
|
|
|
allow(controller).to receive(:user_id).and_return(42)
|
|
|
|
|
2014-02-22 00:00:15 +00:00
|
|
|
controller.impressionist_subapp_filter(unique: [:user_id])
|
|
|
|
controller.impressionist_subapp_filter(unique: [:user_id])
|
2020-07-20 13:35:41 +00:00
|
|
|
|
|
|
|
expect(Impression.count).to equal(@impression_count + 1)
|
2011-11-10 13:16:57 +00:00
|
|
|
end
|
2011-11-27 19:39:29 +00:00
|
|
|
|
2020-07-20 13:35:41 +00:00
|
|
|
it "recognize unique referer" do
|
|
|
|
allow(controller.request).to receive(:referer).and_return("http://foo/bar")
|
|
|
|
|
2014-02-22 00:00:15 +00:00
|
|
|
controller.impressionist_subapp_filter(unique: [:referrer])
|
|
|
|
controller.impressionist_subapp_filter(unique: [:referrer])
|
2020-07-20 13:35:41 +00:00
|
|
|
|
|
|
|
expect(Impression.count).to equal(@impression_count + 1)
|
2011-11-10 13:16:57 +00:00
|
|
|
end
|
2011-11-27 19:39:29 +00:00
|
|
|
|
2020-07-20 13:35:41 +00:00
|
|
|
it "recognize unique id" do
|
|
|
|
allow(controller).to receive(:params).and_return({ :id => "666" }) # for correct impressionable id in filter
|
|
|
|
|
2014-02-22 00:00:15 +00:00
|
|
|
controller.impressionist_subapp_filter(unique: [:impressionable_id])
|
|
|
|
controller.impressionist_subapp_filter(unique: [:impressionable_id])
|
2020-07-20 13:35:41 +00:00
|
|
|
expect(Impression.count).to equal(@impression_count + 1)
|
2011-11-10 13:16:57 +00:00
|
|
|
end
|
2011-11-27 19:39:29 +00:00
|
|
|
|
2011-11-10 13:16:57 +00:00
|
|
|
# extra redundant test for important controller and action combination.
|
2020-07-20 13:35:41 +00:00
|
|
|
it "recognize different controller and action" do
|
|
|
|
allow(controller).to receive(:controller_name).and_return("post")
|
|
|
|
allow(controller).to receive(:action_name).and_return("test_action")
|
|
|
|
|
2014-02-22 00:00:15 +00:00
|
|
|
controller.impressionist_subapp_filter(unique: [:controller_name, :action_name])
|
|
|
|
controller.impressionist_subapp_filter(unique: [:controller_name, :action_name])
|
2020-07-20 13:35:41 +00:00
|
|
|
|
|
|
|
expect(Impression.count).to equal(@impression_count + 1)
|
|
|
|
|
|
|
|
allow(controller).to receive(:action_name).and_return("another_action")
|
|
|
|
|
2014-02-22 00:00:15 +00:00
|
|
|
controller.impressionist_subapp_filter(unique: [:controller_name, :action_name])
|
|
|
|
controller.impressionist_subapp_filter(unique: [:controller_name, :action_name])
|
2020-07-20 13:35:41 +00:00
|
|
|
|
|
|
|
expect(Impression.count).to equal(@impression_count + 2)
|
|
|
|
|
|
|
|
allow(controller).to receive(:controller_name).and_return("article")
|
|
|
|
|
2014-02-22 00:00:15 +00:00
|
|
|
controller.impressionist_subapp_filter(unique: [:controller_name, :action_name])
|
|
|
|
controller.impressionist_subapp_filter(unique: [:controller_name, :action_name])
|
2020-07-20 13:35:41 +00:00
|
|
|
|
|
|
|
expect(Impression.count).to equal(@impression_count + 3)
|
2011-11-10 13:16:57 +00:00
|
|
|
end
|
2011-11-27 19:39:29 +00:00
|
|
|
|
2020-07-20 13:35:41 +00:00
|
|
|
it "recognize different action" do
|
|
|
|
allow(controller).to receive(:action_name).and_return("test_action")
|
2014-02-22 00:00:15 +00:00
|
|
|
controller.impressionist_subapp_filter(unique: [:action_name])
|
|
|
|
controller.impressionist_subapp_filter(unique: [:action_name])
|
2020-07-20 13:35:41 +00:00
|
|
|
expect(Impression.count).to equal(@impression_count + 1)
|
|
|
|
allow(controller).to receive(:action_name).and_return("another_action")
|
2014-02-22 00:00:15 +00:00
|
|
|
controller.impressionist_subapp_filter(unique: [:action_name])
|
|
|
|
controller.impressionist_subapp_filter(unique: [:action_name])
|
2020-07-20 13:35:41 +00:00
|
|
|
expect(Impression.count).to equal(@impression_count + 2)
|
2011-11-10 13:16:57 +00:00
|
|
|
end
|
2011-11-27 19:39:29 +00:00
|
|
|
|
2020-07-20 13:35:41 +00:00
|
|
|
it "recognize different controller" do
|
|
|
|
allow(controller).to receive(:controller_name).and_return("post")
|
2014-02-22 00:00:15 +00:00
|
|
|
controller.impressionist_subapp_filter(unique: [:controller_name])
|
|
|
|
controller.impressionist_subapp_filter(unique: [:controller_name])
|
2020-07-20 13:35:41 +00:00
|
|
|
expect(Impression.count).to equal(@impression_count + 1)
|
|
|
|
allow(controller).to receive(:controller_name).and_return("article")
|
2014-02-22 00:00:15 +00:00
|
|
|
controller.impressionist_subapp_filter(unique: [:controller_name])
|
|
|
|
controller.impressionist_subapp_filter(unique: [:controller_name])
|
2020-07-20 13:35:41 +00:00
|
|
|
expect(Impression.count).to equal(@impression_count + 2)
|
2011-11-10 13:16:57 +00:00
|
|
|
end
|
2011-11-27 19:39:29 +00:00
|
|
|
|
2020-07-20 13:35:41 +00:00
|
|
|
it "recognize different session" do
|
|
|
|
allow(controller).to receive(:session_hash).and_return("foo")
|
2014-02-22 00:00:15 +00:00
|
|
|
controller.impressionist_subapp_filter(unique: [:session_hash])
|
|
|
|
controller.impressionist_subapp_filter(unique: [:session_hash])
|
2020-07-20 13:35:41 +00:00
|
|
|
expect(Impression.count).to equal(@impression_count + 1)
|
|
|
|
allow(controller).to receive(:session_hash).and_return("bar")
|
2014-02-22 00:00:15 +00:00
|
|
|
controller.impressionist_subapp_filter(unique: [:session_hash])
|
|
|
|
controller.impressionist_subapp_filter(unique: [:session_hash])
|
2020-07-20 13:35:41 +00:00
|
|
|
expect(Impression.count).to equal(@impression_count + 2)
|
2011-11-10 13:16:57 +00:00
|
|
|
end
|
2011-11-27 19:39:29 +00:00
|
|
|
|
2020-07-20 13:35:41 +00:00
|
|
|
it "recognize different ip" do
|
|
|
|
allow(controller.request).to receive(:remote_ip).and_return("1.2.3.4")
|
|
|
|
|
2014-02-22 00:00:15 +00:00
|
|
|
controller.impressionist_subapp_filter(unique: [:ip_address])
|
|
|
|
controller.impressionist_subapp_filter(unique: [:ip_address])
|
2020-07-20 13:35:41 +00:00
|
|
|
|
|
|
|
expect(Impression.count).to equal(@impression_count + 1)
|
|
|
|
|
|
|
|
allow(controller.request).to receive(:remote_ip).and_return("5.6.7.8")
|
|
|
|
|
2014-02-22 00:00:15 +00:00
|
|
|
controller.impressionist_subapp_filter(unique: [:ip_address])
|
|
|
|
controller.impressionist_subapp_filter(unique: [:ip_address])
|
2020-07-20 13:35:41 +00:00
|
|
|
|
|
|
|
expect(Impression.count).to equal(@impression_count + 2)
|
2011-11-10 13:16:57 +00:00
|
|
|
end
|
2011-11-27 19:39:29 +00:00
|
|
|
|
2020-07-20 13:35:41 +00:00
|
|
|
it "recognize different referer" do
|
|
|
|
allow(controller.request).to receive(:referer).and_return("http://foo/bar")
|
|
|
|
|
2014-02-22 00:00:15 +00:00
|
|
|
controller.impressionist_subapp_filter(unique: [:referrer])
|
|
|
|
controller.impressionist_subapp_filter(unique: [:referrer])
|
2020-07-20 13:35:41 +00:00
|
|
|
|
|
|
|
expect(Impression.count).to equal(@impression_count + 1)
|
|
|
|
|
|
|
|
allow(controller.request).to receive(:referer).and_return("http://bar/fo")
|
|
|
|
|
2014-02-22 00:00:15 +00:00
|
|
|
controller.impressionist_subapp_filter(unique: [:referrer])
|
|
|
|
controller.impressionist_subapp_filter(unique: [:referrer])
|
2020-07-20 13:35:41 +00:00
|
|
|
expect(Impression.count).to equal(@impression_count + 2)
|
2011-11-10 13:16:57 +00:00
|
|
|
end
|
2011-11-27 19:39:29 +00:00
|
|
|
|
2020-07-20 13:35:41 +00:00
|
|
|
it "recognize different id" do
|
|
|
|
allow(controller).to receive(:params).and_return({ :id => "666" }) # for correct impressionable id in filter
|
|
|
|
|
2014-02-22 00:00:15 +00:00
|
|
|
controller.impressionist_subapp_filter(unique: [:impressionable_type, :impressionable_id])
|
|
|
|
controller.impressionist_subapp_filter(unique: [:impressionable_type, :impressionable_id])
|
2020-07-20 13:35:41 +00:00
|
|
|
|
|
|
|
allow(controller).to receive(:params).and_return({ :id => "42" }) # for correct impressionable id in filter
|
|
|
|
|
2014-02-22 00:00:15 +00:00
|
|
|
controller.impressionist_subapp_filter(unique: [:impressionable_type, :impressionable_id])
|
|
|
|
controller.impressionist_subapp_filter(unique: [:impressionable_type, :impressionable_id])
|
2020-07-20 13:35:41 +00:00
|
|
|
|
|
|
|
expect(Impression.count).to equal(@impression_count + 2)
|
2011-11-10 13:16:57 +00:00
|
|
|
end
|
2011-11-27 19:39:29 +00:00
|
|
|
|
2020-07-20 13:35:41 +00:00
|
|
|
it "recognize combined uniqueness" do
|
|
|
|
allow(controller).to receive(:action_name).and_return("test_action")
|
|
|
|
|
2014-02-22 00:00:15 +00:00
|
|
|
controller.impressionist_subapp_filter(unique: [:ip_address, :request_hash, :action_name])
|
|
|
|
controller.impressionist_subapp_filter(unique: [:request_hash, :ip_address, :action_name])
|
|
|
|
controller.impressionist_subapp_filter(unique: [:request_hash, :action_name])
|
|
|
|
controller.impressionist_subapp_filter(unique: [:ip_address, :action_name])
|
|
|
|
controller.impressionist_subapp_filter(unique: [:ip_address, :request_hash])
|
|
|
|
controller.impressionist_subapp_filter(unique: [:action_name])
|
|
|
|
controller.impressionist_subapp_filter(unique: [:ip_address])
|
|
|
|
controller.impressionist_subapp_filter(unique: [:request_hash])
|
2020-07-20 13:35:41 +00:00
|
|
|
|
|
|
|
expect(Impression.count).to equal(@impression_count + 1)
|
2011-11-10 13:16:57 +00:00
|
|
|
end
|
2011-11-27 19:39:29 +00:00
|
|
|
|
2020-07-20 13:35:41 +00:00
|
|
|
it "recognize combined non-uniqueness" do
|
|
|
|
allow(controller).to receive(:action_name).and_return(nil)
|
|
|
|
|
2014-02-22 00:00:15 +00:00
|
|
|
controller.impressionist_subapp_filter(unique: [:ip_address, :action_name])
|
2020-07-20 13:35:41 +00:00
|
|
|
|
|
|
|
allow(controller).to receive(:action_name).and_return("test_action")
|
|
|
|
|
2014-02-22 00:00:15 +00:00
|
|
|
controller.impressionist_subapp_filter(unique: [:ip_address, :action_name])
|
2020-07-20 13:35:41 +00:00
|
|
|
|
|
|
|
allow(controller).to receive(:action_name).and_return("another_action")
|
|
|
|
|
2014-02-22 00:00:15 +00:00
|
|
|
controller.impressionist_subapp_filter(unique: [:ip_address, :action_name])
|
2011-11-27 19:39:29 +00:00
|
|
|
|
2020-07-20 13:35:41 +00:00
|
|
|
expect(Impression.count).to equal(@impression_count + 3)
|
|
|
|
end
|
2011-11-10 13:16:57 +00:00
|
|
|
end
|
2011-11-27 19:39:29 +00:00
|
|
|
|
|
|
|
describe "impressionist method uniqueness for impressionables" do
|
2011-11-10 13:16:57 +00:00
|
|
|
# in this test we reuse the post model. might break if model changes.
|
2011-11-27 19:39:29 +00:00
|
|
|
|
2020-07-20 13:35:41 +00:00
|
|
|
it "ignore uniqueness if not requested" do
|
2011-11-10 13:16:57 +00:00
|
|
|
impressionable = Post.create
|
|
|
|
controller.impressionist impressionable
|
|
|
|
controller.impressionist impressionable
|
2020-07-20 13:35:41 +00:00
|
|
|
expect(Impression.count).to equal(@impression_count + 2)
|
2011-11-10 13:16:57 +00:00
|
|
|
end
|
2011-11-27 19:39:29 +00:00
|
|
|
|
2020-07-20 13:35:41 +00:00
|
|
|
it "recognize unique session" do
|
|
|
|
allow(controller).to receive(:session_hash).and_return(request.session_options[:id])
|
|
|
|
|
2011-11-10 13:16:57 +00:00
|
|
|
impressionable = Post.create
|
|
|
|
controller.impressionist(impressionable, nil, :unique => [:session_hash])
|
|
|
|
controller.impressionist(impressionable, nil, :unique => [:session_hash])
|
2020-07-20 13:35:41 +00:00
|
|
|
|
|
|
|
expect(Impression.count).to equal(@impression_count + 1)
|
2011-11-10 13:16:57 +00:00
|
|
|
end
|
2011-11-27 19:39:29 +00:00
|
|
|
|
2020-07-20 13:35:41 +00:00
|
|
|
it "recognize unique ip" do
|
|
|
|
allow(controller.request).to receive(:remote_ip).and_return("1.2.3.4")
|
|
|
|
|
2011-11-10 13:16:57 +00:00
|
|
|
impressionable = Post.create
|
|
|
|
controller.impressionist(impressionable, nil, :unique => [:ip_address])
|
|
|
|
controller.impressionist(impressionable, nil, :unique => [:ip_address])
|
2020-07-20 13:35:41 +00:00
|
|
|
|
|
|
|
expect(Impression.count).to equal(@impression_count + 1)
|
2011-11-10 13:16:57 +00:00
|
|
|
end
|
2011-11-27 19:39:29 +00:00
|
|
|
|
2020-07-20 13:35:41 +00:00
|
|
|
it "recognize unique request" do
|
2011-11-10 13:16:57 +00:00
|
|
|
impressionable = Post.create
|
|
|
|
controller.impressionist(impressionable, nil, :unique => [:request_hash])
|
|
|
|
controller.impressionist(impressionable, nil, :unique => [:request_hash])
|
2020-07-20 13:35:41 +00:00
|
|
|
|
|
|
|
expect(Impression.count).to equal(@impression_count + 1)
|
2011-11-10 13:16:57 +00:00
|
|
|
end
|
2011-11-27 19:39:29 +00:00
|
|
|
|
2020-07-20 13:35:41 +00:00
|
|
|
it "recognize unique user" do
|
|
|
|
allow(controller).to receive(:user_id).and_return(666)
|
|
|
|
|
2011-11-10 13:16:57 +00:00
|
|
|
impressionable = Post.create
|
|
|
|
controller.impressionist(impressionable, nil, :unique => [:user_id])
|
|
|
|
controller.impressionist(impressionable, nil, :unique => [:user_id])
|
2020-07-20 13:35:41 +00:00
|
|
|
|
|
|
|
expect(Impression.count).to equal(@impression_count + 1)
|
2011-11-10 13:16:57 +00:00
|
|
|
end
|
2011-11-27 19:39:29 +00:00
|
|
|
|
2020-07-20 13:35:41 +00:00
|
|
|
it "recognize unique referer" do
|
|
|
|
allow(controller.request).to receive(:referer).and_return("http://foo/bar")
|
|
|
|
|
2011-11-10 13:16:57 +00:00
|
|
|
impressionable = Post.create
|
|
|
|
controller.impressionist(impressionable, nil, :unique => [:referrer])
|
|
|
|
controller.impressionist(impressionable, nil, :unique => [:referrer])
|
2020-07-20 13:35:41 +00:00
|
|
|
|
|
|
|
expect(Impression.count).to equal(@impression_count + 1)
|
2011-11-10 13:16:57 +00:00
|
|
|
end
|
2011-11-27 19:39:29 +00:00
|
|
|
|
2020-07-20 13:35:41 +00:00
|
|
|
it "recognize different session" do
|
2011-11-10 13:16:57 +00:00
|
|
|
impressionable = Post.create
|
2020-07-20 13:35:41 +00:00
|
|
|
|
|
|
|
allow(controller).to receive(:session_hash).and_return("foo")
|
|
|
|
|
2011-11-10 13:16:57 +00:00
|
|
|
controller.impressionist(impressionable, nil, :unique => [:session_hash])
|
|
|
|
controller.impressionist(impressionable, nil, :unique => [:session_hash])
|
2020-07-20 13:35:41 +00:00
|
|
|
|
|
|
|
expect(Impression.count).to equal(@impression_count + 1)
|
|
|
|
|
|
|
|
allow(controller).to receive(:session_hash).and_return("bar")
|
|
|
|
|
2011-11-10 13:16:57 +00:00
|
|
|
controller.impressionist(impressionable, nil, :unique => [:session_hash])
|
|
|
|
controller.impressionist(impressionable, nil, :unique => [:session_hash])
|
2020-07-20 13:35:41 +00:00
|
|
|
|
|
|
|
expect(Impression.count).to equal(@impression_count + 2)
|
2011-11-10 13:16:57 +00:00
|
|
|
end
|
2011-11-27 19:39:29 +00:00
|
|
|
|
2020-07-20 13:35:41 +00:00
|
|
|
it "recognize different ip" do
|
|
|
|
allow(controller.request).to receive(:remote_ip).and_return("1.2.3.4")
|
|
|
|
|
2011-11-10 13:16:57 +00:00
|
|
|
impressionable = Post.create
|
|
|
|
controller.impressionist(impressionable, nil, :unique => [:ip_address])
|
|
|
|
controller.impressionist(impressionable, nil, :unique => [:ip_address])
|
2020-07-20 13:35:41 +00:00
|
|
|
|
|
|
|
expect(Impression.count).to equal(@impression_count + 1)
|
|
|
|
|
|
|
|
allow(controller.request).to receive(:remote_ip).and_return("5.6.7.8")
|
|
|
|
|
2011-11-10 13:16:57 +00:00
|
|
|
controller.impressionist(impressionable, nil, :unique => [:ip_address])
|
|
|
|
controller.impressionist(impressionable, nil, :unique => [:ip_address])
|
2020-07-20 13:35:41 +00:00
|
|
|
|
|
|
|
expect(Impression.count).to equal(@impression_count + 2)
|
2011-11-10 13:16:57 +00:00
|
|
|
end
|
2011-11-27 19:39:29 +00:00
|
|
|
|
2020-07-20 13:35:41 +00:00
|
|
|
it "recognize different user" do
|
2011-11-10 13:16:57 +00:00
|
|
|
impressionable = Post.create
|
2020-07-20 13:35:41 +00:00
|
|
|
|
|
|
|
allow(controller).to receive(:user_id).and_return(666)
|
|
|
|
|
2011-11-10 13:16:57 +00:00
|
|
|
controller.impressionist(impressionable, nil, :unique => [:user_id])
|
|
|
|
controller.impressionist(impressionable, nil, :unique => [:user_id])
|
2020-07-20 13:35:41 +00:00
|
|
|
|
|
|
|
expect(Impression.count).to equal(@impression_count + 1)
|
|
|
|
|
|
|
|
allow(controller).to receive(:user_id).and_return(42)
|
|
|
|
|
2011-11-10 13:16:57 +00:00
|
|
|
controller.impressionist(impressionable, nil, :unique => [:user_id])
|
|
|
|
controller.impressionist(impressionable, nil, :unique => [:user_id])
|
2020-07-20 13:35:41 +00:00
|
|
|
|
|
|
|
expect(Impression.count).to equal(@impression_count + 2)
|
2011-11-10 13:16:57 +00:00
|
|
|
end
|
2011-11-27 19:39:29 +00:00
|
|
|
|
2020-07-20 13:35:41 +00:00
|
|
|
it "recognize combined uniqueness" do
|
2011-11-10 13:16:57 +00:00
|
|
|
impressionable = Post.create
|
2020-07-20 13:35:41 +00:00
|
|
|
allow(controller).to receive(:session_hash).and_return("foo")
|
|
|
|
|
2011-11-10 13:16:57 +00:00
|
|
|
controller.impressionist(impressionable, nil, :unique => [:ip_address, :request_hash, :session_hash])
|
|
|
|
controller.impressionist(impressionable, nil, :unique => [:request_hash, :ip_address, :session_hash])
|
|
|
|
controller.impressionist(impressionable, nil, :unique => [:request_hash, :session_hash])
|
|
|
|
controller.impressionist(impressionable, nil, :unique => [:ip_address, :session_hash])
|
|
|
|
controller.impressionist(impressionable, nil, :unique => [:ip_address, :request_hash])
|
|
|
|
controller.impressionist(impressionable, nil, :unique => [:session_hash])
|
|
|
|
controller.impressionist(impressionable, nil, :unique => [:ip_address])
|
|
|
|
controller.impressionist(impressionable, nil, :unique => [:request_hash])
|
2020-07-20 13:35:41 +00:00
|
|
|
|
|
|
|
expect(Impression.count).to equal(@impression_count + 1)
|
2011-11-10 13:16:57 +00:00
|
|
|
end
|
2011-11-27 19:39:29 +00:00
|
|
|
|
2020-07-20 13:35:41 +00:00
|
|
|
it "recognize combined non-uniqueness" do
|
2011-11-10 13:16:57 +00:00
|
|
|
impressionable = Post.create
|
2020-07-20 13:35:41 +00:00
|
|
|
allow(controller).to receive(:session_hash).and_return(nil)
|
2011-11-10 13:16:57 +00:00
|
|
|
controller.impressionist(impressionable, nil, :unique => [:ip_address, :session_hash])
|
2020-07-20 13:35:41 +00:00
|
|
|
allow(controller).to receive(:session_hash).and_return("foo")
|
2011-11-10 13:16:57 +00:00
|
|
|
controller.impressionist(impressionable, nil, :unique => [:ip_address, :session_hash])
|
2020-07-20 13:35:41 +00:00
|
|
|
allow(controller).to receive(:session_hash).and_return("bar")
|
2011-11-10 13:16:57 +00:00
|
|
|
controller.impressionist(impressionable, nil, :unique => [:ip_address, :session_hash])
|
2020-07-20 13:35:41 +00:00
|
|
|
expect(Impression.count).to equal(@impression_count + 3)
|
2011-11-10 13:16:57 +00:00
|
|
|
end
|
|
|
|
end
|
2011-11-27 19:39:29 +00:00
|
|
|
|
2011-11-10 13:16:57 +00:00
|
|
|
describe "impressionist filter and method uniqueness" do
|
2020-07-20 13:35:41 +00:00
|
|
|
it "recognize uniqueness" do
|
2011-11-10 13:16:57 +00:00
|
|
|
impressionable = Post.create
|
2020-07-20 13:35:41 +00:00
|
|
|
allow(controller).to receive(:controller_name).and_return("posts") # for correct impressionable type in filter
|
|
|
|
allow(controller).to receive(:params).and_return({ :id => impressionable.id.to_s }) # for correct impressionable id in filter
|
|
|
|
allow(controller).to receive(:session_hash).and_return("foo")
|
|
|
|
allow(controller.request).to receive(:remote_ip).and_return("1.2.3.4")
|
2011-11-10 13:16:57 +00:00
|
|
|
# order of the following methods is important for the test!
|
2014-02-22 00:00:15 +00:00
|
|
|
controller.impressionist_subapp_filter(unique: [:ip_address, :request_hash, :session_hash])
|
2011-11-10 13:16:57 +00:00
|
|
|
controller.impressionist(impressionable, nil, :unique => [:ip_address, :request_hash, :session_hash])
|
2020-07-20 13:35:41 +00:00
|
|
|
expect(Impression.count).to equal(@impression_count + 1)
|
2011-11-10 13:16:57 +00:00
|
|
|
end
|
|
|
|
end
|
2011-11-27 19:39:29 +00:00
|
|
|
|
2015-02-07 16:08:32 +00:00
|
|
|
describe 'impressionist with friendly id' do
|
2020-07-20 13:35:41 +00:00
|
|
|
it 'unique' do
|
|
|
|
impressionable = Profile.create({ username: 'test_profile', slug: 'test_profile' })
|
2015-02-07 20:03:34 +00:00
|
|
|
|
2020-07-20 13:35:41 +00:00
|
|
|
allow(controller).to receive(:controller_name).and_return('profile')
|
|
|
|
allow(controller).to receive(:action_name).and_return('show')
|
|
|
|
allow(controller).to receive(:params).and_return({ id: impressionable.slug })
|
|
|
|
allow(controller.request).to receive(:remote_ip).and_return('1.2.3.4')
|
2015-02-07 16:08:32 +00:00
|
|
|
|
|
|
|
controller.impressionist(impressionable, nil, :unique => [:impressionable_type, :impressionable_id])
|
|
|
|
controller.impressionist(impressionable, nil, :unique => [:impressionable_type, :impressionable_id])
|
2020-07-20 13:35:41 +00:00
|
|
|
expect(Impression.count).to equal(@impression_count + 1)
|
2015-02-07 16:08:32 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-02-22 00:00:15 +00:00
|
|
|
shared_examples_for 'an impressionable action' do
|
2020-07-20 13:35:41 +00:00
|
|
|
it 'record an impression' do
|
2014-02-22 00:00:15 +00:00
|
|
|
controller.impressionist_subapp_filter(condition)
|
2020-07-20 13:35:41 +00:00
|
|
|
expect(Impression.count).to equal(@impression_count + 1)
|
2014-02-22 00:00:15 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
shared_examples_for 'an unimpressionable action' do
|
2020-07-20 13:35:41 +00:00
|
|
|
it 'record an impression' do
|
2014-02-22 00:00:15 +00:00
|
|
|
controller.impressionist_subapp_filter(condition)
|
2020-07-20 13:35:41 +00:00
|
|
|
expect(Impression.count).to equal(@impression_count)
|
2014-02-22 00:00:15 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "conditional impressions" do
|
|
|
|
describe ":if condition" do
|
|
|
|
context "true condition" do
|
|
|
|
before do
|
2020-07-20 13:35:41 +00:00
|
|
|
allow(controller).to receive(:true_condition).and_return(true)
|
2014-02-22 00:00:15 +00:00
|
|
|
end
|
2020-07-20 13:35:41 +00:00
|
|
|
|
2014-02-22 00:00:15 +00:00
|
|
|
it_behaves_like 'an impressionable action' do
|
2020-07-20 13:35:41 +00:00
|
|
|
let(:condition) { { if: :true_condition } }
|
2014-02-22 00:00:15 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it_behaves_like 'an impressionable action' do
|
2020-07-20 13:35:41 +00:00
|
|
|
let(:condition) { { if: -> { true } } }
|
2014-02-22 00:00:15 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "false condition" do
|
|
|
|
before do
|
2020-07-20 13:35:41 +00:00
|
|
|
allow(controller).to receive(:false_condition).and_return(false)
|
2014-02-22 00:00:15 +00:00
|
|
|
end
|
2020-07-20 13:35:41 +00:00
|
|
|
|
2014-02-22 00:00:15 +00:00
|
|
|
it_behaves_like 'an unimpressionable action' do
|
2020-07-20 13:35:41 +00:00
|
|
|
let(:condition) { { if: :false_condition } }
|
2014-02-22 00:00:15 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it_behaves_like 'an unimpressionable action' do
|
2020-07-20 13:35:41 +00:00
|
|
|
let(:condition) { { if: -> { false } } }
|
2014-02-22 00:00:15 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe ":unless condition" do
|
|
|
|
context "true condition" do
|
|
|
|
before do
|
2020-07-20 13:35:41 +00:00
|
|
|
allow(controller).to receive(:true_condition).and_return(true)
|
2014-02-22 00:00:15 +00:00
|
|
|
end
|
2020-07-20 13:35:41 +00:00
|
|
|
|
2014-02-22 00:00:15 +00:00
|
|
|
it_behaves_like 'an unimpressionable action' do
|
2020-07-20 13:35:41 +00:00
|
|
|
let(:condition) { { unless: :true_condition } }
|
2014-02-22 00:00:15 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it_behaves_like 'an unimpressionable action' do
|
2020-07-20 13:35:41 +00:00
|
|
|
let(:condition) { { unless: -> { true } } }
|
2014-02-22 00:00:15 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "false condition" do
|
|
|
|
before do
|
2020-07-20 13:35:41 +00:00
|
|
|
allow(controller).to receive(:false_condition).and_return(false)
|
2014-02-22 00:00:15 +00:00
|
|
|
end
|
2020-07-20 13:35:41 +00:00
|
|
|
|
2014-02-22 00:00:15 +00:00
|
|
|
it_behaves_like 'an impressionable action' do
|
2020-07-20 13:35:41 +00:00
|
|
|
let(:condition) { { unless: :false_condition } }
|
2014-02-22 00:00:15 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it_behaves_like 'an impressionable action' do
|
2020-07-20 13:35:41 +00:00
|
|
|
let(:condition) { { unless: -> { false } } }
|
2014-02-22 00:00:15 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2011-11-10 13:16:57 +00:00
|
|
|
end
|