· 8 years ago · Aug 06, 2018, 02:14 AM
1require 'test_helper'
2require 'integration/integration_test_helpers'
3
4class VisitorTrackingCookieFlowsTest < ActionController::IntegrationTest
5
6 setup do
7 Visitor.delete_all
8 Visit.delete_all
9 Search.delete_all
10 SearchResult.delete_all
11 ReservationRequest.delete_all
12 SearchResultOperator.delete_all
13 end
14
15 ## tests of the _ldc_vis cookie
16 test "visiting the site sets a visitor cookie" do
17 # make sure the session starts empty
18 assert cookies.empty?
19
20 # go to a page on the site and make sure the visitor cookie is set
21 post_via_redirect root_path
22 assert cookies.has_key?("_ldc_sess")
23 assert cookies.has_key?("_ldc_vis")
24 end
25
26 test "visitor id from cookie is not stored if it already exists in the visitors table" do
27 # add a visitor to the database with the visitor id
28 Visitor.create(:visitor_uuid => "bb2eeaaf")
29
30 # set the cookie to be passed to the request
31 cookies["_ldc_vis"] = "bb2eeaaf"
32
33 # check that the visitor was not created
34 assert_no_difference "Visitor.count" do
35 post_via_redirect root_path
36 end
37 end
38
39 test "visitor id from cookie is stored if it does not already exist in the visitors table" do
40 # set the cookie to be passed to the request
41 cookies["_ldc_vis"] = "bb2eeaag"
42
43 # check that the visitor was created
44 assert_difference "Visitor.count", 1 do
45 post_via_redirect root_path
46 end
47 end
48
49 test "visitor id is generated if no _ldc_vis cookie is provided" do
50 assert !cookies.has_key?("_ldc_vis")
51 get_via_redirect root_path
52 assert cookies["_ldc_vis"].present?
53 assert_equal 32, cookies["_ldc_vis"].length
54 end
55
56 ## tests of the visits that are created
57 test "a visit is created with just the visitor_uuid on the first hit to the page" do
58 assert cookies.empty?
59
60 assert_difference "Visit.count", 1 do
61 get_via_redirect root_path
62 end
63 uuid = Visit.last.visitor_uuid
64 assert_equal 32, uuid.length
65 assert_equal cookies['_ldc_vis'], uuid
66 end
67
68 test "a visit is updated with session ids once it is available in the flow" do
69 # hit the first page to generate the session and visitor ids
70 get_via_redirect root_path
71 assert cookies.has_key?('_ldc_sess')
72 assert cookies['_ldc_sess'].present?
73 assert cookies.has_key?('_ldc_vis')
74 assert cookies['_ldc_vis'].present?
75
76 # after the first hit, the session_id won't be set in the database
77 v = Visit.last
78 assert_nil v.session_id
79
80 # hit the second page and see if the session id has been updated properly
81 # also ensure that a second visit isn't created
82 assert_no_difference "Visit.count" do
83 post_via_redirect root_path
84 end
85 v.reload
86 assert_not_nil v.session_id
87 assert_equal cookies['_ldc_sess'], v.session_id
88
89 # they haven't logged in yet, so make sure the visit doesn't have a user_id
90 assert_nil v.user_id
91 end
92
93 test "a visit is updated with user id once they log in" do
94 # ensure that they aren't already logged in
95 Capybara.reset_sessions!
96 assert_difference "Visit.count", 1 do
97 visit root_path
98 end
99 click_link("Sign Out.") if page.has_content?("Sign Out.")
100
101 # after the first hit, the user id won't be set in the database
102 v = Visit.last
103 assert_nil v.user_id
104
105 # log them in
106 u = User.find_by_email("cedric.bouffet@gmail.com")
107 assert_no_difference "Visit.count" do
108 click_link("Sign In.")
109 fill_in("email", :with => u.email)
110 fill_in("password", :with => "password")
111 click_button("signin_btn")
112 wait_until { page.has_content?("Hello Cedric.") }
113 end
114
115 # see if the user id has been updated properly
116 v.reload
117 assert_not_nil v.user_id
118 assert_equal u[:id], v.user_id
119 end
120
121 test "two visits more than 24 hours apart create two visitor users" do
122 v = Visit.create!(:visitor_uuid => UUID.generate(:compact))
123 cookies["_ldc_vis"] = v.visitor_uuid
124
125 # when it's less than 24 hours, no record is created
126 assert_no_difference "Visit.count" do
127 get_via_redirect root_path
128 end
129 v.update_attribute(:created_at, 2.days.ago)
130 assert_difference "Visit.count", 1 do
131 get_via_redirect root_path
132 end
133 end
134
135 # test "cookie generated for the visitor expires ten years from now"
136 # test "only the visit within the last 24 hours is updated"
137 # test "reservation_request_history is updated with the visit_id when reservation_request is created or changed"
138 # test "reservation_request is updated with the visit_id when it becomes available"
139 # test "visitor is unique on visitor_uuid"
140 # test "visit is unique on all fields"
141 # test "geo pages behave correctly
142
143
144 test "reservation_request is updated with the visit_id when it becomes available" do
145 get_via_redirect root_path
146 post_via_redirect search_with_error_path, {
147 :search => {
148 "ride_date" => 1.week.from_now.strftime("%m/%d/%Y"),
149 "drop_off_time_hour" => "13",
150 "pickup_place" => "1556 Broadway, San Francisco, CA",
151 "service_type" => "4096",
152 "pickup_time_minute" => "00",
153 "pax" => "2",
154 "drop_off_time_minute" => "00",
155 "pickup_time_hour" => "12"
156 }
157 }
158 assert_response :success
159 assert_not_nil assigns(:search)
160 assert_not_nil assigns(:search_results)
161
162 # The results is the category and list of vehicles for that type of category
163 get_via_redirect "/reservation_request/#{assigns(:search_results).first[1].first.id}/new"
164 assert_response :success
165
166 assert_not_nil assigns(:original_search)
167 assert_not_nil assigns(:reservation_request)
168 assert_equal cookies["_ldc_vis"], assigns(:reservation_request).visit.visitor_uuid
169 assert_equal assigns(:reservation_request).visit,
170 assigns(:reservation_request).reservation_request_histories.last.reservation_request.visit
171 end
172
173 test "search is updated with the visit_id when it becomes available" do
174 get_via_redirect root_path
175 assert_not_nil assigns(:search).visit
176 assert_equal cookies["_ldc_vis"], assigns(:search).visit.visitor_uuid
177 end
178
179end