1 # -*- encoding: utf-8 -*-
7 @admin = FactoryGirl.create :admin
8 @sp = FactoryGirl.create :system_picture
9 @lg = FactoryGirl.create :license_group
10 @license = FactoryGirl.create :license, :license_group_id => @lg.id, :system_picture_id => @sp.id
11 @user = FactoryGirl.create( :user_yas)
12 @author = FactoryGirl.create :author, :user_id => @user.id
13 @artist = FactoryGirl.create :artist_yas, :author_id => @author.id
14 @other_user = FactoryGirl.create( :user_yas)
15 @other_author = FactoryGirl.create :author, :user_id => @other_user.id
16 @other_artist = FactoryGirl.create :artist_yas, :author_id => @other_author.id
21 @comic = FactoryGirl.build :comic, :author_id => @author.id
24 context 'オーソドックスなデータのとき' do
28 @comic.should be_valid
31 @comic.title = 'a'*100
33 @comic.should be_valid
37 context 'titleを検証するとき' do
40 @comic.should_not be_valid
43 @comic.title = 'a'*101
44 @comic.should_not be_valid
47 context 'visibleを検証するとき' do
50 @comic.should_not be_valid
54 @comic.should_not be_valid
58 @comic.should_not be_valid
63 describe 'デフォルト値補充に於いて' do
64 it 'visibleが0になっている' do
65 @comic = FactoryGirl.build :comic, :visible => nil
67 @comic.visible.should eq 0
71 describe '上書き補充に於いて' do
73 @comic = FactoryGirl.build :comic, :author_id => nil
74 @comic.overwrite @author
75 @comic.author_id.should eq @author.id
79 describe '所持判定に於いて' do
81 @comic = FactoryGirl.build :comic, :author_id => @author.id
84 @comic.own?(@author).should == true
87 @comic.own?(@other_author).should == false
89 it 'パラメータが作家でないならno' do
90 @comic.own?(nil).should == false
94 describe '閲覧許可に於いて' do
96 @comic = FactoryGirl.build :comic, :author_id => @author.id
98 context '検査対象がnil(ゲスト)のとき' do
99 context 'クローズドモードのとき' do
101 MagicNumber['run_mode'] = 1
104 r = @comic.visible?(nil)
108 context 'オープンモードのとき' do
110 MagicNumber['run_mode'] = 0
113 Comic.any_instance.stub(:visible).and_return(1)
114 r = @comic.visible?(nil)
117 it '非公開コミックなら許可しない' do
118 Comic.any_instance.stub(:visible).and_return(0)
119 r = @comic.visible?(nil)
124 context '検査対象が作家のとき' do
125 it '自分のコミックなら許可する' do
126 Comic.any_instance.stub(:own?).and_return(true)
127 Comic.any_instance.stub(:visible).and_return(0)
128 r = @comic.visible?(@author)
131 it '他人の非公開コミックなら許可しない' do
132 Comic.any_instance.stub(:own?).and_return(false)
133 Comic.any_instance.stub(:visible).and_return(0)
134 r = @comic.visible?(@author)
137 it '他人のコミックでも公開なら許可する' do
138 Comic.any_instance.stub(:own?).and_return(false)
139 Comic.any_instance.stub(:visible).and_return(1)
140 r = @comic.visible?(@author)
144 context '検査対象がそれ以外のとき' do
146 r = @comic.visible?(@admin)
152 describe '一覧取得に於いて' do
154 @comic = FactoryGirl.create :comic, :author_id => @author.id
156 context 'page補正について' do
157 it '文字列から数値に変換される' do
158 Comic.page('8').should eq 8
161 Comic.page().should eq 1
164 Comic.page('0').should eq 1
167 context 'page_size補正について' do
168 it '文字列から数値に変換される' do
169 Comic.page_size('7').should eq 7
171 it 'nilの場合はComic.default_page_sizeになる' do
172 Comic.page_size().should eq Comic.default_page_size
174 it '0以下の場合はComic.default_page_sizeになる' do
175 Comic.page_size('0').should eq Comic.default_page_size
177 it 'Comic.max_page_sizeを超えた場合はComic.max_page_sizeになる' do
178 Comic.page_size('1000').should eq Comic.max_page_size
181 context 'つつがなく終わるとき' do
182 it '一覧取得オプションを利用している' do
183 Comic.stub(:list_opt).with(any_args).and_return({})
184 Comic.should_receive(:list_opt).with(any_args).exactly(1)
192 it '非公開コミックは(自分のコミックであっても)含んでいない' do
193 FactoryGirl.create :comic, :author_id => @author.id, :visible => 0
198 #公開コミックは(他人のコミックであっても)含んでいる
199 v = FactoryGirl.create :comic, :author_id => @other_author.id, :updated_at => Time.now + 100
201 c.should eq [v, @comic]
203 context 'DBに5件あって1ページの件数を2件に変えたとして' do
205 @comic2 = FactoryGirl.create :comic, :author_id => @author.id, :updated_at => Time.now + 100
206 @comic3 = FactoryGirl.create :comic, :author_id => @author.id, :updated_at => Time.now + 200
207 @comic4 = FactoryGirl.create :comic, :author_id => @author.id, :updated_at => Time.now + 300
208 @comic5 = FactoryGirl.create :comic, :author_id => @author.id, :updated_at => Time.now + 400
209 Comic.stub(:default_page_size).and_return(2)
213 c.should have(2).items
215 it 'page=1なら末尾2件を返す' do
218 c.should eq [@comic5, @comic4]
220 it 'page=2なら中間2件を返す' do
222 c.should eq [@comic3, @comic2]
224 it 'page=3なら先頭1件を返す' do
229 context 'DBに5件あって1ページの件数を2件に変えたとして' do
231 @comic2 = FactoryGirl.create :comic, :author_id => @author.id, :updated_at => Time.now + 100
232 @comic3 = FactoryGirl.create :comic, :author_id => @author.id, :updated_at => Time.now + 200
233 @comic4 = FactoryGirl.create :comic, :author_id => @author.id, :updated_at => Time.now + 300
234 @comic5 = FactoryGirl.create :comic, :author_id => @author.id, :updated_at => Time.now + 400
235 Comic.stub(:default_page_size).and_return(2)
237 it '件数0は全件(5件)を返す' do
239 r.should have(5).items
243 describe '一覧取得オプションに於いて' do
244 it 'includeキーを含んでいる' do
246 r.has_key?(:include).should be_true
249 r = Comic.list_opt[:include]
250 r.should have(2).items
253 r = Comic.list_opt[:include]
254 r.has_key?(:stories).should be_true
256 it 'ストーリーはコマを含んでいる' do
257 r = Comic.list_opt[:include]
258 r[:stories].has_key?(:panel).should be_true
261 r = Comic.list_opt[:include]
262 r.has_key?(:author).should be_true
265 describe 'json一覧出力オプションに於いて' do
267 @op = FactoryGirl.create :original_picture, :artist_id => @artist.id
268 @p = FactoryGirl.create :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @artist.id
269 @rp = FactoryGirl.create :resource_picture, :artist_id => @artist.id, :license_id => @license.id, :original_picture_id => @op.id, :picture_id => @p.id
270 @sbt = FactoryGirl.create :speech_balloon_template
271 @comic = FactoryGirl.create :comic, :author_id => @author.id, :visible => 1
272 @panel = FactoryGirl.create :panel, :author_id => @author.id, :publish => 1
273 @story = FactoryGirl.create :story, :author_id => @author.id, :comic_id => @comic.id, :panel_id => @panel.id
276 r = Comic.list.to_json Comic.list_json_opt
279 i.has_key?('stories').should be_true
281 it 'ストーリーはコマを含んでいる' do
282 r = Comic.list.to_json Comic.list_json_opt
285 s = i['stories'].first
286 s.has_key?('panel').should be_true
289 r = Comic.list.to_json Comic.list_json_opt
292 i.has_key?('author').should be_true
296 describe '自分のコミック一覧取得に於いて' do
298 @comic = FactoryGirl.create :comic, :author_id => @author.id
300 context 'つつがなく終わるとき' do
301 it '一覧取得オプションを利用している' do
302 Comic.stub(:list_opt).with(any_args).and_return({})
303 Comic.should_receive(:list_opt).with(any_args).exactly(1)
304 r = Comic.mylist @author
308 c = Comic.mylist @author
312 nc = FactoryGirl.create :comic, :author_id => @author.id, :updated_at => Time.now + 100
313 cl = Comic.mylist @author
314 cl.should eq [nc, @comic]
316 it '他人のコミックは公開でも含まない' do
317 nc = FactoryGirl.create :comic, :author_id => @other_author.id, :visible => 1
318 cl = Comic.mylist @author
319 cl.should eq [@comic]
321 it '自分のコミックは非公開でも含んでいる' do
322 nc = FactoryGirl.create :comic, :author_id => @author.id, :visible => 0, :updated_at => Time.now + 100
323 cl = Comic.mylist @author
324 cl.should eq [nc, @comic]
326 context 'DBに5件あって1ページの件数を2件に変えたとして' do
328 @comic2 = FactoryGirl.create :comic, :author_id => @author.id, :updated_at => Time.now + 100
329 @comic3 = FactoryGirl.create :comic, :author_id => @author.id, :updated_at => Time.now + 200
330 @comic4 = FactoryGirl.create :comic, :author_id => @author.id, :updated_at => Time.now + 300
331 @comic5 = FactoryGirl.create :comic, :author_id => @author.id, :updated_at => Time.now + 400
334 c = Comic.mylist @author, 1, 2
335 c.should have(2).items
337 it 'page=1なら末尾2件を返す' do
339 c = Comic.mylist(@author, 1, 2)
340 c.should eq [@comic5, @comic4]
342 it 'page=2なら中間2件を返す' do
343 c = Comic.mylist(@author, 2, 2)
344 c.should eq [@comic3, @comic2]
346 it 'page=3なら先頭1件を返す' do
347 c = Comic.mylist(@author, 3, 2)
351 context 'DBに5件あって1ページの件数を0件に変えたとして' do
353 @comic2 = FactoryGirl.create :comic, :author_id => @author.id, :updated_at => Time.now + 100
354 @comic3 = FactoryGirl.create :comic, :author_id => @author.id, :updated_at => Time.now + 200
355 @comic4 = FactoryGirl.create :comic, :author_id => @author.id, :updated_at => Time.now + 300
356 @comic5 = FactoryGirl.create :comic, :author_id => @author.id, :updated_at => Time.now + 400
357 Author.stub(:default_comic_page_size).and_return(2)
360 r = Comic.mylist @author, 5, 0
361 r.should have(5).items
366 describe '単体取得に於いて' do
368 @comic = FactoryGirl.create :comic, :author_id => @author.id
370 context 'つつがなく終わるとき' do
371 it '単体取得オプションを利用している' do
372 Comic.stub(:show_opt).with(any_args).and_return({})
373 Comic.should_receive(:show_opt).with(any_args).exactly(1)
374 r = Comic.show @comic.id, @author
376 it '閲覧許可を問い合わせている' do
377 Comic.any_instance.stub(:visible?).with(any_args).and_return(true)
378 Comic.any_instance.should_receive(:visible?).with(any_args).exactly(1)
379 r = Comic.show @comic.id, @author
383 c = Comic.show @comic.id, @author
386 context '閲覧許可が出なかったとき' do
387 it '403Forbidden例外を返す' do
388 Comic.any_instance.stub(:visible?).and_return(false)
390 Comic.show @comic.id, @author
391 }.should raise_error(ActiveRecord::Forbidden)
394 context '存在しないコミックを開こうとしたとき' do
395 it '404RecordNotFound例外を返す' do
397 Comic.show 110, @author
398 }.should raise_error(ActiveRecord::RecordNotFound)
403 describe '編集取得に於いて' do
405 @comic = FactoryGirl.create :comic, :author_id => @author.id
407 context 'つつがなく終わるとき' do
408 it '単体取得オプションを利用している' do
409 Comic.stub(:show_opt).with(any_args).and_return({})
410 Comic.should_receive(:show_opt).with(any_args).exactly(1)
411 r = Comic.edit @comic.id, @author
413 it '所持判定を問い合わせている' do
414 Comic.any_instance.stub(:own?).with(any_args).and_return(true)
415 Comic.any_instance.should_receive(:own?).with(any_args).exactly(1)
416 r = Comic.edit @comic.id, @author
420 Comic.any_instance.stub(:own?).and_return(true)
421 c = Comic.edit @comic.id, @author.id
424 context '他人のコミックを開こうとしたとき' do
425 it '403Forbidden例外を返す' do
426 Comic.any_instance.stub(:own?).and_return(false)
428 Comic.edit @comic.id, @author
429 }.should raise_error(ActiveRecord::Forbidden)
432 context '存在しないコミックを開こうとしたとき' do
433 it '404RecordNotFound例外を返す' do
435 Comic.edit 110, @author
436 }.should raise_error(ActiveRecord::RecordNotFound)
440 describe '単体取得オプションに於いて' do
441 it 'includeキーを含んでいる' do
443 r.has_key?(:include).should be_true
446 r = Comic.show_opt[:include]
447 r.should have(2).items
450 r = Comic.show_opt[:include]
451 r.has_key?(:author).should be_true
454 r = Comic.show_opt[:include]
455 r.has_key?(:stories).should be_true
457 it 'ストーリーはコマを含んでいる' do
458 r = Comic.show_opt[:include]
459 r[:stories].has_key?(:panel).should be_true
462 describe 'json単体出力オプションに於いて' do
464 @op = FactoryGirl.create :original_picture, :artist_id => @artist.id
465 @p = FactoryGirl.create :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @artist.id
466 @rp = FactoryGirl.create :resource_picture, :artist_id => @artist.id, :license_id => @license.id, :original_picture_id => @op.id, :picture_id => @p.id
467 @sbt = FactoryGirl.create :speech_balloon_template
468 @comic = FactoryGirl.create :comic, :author_id => @author.id, :visible => 1
469 @panel = FactoryGirl.create :panel, :author_id => @author.id, :publish => 1
470 @story = FactoryGirl.create :story, :author_id => @author.id, :comic_id => @comic.id, :panel_id => @panel.id
473 r = Comic.show(@comic.id, @author).to_json Comic.show_json_opt
476 i.has_key?('stories').should be_true
478 it 'ストーリーはコマを含んでいる' do
479 r = Comic.show(@comic.id, @author).to_json Comic.show_json_opt
482 s = i['stories'].first
483 s.has_key?('panel').should be_true
486 r = Comic.show(@comic.id, @author).to_json Comic.show_json_opt
489 i.has_key?('author').should be_true
495 @comic = FactoryGirl.create :comic, :author_id => @author.id
496 @panel = FactoryGirl.create :panel, :author_id => @author.id, :publish => 1
497 @story = FactoryGirl.create :story, :author_id => @author.id, :comic_id => @comic.id, :panel_id => @panel.id
498 @other_comic = FactoryGirl.create :comic, :author_id => @author.id
499 @other_story = FactoryGirl.create :story, :author_id => @author.id, :comic_id => @other_comic.id, :panel_id => @panel.id
501 context 'つつがなく終わるとき' do
504 r = @comic.destroy_with_story
505 }.should change(Comic, :count).by(-1)
507 r = Comic.find @comic.id
510 it '自身にリンクしているストーリーをすべて削除する' do
512 r = @comic.destroy_with_story
513 }.should change(Story, :count).by(-1)
515 r = Story.find @story.id
519 r = @comic.destroy_with_story
523 context '削除に失敗したとき' do
525 Story.any_instance.stub(:destroy).with(any_args).and_return(false)
528 r = @comic.destroy_with_story
533 r = @comic.destroy_with_story
534 }.should_not change(Comic, :count)
536 r = @comic.destroy_with_story
537 }.should_not change(Story, :count)