OSDN Git Service

v07
[pettanr/pettanr.git] / spec / models / comic_spec.rb
1 # -*- encoding: utf-8 -*-
2 require 'spec_helper'
3 #コミック
4
5 describe Comic do
6   before do
7     SpeechBalloonTemplate.delete_all
8     @admin = FactoryGirl.create :admin
9     @demand_user = FactoryGirl.create :demand_user
10     @sp = FactoryGirl.create :system_picture
11     @lg = FactoryGirl.create :license_group
12     @license = FactoryGirl.create :license, :license_group_id => @lg.id, :system_picture_id => @sp.id
13     @user = FactoryGirl.create( :user_yas)
14     @author = FactoryGirl.create :author, :user_id => @user.id
15     @artist = FactoryGirl.create :artist_yas, :author_id => @author.id
16     @other_user = FactoryGirl.create( :user_yas)
17     @other_author = FactoryGirl.create :author, :user_id => @other_user.id
18     @other_artist = FactoryGirl.create :artist_yas, :author_id => @other_author.id
19   end
20   
21   describe '検証に於いて' do
22     before do
23       @comic = FactoryGirl.build :comic, :author_id => @author.id
24     end
25     
26     context 'オーソドックスなデータのとき' do
27       it '下限データが通る' do
28         @comic.title = 'a'
29         @comic.visible = 0
30         @comic.should be_valid
31       end
32       it '上限データが通る' do
33         @comic.title = 'a'*100
34         @comic.visible = 1
35         @comic.should be_valid
36       end
37     end
38     
39     context 'titleを検証するとき' do
40       it 'nullなら失敗する' do
41         @comic.title = nil
42         @comic.should_not be_valid
43       end
44       it '100文字以上なら失敗する' do
45         @comic.title = 'a'*101
46         @comic.should_not be_valid
47       end
48     end
49     
50     context 'visibleを検証するとき' do
51       it 'nullなら失敗する' do
52         @comic.visible = nil
53         @comic.should_not be_valid
54       end
55       it '負なら失敗する' do
56         @comic.visible = -1
57         @comic.should_not be_valid
58       end
59       it '2以上なら失敗する' do
60         @comic.visible = 2
61         @comic.should_not be_valid
62       end
63     end
64     
65     context 'author_idを検証するとき' do
66       it 'nullなら失敗する' do
67         @comic.author_id = nil
68         @comic.should_not be_valid
69       end
70       it '数値でなければ失敗する' do
71         @comic.author_id = 'a'
72         @comic.should_not be_valid
73       end
74       it '存在する作家でなければ失敗する' do
75         @comic.author_id = 0
76         @comic.should_not be_valid
77       end
78     end
79     
80   end
81   
82   describe '文字コード検証に於いて' do
83     before do
84       @comic = FactoryGirl.build :comic, :author_id => @author.id
85     end
86     
87     context 'titleを検証するとき' do
88       it 'Shift JISなら失敗する' do
89         @comic.title = "\x83G\x83r\x83]\x83D"
90         lambda{
91           @comic.valid_encode
92         }.should raise_error(Pettanr::BadRequest)
93       end
94     end
95     
96     context 'descriptionを検証するとき' do
97       it 'Shift JISなら失敗する' do
98         @comic.description = "\x83G\x83r\x83]\x83D"
99         lambda{
100           @comic.valid_encode
101         }.should raise_error(Pettanr::BadRequest)
102       end
103     end
104   end
105   
106   describe 'デフォルト値補充に於いて' do
107     it 'visibleが0になっている' do
108       @comic = FactoryGirl.build :comic, :visible => nil
109       @comic.supply_default
110       @comic.visible.should eq 0
111     end
112   end
113   
114   describe '上書き補充に於いて' do
115     it '作家idが設定されている' do
116       @comic = FactoryGirl.build :comic, :author_id => nil
117       @comic.overwrite @author
118       @comic.author_id.should eq @author.id
119     end
120   end
121   
122   describe 'ロールリストからの作家取得に於いて' do
123     context 'ロールがユーザのとき' do
124       it 'ユーザから作家を取得して、それを返す' do
125         r = Comic.get_author_from_roles([@user])
126         r.should eq @author
127       end
128     end
129     context 'ロールが作家のとき' do
130       it '作家を返す' do
131         r = Comic.get_author_from_roles([@author])
132         r.should eq @author
133       end
134     end
135     context 'ロールが絵師のとき' do
136       it '絵師から作家を取得できれば、それをを返す' do
137         r = Comic.get_author_from_roles([@artist])
138         r.should eq @author
139       end
140       it '絵師から作家を取得できないときnilを返す' do
141         @artist.author_id = nil
142         @artist.save!
143         r = Comic.get_author_from_roles([@artist])
144         r.should be_nil
145       end
146     end
147     context 'ロールが管理者のとき' do
148       it 'nilを返す' do
149         r = Comic.get_author_from_roles([@admin])
150         r.should be_nil
151       end
152     end
153   end
154   
155   describe 'ロールリストからの絵師取得に於いて' do
156     context 'ロールがユーザのとき' do
157       it 'ユーザから作家を取得して、そこから絵師を取得できれば、それを返す' do
158         r = Comic.get_artist_from_roles([@user])
159         r.should eq @artist
160       end
161       it '作家から絵師を取得できないときnilを返す' do
162         @artist.author_id = nil
163         @artist.save!
164         r = Comic.get_artist_from_roles([@user])
165         r.should be_nil
166       end
167     end
168     context 'ロールが作家のとき' do
169       it 'そこから絵師を取得できれば、それを返す' do
170         r = Comic.get_artist_from_roles([@author])
171         r.should eq @artist
172       end
173       it '作家から絵師を取得できないときnilを返す' do
174         @artist.author_id = nil
175         @artist.save!
176         r = Comic.get_artist_from_roles([@author])
177         r.should be_nil
178       end
179     end
180     context 'ロールが絵師のとき' do
181       it 'それを返す' do
182         r = Comic.get_artist_from_roles([@artist])
183         r.should eq @artist
184       end
185     end
186     context 'ロールが管理者のとき' do
187       it 'nilを返す' do
188         r = Comic.get_artist_from_roles([@admin])
189         r.should be_nil
190       end
191     end
192   end
193   
194   describe '所持判定に於いて' do
195     before do
196       @comic = FactoryGirl.build :comic, :author_id => @author.id
197     end
198     context '事前チェックする' do
199       it '自身にロールリストからの作家取得を依頼している' do
200         Comic.should_receive(:get_author_from_roles).with(any_args).exactly(1)
201         r = @comic.own?([@author])
202       end
203     end
204     context 'ロール内作家が取得できるとき' do
205       before do
206       end
207       it 'ロール内作家のidが自身の作家idと一致するなら許可する' do
208         Comic.stub(:get_author_from_roles).with(any_args).and_return(@author)
209         r = @comic.own?([@author])
210         r.should be_true
211       end
212       it 'ロール内作家のidが自身の作家idと一致しないならno' do
213         Comic.stub(:get_author_from_roles).with(any_args).and_return(@other_author)
214         @comic.own?(@other_author).should be_false
215       end
216     end
217     context 'ロール内作家が取得できないとき' do
218       before do
219         Comic.stub(:get_author_from_roles).with(any_args).and_return(nil)
220       end
221       it 'Falseを返す' do
222         r = @comic.own?([@author])
223         r.should be_false
224       end
225     end
226   end
227   
228   describe '読者用ロールチェックに於いて' do
229     before do
230       @comic = FactoryGirl.build :comic, :author_id => @author.id
231     end
232     context 'ロールリストに作家が含まれるとき' do
233       it 'ロールリストがリストではないとき、リストにする trueを返す' do
234         r = @comic.reader_role_check(@author)
235         r.should be_true
236       end
237       it 'trueを返す' do
238         r = @comic.reader_role_check([@author])
239         r.should be_true
240       end
241     end
242     context 'ロールリストに絵師が含まれるとき' do
243       it 'trueを返す' do
244         r = @comic.reader_role_check([@artist])
245         r.should be_true
246       end
247     end
248     context 'ロールリストにユーザが含まれるとき' do
249       it 'trueを返す' do
250         r = @comic.reader_role_check([@user])
251         r.should be_true
252       end
253     end
254     context 'ロールリストに管理者が含まれるとき' do
255       it 'trueを返す' do
256         r = @comic.reader_role_check([@admin])
257         r.should be_true
258       end
259     end
260     context 'ロールリストにユーザ、管理者、作家、絵師が含まれないとき' do
261       it 'falseを返す' do
262         r = @comic.reader_role_check([nil])
263         r.should be_false
264       end
265     end
266   end
267   
268   describe '素材読者用ロールチェックに於いて' do
269     before do
270       @comic = FactoryGirl.build :comic, :author_id => @author.id
271     end
272     context 'ロールリストに作家が含まれるとき' do
273       it 'ロールリストがリストではないとき、リストにする trueを返す' do
274         r = @comic.resource_reader_role_check(@author)
275         r.should be_true
276       end
277       it 'trueを返す' do
278         r = @comic.resource_reader_role_check([@author])
279         r.should be_true
280       end
281     end
282     context 'ロールリストに絵師が含まれるとき' do
283       it 'trueを返す' do
284         r = @comic.resource_reader_role_check([@artist])
285         r.should be_true
286       end
287     end
288     context 'ロールリストにユーザが含まれるとき' do
289       it 'trueを返す' do
290         r = @comic.resource_reader_role_check([@user])
291         r.should be_true
292       end
293     end
294     context 'ロールリストに管理者が含まれるとき' do
295       it 'trueを返す' do
296         r = @comic.resource_reader_role_check([@admin])
297         r.should be_true
298       end
299     end
300     context 'ロールリストに借手が含まれるとき' do
301       it 'trueを返す' do
302         r = @comic.resource_reader_role_check([@demand_user])
303         r.should be_true
304       end
305     end
306     context 'ロールリストにユーザ、管理者、作家、絵師、借手が含まれないとき' do
307       it 'falseを返す' do
308         r = @comic.resource_reader_role_check([nil])
309         r.should be_false
310       end
311     end
312   end
313   
314   describe '閲覧許可に於いて' do
315     before do
316       @comic = FactoryGirl.build :comic, :author_id => @author.id
317     end
318     context 'オープンモードのとき' do
319       before do
320         MagicNumber['run_mode'] = 0
321       end
322       it '自身にゲスト用ロールチェックを問い合わせしている' do
323         Comic.any_instance.stub(:guest_role_check).and_return(true)
324         Comic.any_instance.should_receive(:guest_role_check).with(any_args).exactly(1)
325         r = @comic.visible?([@author])
326       end
327       it 'ゲスト用ロールチェックが失敗したとき、falseを返す' do
328         Comic.any_instance.stub(:guest_role_check).and_return(false)
329         r = @comic.visible?([@author])
330         r.should be_false
331       end
332     end
333     context 'クローズドモードのとき' do
334       before do
335         MagicNumber['run_mode'] = 1
336       end
337       it '自身に読者用ロールチェックを問い合わせしている' do
338         Comic.any_instance.stub(:reader_role_check).and_return(true)
339         Comic.any_instance.should_receive(:reader_role_check).with(any_args).exactly(1)
340         r = @comic.visible?([@author])
341       end
342       it '読者用ロールチェックが失敗したとき、falseを返す' do
343         Comic.any_instance.stub(:reader_role_check).and_return(false)
344         r = @comic.visible?([@author])
345         r.should be_false
346       end
347     end
348     context '事前チェックする' do
349       before do
350         MagicNumber['run_mode'] = 1
351         Comic.any_instance.stub(:reader_role_check).and_return(true)
352         Comic.any_instance.stub(:own?).and_return(true)
353       end
354       it '自身に所持判定を問い合わせしている' do
355         Comic.any_instance.should_receive(:own?).with(any_args).exactly(1)
356         r = @comic.visible?([@author])
357       end
358     end
359     context 'つつがなく終わるとき' do
360       before do
361         MagicNumber['run_mode'] = 1
362         Comic.any_instance.stub(:reader_role_check).and_return(true)
363       end
364       it '自分のコミックなら許可する' do
365         Comic.any_instance.stub(:own?).and_return(true)
366         Comic.any_instance.stub(:visible).and_return(0)
367         r = @comic.visible?([@author])
368         r.should be_true
369       end
370       it '他人の非公開コミックなら許可しない' do
371         Comic.any_instance.stub(:own?).and_return(false)
372         Comic.any_instance.stub(:visible).and_return(0)
373         r = @comic.visible?([@author])
374         r.should be_false
375       end
376       it '他人のコミックでも公開なら許可する' do
377         Comic.any_instance.stub(:own?).and_return(false)
378         Comic.any_instance.stub(:visible).and_return(1)
379         r = @comic.visible?([@author])
380         r.should be_true
381       end
382     end
383   end
384   
385   describe '一覧取得に於いて' do
386     before do
387       @comic = FactoryGirl.create :comic, :author_id => @author.id
388     end
389     context 'page補正について' do
390       it '文字列から数値に変換される' do
391         Comic.page('8').should eq 8
392       end
393       it 'nilの場合は1になる' do
394         Comic.page().should eq 1
395       end
396       it '0以下の場合は1になる' do
397         Comic.page('0').should eq 1
398       end
399     end
400     context 'page_size補正について' do
401       it '文字列から数値に変換される' do
402         Comic.page_size('7').should eq 7
403       end
404       it 'nilの場合はComic.default_page_sizeになる' do
405         Comic.page_size().should eq Comic.default_page_size
406       end
407       it '0以下の場合はComic.default_page_sizeになる' do
408         Comic.page_size('0').should eq Comic.default_page_size
409       end
410       it 'Comic.max_page_sizeを超えた場合はComic.max_page_sizeになる' do
411         Comic.page_size('1000').should eq Comic.max_page_size
412       end
413     end
414     context 'つつがなく終わるとき' do
415       it '一覧取得オプションを利用している' do
416         Comic.stub(:list_opt).with(any_args).and_return({})
417         Comic.should_receive(:list_opt).with(any_args).exactly(1)
418         r = Comic.list
419       end
420     end
421     it 'リストを返す' do
422       c = Comic.list
423       c.should eq [@comic]
424     end
425     it '非公開コミックは(自分のコミックであっても)含んでいない' do
426       FactoryGirl.create :comic, :author_id => @author.id, :visible => 0
427       c = Comic.list
428       c.should eq [@comic]
429     end
430     it '時系列で並んでいる' do
431       #公開コミックは(他人のコミックであっても)含んでいる
432       v = FactoryGirl.create :comic, :author_id => @other_author.id, :updated_at => Time.now + 100
433       c = Comic.list
434       c.should eq [v, @comic]
435     end
436     context 'DBに5件あって1ページの件数を2件に変えたとして' do
437       before do
438         @comic2 = FactoryGirl.create :comic, :author_id => @author.id, :updated_at => Time.now + 100
439         @comic3 = FactoryGirl.create :comic, :author_id => @author.id, :updated_at => Time.now + 200
440         @comic4 = FactoryGirl.create :comic, :author_id => @author.id, :updated_at => Time.now + 300
441         @comic5 = FactoryGirl.create :comic, :author_id => @author.id, :updated_at => Time.now + 400
442         Comic.stub(:default_page_size).and_return(2)
443       end
444       it '通常は2件を返す' do
445         c = Comic.list
446         c.should have(2).items 
447       end
448       it 'page=1なら末尾2件を返す' do
449         #時系列で並んでいる
450         c = Comic.list(1)
451         c.should eq [@comic5, @comic4]
452       end
453       it 'page=2なら中間2件を返す' do
454         c = Comic.list(2)
455         c.should eq [@comic3, @comic2]
456       end
457       it 'page=3なら先頭1件を返す' do
458         c = Comic.list(3)
459         c.should eq [@comic]
460       end
461     end
462   end
463   
464   describe '自分のコミック一覧取得に於いて' do
465     before do
466       @comic = FactoryGirl.create :comic, :author_id => @author.id
467     end
468     context 'つつがなく終わるとき' do
469       it '一覧取得オプションを利用している' do
470         Comic.stub(:list_opt).with(any_args).and_return({})
471         Comic.should_receive(:list_opt).with(any_args).exactly(1)
472         r = Comic.mylist @author
473       end
474     end
475     it 'リストを返す' do
476       c = Comic.mylist @author
477       c.should eq [@comic]
478     end
479     it '時系列で並んでいる' do
480       nc = FactoryGirl.create :comic, :author_id => @author.id, :updated_at => Time.now + 100
481       cl = Comic.mylist @author
482       cl.should eq [nc, @comic]
483     end
484     it '他人のコミックは公開でも含まない' do
485       nc = FactoryGirl.create :comic, :author_id => @other_author.id, :visible => 1
486       cl = Comic.mylist @author
487       cl.should eq [@comic]
488     end
489     it '自分のコミックは非公開でも含んでいる' do
490       nc = FactoryGirl.create :comic, :author_id => @author.id, :visible => 0, :updated_at => Time.now + 100
491       cl = Comic.mylist @author
492       cl.should eq [nc, @comic]
493     end
494     context 'DBに5件あって1ページの件数を2件に変えたとして' do
495       before do
496         @comic2 = FactoryGirl.create :comic, :author_id => @author.id, :updated_at => Time.now + 100
497         @comic3 = FactoryGirl.create :comic, :author_id => @author.id, :updated_at => Time.now + 200
498         @comic4 = FactoryGirl.create :comic, :author_id => @author.id, :updated_at => Time.now + 300
499         @comic5 = FactoryGirl.create :comic, :author_id => @author.id, :updated_at => Time.now + 400
500       end
501       it '通常は2件を返す' do
502         c = Comic.mylist @author, 1, 2
503         c.should have(2).items 
504       end
505       it 'page=1なら末尾2件を返す' do
506         #時系列で並んでいる
507         c = Comic.mylist(@author, 1, 2)
508         c.should eq [@comic5, @comic4]
509       end
510       it 'page=2なら中間2件を返す' do
511         c = Comic.mylist(@author, 2, 2)
512         c.should eq [@comic3, @comic2]
513       end
514       it 'page=3なら先頭1件を返す' do
515         c = Comic.mylist(@author, 3, 2)
516         c.should eq [@comic]
517       end
518     end
519   end
520   
521   describe '他作家のコミック一覧取得に於いて' do
522     before do
523       @comic = FactoryGirl.create :comic, :author_id => @author.id
524       @other_comic = FactoryGirl.create :comic, :author_id => @other_author.id, :visible => 1
525     end
526     context 'つつがなく終わるとき' do
527       it '一覧取得オプションを利用している' do
528         Comic.stub(:list_opt).with(any_args).and_return({})
529         Comic.should_receive(:list_opt).with(any_args).exactly(1)
530         r = Comic.himlist @other_author
531       end
532     end
533     it '指定した作家のリストを返す' do
534       r = Comic.himlist @other_author
535       r.should eq [@other_comic]
536     end
537     it '時系列で並んでいる' do
538       nc = FactoryGirl.create :comic, :author_id => @other_author.id, :updated_at => Time.now + 100
539       r = Comic.himlist @other_author
540       r.should eq [nc, @other_comic]
541     end
542     it '公開コミックに限る ' do
543       hidden = FactoryGirl.create :comic, :author_id => @other_author.id, :visible => 0
544       r = Comic.himlist @other_author
545       r.should eq [@other_comic]
546     end
547     context 'DBに5件あって1ページの件数を2件に変えたとして' do
548       before do
549         @other_comic2 = FactoryGirl.create :comic, :author_id => @other_author.id, :updated_at => Time.now + 100
550         @other_comic3 = FactoryGirl.create :comic, :author_id => @other_author.id, :updated_at => Time.now + 200
551         @other_comic4 = FactoryGirl.create :comic, :author_id => @other_author.id, :updated_at => Time.now + 300
552         @other_comic5 = FactoryGirl.create :comic, :author_id => @other_author.id, :updated_at => Time.now + 400
553       end
554       it '通常は2件を返す' do
555         c = Comic.himlist @other_author, 1, 2
556         c.should have(2).items 
557       end
558       it 'page=1なら末尾2件を返す' do
559         #時系列で並んでいる
560         c = Comic.himlist(@other_author, 1, 2)
561         c.should eq [@other_comic5, @other_comic4]
562       end
563       it 'page=2なら中間2件を返す' do
564         c = Comic.himlist(@other_author, 2, 2)
565         c.should eq [@other_comic3, @other_comic2]
566       end
567       it 'page=3なら先頭1件を返す' do
568         c = Comic.himlist(@other_author, 3, 2)
569         c.should eq [@other_comic]
570       end
571     end
572   end
573   
574   describe 'コミック一覧ページ制御に於いて' do
575     before do
576       Comic.stub(:count).with(any_args).and_return(100)
577     end
578     it 'ページ制御を返す' do
579       r = Comic.list_paginate 
580       r.is_a?(Kaminari::PaginatableArray).should be_true
581     end
582     it 'コミック一覧の取得条件を利用している' do
583       Comic.stub(:list_where).with(any_args).and_return('')
584       Comic.should_receive(:list_where).with(any_args).exactly(1)
585       r = Comic.list_paginate 
586     end
587     it 'ページ件数10のとき、3ページ目のオフセットは20から始まる' do
588       r = Comic.list_paginate 3, 10
589       r.limit_value.should eq 10
590       r.offset_value.should eq 20
591     end
592   end
593   
594   describe '自分のコミック一覧ページ制御に於いて' do
595     before do
596       Comic.stub(:count).with(any_args).and_return(100)
597     end
598     it 'ページ制御を返す' do
599       r = Comic.mylist_paginate @author
600       r.is_a?(Kaminari::PaginatableArray).should be_true
601     end
602     it '自分のコミック一覧の取得条件を利用している' do
603       Comic.stub(:mylist_where).with(any_args).and_return('')
604       Comic.should_receive(:mylist_where).with(any_args).exactly(1)
605       r = Comic.mylist_paginate @author
606     end
607     it 'ページ件数10のとき、3ページ目のオフセットは20から始まる' do
608       r = Comic.mylist_paginate @author, 3, 10
609       r.limit_value.should eq 10
610       r.offset_value.should eq 20
611     end
612   end
613   
614   describe '他作家のコミック一覧ページ制御に於いて' do
615     before do
616       Comic.stub(:count).with(any_args).and_return(100)
617     end
618     it 'ページ制御を返す' do
619       r = Comic.himlist_paginate @other_author
620       r.is_a?(Kaminari::PaginatableArray).should be_true
621     end
622     it '他作家のコミック一覧の取得条件を利用している' do
623       Comic.stub(:himlist_where).with(any_args).and_return('')
624       Comic.should_receive(:himlist_where).with(any_args).exactly(1)
625       r = Comic.himlist_paginate @other_author
626     end
627     it 'ページ件数10のとき、3ページ目のオフセットは20から始まる' do
628       r = Comic.himlist_paginate @other_author, 3, 10
629       r.limit_value.should eq 10
630       r.offset_value.should eq 20
631     end
632   end
633   
634   describe '一覧取得オプションに於いて' do
635     it '2つの項目を含んでいる' do
636       r = Comic.list_opt
637       r.should have(2).items
638     end
639     it 'ストーリーを含んでいる' do
640       r = Comic.list_opt
641       r.has_key?(:stories).should be_true
642     end
643     it '作家を含んでいる' do
644       r = Comic.list_opt
645       r.has_key?(:author).should be_true
646     end
647   end
648   describe 'json一覧出力オプションに於いて' do
649     before do
650       @op = FactoryGirl.create :original_picture, :artist_id => @artist.id
651       @p = FactoryGirl.create :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @artist.id
652       @rp = FactoryGirl.create :resource_picture, :artist_id => @artist.id, :license_id => @license.id, :original_picture_id => @op.id, :picture_id => @p.id
653       @sbt = FactoryGirl.create :speech_balloon_template
654       @comic = FactoryGirl.create :comic, :author_id => @author.id, :visible => 1
655       @story = FactoryGirl.create :story, :comic_id => @comic.id
656     end
657     it 'ストーリーを含んでいる' do
658       r = Comic.list.to_json Comic.list_json_opt
659       j = JSON.parse r
660       i = j.first
661       i.has_key?('stories').should be_true
662     end
663     it '作家を含んでいる' do
664       r = Comic.list.to_json Comic.list_json_opt
665       j = JSON.parse r
666       i = j.first
667       i.has_key?('author').should be_true
668     end
669   end
670   
671   describe '単体取得に於いて' do
672     before do
673       @comic = FactoryGirl.create :comic, :author_id => @author.id
674     end
675     context 'つつがなく終わるとき' do
676       it '単体取得オプションを利用している' do
677         Comic.stub(:show_opt).with(any_args).and_return({})
678         Comic.should_receive(:show_opt).with(any_args).exactly(1)
679         r = Comic.show @comic.id, @author
680       end
681       it '閲覧許可を問い合わせている' do
682         Comic.any_instance.stub(:visible?).with(any_args).and_return(true)
683         Comic.any_instance.should_receive(:visible?).with(any_args).exactly(1)
684         r = Comic.show @comic.id, @author
685       end
686     end
687     it '指定のコミックを返す' do
688       c = Comic.show @comic.id, @author
689       c.should eq @comic
690     end
691     context '閲覧許可が出なかったとき' do
692       it '403Forbidden例外を返す' do
693         Comic.any_instance.stub(:visible?).and_return(false)
694         lambda{
695           Comic.show @comic.id, @author
696         }.should raise_error(ActiveRecord::Forbidden)
697       end
698     end
699     context '存在しないコミックを開こうとしたとき' do
700       it '404RecordNotFound例外を返す' do
701         lambda{
702           Comic.show 110, @author
703         }.should raise_error(ActiveRecord::RecordNotFound)
704       end
705     end
706   end
707   
708   describe '編集取得に於いて' do
709     before do
710       @comic = FactoryGirl.create :comic, :author_id => @author.id
711     end
712     context 'つつがなく終わるとき' do
713       it '単体取得オプションを利用している' do
714         Comic.stub(:show_opt).with(any_args).and_return({})
715         Comic.should_receive(:show_opt).with(any_args).exactly(1)
716         r = Comic.edit @comic.id, @author
717       end
718       it '所持判定を問い合わせている' do
719         Comic.any_instance.stub(:own?).with(any_args).and_return(true)
720         Comic.any_instance.should_receive(:own?).with(any_args).exactly(1)
721         r = Comic.edit @comic.id, @author
722       end
723     end
724     it '指定のコミックを返す' do
725       Comic.any_instance.stub(:own?).and_return(true)
726       c = Comic.edit @comic.id, @author.id
727       c.should eq @comic
728     end
729     context '他人のコミックを開こうとしたとき' do
730       it '403Forbidden例外を返す' do
731         Comic.any_instance.stub(:own?).and_return(false)
732         lambda{
733           Comic.edit @comic.id, @author
734         }.should raise_error(ActiveRecord::Forbidden)
735       end
736     end
737     context '存在しないコミックを開こうとしたとき' do
738       it '404RecordNotFound例外を返す' do
739         lambda{
740           Comic.edit 110, @author
741         }.should raise_error(ActiveRecord::RecordNotFound)
742       end
743     end
744   end
745   describe '単体取得オプションに於いて' do
746     it 'includeキーを含んでいる' do
747       r = Comic.show_opt
748       r.has_key?(:include).should be_true
749     end
750     it '2つの項目を含んでいる' do
751       r = Comic.show_opt[:include]
752       r.should have(2).items
753     end
754     it '作家を含んでいる' do
755       r = Comic.show_opt[:include]
756       r.has_key?(:author).should be_true
757     end
758     it 'ストーリーを含んでいる' do
759       r = Comic.show_opt[:include]
760       r.has_key?(:stories).should be_true
761     end
762   end
763   describe 'json単体出力オプションに於いて' do
764     before do
765       @op = FactoryGirl.create :original_picture, :artist_id => @artist.id
766       @p = FactoryGirl.create :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @artist.id
767       @rp = FactoryGirl.create :resource_picture, :artist_id => @artist.id, :license_id => @license.id, :original_picture_id => @op.id, :picture_id => @p.id
768       @sbt = FactoryGirl.create :speech_balloon_template
769       @comic = FactoryGirl.create :comic, :author_id => @author.id, :visible => 1
770       @story = FactoryGirl.create :story, :comic_id => @comic.id
771     end
772     it 'ストーリーを含んでいる' do
773       r = Comic.show(@comic.id, @author).to_json Comic.show_json_opt
774       j = JSON.parse r
775       i = j
776       i.has_key?('stories').should be_true
777     end
778     it '作家を含んでいる' do
779       r = Comic.show(@comic.id, @author).to_json Comic.show_json_opt
780       j = JSON.parse r
781       i = j
782       i.has_key?('author').should be_true
783     end
784   end
785   
786   describe '削除に於いて' do
787     before do
788       @comic = FactoryGirl.create :comic, :author_id => @author.id
789       @story = FactoryGirl.create :story, :comic_id => @comic.id
790       @other_comic = FactoryGirl.create :comic, :author_id => @author.id
791       @other_story = FactoryGirl.create :story, :comic_id => @other_comic.id
792     end
793     context 'つつがなく終わるとき' do
794       it '自身を削除する' do
795         lambda {
796           r = @comic.destroy_with_story
797         }.should change(Comic, :count).by(-1)
798         lambda {
799           r = Comic.find @comic.id
800         }.should raise_error
801       end
802       it '自身にリンクしているストーリーをすべて削除する' do
803         lambda {
804           r = @comic.destroy_with_story
805         }.should change(Story, :count).by(-1)
806         lambda {
807           r = Story.find @story.id
808         }.should raise_error
809       end
810       it 'Trueを返す' do
811         r = @comic.destroy_with_story
812         r.should be_true
813       end
814     end
815     context '削除に失敗したとき' do
816       before do
817         Story.any_instance.stub(:destroy).with(any_args).and_return(false)
818       end
819       it 'Falseを返す' do
820         r = @comic.destroy_with_story
821         r.should be_false
822       end
823       it 'ロールバックしている' do
824         lambda {
825           r = @comic.destroy_with_story
826         }.should_not change(Comic, :count)
827         lambda {
828           r = @comic.destroy_with_story
829         }.should_not change(Story, :count)
830       end
831     end
832   end
833 end