OSDN Git Service

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