impressionist/tests/test_app/spec/controllers/impressionist_uniqueness_sp...

311 lines
15 KiB
Ruby
Raw Normal View History

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
2011-11-10 13:16:57 +00:00
before do
@impression_count = Impression.all.size
end
describe "impressionist filter uniqueness" do
2011-11-10 13:16:57 +00:00
it "should ignore uniqueness if not requested" do
controller.impressionist_subapp_filter(nil, nil)
controller.impressionist_subapp_filter(nil, nil)
Impression.should have(@impression_count + 2).records
end
2011-11-10 13:16:57 +00:00
it "should recognize unique session" do
2013-07-11 05:02:26 +00:00
controller.stub(:session_hash).and_return(request.session_options[:id])
2011-11-10 13:16:57 +00:00
controller.impressionist_subapp_filter(nil, [:session_hash])
controller.impressionist_subapp_filter(nil, [:session_hash])
Impression.should have(@impression_count + 1).records
end
2011-11-10 13:16:57 +00:00
it "should recognize unique ip" do
2013-07-11 05:02:26 +00:00
controller.request.stub(:remote_ip).and_return("1.2.3.4")
2011-11-10 13:16:57 +00:00
controller.impressionist_subapp_filter(nil, [:ip_address])
controller.impressionist_subapp_filter(nil, [:ip_address])
Impression.should have(@impression_count + 1).records
end
2011-11-10 13:16:57 +00:00
it "should recognize unique request" do
controller.impressionist_subapp_filter(nil, [:request_hash])
controller.impressionist_subapp_filter(nil, [:request_hash])
Impression.should have(@impression_count + 1).records
end
2011-11-10 13:16:57 +00:00
it "should recognize unique action" do
2013-07-11 05:02:26 +00:00
controller.stub(:action_name).and_return("test_action")
2011-11-10 13:16:57 +00:00
controller.impressionist_subapp_filter(nil, [:action_name])
controller.impressionist_subapp_filter(nil, [:action_name])
Impression.should have(@impression_count + 1).records
end
2011-11-10 13:16:57 +00:00
it "should recognize unique controller" do
2013-07-11 05:02:26 +00:00
controller.stub(:controller_name).and_return("post")
2011-11-10 13:16:57 +00:00
controller.impressionist_subapp_filter(nil, [:controller_name])
controller.impressionist_subapp_filter(nil, [:controller_name])
Impression.should have(@impression_count + 1).records
end
it "should recognize unique user" do
2013-07-11 05:02:26 +00:00
controller.stub(:user_id).and_return(42)
2011-11-10 13:16:57 +00:00
controller.impressionist_subapp_filter(nil, [:user_id])
controller.impressionist_subapp_filter(nil, [:user_id])
Impression.should have(@impression_count + 1).records
end
2011-11-10 13:16:57 +00:00
it "should recognize unique referer" do
2013-07-11 05:02:26 +00:00
controller.request.stub(:referer).and_return("http://foo/bar")
2011-11-10 13:16:57 +00:00
controller.impressionist_subapp_filter(nil, [:referrer])
controller.impressionist_subapp_filter(nil, [:referrer])
Impression.should have(@impression_count + 1).records
end
2011-11-10 13:16:57 +00:00
it "should recognize unique id" do
2013-07-11 05:02:26 +00:00
controller.stub(:params).and_return({:id => "666"}) # for correct impressionable id in filter
2011-11-10 13:16:57 +00:00
controller.impressionist_subapp_filter(nil, [:impressionable_id])
controller.impressionist_subapp_filter(nil, [:impressionable_id])
Impression.should have(@impression_count + 1).records
end
2011-11-10 13:16:57 +00:00
# extra redundant test for important controller and action combination.
it "should recognize different controller and action" do
2013-07-11 05:02:26 +00:00
controller.stub(:controller_name).and_return("post")
controller.stub(:action_name).and_return("test_action")
2011-11-10 13:16:57 +00:00
controller.impressionist_subapp_filter(nil, [:controller_name, :action_name])
controller.impressionist_subapp_filter(nil, [:controller_name, :action_name])
Impression.should have(@impression_count + 1).records
2013-07-11 05:02:26 +00:00
controller.stub(:action_name).and_return("another_action")
2011-11-10 13:16:57 +00:00
controller.impressionist_subapp_filter(nil, [:controller_name, :action_name])
controller.impressionist_subapp_filter(nil, [:controller_name, :action_name])
Impression.should have(@impression_count + 2).records
2013-07-11 05:02:26 +00:00
controller.stub(:controller_name).and_return("article")
2011-11-10 13:16:57 +00:00
controller.impressionist_subapp_filter(nil, [:controller_name, :action_name])
controller.impressionist_subapp_filter(nil, [:controller_name, :action_name])
Impression.should have(@impression_count + 3).records
end
2011-11-10 13:16:57 +00:00
it "should recognize different action" do
2013-07-11 05:02:26 +00:00
controller.stub(:action_name).and_return("test_action")
2011-11-10 13:16:57 +00:00
controller.impressionist_subapp_filter(nil, [:action_name])
controller.impressionist_subapp_filter(nil, [:action_name])
Impression.should have(@impression_count + 1).records
2013-07-11 05:02:26 +00:00
controller.stub(:action_name).and_return("another_action")
2011-11-10 13:16:57 +00:00
controller.impressionist_subapp_filter(nil, [:action_name])
controller.impressionist_subapp_filter(nil, [:action_name])
Impression.should have(@impression_count + 2).records
end
2011-11-10 13:16:57 +00:00
it "should recognize different controller" do
2013-07-11 05:02:26 +00:00
controller.stub(:controller_name).and_return("post")
2011-11-10 13:16:57 +00:00
controller.impressionist_subapp_filter(nil, [:controller_name])
controller.impressionist_subapp_filter(nil, [:controller_name])
Impression.should have(@impression_count + 1).records
2013-07-11 05:02:26 +00:00
controller.stub(:controller_name).and_return("article")
2011-11-10 13:16:57 +00:00
controller.impressionist_subapp_filter(nil, [:controller_name])
controller.impressionist_subapp_filter(nil, [:controller_name])
Impression.should have(@impression_count + 2).records
end
2011-11-10 13:16:57 +00:00
it "should recognize different session" do
2013-07-11 05:02:26 +00:00
controller.stub(:session_hash).and_return("foo")
2011-11-10 13:16:57 +00:00
controller.impressionist_subapp_filter(nil, [:session_hash])
controller.impressionist_subapp_filter(nil, [:session_hash])
Impression.should have(@impression_count + 1).records
2013-07-11 05:02:26 +00:00
controller.stub(:session_hash).and_return("bar")
2011-11-10 13:16:57 +00:00
controller.impressionist_subapp_filter(nil, [:session_hash])
controller.impressionist_subapp_filter(nil, [:session_hash])
Impression.should have(@impression_count + 2).records
end
2011-11-10 13:16:57 +00:00
it "should recognize different ip" do
2013-07-11 05:02:26 +00:00
controller.request.stub(:remote_ip).and_return("1.2.3.4")
2011-11-10 13:16:57 +00:00
controller.impressionist_subapp_filter(nil, [:ip_address])
controller.impressionist_subapp_filter(nil, [:ip_address])
Impression.should have(@impression_count + 1).records
2013-07-11 05:02:26 +00:00
controller.request.stub(:remote_ip).and_return("5.6.7.8")
2011-11-10 13:16:57 +00:00
controller.impressionist_subapp_filter(nil, [:ip_address])
controller.impressionist_subapp_filter(nil, [:ip_address])
Impression.should have(@impression_count + 2).records
end
2011-11-10 13:16:57 +00:00
it "should recognize different referer" do
2013-07-11 05:02:26 +00:00
controller.request.stub(:referer).and_return("http://foo/bar")
2011-11-10 13:16:57 +00:00
controller.impressionist_subapp_filter(nil, [:referrer])
controller.impressionist_subapp_filter(nil, [:referrer])
Impression.should have(@impression_count + 1).records
2013-07-11 05:02:26 +00:00
controller.request.stub(:referer).and_return("http://bar/fo")
2011-11-10 13:16:57 +00:00
controller.impressionist_subapp_filter(nil, [:referrer])
controller.impressionist_subapp_filter(nil, [:referrer])
Impression.should have(@impression_count + 2).records
end
2011-11-10 13:16:57 +00:00
it "should recognize different id" do
2013-07-11 05:02:26 +00:00
controller.stub(:params).and_return({:id => "666"}) # for correct impressionable id in filter
2011-11-10 13:16:57 +00:00
controller.impressionist_subapp_filter(nil, [:impressionable_type, :impressionable_id])
controller.impressionist_subapp_filter(nil, [:impressionable_type, :impressionable_id])
2013-07-11 05:02:26 +00:00
controller.stub(:params).and_return({:id => "42"}) # for correct impressionable id in filter
2011-11-10 13:16:57 +00:00
controller.impressionist_subapp_filter(nil, [:impressionable_type, :impressionable_id])
controller.impressionist_subapp_filter(nil, [:impressionable_type, :impressionable_id])
Impression.should have(@impression_count + 2).records
end
2011-11-10 13:16:57 +00:00
it "should recognize combined uniqueness" do
2013-07-11 05:02:26 +00:00
controller.stub(:action_name).and_return("test_action")
2011-11-10 13:16:57 +00:00
controller.impressionist_subapp_filter(nil, [:ip_address, :request_hash, :action_name])
controller.impressionist_subapp_filter(nil, [:request_hash, :ip_address, :action_name])
controller.impressionist_subapp_filter(nil, [:request_hash, :action_name])
controller.impressionist_subapp_filter(nil, [:ip_address, :action_name])
controller.impressionist_subapp_filter(nil, [:ip_address, :request_hash])
controller.impressionist_subapp_filter(nil, [:action_name])
controller.impressionist_subapp_filter(nil, [:ip_address])
controller.impressionist_subapp_filter(nil, [:request_hash])
Impression.should have(@impression_count + 1).records
end
2011-11-10 13:16:57 +00:00
it "should recognize combined non-uniqueness" do
2013-07-11 05:02:26 +00:00
controller.stub(:action_name).and_return(nil)
2011-11-10 13:16:57 +00:00
controller.impressionist_subapp_filter(nil, [:ip_address, :action_name])
2013-07-11 05:02:26 +00:00
controller.stub(:action_name).and_return("test_action")
2011-11-10 13:16:57 +00:00
controller.impressionist_subapp_filter(nil, [:ip_address, :action_name])
2013-07-11 05:02:26 +00:00
controller.stub(:action_name).and_return("another_action")
2011-11-10 13:16:57 +00:00
controller.impressionist_subapp_filter(nil, [:ip_address, :action_name])
Impression.should have(@impression_count + 3).records
end
2011-11-10 13:16:57 +00:00
end
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-10 13:16:57 +00:00
it "should ignore uniqueness if not requested" do
impressionable = Post.create
controller.impressionist impressionable
controller.impressionist impressionable
Impression.should have(@impression_count + 2).records
end
2011-11-10 13:16:57 +00:00
it "should recognize unique session" do
2013-07-11 05:02:26 +00:00
controller.stub(: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])
Impression.should have(@impression_count + 1).records
end
2011-11-10 13:16:57 +00:00
it "should recognize unique ip" do
2013-07-11 05:02:26 +00:00
controller.request.stub(: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])
Impression.should have(@impression_count + 1).records
end
2011-11-10 13:16:57 +00:00
it "should recognize unique request" do
impressionable = Post.create
controller.impressionist(impressionable, nil, :unique => [:request_hash])
controller.impressionist(impressionable, nil, :unique => [:request_hash])
Impression.should have(@impression_count + 1).records
end
it "should recognize unique user" do
2013-07-11 05:02:26 +00:00
controller.stub(: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])
Impression.should have(@impression_count + 1).records
end
2011-11-10 13:16:57 +00:00
it "should recognize unique referer" do
2013-07-11 05:02:26 +00:00
controller.request.stub(: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])
Impression.should have(@impression_count + 1).records
end
2011-11-10 13:16:57 +00:00
it "should recognize different session" do
impressionable = Post.create
2013-07-11 05:02:26 +00:00
controller.stub(: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])
Impression.should have(@impression_count + 1).records
2013-07-11 05:02:26 +00:00
controller.stub(: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])
Impression.should have(@impression_count + 2).records
end
2011-11-10 13:16:57 +00:00
it "should recognize different ip" do
2013-07-11 05:02:26 +00:00
controller.request.stub(: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])
Impression.should have(@impression_count + 1).records
2013-07-11 05:02:26 +00:00
controller.request.stub(: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])
Impression.should have(@impression_count + 2).records
end
2011-11-10 13:16:57 +00:00
it "should recognize different user" do
impressionable = Post.create
2013-07-11 05:02:26 +00:00
controller.stub(: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])
Impression.should have(@impression_count + 1).records
2013-07-11 05:02:26 +00:00
controller.stub(: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])
Impression.should have(@impression_count + 2).records
end
2011-11-10 13:16:57 +00:00
it "should recognize combined uniqueness" do
impressionable = Post.create
2013-07-11 05:02:26 +00:00
controller.stub(: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])
Impression.should have(@impression_count + 1).records
end
2011-11-10 13:16:57 +00:00
it "should recognize combined non-uniqueness" do
impressionable = Post.create
2013-07-11 05:02:26 +00:00
controller.stub(:session_hash).and_return(nil)
2011-11-10 13:16:57 +00:00
controller.impressionist(impressionable, nil, :unique => [:ip_address, :session_hash])
2013-07-11 05:02:26 +00:00
controller.stub(:session_hash).and_return("foo")
2011-11-10 13:16:57 +00:00
controller.impressionist(impressionable, nil, :unique => [:ip_address, :session_hash])
2013-07-11 05:02:26 +00:00
controller.stub(:session_hash).and_return("bar")
2011-11-10 13:16:57 +00:00
controller.impressionist(impressionable, nil, :unique => [:ip_address, :session_hash])
Impression.should have(@impression_count + 3).records
end
2011-11-10 13:16:57 +00:00
end
2011-11-10 13:16:57 +00:00
describe "impressionist filter and method uniqueness" do
2011-11-10 13:16:57 +00:00
it "should recognize uniqueness" do
impressionable = Post.create
2013-07-11 05:02:26 +00:00
controller.stub(:controller_name).and_return("posts") # for correct impressionable type in filter
controller.stub(:params).and_return({:id => impressionable.id.to_s}) # for correct impressionable id in filter
controller.stub(:session_hash).and_return("foo")
controller.request.stub(: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!
controller.impressionist_subapp_filter(nil, [:ip_address, :request_hash, :session_hash])
controller.impressionist(impressionable, nil, :unique => [:ip_address, :request_hash, :session_hash])
Impression.should have(@impression_count + 1).records
end
2011-11-10 13:16:57 +00:00
end
2011-11-10 13:16:57 +00:00
end