OSDN Git Service

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