OSDN Git Service

t#31470:create pager
[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   end
445   
446   describe '自分のコミック一覧取得に於いて' do
447     before do
448       @comic = FactoryGirl.create :comic, :author_id => @author.id
449     end
450     context 'つつがなく終わるとき' do
451       it '一覧取得オプションを利用している' do
452         Comic.stub(:list_opt).with(any_args).and_return({})
453         Comic.should_receive(:list_opt).with(any_args).exactly(1)
454         r = Comic.mylist @author
455       end
456     end
457     it 'リストを返す' do
458       c = Comic.mylist @author
459       c.should eq [@comic]
460     end
461     it '時系列で並んでいる' do
462       nc = FactoryGirl.create :comic, :author_id => @author.id, :updated_at => Time.now + 100
463       cl = Comic.mylist @author
464       cl.should eq [nc, @comic]
465     end
466     it '他人のコミックは公開でも含まない' do
467       nc = FactoryGirl.create :comic, :author_id => @other_author.id, :visible => 1
468       cl = Comic.mylist @author
469       cl.should eq [@comic]
470     end
471     it '自分のコミックは非公開でも含んでいる' do
472       nc = FactoryGirl.create :comic, :author_id => @author.id, :visible => 0, :updated_at => Time.now + 100
473       cl = Comic.mylist @author
474       cl.should eq [nc, @comic]
475     end
476     context 'DBに5件あって1ページの件数を2件に変えたとして' do
477       before do
478         @comic2 = FactoryGirl.create :comic, :author_id => @author.id, :updated_at => Time.now + 100
479         @comic3 = FactoryGirl.create :comic, :author_id => @author.id, :updated_at => Time.now + 200
480         @comic4 = FactoryGirl.create :comic, :author_id => @author.id, :updated_at => Time.now + 300
481         @comic5 = FactoryGirl.create :comic, :author_id => @author.id, :updated_at => Time.now + 400
482       end
483       it '通常は2件を返す' do
484         c = Comic.mylist @author, 1, 2
485         c.should have(2).items 
486       end
487       it 'page=1なら末尾2件を返す' do
488         #時系列で並んでいる
489         c = Comic.mylist(@author, 1, 2)
490         c.should eq [@comic5, @comic4]
491       end
492       it 'page=2なら中間2件を返す' do
493         c = Comic.mylist(@author, 2, 2)
494         c.should eq [@comic3, @comic2]
495       end
496       it 'page=3なら先頭1件を返す' do
497         c = Comic.mylist(@author, 3, 2)
498         c.should eq [@comic]
499       end
500     end
501   end
502   
503   describe '他作家のコミック一覧取得に於いて' do
504     before do
505       @comic = FactoryGirl.create :comic, :author_id => @author.id
506       @other_comic = FactoryGirl.create :comic, :author_id => @other_author.id, :visible => 1
507     end
508     context 'つつがなく終わるとき' do
509       it '一覧取得オプションを利用している' do
510         Comic.stub(:list_opt).with(any_args).and_return({})
511         Comic.should_receive(:list_opt).with(any_args).exactly(1)
512         r = Comic.himlist @other_author
513       end
514     end
515     it '指定した作家のリストを返す' do
516       r = Comic.himlist @other_author
517       r.should eq [@other_comic]
518     end
519     it '時系列で並んでいる' do
520       nc = FactoryGirl.create :comic, :author_id => @other_author.id, :updated_at => Time.now + 100
521       r = Comic.himlist @other_author
522       r.should eq [nc, @other_comic]
523     end
524     it '公開コミックに限る ' do
525       hidden = FactoryGirl.create :comic, :author_id => @other_author.id, :visible => 0
526       r = Comic.himlist @other_author
527       r.should eq [@other_comic]
528     end
529     context 'DBに5件あって1ページの件数を2件に変えたとして' do
530       before do
531         @other_comic2 = FactoryGirl.create :comic, :author_id => @other_author.id, :updated_at => Time.now + 100
532         @other_comic3 = FactoryGirl.create :comic, :author_id => @other_author.id, :updated_at => Time.now + 200
533         @other_comic4 = FactoryGirl.create :comic, :author_id => @other_author.id, :updated_at => Time.now + 300
534         @other_comic5 = FactoryGirl.create :comic, :author_id => @other_author.id, :updated_at => Time.now + 400
535       end
536       it '通常は2件を返す' do
537         c = Comic.himlist @other_author, 1, 2
538         c.should have(2).items 
539       end
540       it 'page=1なら末尾2件を返す' do
541         #時系列で並んでいる
542         c = Comic.himlist(@other_author, 1, 2)
543         c.should eq [@other_comic5, @other_comic4]
544       end
545       it 'page=2なら中間2件を返す' do
546         c = Comic.himlist(@other_author, 2, 2)
547         c.should eq [@other_comic3, @other_comic2]
548       end
549       it 'page=3なら先頭1件を返す' do
550         c = Comic.himlist(@other_author, 3, 2)
551         c.should eq [@other_comic]
552       end
553     end
554   end
555   
556   describe 'コミック一覧ページ制御に於いて' do
557     before do
558       Comic.stub(:count).with(any_args).and_return(100)
559     end
560     it 'ページ制御を返す' do
561       r = Comic.list_paginate 
562       r.is_a?(Kaminari::PaginatableArray).should be_true
563     end
564     it 'コミック一覧の取得条件を利用している' do
565       Comic.stub(:list_where).with(any_args).and_return('')
566       Comic.should_receive(:list_where).with(any_args).exactly(1)
567       r = Comic.list_paginate 
568     end
569     it 'ページ件数10のとき、3ページ目のオフセットは20から始まる' do
570       r = Comic.list_paginate 3, 10
571       r.limit_value.should eq 10
572       r.offset_value.should eq 20
573     end
574   end
575   
576   describe '自分のコミック一覧ページ制御に於いて' do
577     before do
578       Comic.stub(:count).with(any_args).and_return(100)
579     end
580     it 'ページ制御を返す' do
581       r = Comic.mylist_paginate @author
582       r.is_a?(Kaminari::PaginatableArray).should be_true
583     end
584     it '自分のコミック一覧の取得条件を利用している' do
585       Comic.stub(:mylist_where).with(any_args).and_return('')
586       Comic.should_receive(:mylist_where).with(any_args).exactly(1)
587       r = Comic.mylist_paginate @author
588     end
589     it 'ページ件数10のとき、3ページ目のオフセットは20から始まる' do
590       r = Comic.mylist_paginate @author, 3, 10
591       r.limit_value.should eq 10
592       r.offset_value.should eq 20
593     end
594   end
595   
596   describe '他作家のコミック一覧ページ制御に於いて' do
597     before do
598       Comic.stub(:count).with(any_args).and_return(100)
599     end
600     it 'ページ制御を返す' do
601       r = Comic.himlist_paginate @other_author
602       r.is_a?(Kaminari::PaginatableArray).should be_true
603     end
604     it '他作家のコミック一覧の取得条件を利用している' do
605       Comic.stub(:himlist_where).with(any_args).and_return('')
606       Comic.should_receive(:himlist_where).with(any_args).exactly(1)
607       r = Comic.himlist_paginate @other_author
608     end
609     it 'ページ件数10のとき、3ページ目のオフセットは20から始まる' do
610       r = Comic.himlist_paginate @other_author, 3, 10
611       r.limit_value.should eq 10
612       r.offset_value.should eq 20
613     end
614   end
615   
616   describe '一覧取得オプションに於いて' do
617     it '2つの項目を含んでいる' do
618       r = Comic.list_opt
619       r.should have(2).items
620     end
621     it 'ストーリーを含んでいる' do
622       r = Comic.list_opt
623       r.has_key?(:stories).should be_true
624     end
625       it 'ストーリーはコマを含んでいる' do
626         r = Comic.list_opt
627         r[:stories].has_key?(:panel).should be_true
628       end
629     it '作家を含んでいる' do
630       r = Comic.list_opt
631       r.has_key?(:author).should be_true
632     end
633   end
634   describe 'json一覧出力オプションに於いて' do
635     before do
636       @op = FactoryGirl.create :original_picture, :artist_id => @artist.id
637       @p = FactoryGirl.create :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @artist.id
638       @rp = FactoryGirl.create :resource_picture, :artist_id => @artist.id, :license_id => @license.id, :original_picture_id => @op.id, :picture_id => @p.id
639       @sbt = FactoryGirl.create :speech_balloon_template
640       @comic = FactoryGirl.create :comic, :author_id => @author.id, :visible => 1
641       @panel = FactoryGirl.create :panel, :author_id => @author.id, :publish => 1
642       @story = FactoryGirl.create :story, :author_id => @author.id, :comic_id => @comic.id, :panel_id => @panel.id
643     end
644     it 'ストーリーを含んでいる' do
645       r = Comic.list.to_json Comic.list_json_opt
646       j = JSON.parse r
647       i = j.first
648       i.has_key?('stories').should be_true
649     end
650       it 'ストーリーはコマを含んでいる' do
651         r = Comic.list.to_json Comic.list_json_opt
652         j = JSON.parse r
653         i = j.first
654         s = i['stories'].first
655         s.has_key?('panel').should be_true
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?('author').should be_true
662     end
663   end
664   
665   describe '単体取得に於いて' do
666     before do
667       @comic = FactoryGirl.create :comic, :author_id => @author.id
668     end
669     context 'つつがなく終わるとき' do
670       it '単体取得オプションを利用している' do
671         Comic.stub(:show_opt).with(any_args).and_return({})
672         Comic.should_receive(:show_opt).with(any_args).exactly(1)
673         r = Comic.show @comic.id, @author
674       end
675       it '閲覧許可を問い合わせている' do
676         Comic.any_instance.stub(:visible?).with(any_args).and_return(true)
677         Comic.any_instance.should_receive(:visible?).with(any_args).exactly(1)
678         r = Comic.show @comic.id, @author
679       end
680     end
681     it '指定のコミックを返す' do
682       c = Comic.show @comic.id, @author
683       c.should eq @comic
684     end
685     context '閲覧許可が出なかったとき' do
686       it '403Forbidden例外を返す' do
687         Comic.any_instance.stub(:visible?).and_return(false)
688         lambda{
689           Comic.show @comic.id, @author
690         }.should raise_error(ActiveRecord::Forbidden)
691       end
692     end
693     context '存在しないコミックを開こうとしたとき' do
694       it '404RecordNotFound例外を返す' do
695         lambda{
696           Comic.show 110, @author
697         }.should raise_error(ActiveRecord::RecordNotFound)
698       end
699     end
700   end
701   
702   describe '編集取得に於いて' do
703     before do
704       @comic = FactoryGirl.create :comic, :author_id => @author.id
705     end
706     context 'つつがなく終わるとき' do
707       it '単体取得オプションを利用している' do
708         Comic.stub(:show_opt).with(any_args).and_return({})
709         Comic.should_receive(:show_opt).with(any_args).exactly(1)
710         r = Comic.edit @comic.id, @author
711       end
712       it '所持判定を問い合わせている' do
713         Comic.any_instance.stub(:own?).with(any_args).and_return(true)
714         Comic.any_instance.should_receive(:own?).with(any_args).exactly(1)
715         r = Comic.edit @comic.id, @author
716       end
717     end
718     it '指定のコミックを返す' do
719       Comic.any_instance.stub(:own?).and_return(true)
720       c = Comic.edit @comic.id, @author.id
721       c.should eq @comic
722     end
723     context '他人のコミックを開こうとしたとき' do
724       it '403Forbidden例外を返す' do
725         Comic.any_instance.stub(:own?).and_return(false)
726         lambda{
727           Comic.edit @comic.id, @author
728         }.should raise_error(ActiveRecord::Forbidden)
729       end
730     end
731     context '存在しないコミックを開こうとしたとき' do
732       it '404RecordNotFound例外を返す' do
733         lambda{
734           Comic.edit 110, @author
735         }.should raise_error(ActiveRecord::RecordNotFound)
736       end
737     end
738   end
739   describe '単体取得オプションに於いて' do
740     it 'includeキーを含んでいる' do
741       r = Comic.show_opt
742       r.has_key?(:include).should be_true
743     end
744     it '2つの項目を含んでいる' do
745       r = Comic.show_opt[:include]
746       r.should have(2).items
747     end
748     it '作家を含んでいる' do
749       r = Comic.show_opt[:include]
750       r.has_key?(:author).should be_true
751     end
752     it 'ストーリーを含んでいる' do
753       r = Comic.show_opt[:include]
754       r.has_key?(:stories).should be_true
755     end
756       it 'ストーリーはコマを含んでいる' do
757         r = Comic.show_opt[:include]
758         r[:stories].has_key?(:panel).should be_true
759       end
760   end
761   describe 'json単体出力オプションに於いて' do
762     before do
763       @op = FactoryGirl.create :original_picture, :artist_id => @artist.id
764       @p = FactoryGirl.create :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @artist.id
765       @rp = FactoryGirl.create :resource_picture, :artist_id => @artist.id, :license_id => @license.id, :original_picture_id => @op.id, :picture_id => @p.id
766       @sbt = FactoryGirl.create :speech_balloon_template
767       @comic = FactoryGirl.create :comic, :author_id => @author.id, :visible => 1
768       @panel = FactoryGirl.create :panel, :author_id => @author.id, :publish => 1
769       @story = FactoryGirl.create :story, :author_id => @author.id, :comic_id => @comic.id, :panel_id => @panel.id
770     end
771     it 'ストーリーを含んでいる' do
772       r = Comic.show(@comic.id, @author).to_json Comic.show_json_opt
773       j = JSON.parse r
774       i = j
775       i.has_key?('stories').should be_true
776     end
777       it 'ストーリーはコマを含んでいる' do
778         r = Comic.show(@comic.id, @author).to_json Comic.show_json_opt
779         j = JSON.parse r
780         i = j
781         s = i['stories'].first
782         s.has_key?('panel').should be_true
783       end
784     it '作家を含んでいる' do
785       r = Comic.show(@comic.id, @author).to_json Comic.show_json_opt
786       j = JSON.parse r
787       i = j
788       i.has_key?('author').should be_true
789     end
790   end
791   
792   describe '削除に於いて' do
793     before do
794       @comic = FactoryGirl.create :comic, :author_id => @author.id
795       @panel = FactoryGirl.create :panel, :author_id => @author.id, :publish => 1
796       @story = FactoryGirl.create :story, :author_id => @author.id, :comic_id => @comic.id, :panel_id => @panel.id
797       @other_comic = FactoryGirl.create :comic, :author_id => @author.id
798       @other_story = FactoryGirl.create :story, :author_id => @author.id, :comic_id => @other_comic.id, :panel_id => @panel.id
799     end
800     context 'つつがなく終わるとき' do
801       it '自身を削除する' do
802         lambda {
803           r = @comic.destroy_with_story
804         }.should change(Comic, :count).by(-1)
805         lambda {
806           r = Comic.find @comic.id
807         }.should raise_error
808       end
809       it '自身にリンクしているストーリーをすべて削除する' do
810         lambda {
811           r = @comic.destroy_with_story
812         }.should change(Story, :count).by(-1)
813         lambda {
814           r = Story.find @story.id
815         }.should raise_error
816       end
817       it 'Trueを返す' do
818         r = @comic.destroy_with_story
819         r.should be_true
820       end
821     end
822     context '削除に失敗したとき' do
823       before do
824         Story.any_instance.stub(:destroy).with(any_args).and_return(false)
825       end
826       it 'Falseを返す' do
827         r = @comic.destroy_with_story
828         r.should be_false
829       end
830       it 'ロールバックしている' do
831         lambda {
832           r = @comic.destroy_with_story
833         }.should_not change(Comic, :count)
834         lambda {
835           r = @comic.destroy_with_story
836         }.should_not change(Story, :count)
837       end
838     end
839   end
840 end