require 'test_helper' require 'observation' class ObservationTest < Test::Unit::TestCase context "The Observation when initialized" do should "copy and rename attributes" do log_attr = {:r => "referrer", :_item_id => "id", :_user1 => "custom", :ip => "ip"} o = Observation.new(log_attr) assert_equal 0, o.unknown_attributes.size assert_equal 4, o.attributes.size assert_equal "referrer", o.attributes[:referrer] assert_equal "id", o.attributes[:item_id] assert_equal "custom", o.attributes[:user1] assert_equal "ip", o.attributes[:ip] end should "convert local urls to absolute urls" do log_attr = { :_url => "../index.html", :_thumbnail => 'http://www.test.com/te st.html#debug', :_image => 'http://www.test.com/te%st.html#debugon', :_basket_url => 'http://www.test.com/te%20st.html#debugoff', :u => 'http://www.test.com/f1/f2/index.html#debugon' } o = Observation.new(log_attr) assert_equal 0, o.unknown_attributes.size assert_equal 5, o.attributes.size assert_equal "http://www.test.com/f1/index.html", o.attributes[:url] assert_equal "http://www.test.com/te%20st.html", o.attributes[:thumbnail] assert_equal "http://www.test.com/te%25st.html", o.attributes[:image] assert_equal "http://www.test.com/te%20st.html", o.attributes[:basket_url] end should "convert not tamper with javascript urls" do log_attr = { :_basket_url => "javascript:alert('basket')", :u => 'http://www.test.com/' } o = Observation.new(log_attr) assert_equal "javascript:alert('basket')", o.attributes[:basket_url] end should "report error for invalid urls" do log_attr = { :_url => "../index.html", :u => 'index.html#anchor2' } o = Observation.new(log_attr) assert o.errors.include?(:url_error_on_url) end should "validate that status is ok (200)" do log_attr = { :status => '200' } o = Observation.new(log_attr) assert !o.errors.include?(:status_not_ok) log_attr = { :status => '404' } o = Observation.new(log_attr) assert o.errors.include?(:status_not_ok) end should "validate type attribute" do log_attr = { :o => "view_item", :status => "200" } o = Observation.new(log_attr) assert !o.errors.include?(:unknown_type) assert_equal :view_item, o.type log_attr = { :o => "pick_item", :status => "200" } o = Observation.new(log_attr) assert !o.errors.include?(:unknown_type) assert_equal :pick_item, o.type log_attr = { :o => "visit_page", :status => "200" } o = Observation.new(log_attr) assert !o.errors.include?(:unknown_type) assert_equal :visit_page, o.type log_attr = { :o => "apa", :status => "200" } o = Observation.new(log_attr) assert o.errors.include?(:unknown_type) assert_equal :unknown, o.type end should "validate required attributes" do other_names = Observation::BASIC_ATTRIBUTES_NAMES.merge(Observation::OBSERVATION_ATTRIBUTES_NAMES) [ :uid, :sid, :aid, :_item_id ].each do |name| error_message = "missing_#{other_names[name]}".to_sym log_attr = { :o => "view_item", :status => "200", name => "1000" } o = Observation.new(log_attr) assert !o.errors.include?(error_message) log_attr = { :o => "view_item", :status => "200" } o = Observation.new(log_attr) assert o.errors.include?(error_message), "'#{error_message}' not found in #{o.errors.inspect}" end end should "validate visit_page with shop, account and user id" do assert Observation.new({ :o => "visit_page", :aid => "jshop", :sid => "shirtstore", :uid => "1234", :status => "200" }).valid? assert !Observation.new({ :o => "visit_page", :sid => "shirtstore", :uid => "1234", :status => "200" }).valid? assert !Observation.new({ :o => "visit_page", :aid => "jshop", :uid => "1234", :status => "200" }).valid? assert !Observation.new({ :o => "visit_page", :aid => "jshop", :sid => "shirtstore", :status => "200" }).valid? end should "accept visit_page without item_id as a valid observation" do assert Observation.new({ :o => "visit_page", :status => "200", :uid => '1' * 16, :sid => 'shoop', :aid => 'act'}).valid? end end end