choices_spec.rb
5.88 KB
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
describe "Choices" do
include IntegrationSupport
describe "POST 'create'" do
before(:each) do
@question = Factory.create(:aoi_question, :site => @api_user)
@visitor = Factory.create(:visitor, :site => @api_user)
end
describe "succeeds and returns a new choice" do
specify "given no optional arguments"# do @params = nil end
specify "given only data" do
@params = { :choice => { :data => "hey"} }
end
specify "given only the visitor identifier" do
@params = { :choice => { :visitor_identifier => @visitor.identifier } }
end
after do
post_auth question_choices_path(@question, :format => 'xml'), @params
response.should be_success
response.should have_tag "choice"
end
end
it "correctly sets the supplied attributes" do
@params = {
:choice => {
:visitor_identifier => @visitor.identifier,
:data => "foo",
:local_identifier => "bar" } }
post_auth question_choices_path(@question, :format => 'xml'), @params
response.should be_success
response.should have_tag "choice creator-id", @visitor.id.to_s
response.should have_tag "choice data", "foo"
response.should have_tag "choice local-identifier", "bar"
end
end
describe "PUT 'flag'" do
before do
@question = Factory.create(:aoi_question, :site => @api_user)
@choice = Factory.create(:choice, :question => @question)
@choice.activate!
end
it "should return the deactivated choice given no arguments" do
put_auth flag_question_choice_path(@question, @choice, :format => 'xml')
response.should be_success
response.should have_tag "choice active", "false"
end
it "should return the deactivated choice given an explanation" do
put_auth flag_question_choice_path(@question, @choice, :format => 'xml'), :explanation => "foo"
response.should be_success
response.should have_tag "choice active", "false"
end
context "when trying to flag another site's choices" do
before do
# this is ugly
@orig_user = @api_user
@api_user = Factory(:email_confirmed_user)
end
it "should fail" do
put_auth flag_question_choice_path(@question, @choice, :format => 'xml'), :explanation => "foo"
response.should_not be_success
end
after { @api_user = @orig_user }
end
end
describe "GET 'index'" do
before(:each) do
@question = Factory.create(:aoi_question, :site => @api_user, :choices => [], :prompts => [])
5.times{ Factory.create(:choice, :question => @question).deactivate! }
5.times{ Factory.create(:choice, :question => @question).activate! }
end
it "should return all active choices given no optional parameters" do
get_auth question_choices_path(@question, :format => 'xml')
response.should be_success
response.should have_tag "choices choice", 5
end
it "should return all choices if include_inactive is set" do
get_auth question_choices_path(@question, :format => 'xml'), :include_inactive => true
response.should be_success
response.should have_tag "choices choice", 10
response.should have_tag "choices choice active", "false"
end
it "should return 3 choices when limt is set to 3" do
get_auth question_choices_path(@question, :format => 'xml'), :limit => 3
response.should be_success
response.should have_tag "choices choice", 3
end
it "should return the remaining choices when offset is provided" do
get_auth question_choices_path(@question, :format => 'xml'), :offset => 2, :limit => 4
response.should be_success
response.should have_tag "choices choice", 3
end
context "when trying to access another site's choices" do
before do
@orig_user = @api_user
@api_user = Factory(:email_confirmed_user)
end
it "should fail" do
get_auth question_choices_path(@question, :format => 'xml'), :offset => 2, :limit => 4
response.should_not be_success
end
after { @api_user = @orig_user }
end
end
describe "GET 'show'" do
before do
@question = Factory.create(:aoi_question, :site => @api_user)
@choice = Factory.create(:choice, :question => @question)
end
it "should return a choice" do
get_auth question_choice_path(@question, @choice, :format => 'xml')
response.should be_success
response.should have_tag "choice", 1
end
context "when requesting a choice from another site" do
before do
@other_user = Factory(:email_confirmed_user)
@other_question = Factory.create(:aoi_question, :site => @other_user)
@other_choice = Factory.create(:choice, :question => @other_question)
end
it "should fail" do
get_auth question_choice_path(@other_question, @other_choice, :format => 'xml')
response.should_not be_success
end
end
end
describe "PUT 'update'" do
before do
@question = Factory.create(:aoi_question, :site => @api_user)
@choice = Factory.create(:choice, :question => @question)
@choice.activate!
end
it "should succeed given valid attributes" do
params = { :choice => { :data => "foo" } }
put_auth question_choice_path(@question, @choice, :format => 'xml'), params
response.should be_success
end
context "when updatng another site's choice" do
before do
@orig_user = @api_user
@api_user = Factory(:email_confirmed_user)
end
it "should fail" do
params = { :choice => { :data => "foo" } }
put_auth question_choice_path(@question, @choice, :format => 'xml'), params
response.should_not be_success
end
after { @api_user = @orig_user }
end
end
end