Instantly share code, notes, and snippets.
Created
July 25, 2011 17:04
-
Star
0
(0)
You must be signed in to star a gist -
Fork
0
(0)
You must be signed in to fork a gist
-
Save kinsteronline/1104592 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'test_helper' | |
require 'integration/integration_test_helpers' | |
class VisitorTrackingCookieFlowsTest < ActionController::IntegrationTest | |
setup do | |
Visitor.delete_all | |
Visit.delete_all | |
Search.delete_all | |
SearchResult.delete_all | |
ReservationRequest.delete_all | |
SearchResultOperator.delete_all | |
end | |
## tests of the _ldc_vis cookie | |
test "visiting the site sets a visitor cookie" do | |
# make sure the session starts empty | |
assert cookies.empty? | |
# go to a page on the site and make sure the visitor cookie is set | |
post_via_redirect root_path | |
assert cookies.has_key?("_ldc_sess") | |
assert cookies.has_key?("_ldc_vis") | |
end | |
test "visitor id from cookie is not stored if it already exists in the visitors table" do | |
# add a visitor to the database with the visitor id | |
Visitor.create(:visitor_uuid => "bb2eeaaf") | |
# set the cookie to be passed to the request | |
cookies["_ldc_vis"] = "bb2eeaaf" | |
# check that the visitor was not created | |
assert_no_difference "Visitor.count" do | |
post_via_redirect root_path | |
end | |
end | |
test "visitor id from cookie is stored if it does not already exist in the visitors table" do | |
# set the cookie to be passed to the request | |
cookies["_ldc_vis"] = "bb2eeaag" | |
# check that the visitor was created | |
assert_difference "Visitor.count", 1 do | |
post_via_redirect root_path | |
end | |
end | |
test "visitor id is generated if no _ldc_vis cookie is provided" do | |
assert !cookies.has_key?("_ldc_vis") | |
get_via_redirect root_path | |
assert cookies["_ldc_vis"].present? | |
assert_equal 32, cookies["_ldc_vis"].length | |
end | |
## tests of the visits that are created | |
test "a visit is created with just the visitor_uuid on the first hit to the page" do | |
assert cookies.empty? | |
assert_difference "Visit.count", 1 do | |
get_via_redirect root_path | |
end | |
uuid = Visit.last.visitor_uuid | |
assert_equal 32, uuid.length | |
assert_equal cookies['_ldc_vis'], uuid | |
end | |
test "a visit is updated with session ids once it is available in the flow" do | |
# hit the first page to generate the session and visitor ids | |
get_via_redirect root_path | |
assert cookies.has_key?('_ldc_sess') | |
assert cookies['_ldc_sess'].present? | |
assert cookies.has_key?('_ldc_vis') | |
assert cookies['_ldc_vis'].present? | |
# after the first hit, the session_id won't be set in the database | |
v = Visit.last | |
assert_nil v.session_id | |
# hit the second page and see if the session id has been updated properly | |
# also ensure that a second visit isn't created | |
assert_no_difference "Visit.count" do | |
post_via_redirect root_path | |
end | |
v.reload | |
assert_not_nil v.session_id | |
assert_equal cookies['_ldc_sess'], v.session_id | |
# they haven't logged in yet, so make sure the visit doesn't have a user_id | |
assert_nil v.user_id | |
end | |
test "a visit is updated with user id once they log in" do | |
# ensure that they aren't already logged in | |
Capybara.reset_sessions! | |
assert_difference "Visit.count", 1 do | |
visit root_path | |
end | |
click_link("Sign Out.") if page.has_content?("Sign Out.") | |
# after the first hit, the user id won't be set in the database | |
v = Visit.last | |
assert_nil v.user_id | |
# log them in | |
u = User.find_by_email("[email protected]") | |
assert_no_difference "Visit.count" do | |
click_link("Sign In.") | |
fill_in("email", :with => u.email) | |
fill_in("password", :with => "password") | |
click_button("signin_btn") | |
wait_until { page.has_content?("Hello Cedric.") } | |
end | |
# see if the user id has been updated properly | |
v.reload | |
assert_not_nil v.user_id | |
assert_equal u[:id], v.user_id | |
end | |
test "two visits more than 24 hours apart create two visitor users" do | |
v = Visit.create!(:visitor_uuid => UUID.generate(:compact)) | |
cookies["_ldc_vis"] = v.visitor_uuid | |
# when it's less than 24 hours, no record is created | |
assert_no_difference "Visit.count" do | |
get_via_redirect root_path | |
end | |
v.update_attribute(:created_at, 2.days.ago) | |
assert_difference "Visit.count", 1 do | |
get_via_redirect root_path | |
end | |
end | |
# test "cookie generated for the visitor expires ten years from now" | |
# test "only the visit within the last 24 hours is updated" | |
# test "reservation_request_history is updated with the visit_id when reservation_request is created or changed" | |
# test "reservation_request is updated with the visit_id when it becomes available" | |
# test "visitor is unique on visitor_uuid" | |
# test "visit is unique on all fields" | |
# test "geo pages behave correctly | |
test "reservation_request is updated with the visit_id when it becomes available" do | |
get_via_redirect root_path | |
post_via_redirect search_with_error_path, { | |
:search => { | |
"ride_date" => 1.week.from_now.strftime("%m/%d/%Y"), | |
"drop_off_time_hour" => "13", | |
"pickup_place" => "1556 Broadway, San Francisco, CA", | |
"service_type" => "4096", | |
"pickup_time_minute" => "00", | |
"pax" => "2", | |
"drop_off_time_minute" => "00", | |
"pickup_time_hour" => "12" | |
} | |
} | |
assert_response :success | |
assert_not_nil assigns(:search) | |
assert_not_nil assigns(:search_results) | |
# The results is the category and list of vehicles for that type of category | |
get_via_redirect "/reservation_request/#{assigns(:search_results).first[1].first.id}/new" | |
assert_response :success | |
assert_not_nil assigns(:original_search) | |
assert_not_nil assigns(:reservation_request) | |
assert_equal cookies["_ldc_vis"], assigns(:reservation_request).visit.visitor_uuid | |
assert_equal assigns(:reservation_request).visit, | |
assigns(:reservation_request).reservation_request_histories.last.reservation_request.visit | |
end | |
test "search is updated with the visit_id when it becomes available" do | |
get_via_redirect root_path | |
assert_not_nil assigns(:search).visit | |
assert_equal cookies["_ldc_vis"], assigns(:search).visit.visitor_uuid | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment