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.create :speech_balloon, :panel_id => @panel.id, :speech_balloon_template_id => @speech_balloon_template.id
95 context 'オープンモードのとき' do
97 MagicNumber['run_mode'] = 0
99 it '自身にゲスト用ロールチェックを問い合わせしている' do
100 SpeechBalloon.any_instance.stub(:guest_role_check).and_return(true)
101 SpeechBalloon.any_instance.should_receive(:guest_role_check).with(any_args).exactly(1)
102 r = @sb.visible?([@author])
104 it 'ゲスト用ロールチェックが失敗したとき、falseを返す' do
105 SpeechBalloon.any_instance.stub(:guest_role_check).and_return(false)
106 r = @sb.visible?([@author])
110 context 'クローズドモードのとき' do
112 MagicNumber['run_mode'] = 1
114 it '自身に読者用ロールチェックを問い合わせしている' do
115 SpeechBalloon.any_instance.stub(:reader_role_check).and_return(true)
116 SpeechBalloon.any_instance.should_receive(:reader_role_check).with(any_args).exactly(1)
117 r = @sb.visible?([@author])
119 it '読者用ロールチェックが失敗したとき、falseを返す' do
120 SpeechBalloon.any_instance.stub(:reader_role_check).and_return(false)
121 r = @sb.visible?([@author])
125 context '事前チェックする' do
127 MagicNumber['run_mode'] = 1
128 SpeechBalloon.any_instance.stub(:reader_role_check).and_return(true)
130 it '自身のコマに所持判定を問い合わせしている' do
131 Panel.any_instance.stub(:own?).and_return(true)
132 Panel.any_instance.should_receive(:own?).with(any_args).exactly(1)
133 r = @sb.visible?([@author])
135 it '自身のコマに閲覧許可を問い合わせしている' do
136 Panel.any_instance.stub(:own?).and_return(false)
137 Panel.any_instance.stub(:visible?).and_return(true)
138 Panel.any_instance.should_receive(:visible?).with(any_args).exactly(1)
139 r = @sb.visible?([@author])
142 context 'つつがなく終わるとき' do
144 MagicNumber['run_mode'] = 1
145 Panel.any_instance.stub(:reader_role_check).and_return(true)
147 it '自分のコマのフキダシなら許可する' do
148 Panel.any_instance.stub(:own?).and_return(true)
149 Panel.any_instance.stub(:visible?).and_return(false)
150 r = @sb.visible?([@author])
153 it '他人の非公開コマのフキダシなら許可しない' do
154 Panel.any_instance.stub(:own?).and_return(false)
155 Panel.any_instance.stub(:visible?).and_return(false)
156 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 r = @sb.visible?([@author])
168 describe '一覧取得に於いて' do
170 @sb = FactoryGirl.create :speech_balloon, :panel_id => @panel.id, :speech_balloon_template_id => @speech_balloon_template.id
171 @balloon = FactoryGirl.create :balloon, :speech_balloon_id => @sb.id, :system_picture_id => @sp.id
172 @speech = FactoryGirl.create :speech, :speech_balloon_id => @sb.id
174 context 'page補正について' do
175 it '文字列から数値に変換される' do
176 SpeechBalloon.page('8').should eq 8
179 SpeechBalloon.page().should eq 1
182 SpeechBalloon.page('0').should eq 1
185 context 'page_size補正について' do
186 it '文字列から数値に変換される' do
187 SpeechBalloon.page_size('7').should eq 7
189 it 'nilの場合はSpeechBalloon.default_page_sizeになる' do
190 SpeechBalloon.page_size().should eq SpeechBalloon.default_page_size
192 it '0以下の場合はSpeechBalloon.default_page_sizeになる' do
193 SpeechBalloon.page_size('0').should eq SpeechBalloon.default_page_size
195 it 'SpeechBalloon.max_page_sizeを超えた場合はSpeechBalloon.max_page_sizeになる' do
196 SpeechBalloon.page_size('1000').should eq SpeechBalloon.max_page_size
199 context 'つつがなく終わるとき' do
200 it '一覧取得オプションを利用している' do
201 SpeechBalloon.stub(:list_opt).with(any_args).and_return({:include => :panel})
202 SpeechBalloon.should_receive(:list_opt).with(any_args).exactly(1)
203 r = SpeechBalloon.list
207 r = SpeechBalloon.list
211 #公開されたコマのフキダシは(他人のフキダシであっても)含んでいる
212 hc = FactoryGirl.create :panel, :author_id => @other_author.id, :publish => 1
213 npl = FactoryGirl.create :speech_balloon, :panel_id => hc.id, :speech_balloon_template_id => @speech_balloon_template.id, :updated_at => Time.now + 100
214 r = SpeechBalloon.list
215 r.should eq [npl, @sb]
217 it '非公開のコマのフキダシは自分のフキダシであっても含まない' do
218 hc = FactoryGirl.create :panel, :author_id => @author.id, :publish => 0
219 npl = FactoryGirl.create :speech_balloon, :panel_id => hc.id, :speech_balloon_template_id => @speech_balloon_template.id, :updated_at => Time.now + 100
220 pl = SpeechBalloon.list
223 context 'DBに5件あって1ページの件数を2件に変えたとして' do
225 @sb2 = FactoryGirl.create :speech_balloon, :panel_id => @panel.id, :speech_balloon_template_id => @speech_balloon_template.id, :t => 1, :updated_at => Time.now + 100
226 @sb3 = FactoryGirl.create :speech_balloon, :panel_id => @panel.id, :speech_balloon_template_id => @speech_balloon_template.id, :t => 2, :updated_at => Time.now + 200
227 @sb4 = FactoryGirl.create :speech_balloon, :panel_id => @panel.id, :speech_balloon_template_id => @speech_balloon_template.id, :t => 3, :updated_at => Time.now + 300
228 @sb5 = FactoryGirl.create :speech_balloon, :panel_id => @panel.id, :speech_balloon_template_id => @speech_balloon_template.id, :t => 4, :updated_at => Time.now + 400
229 SpeechBalloon.stub(:default_page_size).and_return(2)
232 pl = SpeechBalloon.list
233 pl.should have(2).items
235 it 'page=1なら末尾2件を返す' do
237 pl = SpeechBalloon.list(1)
238 pl.should eq [@sb5, @sb4]
240 it 'page=2なら中間2件を返す' do
241 pl = SpeechBalloon.list(2)
242 pl.should eq [@sb3, @sb2]
244 it 'page=3なら先頭1件を返す' do
245 pl = SpeechBalloon.list(3)
249 context 'DBに5件あって1ページの件数を0件に変えたとして' do
251 @sb2 = FactoryGirl.create :speech_balloon, :panel_id => @panel.id, :speech_balloon_template_id => @speech_balloon_template.id, :t => 1, :updated_at => Time.now + 100
252 @sb3 = FactoryGirl.create :speech_balloon, :panel_id => @panel.id, :speech_balloon_template_id => @speech_balloon_template.id, :t => 2, :updated_at => Time.now + 200
253 @sb4 = FactoryGirl.create :speech_balloon, :panel_id => @panel.id, :speech_balloon_template_id => @speech_balloon_template.id, :t => 3, :updated_at => Time.now + 300
254 @sb5 = FactoryGirl.create :speech_balloon, :panel_id => @panel.id, :speech_balloon_template_id => @speech_balloon_template.id, :t => 4, :updated_at => Time.now + 400
255 SpeechBalloon.stub(:default_page_size).and_return(2)
258 r = SpeechBalloon.list 5, 0
259 r.should have(5).items
263 describe '一覧取得オプションに於いて' do
264 it 'includeキーを含んでいる' do
265 r = SpeechBalloon.list_opt
266 r.has_key?(:include).should be_true
269 r = SpeechBalloon.list_opt[:include]
270 r.should have(4).items
273 r = SpeechBalloon.list_opt[:include]
274 r.has_key?(:panel).should be_true
277 r = SpeechBalloon.list_opt[:include]
278 r[:panel].has_key?(:author).should be_true
281 r = SpeechBalloon.list_opt[:include]
282 r.has_key?(:balloons).should be_true
285 r = SpeechBalloon.list_opt[:include]
286 r.has_key?(:speeches).should be_true
288 it 'フキダシテンプレートを含んでいる' do
289 r = SpeechBalloon.list_opt[:include]
290 r.has_key?(:speech_balloon_template).should be_true
293 describe 'json一覧出力オプションに於いて' do
295 @panel = FactoryGirl.create :panel, :author_id => @author.id, :publish => 1
296 @sb = FactoryGirl.create :speech_balloon, :panel_id => @panel.id, :speech_balloon_template_id => @speech_balloon_template.id
297 @balloon = FactoryGirl.create :balloon, :speech_balloon_id => @sb.id, :system_picture_id => @sp.id
298 @speech = FactoryGirl.create :speech, :speech_balloon_id => @sb.id
301 r = SpeechBalloon.list.to_json SpeechBalloon.list_json_opt
304 i.has_key?('panel').should be_true
307 r = SpeechBalloon.list.to_json SpeechBalloon.list_json_opt
311 s.has_key?('author').should be_true
314 r = SpeechBalloon.list.to_json SpeechBalloon.list_json_opt
317 i.has_key?('balloons').should be_true
320 r = SpeechBalloon.list.to_json SpeechBalloon.list_json_opt
323 i.has_key?('speeches').should be_true
325 it 'フキダシテンプレートを含んでいる' do
326 r = SpeechBalloon.list.to_json SpeechBalloon.list_json_opt
329 i.has_key?('speech_balloon_template').should be_true
333 describe '単体取得に於いて' do
335 @sb = FactoryGirl.create :speech_balloon, :panel_id => @panel.id, :speech_balloon_template_id => @speech_balloon_template.id
336 @balloon = FactoryGirl.create :balloon, :speech_balloon_id => @sb.id, :system_picture_id => @sp.id
337 @speech = FactoryGirl.create :speech, :speech_balloon_id => @sb.id
339 context 'つつがなく終わるとき' do
340 it '単体取得オプションを利用している' do
341 SpeechBalloon.stub(:show_opt).with(any_args).and_return({:include => :panel})
342 SpeechBalloon.should_receive(:show_opt).with(any_args).exactly(1)
343 r = SpeechBalloon.show @sb.id, @author
345 it '閲覧許可を問い合わせている' do
346 SpeechBalloon.any_instance.stub(:visible?).with(any_args).and_return(true)
347 SpeechBalloon.any_instance.should_receive(:visible?).with(any_args).exactly(1)
348 r = SpeechBalloon.show @sb.id, @author
352 SpeechBalloon.any_instance.stub(:visible?).and_return(true)
353 pl = SpeechBalloon.show @sb.id, @author
356 context '閲覧許可が出なかったとき' do
357 it '403Forbidden例外を返す' do
358 SpeechBalloon.any_instance.stub(:visible?).and_return(false)
360 SpeechBalloon.show @sb.id, @author
361 }.should raise_error(ActiveRecord::Forbidden)
364 context '存在しないフキダシを開こうとしたとき' do
365 it '404RecordNotFound例外を返す' do
367 SpeechBalloon.show 110, @author
368 }.should raise_error(ActiveRecord::RecordNotFound)
372 describe '単体取得オプションに於いて' do
373 it 'includeキーを含んでいる' do
374 r = SpeechBalloon.show_opt
375 r.has_key?(:include).should be_true
378 r = SpeechBalloon.show_opt[:include]
379 r.should have(4).items
382 r = SpeechBalloon.show_opt[:include]
383 r.has_key?(:panel).should be_true
386 r = SpeechBalloon.show_opt[:include]
387 r[:panel].has_key?(:author).should be_true
390 r = SpeechBalloon.show_opt[:include]
391 r.has_key?(:balloons).should be_true
394 r = SpeechBalloon.show_opt[:include]
395 r.has_key?(:speeches).should be_true
397 it 'フキダシテンプレートを含んでいる' do
398 r = SpeechBalloon.show_opt[:include]
399 r.has_key?(:speech_balloon_template).should be_true
402 describe 'json単体出力オプションに於いて' do
404 @panel = FactoryGirl.create :panel, :author_id => @author.id, :publish => 1
405 @sb = FactoryGirl.create :speech_balloon, :panel_id => @panel.id, :speech_balloon_template_id => @speech_balloon_template.id
406 @balloon = FactoryGirl.create :balloon, :speech_balloon_id => @sb.id, :system_picture_id => @sp.id
407 @speech = FactoryGirl.create :speech, :speech_balloon_id => @sb.id
410 r = SpeechBalloon.show(@sb.id, @author).to_json SpeechBalloon.show_json_opt
413 i.has_key?('panel').should be_true
416 r = SpeechBalloon.show(@sb.id, @author).to_json SpeechBalloon.show_json_opt
420 s.has_key?('author').should be_true
423 r = SpeechBalloon.show(@sb.id, @author).to_json SpeechBalloon.show_json_opt
426 i.has_key?('balloons').should be_true
429 r = SpeechBalloon.show(@sb.id, @author).to_json SpeechBalloon.show_json_opt
432 i.has_key?('speeches').should be_true
434 it 'フキダシテンプレートを含んでいる' do
435 r = SpeechBalloon.show(@sb.id, @author).to_json SpeechBalloon.show_json_opt
438 i.has_key?('speech_balloon_template').should be_true