aboutsummaryrefslogtreecommitdiffstats
path: root/spec/controllers/widgets_controller_spec.rb
blob: 28f6c19048386ae6a6c064deffb1caf9ea441f84 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# coding: utf-8
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')

describe WidgetsController do

    include LinkToHelper

    describe "#show" do

        before do
            @info_request = FactoryGirl.create(:info_request)
            AlaveteliConfiguration.stub!(:enable_widgets).and_return(true)
        end

        it 'should render the widget template' do
            get :show, :request_id => @info_request.id
            expect(response).to render_template('show')
        end

        it 'should find the info request' do
            get :show, :request_id => @info_request.id
            assigns[:info_request].should == @info_request
        end

        it 'should create a track thing for the request' do
            get :show, :request_id => @info_request.id
            assigns[:track_thing].info_request.should == @info_request
        end

        it 'should assign the request status' do
            get :show, :request_id => @info_request.id
            assigns[:status].should == @info_request.calculate_status
        end

        context 'for a non-logged-in user' do

            context 'if no widget-vote cookie is set' do

                it 'should set a widget-vote cookie' do
                    cookies[:widget_vote].should be_nil
                    get :show, :request_id => @info_request.id
                    cookies[:widget_vote].should_not be_nil
                end

            end

        end

        context 'when widgets are not enabled' do

            it 'should return a 404' do
                AlaveteliConfiguration.stub!(:enable_widgets).and_return(false)
                lambda{ get :show, :request_id => @info_request.id }.should
                    raise_error(ActiveRecord::RecordNotFound)
            end

        end


    end

    describe "#new" do

        before do
            @info_request = FactoryGirl.create(:info_request)
            AlaveteliConfiguration.stub!(:enable_widgets).and_return(true)
        end

        it 'should render the create widget template' do
            get :new, :request_id => @info_request.id
            expect(response).to render_template('new')
        end

        it 'should find the info request' do
            get :new, :request_id => @info_request.id
            assigns[:info_request].should == @info_request
        end

        context 'when widgets are not enabled' do

            it 'should return a 404' do
                AlaveteliConfiguration.stub!(:enable_widgets).and_return(false)
                lambda{ get :new, :request_id => @info_request.id }.should
                    raise_error(ActiveRecord::RecordNotFound)
            end

        end

    end

    describe :update do

        before do
            @info_request = FactoryGirl.create(:info_request)
            AlaveteliConfiguration.stub!(:enable_widgets).and_return(true)
        end

        it 'should find the info request' do
            get :update, :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 :update, :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 :update, :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 :update, :request_id => @info_request.id
                @info_request.widget_votes.where(:cookie => @cookie_value).size.should == 1
            end

        end

        context 'when widgets are not enabled' do

            it 'should raise ActiveRecord::RecordNotFound' do
                AlaveteliConfiguration.stub!(:enable_widgets).and_return(false)
                lambda{ get :update, :request_id => @info_request.id }.should
                    raise_error(ActiveRecord::RecordNotFound)
            end

        end

    end

end