choices_spec.rb
4.9 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
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
after do
post_auth question_choices_path(@question), @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), @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)
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), :explanation => "foo"
response.should be_success
response.should have_tag "choice active", "false"
end
it "should fail when trying to flag another site's choices" do
other_user = Factory(:email_confirmed_user)
put_auth other_user, flag_question_choice_path(@question, @choice), :explanation => "foo"
response.should_not be_success
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)
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), :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), :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), :offset => 2, :limit => 4
response.should be_success
response.should have_tag "choices choice", 3
end
it "should fail when trying to access another site's choices" do
other_user = Factory(:email_confirmed_user)
get_auth other_user, question_choices_path(@question), :offset => 2, :limit => 4
response.should_not be_success
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)
response.should be_success
response.should have_tag "choice", 1
end
it "should fail when requesting a choice from another site" do
other_user = Factory(:email_confirmed_user)
get_auth other_user, question_choice_path(@question, @choice)
response.should_not be_success
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), params
response.should be_success
end
it "should fail when updating another site's choice" do
other_user = Factory(:email_confirmed_user)
params = { :choice => { :data => "foo" } }
put_auth other_user, question_choice_path(@question, @choice), params
response.should_not be_success
end
end
end