OSDN Git Service

dc92cb7b656f421f651215f950a5f7108ec9e8f7
[pettanr/pettanr.git] / spec / controllers / balloons_controller_spec.rb
1 # -*- encoding: utf-8 -*-
2 #フキダシ枠
3 require 'spec_helper'
4
5 describe BalloonsController do
6   before do
7     @admin = FactoryGirl.create :admin
8     @user = FactoryGirl.create( :user_yas)
9     @author = FactoryGirl.create :author, :user_id => @user.id
10     @artist = FactoryGirl.create :artist_yas, :author_id => @author.id
11     @sp = FactoryGirl.create :system_picture
12     @lg = FactoryGirl.create :license_group
13     @license = FactoryGirl.create :license, :license_group_id => @lg.id, :system_picture_id => @sp.id
14
15     @speech_balloon_template = FactoryGirl.create :speech_balloon_template, "name" => "circle@pettan.com", "classname" => "CircleSpeechBalloon", "caption" => "cc",  "system_picture_id" => @sp.id, "settings" => '{}'
16     @writing_format = FactoryGirl.create :writing_format
17     @panel = FactoryGirl.create :panel, :author_id => @author.id
18   end
19
20 if MagicNumber['run_mode'] == 1
21   describe '一覧表示に於いて' do
22     before do
23       @sb = FactoryGirl.build :speech_balloon, :panel_id => @panel.id, :speech_balloon_template_id => @speech_balloon_template.id
24       @speech = @sb.build_speech(
25         FactoryGirl.attributes_for(:speech, :writing_format_id => @writing_format.id)
26       )
27       @balloon = @sb.build_balloon(
28         FactoryGirl.attributes_for(:balloon, :system_picture_id => @sp.id)
29       )
30       @sb.boost
31       @sb.save!
32       sign_in @user
33       Balloon.stub(:list).and_return([@balloon, @balloon, @balloon])
34     end
35     context 'パラメータpageについて' do
36       it '@pageに値が入る' do
37         get :index, :page => 5
38         assigns(:page).should eq 5
39       end
40       it '省略されると@pageに1値が入る' do
41         get :index
42         assigns(:page).should eq 1
43       end
44       it '与えられたpage_sizeがセットされている' do
45         get :index, :page_size => 15
46         assigns(:page_size).should eq 15
47       end
48       it '省略されると@page_sizeにデフォルト値が入る' do
49         get :index
50         assigns(:page_size).should eq Balloon.default_page_size
51       end
52       it '最大を超えると@page_sizeにデフォルト最大値が入る' do
53         get :index, :page_size => 1500
54         assigns(:page_size).should eq Balloon.max_page_size
55       end
56       it '不正な値が入ると@page_sizeにデフォルト最大値が入る' do
57         get :index, :page_size => 0
58         assigns(:page_size).should eq Balloon.default_page_size
59       end
60     end
61     context 'つつがなく終わるとき' do
62       it 'ステータスコード200 OKを返す' do
63         get :index
64         response.should be_success 
65       end
66       it 'フキダシ枠モデルに一覧を問い合わせている' do
67         Balloon.should_receive(:list).exactly(1)
68         get :index
69       end
70       it '@balloonsにリストを取得している' do
71         get :index
72         assigns(:balloons).should have_at_least(3).items
73       end
74       context 'html形式' do
75         it 'indexテンプレートを描画する' do
76           get :index
77           response.should render_template("index")
78         end
79       end
80       context 'json形式' do
81         it 'jsonデータを返す' do
82           get :index, :format => :json
83           lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
84         end
85         it 'フキダシ枠モデルにjson一覧出力オプションを問い合わせている' do
86           Balloon.should_receive(:list_json_opt).exactly(1)
87           get :index, :format => :json
88         end
89         it 'データがリスト構造になっている' do
90           get :index, :format => :json
91           json = JSON.parse response.body
92           json.should have_at_least(3).items
93         end
94         it 'リストの先頭くらいはフキダシ枠っぽいものであって欲しい' do
95           get :index, :format => :json
96           json = JSON.parse response.body
97           json.first.has_key?("speech_balloon_id").should be_true
98           json.first.has_key?("x").should be_true
99           json.first.has_key?("system_picture_id").should be_true
100         end
101       end
102     end
103     context 'ユーザ権限がないとき' do
104       before do
105         sign_out @user
106       end
107       context 'html形式' do
108         it 'ステータスコード302 Foundを返す' do
109           get :index
110           response.status.should eq 302
111         end
112         it 'サインインページへ遷移する' do
113           get :index
114           response.should redirect_to '/users/sign_in'
115         end
116       end
117       context 'json形式' do
118         it 'ステータスコード401 Unauthorizedを返す' do
119           get :index, :format => :json
120           response.status.should eq 401
121         end
122         it '応答メッセージにUnauthorizedを返す' do
123           get :index, :format => :json
124           response.message.should match(/Unauthorized/)
125         end
126       end
127     end
128     context 'ユーザ権限はないが管理者権限があるとき' do
129       before do
130         sign_out @user
131         sign_in @admin
132       end
133       it 'ステータスコード200 OKを返す' do
134         get :index
135         response.should be_success 
136       end
137     end
138     context 'ユーザだが作家登録していないとき' do
139       before do
140         @author.destroy
141       end
142       it 'ステータスコード200 OKを返す' do
143         get :index
144         response.should be_success 
145       end
146     end
147   end
148   
149   describe '単体表示に於いて' do
150     before do
151       sign_in @user
152       @sb = FactoryGirl.build :speech_balloon, :panel_id => @panel.id, :speech_balloon_template_id => @speech_balloon_template.id
153       @speech = @sb.build_speech(
154         FactoryGirl.attributes_for(:speech, :writing_format_id => @writing_format.id)
155       )
156       @balloon = @sb.build_balloon(
157         FactoryGirl.attributes_for(:balloon, :system_picture_id => @sp.id)
158       )
159       @sb.boost
160       @sb.save!
161       Balloon.stub(:show).and_return(@balloon)
162     end
163     context 'つつがなく終わるとき' do
164       it 'ステータスコード200 OKを返す' do
165         get :show, :id => @balloon.id
166         response.should be_success
167       end
168       it 'フキダシ枠モデルに単体取得を問い合わせている' do
169         Balloon.should_receive(:show).exactly(1)
170         get :show
171       end
172       it '@balloonにアレを取得している' do
173         get :show, :id => @balloon.id
174         assigns(:balloon).should eq(@balloon)
175       end
176       context 'html形式' do
177         it 'showテンプレートを描画する' do
178           get :show, :id => @balloon.id
179           response.should render_template("show")
180         end
181       end
182       context 'json形式' do
183         it 'jsonデータを返す' do
184           get :show, :id => @balloon.id, :format => :json
185           lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
186         end
187         it 'フキダシ枠モデルにjson単体出力オプションを問い合わせている' do
188           Balloon.should_receive(:show_json_opt).exactly(1)
189           get :show, :id => @balloon.id, :format => :json
190         end
191         it 'データがアレになっている' do
192           get :show, :id => @balloon.id, :format => :json
193           json = JSON.parse response.body
194           json["speech_balloon_id"].should_not be_nil
195           json["x"].should_not be_nil
196           json["system_picture_id"].should_not be_nil
197         end
198       end
199     end
200     context 'ユーザ権限がないとき' do
201       before do
202         sign_out @user
203       end
204       context 'html形式' do
205         it 'ステータスコード302 Foundを返す' do
206           get :show, :id => @balloon.id
207           response.status.should eq 302
208         end
209         it 'サインインページへ遷移する' do
210           get :show, :id => @balloon.id
211           response.body.should redirect_to '/users/sign_in'
212         end
213       end
214       context 'json形式' do
215         it 'ステータスコード401 Unauthorizedを返す' do
216           get :show, :id => @balloon.id, :format => :json
217           response.status.should eq 401
218         end
219         it '応答メッセージにUnauthorizedを返す' do
220           get :show, :id => @balloon.id, :format => :json
221           response.message.should match(/Unauthorized/)
222         end
223       end
224     end
225     context 'ユーザ権限はないが管理者権限があるとき' do
226       before do
227         sign_out @user
228         sign_in @admin
229       end
230       it 'ステータスコード200 OKを返す' do
231         get :show, :id => @balloon.id
232         response.should be_success 
233       end
234     end
235     context 'ユーザだが作家登録していないとき' do
236       before do
237         @author.destroy
238       end
239       it 'ステータスコード200 OKを返す' do
240         get :show, :id => @balloon.id
241         response.should be_success 
242       end
243     end
244   end
245   
246 else
247   describe '一覧表示に於いて' do
248     before do
249       @sb = FactoryGirl.build :speech_balloon, :panel_id => @panel.id, :speech_balloon_template_id => @speech_balloon_template.id
250       @speech = @sb.build_speech(
251         FactoryGirl.attributes_for(:speech, :writing_format_id => @writing_format.id)
252       )
253       @balloon = @sb.build_balloon(
254         FactoryGirl.attributes_for(:balloon, :system_picture_id => @sp.id)
255       )
256       @sb.boost
257       @sb.save!
258       sign_in @user
259       Balloon.stub(:list).and_return([@balloon, @balloon, @balloon])
260     end
261     context 'つつがなく終わるとき' do
262       it 'ステータスコード200 OKを返す' do
263         get :index
264         response.should be_success 
265       end
266       context 'html形式' do
267         it 'indexテンプレートを描画する' do
268           get :index
269           response.should render_template("index")
270         end
271       end
272       context 'json形式' do
273         it 'jsonデータを返す' do
274           get :index, :format => :json
275           lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
276         end
277       end
278     end
279     context 'ユーザ権限がないとき' do
280       before do
281         sign_out @user
282       end
283       it 'ステータスコード200 OKを返す' do
284         get :index
285         response.should be_success 
286       end
287       context 'html形式' do
288         it 'indexテンプレートを描画する' do
289           get :index
290           response.should render_template("index")
291         end
292       end
293       context 'json形式' do
294         it 'jsonデータを返す' do
295           get :index, :format => :json
296           lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
297         end
298       end
299     end
300   end
301   
302   describe '単体表示に於いて' do
303     before do
304       sign_in @user
305       @sb = FactoryGirl.build :speech_balloon, :panel_id => @panel.id, :speech_balloon_template_id => @speech_balloon_template.id
306       @speech = @sb.build_speech(
307         FactoryGirl.attributes_for(:speech, :writing_format_id => @writing_format.id)
308       )
309       @balloon = @sb.build_balloon(
310         FactoryGirl.attributes_for(:balloon, :system_picture_id => @sp.id)
311       )
312       @sb.boost
313       @sb.save!
314       Balloon.stub(:show).and_return(@balloon)
315     end
316     context 'つつがなく終わるとき' do
317       it 'ステータスコード200 OKを返す' do
318         get :show, :id => @balloon.id
319         response.should be_success
320       end
321       context 'html形式' do
322         it 'showテンプレートを描画する' do
323           get :show, :id => @balloon.id
324           response.should render_template("show")
325         end
326       end
327       context 'json形式' do
328         it 'jsonデータを返す' do
329           get :show, :id => @balloon.id, :format => :json
330           lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
331         end
332       end
333     end
334     context 'ユーザ権限がないとき' do
335       before do
336         sign_out @user
337       end
338       it 'ステータスコード200 OKを返す' do
339         get :show, :id => @balloon.id
340         response.should be_success
341       end
342       context 'html形式' do
343         it 'showテンプレートを描画する' do
344           get :show, :id => @balloon.id
345           response.should render_template("show")
346         end
347       end
348       context 'json形式' do
349         it 'jsonデータを返す' do
350           get :show, :id => @balloon.id, :format => :json
351           lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
352         end
353       end
354     end
355   end
356   
357 end
358 end