OSDN Git Service

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