OSDN Git Service

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