OSDN Git Service

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