1 # -*- encoding: utf-8 -*-
5 describe SpeechBalloon 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 @other_user = FactoryGirl.create( :user_yas)
12 @other_author = FactoryGirl.create :author, :user_id => @other_user.id
13 @other_artist = FactoryGirl.create :artist_yas, :author_id => @other_author.id
14 @sp = FactoryGirl.create :system_picture
15 @lg = FactoryGirl.create :license_group
16 @license = FactoryGirl.create :license, :license_group_id => @lg.id, :system_picture_id => @sp.id
18 @panel = FactoryGirl.create :panel, :author_id => @author.id
19 @speech_balloon_template = FactoryGirl.create :speech_balloon_template
24 @sb = FactoryGirl.build :speech_balloon, :panel_id => @panel.id, :speech_balloon_template_id => @speech_balloon_template.id
27 context 'オーソドックスなデータのとき' do
35 @sb.classname = 'a'*50
42 context 'panel_idを検証するとき' do
43 #ネストの保存はnilを許可しなければならないので数値チェックだけ
46 @sb.should_not be_valid
49 context 'speech_balloon_template_idを検証するとき' do
51 @sb.speech_balloon_template_id = nil
52 @sb.should_not be_valid
55 @sb.speech_balloon_template_id = 'a'
56 @sb.should_not be_valid
58 it '存在するフキダシテンプレートでなければ失敗する' do
59 @sb.speech_balloon_template_id = 0
60 @sb.should_not be_valid
63 context 'classnameを検証するとき' do
66 @sb.should_not be_valid
69 @sb.classname = 'a'*51
70 @sb.should_not be_valid
76 @sb.should_not be_valid
80 @sb.should_not be_valid
84 @sb.should_not be_valid
87 context 'settingsを検証するとき' do
91 describe '文字コード検証に於いて' do
93 @sb = FactoryGirl.build :speech_balloon, :panel_id => @panel.id, :speech_balloon_template_id => @speech_balloon_template.id
96 context 'classnameを検証するとき' do
97 it 'Shift JISなら失敗する' do
98 @sb.classname = "\x83G\x83r\x83]\x83D"
101 }.should raise_error(Pettanr::BadRequest)
105 context 'settingsを検証するとき' do
106 it 'Shift JISなら失敗する' do
107 @sb.settings = "\x83G\x83r\x83]\x83D"
110 }.should raise_error(Pettanr::BadRequest)
114 context 'captionを検証するとき' do
115 it 'Shift JISなら失敗する' do
116 @sb.caption = "\x83G\x83r\x83]\x83D"
119 }.should raise_error(Pettanr::BadRequest)
125 describe '閲覧許可に於いて' do
127 @sb = FactoryGirl.create :speech_balloon, :panel_id => @panel.id, :speech_balloon_template_id => @speech_balloon_template.id
129 context 'オープンモードのとき' do
131 MagicNumber['run_mode'] = 0
133 it '自身にゲスト用ロールチェックを問い合わせしている' do
134 SpeechBalloon.any_instance.stub(:guest_role_check).and_return(true)
135 SpeechBalloon.any_instance.should_receive(:guest_role_check).with(any_args).exactly(1)
136 r = @sb.visible?([@author])
138 it 'ゲスト用ロールチェックが失敗したとき、falseを返す' do
139 SpeechBalloon.any_instance.stub(:guest_role_check).and_return(false)
140 r = @sb.visible?([@author])
144 context 'クローズドモードのとき' do
146 MagicNumber['run_mode'] = 1
148 it '自身に読者用ロールチェックを問い合わせしている' do
149 SpeechBalloon.any_instance.stub(:reader_role_check).and_return(true)
150 SpeechBalloon.any_instance.should_receive(:reader_role_check).with(any_args).exactly(1)
151 r = @sb.visible?([@author])
153 it '読者用ロールチェックが失敗したとき、falseを返す' do
154 SpeechBalloon.any_instance.stub(:reader_role_check).and_return(false)
155 r = @sb.visible?([@author])
159 context '事前チェックする' do
161 MagicNumber['run_mode'] = 1
162 SpeechBalloon.any_instance.stub(:reader_role_check).and_return(true)
164 it '自身のコマに所持判定を問い合わせしている' do
165 Panel.any_instance.stub(:own?).and_return(true)
166 Panel.any_instance.should_receive(:own?).with(any_args).exactly(1)
167 r = @sb.visible?([@author])
169 it '自身のコマに閲覧許可を問い合わせしている' do
170 Panel.any_instance.stub(:own?).and_return(false)
171 Panel.any_instance.stub(:visible?).and_return(true)
172 Panel.any_instance.should_receive(:visible?).with(any_args).exactly(1)
173 r = @sb.visible?([@author])
176 context 'つつがなく終わるとき' do
178 MagicNumber['run_mode'] = 1
179 Panel.any_instance.stub(:reader_role_check).and_return(true)
181 it '自分のコマのフキダシなら許可する' do
182 Panel.any_instance.stub(:own?).and_return(true)
183 Panel.any_instance.stub(:visible?).and_return(false)
184 r = @sb.visible?([@author])
187 it '他人の非公開コマのフキダシなら許可しない' do
188 Panel.any_instance.stub(:own?).and_return(false)
189 Panel.any_instance.stub(:visible?).and_return(false)
190 r = @sb.visible?([@author])
193 it '他人のコマのフキダシでも公開なら許可する' do
194 Panel.any_instance.stub(:own?).and_return(false)
195 Panel.any_instance.stub(:visible?).and_return(true)
196 r = @sb.visible?([@author])
202 describe '一覧取得に於いて' do
204 @sb = FactoryGirl.create :speech_balloon, :panel_id => @panel.id, :speech_balloon_template_id => @speech_balloon_template.id
205 @balloon = FactoryGirl.create :balloon, :speech_balloon_id => @sb.id, :system_picture_id => @sp.id
206 @speech = FactoryGirl.create :speech, :speech_balloon_id => @sb.id
208 context 'page補正について' do
209 it '文字列から数値に変換される' do
210 SpeechBalloon.page('8').should eq 8
213 SpeechBalloon.page().should eq 1
216 SpeechBalloon.page('0').should eq 1
219 context 'page_size補正について' do
220 it '文字列から数値に変換される' do
221 SpeechBalloon.page_size('7').should eq 7
223 it 'nilの場合はSpeechBalloon.default_page_sizeになる' do
224 SpeechBalloon.page_size().should eq SpeechBalloon.default_page_size
226 it '0以下の場合はSpeechBalloon.default_page_sizeになる' do
227 SpeechBalloon.page_size('0').should eq SpeechBalloon.default_page_size
229 it 'SpeechBalloon.max_page_sizeを超えた場合はSpeechBalloon.max_page_sizeになる' do
230 SpeechBalloon.page_size('1000').should eq SpeechBalloon.max_page_size
233 context 'つつがなく終わるとき' do
234 it '一覧取得オプションを利用している' do
235 SpeechBalloon.stub(:list_opt).with(any_args).and_return({:include => :panel})
236 SpeechBalloon.should_receive(:list_opt).with(any_args).exactly(1)
237 r = SpeechBalloon.list
241 r = SpeechBalloon.list
245 #公開されたコマのフキダシは(他人のフキダシであっても)含んでいる
246 hc = FactoryGirl.create :panel, :author_id => @other_author.id, :publish => 1
247 npl = FactoryGirl.create :speech_balloon, :panel_id => hc.id, :speech_balloon_template_id => @speech_balloon_template.id, :updated_at => Time.now + 100
248 r = SpeechBalloon.list
249 r.should eq [npl, @sb]
251 it '非公開のコマのフキダシは自分のフキダシであっても含まない' do
252 hc = FactoryGirl.create :panel, :author_id => @author.id, :publish => 0
253 npl = FactoryGirl.create :speech_balloon, :panel_id => hc.id, :speech_balloon_template_id => @speech_balloon_template.id, :updated_at => Time.now + 100
254 pl = SpeechBalloon.list
257 context 'DBに5件あって1ページの件数を2件に変えたとして' do
259 @sb2 = FactoryGirl.create :speech_balloon, :panel_id => @panel.id, :speech_balloon_template_id => @speech_balloon_template.id, :t => 1, :updated_at => Time.now + 100
260 @sb3 = FactoryGirl.create :speech_balloon, :panel_id => @panel.id, :speech_balloon_template_id => @speech_balloon_template.id, :t => 2, :updated_at => Time.now + 200
261 @sb4 = FactoryGirl.create :speech_balloon, :panel_id => @panel.id, :speech_balloon_template_id => @speech_balloon_template.id, :t => 3, :updated_at => Time.now + 300
262 @sb5 = FactoryGirl.create :speech_balloon, :panel_id => @panel.id, :speech_balloon_template_id => @speech_balloon_template.id, :t => 4, :updated_at => Time.now + 400
263 SpeechBalloon.stub(:default_page_size).and_return(2)
266 pl = SpeechBalloon.list
267 pl.should have(2).items
269 it 'page=1なら末尾2件を返す' do
271 pl = SpeechBalloon.list(1)
272 pl.should eq [@sb5, @sb4]
274 it 'page=2なら中間2件を返す' do
275 pl = SpeechBalloon.list(2)
276 pl.should eq [@sb3, @sb2]
278 it 'page=3なら先頭1件を返す' do
279 pl = SpeechBalloon.list(3)
283 context 'DBに5件あって1ページの件数を0件に変えたとして' do
285 @sb2 = FactoryGirl.create :speech_balloon, :panel_id => @panel.id, :speech_balloon_template_id => @speech_balloon_template.id, :t => 1, :updated_at => Time.now + 100
286 @sb3 = FactoryGirl.create :speech_balloon, :panel_id => @panel.id, :speech_balloon_template_id => @speech_balloon_template.id, :t => 2, :updated_at => Time.now + 200
287 @sb4 = FactoryGirl.create :speech_balloon, :panel_id => @panel.id, :speech_balloon_template_id => @speech_balloon_template.id, :t => 3, :updated_at => Time.now + 300
288 @sb5 = FactoryGirl.create :speech_balloon, :panel_id => @panel.id, :speech_balloon_template_id => @speech_balloon_template.id, :t => 4, :updated_at => Time.now + 400
289 SpeechBalloon.stub(:default_page_size).and_return(2)
292 r = SpeechBalloon.list 5, 0
293 r.should have(5).items
297 describe '一覧取得オプションに於いて' do
298 it 'includeキーを含んでいる' do
299 r = SpeechBalloon.list_opt
300 r.has_key?(:include).should be_true
303 r = SpeechBalloon.list_opt[:include]
304 r.should have(4).items
307 r = SpeechBalloon.list_opt[:include]
308 r.has_key?(:panel).should be_true
311 r = SpeechBalloon.list_opt[:include]
312 r[:panel].has_key?(:author).should be_true
315 r = SpeechBalloon.list_opt[:include]
316 r.has_key?(:balloon).should be_true
319 r = SpeechBalloon.list_opt[:include]
320 r.has_key?(:speech).should be_true
322 it 'フキダシテンプレートを含んでいる' do
323 r = SpeechBalloon.list_opt[:include]
324 r.has_key?(:speech_balloon_template).should be_true
327 describe 'json一覧出力オプションに於いて' do
329 @panel = FactoryGirl.create :panel, :author_id => @author.id, :publish => 1
330 @sb = FactoryGirl.create :speech_balloon, :panel_id => @panel.id, :speech_balloon_template_id => @speech_balloon_template.id
331 @balloon = FactoryGirl.create :balloon, :speech_balloon_id => @sb.id, :system_picture_id => @sp.id
332 @speech = FactoryGirl.create :speech, :speech_balloon_id => @sb.id
335 r = SpeechBalloon.list.to_json SpeechBalloon.list_json_opt
338 i.has_key?('panel').should be_true
341 r = SpeechBalloon.list.to_json SpeechBalloon.list_json_opt
345 s.has_key?('author').should be_true
348 r = SpeechBalloon.list.to_json SpeechBalloon.list_json_opt
351 i.has_key?('balloon').should be_true
354 r = SpeechBalloon.list.to_json SpeechBalloon.list_json_opt
357 i.has_key?('speech').should be_true
359 it 'フキダシテンプレートを含んでいる' do
360 r = SpeechBalloon.list.to_json SpeechBalloon.list_json_opt
363 i.has_key?('speech_balloon_template').should be_true
367 describe '単体取得に於いて' do
369 @sb = FactoryGirl.create :speech_balloon, :panel_id => @panel.id, :speech_balloon_template_id => @speech_balloon_template.id
370 @balloon = FactoryGirl.create :balloon, :speech_balloon_id => @sb.id, :system_picture_id => @sp.id
371 @speech = FactoryGirl.create :speech, :speech_balloon_id => @sb.id
373 context 'つつがなく終わるとき' do
374 it '単体取得オプションを利用している' do
375 SpeechBalloon.stub(:show_opt).with(any_args).and_return({:include => :panel})
376 SpeechBalloon.should_receive(:show_opt).with(any_args).exactly(1)
377 r = SpeechBalloon.show @sb.id, @author
379 it '閲覧許可を問い合わせている' do
380 SpeechBalloon.any_instance.stub(:visible?).with(any_args).and_return(true)
381 SpeechBalloon.any_instance.should_receive(:visible?).with(any_args).exactly(1)
382 r = SpeechBalloon.show @sb.id, @author
386 SpeechBalloon.any_instance.stub(:visible?).and_return(true)
387 pl = SpeechBalloon.show @sb.id, @author
390 context '閲覧許可が出なかったとき' do
391 it '403Forbidden例外を返す' do
392 SpeechBalloon.any_instance.stub(:visible?).and_return(false)
394 SpeechBalloon.show @sb.id, @author
395 }.should raise_error(ActiveRecord::Forbidden)
398 context '存在しないフキダシを開こうとしたとき' do
399 it '404RecordNotFound例外を返す' do
401 SpeechBalloon.show 110, @author
402 }.should raise_error(ActiveRecord::RecordNotFound)
406 describe '単体取得オプションに於いて' do
407 it 'includeキーを含んでいる' do
408 r = SpeechBalloon.show_opt
409 r.has_key?(:include).should be_true
412 r = SpeechBalloon.show_opt[:include]
413 r.should have(4).items
416 r = SpeechBalloon.show_opt[:include]
417 r.has_key?(:panel).should be_true
420 r = SpeechBalloon.show_opt[:include]
421 r[:panel].has_key?(:author).should be_true
424 r = SpeechBalloon.show_opt[:include]
425 r.has_key?(:balloon).should be_true
428 r = SpeechBalloon.show_opt[:include]
429 r.has_key?(:speech).should be_true
431 it 'フキダシテンプレートを含んでいる' do
432 r = SpeechBalloon.show_opt[:include]
433 r.has_key?(:speech_balloon_template).should be_true
436 describe 'json単体出力オプションに於いて' do
438 @panel = FactoryGirl.create :panel, :author_id => @author.id, :publish => 1
439 @sb = FactoryGirl.create :speech_balloon, :panel_id => @panel.id, :speech_balloon_template_id => @speech_balloon_template.id
440 @balloon = FactoryGirl.create :balloon, :speech_balloon_id => @sb.id, :system_picture_id => @sp.id
441 @speech = FactoryGirl.create :speech, :speech_balloon_id => @sb.id
444 r = SpeechBalloon.show(@sb.id, @author).to_json SpeechBalloon.show_json_opt
447 i.has_key?('panel').should be_true
450 r = SpeechBalloon.show(@sb.id, @author).to_json SpeechBalloon.show_json_opt
454 s.has_key?('author').should be_true
457 r = SpeechBalloon.show(@sb.id, @author).to_json SpeechBalloon.show_json_opt
460 i.has_key?('balloon').should be_true
463 r = SpeechBalloon.show(@sb.id, @author).to_json SpeechBalloon.show_json_opt
466 i.has_key?('speech').should be_true
468 it 'フキダシテンプレートを含んでいる' do
469 r = SpeechBalloon.show(@sb.id, @author).to_json SpeechBalloon.show_json_opt
472 i.has_key?('speech_balloon_template').should be_true