OSDN Git Service

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