diff options
-rw-r--r-- | app/controllers/track_controller.rb | 6 | ||||
-rw-r--r-- | spec/controllers/track_controller_spec.rb | 46 |
2 files changed, 49 insertions, 3 deletions
diff --git a/app/controllers/track_controller.rb b/app/controllers/track_controller.rb index 708dd8737..afe10be91 100644 --- a/app/controllers/track_controller.rb +++ b/app/controllers/track_controller.rb @@ -216,12 +216,12 @@ class TrackController < ApplicationController # Track interest in a request from a non-logged in user def widget_vote - info_request = InfoRequest.find(params[:info_request_id]) + @info_request = InfoRequest.find(params[:info_request_id]) if not @user and cookies[:widget_vote] - wv = info_request.widget_votes.where(:cookie => cookies[:widget_vote]).first_or_create + wv = @info_request.widget_votes.where(:cookie => cookies[:widget_vote]).first_or_create end - track_thing = TrackThing.create_track_for_request(info_request) + track_thing = TrackThing.create_track_for_request(@info_request) redirect_to do_track_path(track_thing), status => :temporary_redirect end end diff --git a/spec/controllers/track_controller_spec.rb b/spec/controllers/track_controller_spec.rb index 29f5c7fe1..b7f679cdb 100644 --- a/spec/controllers/track_controller_spec.rb +++ b/spec/controllers/track_controller_spec.rb @@ -275,3 +275,49 @@ describe TrackController, "when tracking a public body" do end end + +describe TrackController do + include LinkToHelper + + describe :widget_vote do + + before do + @info_request = FactoryGirl.create(:info_request) + end + + it 'should find the info request' do + get :widget_vote, :info_request_id => @info_request.id + assigns[:info_request].should == @info_request + end + + it 'should redirect to the track path for the info request' do + get :widget_vote, :info_request_id => @info_request.id + track_thing = TrackThing.create_track_for_request(@info_request) + expect(response).to redirect_to(do_track_path(track_thing)) + end + + context 'when there is no logged-in user and a widget vote cookie' do + + before do + @cookie_value = 'x' * 20 + end + + it 'should create a widget vote if none exists for the info request and cookie' do + @info_request.widget_votes.where(:cookie => @cookie_value).size.should == 0 + request.cookies['widget_vote'] = @cookie_value + get :widget_vote, :info_request_id => @info_request.id + @info_request.widget_votes.where(:cookie => @cookie_value).size.should == 1 + end + + it 'should not create a widget vote if one exists for the info request and cookie' do + @info_request.widget_votes.create(:cookie => @cookie_value) + request.cookies['widget_vote'] = @cookie_value + get :widget_vote, :info_request_id => @info_request.id + @info_request.widget_votes.where(:cookie => @cookie_value).size.should == 1 + end + + end + + end + +end |