OSDN Git Service

t#32046:
[pettanr/pettanr.git] / spec / models / ground_color_spec.rb
1 # -*- encoding: utf-8 -*-
2 require 'spec_helper'
3 #色地
4
5 describe GroundColor do
6   before do
7     SpeechBalloonTemplate.delete_all
8     @admin = FactoryGirl.create :admin
9     @user = FactoryGirl.create( :user_yas)
10     @author = FactoryGirl.create :author, :user_id => @user.id
11     @artist = FactoryGirl.create :artist_yas, :author_id => @author.id
12     @other_user = FactoryGirl.create( :user_yas)
13     @other_author = FactoryGirl.create :author, :user_id => @other_user.id
14     @other_artist = FactoryGirl.create :artist_yas, :author_id => @other_author.id
15     @sp = FactoryGirl.create :system_picture
16     @lg = FactoryGirl.create :license_group
17     @license = FactoryGirl.create :license, :license_group_id => @lg.id, :system_picture_id => @sp.id
18     @op = FactoryGirl.create :original_picture, :artist_id => @artist.id
19     @p = FactoryGirl.create :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @artist.id
20     @rp = FactoryGirl.create :resource_picture, :artist_id => @artist.id, :license_id => @license.id, :original_picture_id => @op.id, :picture_id => @p.id
21     @panel = FactoryGirl.create :panel, :author_id => @author.id
22   end
23   
24   describe '検証に於いて' do
25     before do
26       @gc = FactoryGirl.build :ground_color, :panel_id => @panel.id
27     end
28     
29     context 'オーソドックスなデータのとき' do
30       it '下限データが通る' do
31         @gc.code = 0
32         @gc.z = 1
33         @gc.t = 0
34         @gc.should be_valid
35       end
36       it '上限データが通る' do
37         @gc.code = 99999
38         @gc.z = 99999
39         @gc.t = 99999
40         @gc.should be_valid
41       end
42     end
43     
44     context 'panel_idを検証するとき' do
45       #ネストの保存はnilを許可しなければならないので数値チェックだけ
46       it '数値でなければ失敗する' do
47         @gc.panel_id = 'a'
48         @gc.should_not be_valid
49       end
50     end
51     context 'codeを検証するとき' do
52       it 'テストデータの確認' do
53         @gc.code = 0xffffff
54         @gc.should be_valid
55       end
56       it 'nullなら失敗する' do
57         @gc.code = nil
58         @gc.should_not be_valid
59       end
60       it '数値でなければ失敗する' do
61         @gc.code = 'a'
62         @gc.should_not be_valid
63       end
64       it '負なら失敗する' do
65         @gc.code = -1
66         @gc.should_not be_valid
67       end
68       it '24bit colorでなければ失敗する' do
69         @gc.code = 0x1000000
70         @gc.should_not be_valid
71       end
72     end
73     context 'zを検証するとき' do
74       it 'nullなら失敗する' do
75         @gc.z = nil
76         @gc.should_not be_valid
77       end
78       it '数値でなければ失敗する' do
79         @gc.z = 'a'
80         @gc.should_not be_valid
81       end
82       it '負なら失敗する' do
83         @gc.z = -1
84         @gc.should_not be_valid
85       end
86       it '負なら失敗する' do
87         @gc.z = 0
88         @gc.should_not be_valid
89       end
90     end
91     context 'tを検証するとき' do
92       it 'nullなら失敗する' do
93         @gc.t = nil
94         @gc.should_not be_valid
95       end
96       it '数値でなければ失敗する' do
97         @gc.t = 'a'
98         @gc.should_not be_valid
99       end
100       it '負なら失敗する' do
101         @gc.t = -1
102         @gc.should_not be_valid
103       end
104     end
105   end
106   
107   describe '文字コード検証に於いて' do
108     before do
109       @gc = FactoryGirl.build :ground_color, :panel_id => @panel.id
110     end
111     
112     context 'captionを検証するとき' do
113       it 'Shift JISなら失敗する' do
114         @gc.caption = "\x83G\x83r\x83]\x83D"
115         lambda{
116           @gc.valid_encode
117         }.should raise_error(Pettanr::BadRequest)
118       end
119     end
120   end
121   
122   describe 'デフォルト値補充に於いて' do
123     it 'defined' do
124       @gc = FactoryGirl.build :ground_color, :panel_id => @panel.id
125       @gc.supply_default
126     end
127   end
128   
129   describe '上書き補充に於いて' do
130     it 'panel_idが設定されている' do
131       @gc = FactoryGirl.build :ground_color, :panel_id => @panel.id
132       @gc.overwrite  @panel.id
133       @gc.panel_id.should eq @panel.id
134     end
135   end
136   
137   describe '閲覧許可に於いて' do
138     before do
139       @gc = FactoryGirl.create :ground_color, :panel_id => @panel.id
140     end
141     context 'オープンモードのとき' do
142       before do
143         MagicNumber['run_mode'] = 0
144       end
145       it '自身にゲスト用ロールチェックを問い合わせしている' do
146         GroundColor.any_instance.stub(:guest_role_check).and_return(true)
147         GroundColor.any_instance.should_receive(:guest_role_check).with(any_args).exactly(1)
148         r = @gc.visible?([@author])
149       end
150       it 'ゲスト用ロールチェックが失敗したとき、falseを返す' do
151         GroundColor.any_instance.stub(:guest_role_check).and_return(false)
152         r = @gc.visible?([@author])
153         r.should be_false
154       end
155     end
156     context 'クローズドモードのとき' do
157       before do
158         MagicNumber['run_mode'] = 1
159       end
160       it '自身に読者用ロールチェックを問い合わせしている' do
161         GroundColor.any_instance.stub(:reader_role_check).and_return(true)
162         GroundColor.any_instance.should_receive(:reader_role_check).with(any_args).exactly(1)
163         r = @gc.visible?([@author])
164       end
165       it '読者用ロールチェックが失敗したとき、falseを返す' do
166         GroundColor.any_instance.stub(:reader_role_check).and_return(false)
167         r = @gc.visible?([@author])
168         r.should be_false
169       end
170     end
171     context '事前チェックする' do
172       before do
173         MagicNumber['run_mode'] = 1
174         GroundColor.any_instance.stub(:reader_role_check).and_return(true)
175       end
176       it '自身のコマに所持判定を問い合わせしている' do
177         Panel.any_instance.stub(:own?).and_return(true)
178         Panel.any_instance.should_receive(:own?).with(any_args).exactly(1)
179         r = @gc.visible?([@author])
180       end
181       it '自身のコマに閲覧許可を問い合わせしている' do
182         Panel.any_instance.stub(:own?).and_return(false)
183         Panel.any_instance.stub(:visible?).and_return(true)
184         Panel.any_instance.should_receive(:visible?).with(any_args).exactly(1)
185         r = @gc.visible?([@author])
186       end
187     end
188     context 'つつがなく終わるとき' do
189       before do
190         MagicNumber['run_mode'] = 1
191         Panel.any_instance.stub(:reader_role_check).and_return(true)
192       end
193       it '自分のコマの色地なら許可する' do
194         Panel.any_instance.stub(:own?).and_return(true)
195         Panel.any_instance.stub(:visible?).and_return(false)
196         r = @gc.visible?([@author])
197         r.should be_true
198       end
199       it '他人の非公開コマの色地なら許可しない' do
200         Panel.any_instance.stub(:own?).and_return(false)
201         Panel.any_instance.stub(:visible?).and_return(false)
202         r = @gc.visible?([@author])
203         r.should be_false
204       end
205       it '他人のコマの色地でも公開なら許可する' do
206         Panel.any_instance.stub(:own?).and_return(false)
207         Panel.any_instance.stub(:visible?).and_return(true)
208         r = @gc.visible?([@author])
209         r.should be_true
210       end
211     end
212   end
213   
214   describe '一覧取得に於いて' do
215     before do
216       @gc = FactoryGirl.create :ground_color, :panel_id => @panel.id
217     end
218     context 'page補正について' do
219       it '文字列から数値に変換される' do
220         GroundColor.page('8').should eq 8
221       end
222       it 'nilの場合は1になる' do
223         GroundColor.page().should eq 1
224       end
225       it '0以下の場合は1になる' do
226         GroundColor.page('0').should eq 1
227       end
228     end
229     context 'page_size補正について' do
230       it '文字列から数値に変換される' do
231         GroundColor.page_size('7').should eq 7
232       end
233       it 'nilの場合はGroundColor.default_page_sizeになる' do
234         GroundColor.page_size().should eq GroundColor.default_page_size
235       end
236       it '0以下の場合はGroundColor.default_page_sizeになる' do
237         GroundColor.page_size('0').should eq GroundColor.default_page_size
238       end
239       it 'GroundColor.max_page_sizeを超えた場合はGroundColor.max_page_sizeになる' do
240         GroundColor.page_size('1000').should eq GroundColor.max_page_size
241       end
242     end
243     context 'つつがなく終わるとき' do
244       it '一覧取得オプションを利用している' do
245         GroundColor.stub(:list_opt).with(any_args).and_return({:include => :panel})
246         GroundColor.should_receive(:list_opt).with(any_args).exactly(1)
247         r = GroundColor.list
248       end
249     end
250     it 'リストを返す' do
251       pl = GroundColor.list
252       pl.should eq [@gc]
253     end
254     it '時系列で並んでいる' do
255       #公開コマなら(他人のコマであっても)含んでいる
256       opl = FactoryGirl.create :panel, :author_id => @other_author.id, :publish => 1
257       npl = FactoryGirl.create :ground_color, :panel_id => opl.id, :updated_at => Time.now + 100
258       pl = GroundColor.list
259       pl.should eq [npl, @gc]
260     end
261     it '非公開のコマの色地は含まない' do
262       hpl = FactoryGirl.create :panel, :author_id => @author.id, :publish => 0
263       npl = FactoryGirl.create :ground_color, :panel_id => hpl.id, :updated_at => Time.now + 100
264       pl = GroundColor.list
265       pl.should eq [@gc]
266     end
267     context 'DBに5件あって1ページの件数を2件に変えたとして' do
268       before do
269         @gc2 = FactoryGirl.create :ground_color, :panel_id => @panel.id, :z => 1, :updated_at => Time.now + 100
270         @gc3 = FactoryGirl.create :ground_color, :panel_id => @panel.id, :z => 2, :updated_at => Time.now + 200
271         @gc4 = FactoryGirl.create :ground_color, :panel_id => @panel.id, :z => 3, :updated_at => Time.now + 300
272         @gc5 = FactoryGirl.create :ground_color, :panel_id => @panel.id, :z => 4, :updated_at => Time.now + 400
273         GroundColor.stub(:default_page_size).and_return(2)
274       end
275       it '通常は2件を返す' do
276         pl = GroundColor.list
277         pl.should have(2).items 
278       end
279       it 'page=1なら末尾2件を返す' do
280         #時系列で並んでいる
281         pl = GroundColor.list(1)
282         pl.should eq [@gc5, @gc4]
283       end
284       it 'page=2なら中間2件を返す' do
285         pl = GroundColor.list(2)
286         pl.should eq [@gc3, @gc2]
287       end
288       it 'page=3なら先頭1件を返す' do
289         pl = GroundColor.list(3)
290         pl.should eq [@gc]
291       end
292     end
293   end
294   
295   describe '自分のコマで使った色地一覧取得に於いて' do
296     before do
297       @gc = FactoryGirl.create :ground_color, :panel_id => @panel.id
298     end
299     context 'つつがなく終わるとき' do
300       it '一覧取得オプションを利用している' do
301         GroundColor.stub(:list_opt).with(any_args).and_return({:include => :panel})
302         GroundColor.should_receive(:list_opt).with(any_args).exactly(1)
303         r = GroundColor.mylist @author
304       end
305     end
306     it 'リストを返す' do
307       pl = GroundColor.mylist @author
308       pl.should eq [@gc]
309     end
310     it '時系列で並んでいる' do
311       npl = FactoryGirl.create :ground_color, :panel_id => @panel.id, :z => 2, :updated_at => Time.now + 100
312       pl = GroundColor.mylist @author
313       pl.should eq [npl, @gc]
314     end
315     it '他人のコマの色地は公開でも含まない' do
316       hpl = FactoryGirl.create :panel, :author_id => @other_author.id, :publish => 1
317       npl = FactoryGirl.create :ground_color, :panel_id => hpl.id
318       pl = GroundColor.mylist @author
319       pl.should eq [@gc]
320     end
321     it '自分のコマの色地は非公開でも含んでいる' do
322       hpl = FactoryGirl.create :panel, :author_id => @author.id, :publish => 0
323       npl = FactoryGirl.create :ground_color, :panel_id => hpl.id, :z => 2, :updated_at => Time.now + 100
324       pl = GroundColor.mylist @author
325       pl.should eq [npl, @gc]
326     end
327     context 'DBに5件あって1ページの件数を2件に変えたとして' do
328       before do
329         @gc2 = FactoryGirl.create :ground_color, :panel_id => @panel.id, :z => 1, :updated_at => Time.now + 100
330         @gc3 = FactoryGirl.create :ground_color, :panel_id => @panel.id, :z => 2, :updated_at => Time.now + 200
331         @gc4 = FactoryGirl.create :ground_color, :panel_id => @panel.id, :z => 3, :updated_at => Time.now + 300
332         @gc5 = FactoryGirl.create :ground_color, :panel_id => @panel.id, :z => 4, :updated_at => Time.now + 400
333       end
334       it '通常は2件を返す' do
335         c = GroundColor.mylist @author, 1, 2
336         c.should have(2).items 
337       end
338       it 'page=1なら末尾2件を返す' do
339         #時系列で並んでいる
340         c = GroundColor.mylist(@author, 1, 2)
341         c.should eq [@gc5, @gc4]
342       end
343       it 'page=2なら中間2件を返す' do
344         c = GroundColor.mylist(@author, 2, 2)
345         c.should eq [@gc3, @gc2]
346       end
347       it 'page=3なら先頭1件を返す' do
348         c = GroundColor.mylist(@author, 3, 2)
349         c.should eq [@gc]
350       end
351     end
352   end
353   
354   describe '他作家の色地一覧取得に於いて' do
355     before do
356       @gc = FactoryGirl.create :ground_color, :panel_id => @panel.id
357       @other_panel = FactoryGirl.create :panel, :author_id => @other_author.id, :publish => 1
358       @other_gc = FactoryGirl.create :ground_color, :panel_id => @other_panel.id
359     end
360     it 'リストを返す' do
361       r = GroundColor.himlist @other_author
362       r.should eq [@other_gc]
363     end
364     it '時系列で並んでいる' do
365       new_panel = FactoryGirl.create :panel, :author_id => @other_author.id, :updated_at => Time.now + 100
366       new_gc = FactoryGirl.create :ground_color, :panel_id => @other_panel.id, :updated_at => Time.now + 100
367       r = GroundColor.himlist @other_author
368       r.should eq [new_gc, @other_gc]
369     end
370     it '公開コマに限る' do
371       hidden_panel = FactoryGirl.create :panel, :author_id => @other_author.id, :publish => 0
372       hidden_gc = FactoryGirl.create :ground_color, :panel_id => hidden_panel.id
373       r = GroundColor.himlist @other_author
374       r.should eq [@other_gc]
375     end
376     context 'DBに5件あって1ページの件数を2件に変えたとして' do
377       before do
378         @other_gc2 = FactoryGirl.create :ground_color, :panel_id => @other_panel.id, :updated_at => Time.now + 100
379         @other_gc3 = FactoryGirl.create :ground_color, :panel_id => @other_panel.id, :updated_at => Time.now + 200
380         @other_gc4 = FactoryGirl.create :ground_color, :panel_id => @other_panel.id, :updated_at => Time.now + 300
381         @other_gc5 = FactoryGirl.create :ground_color, :panel_id => @other_panel.id, :updated_at => Time.now + 400
382       end
383       it '通常は2件を返す' do
384         pl = GroundColor.himlist @other_author, 1, 2
385         pl.should have(2).items 
386       end
387       it 'page=1なら末尾2件を返す' do
388         #時系列で並んでいる
389         pl = GroundColor.himlist @other_author, 1, 2
390         pl.should eq [@other_gc5, @other_gc4]
391       end
392       it 'page=2なら中間2件を返す' do
393         pl = GroundColor.himlist @other_author, 2, 2
394         pl.should eq [@other_gc3, @other_gc2]
395       end
396       it 'page=3なら先頭1件を返す' do
397         pl = GroundColor.himlist @other_author, 3, 2
398         pl.should eq [@other_gc]
399       end
400     end
401   end
402   
403   describe '色地一覧ページ制御に於いて' do
404     before do
405       GroundColor.stub(:count).with(any_args).and_return(100)
406     end
407     it 'ページ制御を返す' do
408       r = GroundColor.list_paginate 
409       r.is_a?(Kaminari::PaginatableArray).should be_true
410     end
411     it '色地一覧の取得条件を利用している' do
412       GroundColor.stub(:list_where).with(any_args).and_return('')
413       GroundColor.should_receive(:list_where).with(any_args).exactly(1)
414       r = GroundColor.list_paginate 
415     end
416     it 'ページ件数10のとき、3ページ目のオフセットは20から始まる' do
417       r = GroundColor.list_paginate 3, 10
418       r.limit_value.should eq 10
419       r.offset_value.should eq 20
420     end
421   end
422   
423   describe '自分の色地一覧ページ制御に於いて' do
424     before do
425       GroundColor.stub(:count).with(any_args).and_return(100)
426     end
427     it 'ページ制御を返す' do
428       r = GroundColor.mylist_paginate @author
429       r.is_a?(Kaminari::PaginatableArray).should be_true
430     end
431     it '自分の色地一覧の取得条件を利用している' do
432       GroundColor.stub(:mylist_where).with(any_args).and_return('')
433       GroundColor.should_receive(:mylist_where).with(any_args).exactly(1)
434       r = GroundColor.mylist_paginate @author
435     end
436     it 'ページ件数10のとき、3ページ目のオフセットは20から始まる' do
437       r = GroundColor.mylist_paginate @author, 3, 10
438       r.limit_value.should eq 10
439       r.offset_value.should eq 20
440     end
441   end
442   
443   describe '他作家の色地一覧ページ制御に於いて' do
444     before do
445       GroundColor.stub(:count).with(any_args).and_return(100)
446     end
447     it 'ページ制御を返す' do
448       r = GroundColor.himlist_paginate @other_author
449       r.is_a?(Kaminari::PaginatableArray).should be_true
450     end
451     it '他作家の色地一覧の取得条件を利用している' do
452       GroundColor.stub(:himlist_where).with(any_args).and_return('')
453       GroundColor.should_receive(:himlist_where).with(any_args).exactly(1)
454       r = GroundColor.himlist_paginate @other_author
455     end
456     it 'ページ件数10のとき、3ページ目のオフセットは20から始まる' do
457       r = GroundColor.himlist_paginate @other_author, 3, 10
458       r.limit_value.should eq 10
459       r.offset_value.should eq 20
460     end
461   end
462   
463   describe '一覧取得オプションに於いて' do
464     it '1つの項目を含んでいる' do
465       r = GroundColor.list_opt
466       r.should have(1).items
467     end
468     it 'コマを含んでいる' do
469       r = GroundColor.list_opt
470       r.has_key?(:panel).should be_true
471     end
472       it 'コマは作家を含んでいる' do
473         r = GroundColor.list_opt
474         r[:panel].has_key?(:author).should be_true
475       end
476   end
477   describe 'json一覧出力オプションに於いて' do
478     before do
479       @op = FactoryGirl.create :original_picture, :artist_id => @artist.id
480       @p = FactoryGirl.create :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @artist.id
481       @rp = FactoryGirl.create :resource_picture, :artist_id => @artist.id, :license_id => @license.id, :original_picture_id => @op.id, :picture_id => @p.id
482       @sbt = FactoryGirl.create :speech_balloon_template
483       @scroll = FactoryGirl.create :scroll, :author_id => @author.id, :visible => 1
484       @panel = FactoryGirl.create :panel, :author_id => @author.id, :publish => 1
485       @scroll_panel = FactoryGirl.create :scroll_panel, :author_id => @author.id, :scroll_id => @scroll.id, :panel_id => @panel.id
486       @gc = FactoryGirl.create :ground_color, :panel_id => @panel.id
487     end
488     it 'コマを含んでいる' do
489       r = GroundColor.list.to_json GroundColor.list_json_opt
490       j = JSON.parse r
491       i = j.first
492       i.has_key?('panel').should be_true
493     end
494       it 'コマは作家を含んでいる' do
495         r = GroundColor.list.to_json GroundColor.list_json_opt
496         j = JSON.parse r
497         i = j.first
498         s = i['panel']
499         s.has_key?('author').should be_true
500       end
501   end
502   
503   describe '単体取得に於いて' do
504     before do
505       @gc = FactoryGirl.create :ground_color, :panel_id => @panel.id
506     end
507     context 'つつがなく終わるとき' do
508       it '単体取得オプションを利用している' do
509         GroundColor.stub(:show_opt).with(any_args).and_return({:include => :panel})
510         GroundColor.should_receive(:show_opt).with(any_args).exactly(1)
511         r = GroundColor.show @gc.id, @author
512       end
513       it '閲覧許可を問い合わせている' do
514         GroundColor.any_instance.stub(:visible?).with(any_args).and_return(true)
515         GroundColor.any_instance.should_receive(:visible?).with(any_args).exactly(1)
516         r = GroundColor.show @gc.id, @author
517       end
518     end
519     it '指定の色地を返す' do
520       GroundColor.any_instance.stub(:visible?).and_return(true)
521       pl = GroundColor.show @gc.id, @author
522       pl.should eq @gc
523     end
524     context '閲覧許可が出なかったとき' do
525       it '403Forbidden例外を返す' do
526         GroundColor.any_instance.stub(:visible?).and_return(false)
527         lambda{
528           GroundColor.show @gc.id, @author
529         }.should raise_error(ActiveRecord::Forbidden)
530       end
531     end
532     context '存在しない色地を開こうとしたとき' do
533       it '404RecordNotFound例外を返す' do
534         lambda{
535           GroundColor.show 110, @author
536         }.should raise_error(ActiveRecord::RecordNotFound)
537       end
538     end
539   end
540   describe '単体取得オプションに於いて' do
541     it 'includeキーを含んでいる' do
542       r = GroundColor.show_opt
543       r.has_key?(:include).should be_true
544     end
545     it '1つの項目を含んでいる' do
546       r = GroundColor.show_opt[:include]
547       r.should have(1).items
548     end
549     it 'コマを含んでいる' do
550       r = GroundColor.show_opt[:include]
551       r.has_key?(:panel).should be_true
552     end
553       it 'コマは作家を含んでいる' do
554         r = GroundColor.show_opt[:include]
555         r[:panel].has_key?(:author).should be_true
556       end
557   end
558   describe 'json単体出力オプションに於いて' do
559     before do
560       @gc = FactoryGirl.create :ground_color, :panel_id => @panel.id
561     end
562     it 'コマを含んでいる' do
563       r = GroundColor.show(@gc.id, @author).to_json GroundColor.show_json_opt
564       j = JSON.parse r
565       i = j
566       i.has_key?('panel').should be_true
567     end
568       it 'コマは作家を含んでいる' do
569         r = GroundColor.show(@gc.id, @author).to_json GroundColor.show_json_opt
570         j = JSON.parse r
571         i = j
572         s = i['panel']
573         s.has_key?('author').should be_true
574       end
575   end
576   
577 end