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)
115 describe '閲覧許可に於いて' do
117 @sb = FactoryGirl.create :speech_balloon, :panel_id => @panel.id, :speech_balloon_template_id => @speech_balloon_template.id
119 context 'オープンモードのとき' do
121 MagicNumber['run_mode'] = 0
123 it '自身にゲスト用ロールチェックを問い合わせしている' do
124 SpeechBalloon.any_instance.stub(:guest_role_check).and_return(true)
125 SpeechBalloon.any_instance.should_receive(:guest_role_check).with(any_args).exactly(1)
126 r = @sb.visible?([@author])
128 it 'ゲスト用ロールチェックが失敗したとき、falseを返す' do
129 SpeechBalloon.any_instance.stub(:guest_role_check).and_return(false)
130 r = @sb.visible?([@author])
134 context 'クローズドモードのとき' do
136 MagicNumber['run_mode'] = 1
138 it '自身に読者用ロールチェックを問い合わせしている' do
139 SpeechBalloon.any_instance.stub(:reader_role_check).and_return(true)
140 SpeechBalloon.any_instance.should_receive(:reader_role_check).with(any_args).exactly(1)
141 r = @sb.visible?([@author])
143 it '読者用ロールチェックが失敗したとき、falseを返す' do
144 SpeechBalloon.any_instance.stub(:reader_role_check).and_return(false)
145 r = @sb.visible?([@author])
149 context '事前チェックする' do
151 MagicNumber['run_mode'] = 1
152 SpeechBalloon.any_instance.stub(:reader_role_check).and_return(true)
154 it '自身のコマに所持判定を問い合わせしている' do
155 Panel.any_instance.stub(:own?).and_return(true)
156 Panel.any_instance.should_receive(:own?).with(any_args).exactly(1)
157 r = @sb.visible?([@author])
159 it '自身のコマに閲覧許可を問い合わせしている' do
160 Panel.any_instance.stub(:own?).and_return(false)
161 Panel.any_instance.stub(:visible?).and_return(true)
162 Panel.any_instance.should_receive(:visible?).with(any_args).exactly(1)
163 r = @sb.visible?([@author])
166 context 'つつがなく終わるとき' do
168 MagicNumber['run_mode'] = 1
169 Panel.any_instance.stub(:reader_role_check).and_return(true)
171 it '自分のコマのフキダシなら許可する' do
172 Panel.any_instance.stub(:own?).and_return(true)
173 Panel.any_instance.stub(:visible?).and_return(false)
174 r = @sb.visible?([@author])
177 it '他人の非公開コマのフキダシなら許可しない' do
178 Panel.any_instance.stub(:own?).and_return(false)
179 Panel.any_instance.stub(:visible?).and_return(false)
180 r = @sb.visible?([@author])
183 it '他人のコマのフキダシでも公開なら許可する' do
184 Panel.any_instance.stub(:own?).and_return(false)
185 Panel.any_instance.stub(:visible?).and_return(true)
186 r = @sb.visible?([@author])
192 describe '一覧取得に於いて' do
194 @sb = FactoryGirl.create :speech_balloon, :panel_id => @panel.id, :speech_balloon_template_id => @speech_balloon_template.id
195 @balloon = FactoryGirl.create :balloon, :speech_balloon_id => @sb.id, :system_picture_id => @sp.id
196 @speech = FactoryGirl.create :speech, :speech_balloon_id => @sb.id
198 context 'page補正について' do
199 it '文字列から数値に変換される' do
200 SpeechBalloon.page('8').should eq 8
203 SpeechBalloon.page().should eq 1
206 SpeechBalloon.page('0').should eq 1
209 context 'page_size補正について' do
210 it '文字列から数値に変換される' do
211 SpeechBalloon.page_size('7').should eq 7
213 it 'nilの場合はSpeechBalloon.default_page_sizeになる' do
214 SpeechBalloon.page_size().should eq SpeechBalloon.default_page_size
216 it '0以下の場合はSpeechBalloon.default_page_sizeになる' do
217 SpeechBalloon.page_size('0').should eq SpeechBalloon.default_page_size
219 it 'SpeechBalloon.max_page_sizeを超えた場合はSpeechBalloon.max_page_sizeになる' do
220 SpeechBalloon.page_size('1000').should eq SpeechBalloon.max_page_size
223 context 'つつがなく終わるとき' do
224 it '一覧取得オプションを利用している' do
225 SpeechBalloon.stub(:list_opt).with(any_args).and_return({:include => :panel})
226 SpeechBalloon.should_receive(:list_opt).with(any_args).exactly(1)
227 r = SpeechBalloon.list
231 r = SpeechBalloon.list
235 #公開されたコマのフキダシは(他人のフキダシであっても)含んでいる
236 hc = FactoryGirl.create :panel, :author_id => @other_author.id, :publish => 1
237 npl = FactoryGirl.create :speech_balloon, :panel_id => hc.id, :speech_balloon_template_id => @speech_balloon_template.id, :updated_at => Time.now + 100
238 r = SpeechBalloon.list
239 r.should eq [npl, @sb]
241 it '非公開のコマのフキダシは自分のフキダシであっても含まない' do
242 hc = FactoryGirl.create :panel, :author_id => @author.id, :publish => 0
243 npl = FactoryGirl.create :speech_balloon, :panel_id => hc.id, :speech_balloon_template_id => @speech_balloon_template.id, :updated_at => Time.now + 100
244 pl = SpeechBalloon.list
247 context 'DBに5件あって1ページの件数を2件に変えたとして' do
249 @sb2 = FactoryGirl.create :speech_balloon, :panel_id => @panel.id, :speech_balloon_template_id => @speech_balloon_template.id, :t => 1, :updated_at => Time.now + 100
250 @sb3 = FactoryGirl.create :speech_balloon, :panel_id => @panel.id, :speech_balloon_template_id => @speech_balloon_template.id, :t => 2, :updated_at => Time.now + 200
251 @sb4 = FactoryGirl.create :speech_balloon, :panel_id => @panel.id, :speech_balloon_template_id => @speech_balloon_template.id, :t => 3, :updated_at => Time.now + 300
252 @sb5 = FactoryGirl.create :speech_balloon, :panel_id => @panel.id, :speech_balloon_template_id => @speech_balloon_template.id, :t => 4, :updated_at => Time.now + 400
253 SpeechBalloon.stub(:default_page_size).and_return(2)
256 pl = SpeechBalloon.list
257 pl.should have(2).items
259 it 'page=1なら末尾2件を返す' do
261 pl = SpeechBalloon.list(1)
262 pl.should eq [@sb5, @sb4]
264 it 'page=2なら中間2件を返す' do
265 pl = SpeechBalloon.list(2)
266 pl.should eq [@sb3, @sb2]
268 it 'page=3なら先頭1件を返す' do
269 pl = SpeechBalloon.list(3)
273 context 'DBに5件あって1ページの件数を0件に変えたとして' do
275 @sb2 = FactoryGirl.create :speech_balloon, :panel_id => @panel.id, :speech_balloon_template_id => @speech_balloon_template.id, :t => 1, :updated_at => Time.now + 100
276 @sb3 = FactoryGirl.create :speech_balloon, :panel_id => @panel.id, :speech_balloon_template_id => @speech_balloon_template.id, :t => 2, :updated_at => Time.now + 200
277 @sb4 = FactoryGirl.create :speech_balloon, :panel_id => @panel.id, :speech_balloon_template_id => @speech_balloon_template.id, :t => 3, :updated_at => Time.now + 300
278 @sb5 = FactoryGirl.create :speech_balloon, :panel_id => @panel.id, :speech_balloon_template_id => @speech_balloon_template.id, :t => 4, :updated_at => Time.now + 400
279 SpeechBalloon.stub(:default_page_size).and_return(2)
282 r = SpeechBalloon.list 5, 0
283 r.should have(5).items
287 describe '一覧取得オプションに於いて' do
288 it 'includeキーを含んでいる' do
289 r = SpeechBalloon.list_opt
290 r.has_key?(:include).should be_true
293 r = SpeechBalloon.list_opt[:include]
294 r.should have(4).items
297 r = SpeechBalloon.list_opt[:include]
298 r.has_key?(:panel).should be_true
301 r = SpeechBalloon.list_opt[:include]
302 r[:panel].has_key?(:author).should be_true
305 r = SpeechBalloon.list_opt[:include]
306 r.has_key?(:balloons).should be_true
309 r = SpeechBalloon.list_opt[:include]
310 r.has_key?(:speeches).should be_true
312 it 'フキダシテンプレートを含んでいる' do
313 r = SpeechBalloon.list_opt[:include]
314 r.has_key?(:speech_balloon_template).should be_true
317 describe 'json一覧出力オプションに於いて' do
319 @panel = FactoryGirl.create :panel, :author_id => @author.id, :publish => 1
320 @sb = FactoryGirl.create :speech_balloon, :panel_id => @panel.id, :speech_balloon_template_id => @speech_balloon_template.id
321 @balloon = FactoryGirl.create :balloon, :speech_balloon_id => @sb.id, :system_picture_id => @sp.id
322 @speech = FactoryGirl.create :speech, :speech_balloon_id => @sb.id
325 r = SpeechBalloon.list.to_json SpeechBalloon.list_json_opt
328 i.has_key?('panel').should be_true
331 r = SpeechBalloon.list.to_json SpeechBalloon.list_json_opt
335 s.has_key?('author').should be_true
338 r = SpeechBalloon.list.to_json SpeechBalloon.list_json_opt
341 i.has_key?('balloons').should be_true
344 r = SpeechBalloon.list.to_json SpeechBalloon.list_json_opt
347 i.has_key?('speeches').should be_true
349 it 'フキダシテンプレートを含んでいる' do
350 r = SpeechBalloon.list.to_json SpeechBalloon.list_json_opt
353 i.has_key?('speech_balloon_template').should be_true
357 describe '単体取得に於いて' do
359 @sb = FactoryGirl.create :speech_balloon, :panel_id => @panel.id, :speech_balloon_template_id => @speech_balloon_template.id
360 @balloon = FactoryGirl.create :balloon, :speech_balloon_id => @sb.id, :system_picture_id => @sp.id
361 @speech = FactoryGirl.create :speech, :speech_balloon_id => @sb.id
363 context 'つつがなく終わるとき' do
364 it '単体取得オプションを利用している' do
365 SpeechBalloon.stub(:show_opt).with(any_args).and_return({:include => :panel})
366 SpeechBalloon.should_receive(:show_opt).with(any_args).exactly(1)
367 r = SpeechBalloon.show @sb.id, @author
369 it '閲覧許可を問い合わせている' do
370 SpeechBalloon.any_instance.stub(:visible?).with(any_args).and_return(true)
371 SpeechBalloon.any_instance.should_receive(:visible?).with(any_args).exactly(1)
372 r = SpeechBalloon.show @sb.id, @author
376 SpeechBalloon.any_instance.stub(:visible?).and_return(true)
377 pl = SpeechBalloon.show @sb.id, @author
380 context '閲覧許可が出なかったとき' do
381 it '403Forbidden例外を返す' do
382 SpeechBalloon.any_instance.stub(:visible?).and_return(false)
384 SpeechBalloon.show @sb.id, @author
385 }.should raise_error(ActiveRecord::Forbidden)
388 context '存在しないフキダシを開こうとしたとき' do
389 it '404RecordNotFound例外を返す' do
391 SpeechBalloon.show 110, @author
392 }.should raise_error(ActiveRecord::RecordNotFound)
396 describe '単体取得オプションに於いて' do
397 it 'includeキーを含んでいる' do
398 r = SpeechBalloon.show_opt
399 r.has_key?(:include).should be_true
402 r = SpeechBalloon.show_opt[:include]
403 r.should have(4).items
406 r = SpeechBalloon.show_opt[:include]
407 r.has_key?(:panel).should be_true
410 r = SpeechBalloon.show_opt[:include]
411 r[:panel].has_key?(:author).should be_true
414 r = SpeechBalloon.show_opt[:include]
415 r.has_key?(:balloons).should be_true
418 r = SpeechBalloon.show_opt[:include]
419 r.has_key?(:speeches).should be_true
421 it 'フキダシテンプレートを含んでいる' do
422 r = SpeechBalloon.show_opt[:include]
423 r.has_key?(:speech_balloon_template).should be_true
426 describe 'json単体出力オプションに於いて' do
428 @panel = FactoryGirl.create :panel, :author_id => @author.id, :publish => 1
429 @sb = FactoryGirl.create :speech_balloon, :panel_id => @panel.id, :speech_balloon_template_id => @speech_balloon_template.id
430 @balloon = FactoryGirl.create :balloon, :speech_balloon_id => @sb.id, :system_picture_id => @sp.id
431 @speech = FactoryGirl.create :speech, :speech_balloon_id => @sb.id
434 r = SpeechBalloon.show(@sb.id, @author).to_json SpeechBalloon.show_json_opt
437 i.has_key?('panel').should be_true
440 r = SpeechBalloon.show(@sb.id, @author).to_json SpeechBalloon.show_json_opt
444 s.has_key?('author').should be_true
447 r = SpeechBalloon.show(@sb.id, @author).to_json SpeechBalloon.show_json_opt
450 i.has_key?('balloons').should be_true
453 r = SpeechBalloon.show(@sb.id, @author).to_json SpeechBalloon.show_json_opt
456 i.has_key?('speeches').should be_true
458 it 'フキダシテンプレートを含んでいる' do
459 r = SpeechBalloon.show(@sb.id, @author).to_json SpeechBalloon.show_json_opt
462 i.has_key?('speech_balloon_template').should be_true