OSDN Git Service

t#32046:
[pettanr/pettanr.git] / spec / models / system_picture_spec.rb
1 # -*- encoding: utf-8 -*-
2 require 'spec_helper'
3 #システム画像
4 describe SystemPicture do
5   before do
6     SpeechBalloonTemplate.delete_all
7     SystemPicture.delete_all
8     @admin = FactoryGirl.create :admin
9   end
10   
11   describe '検証に於いて' do
12     before do
13       @sp = FactoryGirl.build :system_picture
14     end
15     
16     context 'オーソドックスなデータのとき' do
17       it '下限データが通る' do
18         @sp.ext = 'png' #リストにない拡張子は通らないし
19         @sp.width = 1
20         @sp.height = 1
21         @sp.filesize = 1
22         @sp.md5 = 'a'*32
23         @sp.should be_valid
24       end
25       it '上限データが通る' do
26         @sp.ext = 'jpeg'
27         @sp.width = 99999
28         @sp.height = 99999
29         @sp.filesize = 2000000
30         @sp.md5 = 'a'*32
31         @sp.should be_valid
32       end
33     end
34     
35     context 'extを検証するとき' do
36       it 'nullなら失敗する' do
37         @sp.ext = ''
38         @sp.should_not be_valid
39       end
40       it '5文字以上なら失敗する' do
41         @sp.ext = 'a'*5
42         @sp.should_not be_valid
43       end
44       it 'png,gif,jpeg以外なら失敗する' do
45         @sp.ext = 'bmp'
46         @sp.should_not be_valid
47       end
48     end
49     context 'widthを検証するとき' do
50       it 'nullなら失敗する' do
51         @sp.width = nil
52         @sp.should_not be_valid
53       end
54       it '数値でなければ失敗する' do
55         @sp.width = 'a'
56         @sp.should_not be_valid
57       end
58       it '0なら失敗する' do
59         @sp.width = '0'
60         @sp.should_not be_valid
61       end
62       it '負でも失敗する' do
63         @sp.width = -1
64         @sp.should_not be_valid
65       end
66     end
67     context 'heightを検証するとき' do
68       it 'nullなら失敗する' do
69         @sp.height = nil
70         @sp.should_not be_valid
71       end
72       it '数値でなければ失敗する' do
73         @sp.height = 'a'
74         @sp.should_not be_valid
75       end
76       it '0なら失敗する' do
77         @sp.height = '0'
78         @sp.should_not be_valid
79       end
80       it '負でも失敗する' do
81         @sp.height = -1
82         @sp.should_not be_valid
83       end
84     end
85     context 'filesizeを検証するとき' do
86       it 'nullなら失敗する' do
87         @sp.filesize = nil
88         @sp.should_not be_valid
89       end
90       it '数値でなければ失敗する' do
91         @sp.filesize = 'a'
92         @sp.should_not be_valid
93       end
94       it '負なら失敗する' do
95         @sp.filesize = '-1'
96         @sp.should_not be_valid
97       end
98       it '2MB以上なら失敗する' do
99         @sp.filesize = 2000000+1
100         @sp.should_not be_valid
101       end
102     end
103     context 'md5を検証するとき' do
104       it 'nullなら失敗する' do
105         @sp.md5 = ''
106         @sp.should_not be_valid
107       end
108       it '31文字なら失敗する' do
109         @sp.md5 = 'a'*31
110         @sp.should_not be_valid
111       end
112       it '33文字なら失敗する' do
113         @sp.md5 = 'a'*33
114         @sp.should_not be_valid
115       end
116     end
117   end
118   
119   describe 'デフォルト値補充に於いて' do
120     it 'defined' do
121       @sp = FactoryGirl.build :system_picture
122       @sp.supply_default
123     end
124   end
125   
126   describe '上書き補充に於いて' do
127     it 'defined' do
128       @sp = FactoryGirl.build :system_picture
129       @sp.overwrite
130     end
131   end
132   
133   describe '所持判定に於いて' do
134     before do
135       @sp = FactoryGirl.build :system_picture
136     end
137     it '管理者のアクセス権はすべて等しいので、Trueを返す' do
138       @sp.own?(@admin).should == true
139     end
140   end
141   
142   describe '閲覧許可に於いて' do
143     before do
144       @sp = FactoryGirl.build :system_picture
145     end
146     it '必ず許可となる' do
147       r = @sp.visible?(@admin)
148       r.should == true
149     end
150   end
151   
152   describe 'ファイル名に於いて' do
153     before do
154       @sp = FactoryGirl.create :system_picture
155     end
156     it 'id+拡張子のフォーマットで返す' do
157       r = @sp.filename
158       r.should eq "#{@sp.id}.png"
159     end
160   end
161   
162   describe 'MimeTypeに於いて' do
163     before do
164       @sp = FactoryGirl.create :system_picture
165     end
166     it 'image/拡張子のフォーマットで返す' do
167       r = @sp.mime_type
168       r.should eq "image/png"
169     end
170   end
171   
172   describe 'ファイルのurlに於いて' do
173     before do
174       @sp = FactoryGirl.create :system_picture
175       SystemPicture.any_instance.stub(:filename).and_return('3.gif')
176     end
177     it 'ファイル名取得を依頼している' do
178       SystemPicture.any_instance.should_receive(:filename).exactly(1)
179       @sp.url
180     end
181     it '/original_pictures/3.gifのフォーマットで返す' do
182       r = @sp.url
183       r.should eq "/system_pictures/3.gif"
184     end
185   end
186   
187   describe '一覧取得に於いて' do
188     before do
189       @sp = FactoryGirl.create :system_picture
190     end
191     context 'page補正について' do
192       it '文字列から数値に変換される' do
193         SystemPicture.page('8').should eq 8
194       end
195       it 'nilの場合は1になる' do
196         SystemPicture.page().should eq 1
197       end
198       it '0以下の場合は1になる' do
199         SystemPicture.page('0').should eq 1
200       end
201     end
202     context 'page_size補正について' do
203       it '文字列から数値に変換される' do
204         SystemPicture.page_size('7').should eq 7
205       end
206       it 'nilの場合はSystemPicture.default_page_sizeになる' do
207         SystemPicture.page_size().should eq SystemPicture.default_page_size
208       end
209       it '0以下の場合はSystemPicture.default_page_sizeになる' do
210         SystemPicture.page_size('0').should eq SystemPicture.default_page_size
211       end
212       it 'SystemPicture.max_page_sizeを超えた場合はSystemPicture.max_page_sizeになる' do
213         SystemPicture.page_size('1000').should eq SystemPicture.max_page_size
214       end
215     end
216     context 'つつがなく終わるとき' do
217       it '一覧取得オプションを利用している' do
218         SystemPicture.stub(:list_opt).with(any_args).and_return({})
219         SystemPicture.should_receive(:list_opt).with(any_args).exactly(1)
220         r = SystemPicture.list
221       end
222     end
223     it 'リストを返す' do
224       r = SystemPicture.list
225       r.should eq [@sp]
226     end
227     it '時系列で並んでいる' do
228       ni = FactoryGirl.create :system_picture, :updated_at => Time.now + 100
229       r = SystemPicture.list
230       r.should eq [ni, @sp]
231     end
232     context 'DBに5件あって1ページの件数を2件に変えたとして' do
233       before do
234         @sp2 = FactoryGirl.create :system_picture, :updated_at => Time.now + 100
235         @sp3 = FactoryGirl.create :system_picture, :updated_at => Time.now + 200
236         @sp4 = FactoryGirl.create :system_picture, :updated_at => Time.now + 300
237         @sp5 = FactoryGirl.create :system_picture, :updated_at => Time.now + 400
238         SystemPicture.stub(:default_page_size).and_return(2)
239       end
240       it '通常は2件を返す' do
241         r = SystemPicture.list
242         r.should have(2).items 
243       end
244       it 'page=1なら末尾2件を返す' do
245         #時系列で並んでいる
246         r = SystemPicture.list 1, 2
247         r.should eq [@sp5, @sp4]
248       end
249       it 'page=2なら中間2件を返す' do
250         r = SystemPicture.list 2, 2
251         r.should eq [@sp3, @sp2]
252       end
253       it 'page=3なら先頭1件を返す' do
254         r = SystemPicture.list 3, 2
255         r.should eq [@sp]
256       end
257     end
258     context 'DBに5件あって1ページの件数を0件に変えたとして' do
259       before do
260         @sp2 = FactoryGirl.create :system_picture, :updated_at => Time.now + 100
261         @sp3 = FactoryGirl.create :system_picture, :updated_at => Time.now + 200
262         @sp4 = FactoryGirl.create :system_picture, :updated_at => Time.now + 300
263         @sp5 = FactoryGirl.create :system_picture, :updated_at => Time.now + 400
264         SystemPicture.stub(:default_page_size).and_return(2)
265       end
266       it '件数0は全件(5件)を返す' do
267         r = SystemPicture.list 5, 0
268         r.should have(5).items 
269       end
270     end
271   end
272   describe '一覧出力オプションに於いて' do
273     it 'Hashを返す' do
274       r = SystemPicture.list_opt
275       r.is_a?(Hash).should be_true
276     end
277     it '0の項目を含んでいる' do
278       r = SystemPicture.list_opt
279       r.should be_empty
280     end
281   end
282   describe 'json一覧出力オプションに於いて' do
283     it 'Hashを返す' do
284       r = SystemPicture.list_json_opt
285       r.is_a?(Hash).should be_true
286     end
287     it '0の項目を含んでいる' do
288       r = SystemPicture.list_json_opt
289       r.should be_empty
290     end
291   end
292   
293   describe '単体取得に於いて' do
294     before do
295       @sp = FactoryGirl.create :system_picture
296     end
297     context 'つつがなく終わるとき' do
298       it '単体取得オプションを利用している' do
299         SystemPicture.stub(:show_opt).with(any_args).and_return({})
300         SystemPicture.should_receive(:show_opt).with(any_args).exactly(1)
301         r = SystemPicture.show @sp.id, @admin
302       end
303       it '閲覧許可を問い合わせている' do
304         SystemPicture.any_instance.stub(:visible?).with(any_args).and_return(true)
305         SystemPicture.any_instance.should_receive(:visible?).with(any_args).exactly(1)
306         r = SystemPicture.show @sp.id, @admin
307       end
308     end
309     it '指定のシステム画像を返す' do
310       r = SystemPicture.show @sp.id, @admin
311       r.should eq @sp
312     end
313     context '他人のシステム画像を開こうとしたとき' do
314       it '403Forbidden例外を返す' do
315         SystemPicture.any_instance.stub(:visible?).and_return(false)
316         lambda{
317           r = SystemPicture.show @sp.id, @admin
318         }.should raise_error(ActiveRecord::Forbidden)
319       end
320     end
321     context '存在しないシステム画像を開こうとしたとき' do
322       it '404RecordNotFound例外を返す' do
323         lambda{
324           r = SystemPicture.show 0, @admin
325         }.should raise_error(ActiveRecord::RecordNotFound)
326       end
327     end
328   end
329   describe '単体出力オプションに於いて' do
330     it 'Hashを返す' do
331       r = SystemPicture.show_opt
332       r.is_a?(Hash).should be_true
333     end
334     it '0の項目を含んでいる' do
335       r = SystemPicture.show_opt
336       r.should be_empty
337     end
338   end
339   describe 'json単体出力オプションに於いて' do
340     it 'Hashを返す' do
341       r = SystemPicture.show_json_opt
342       r.is_a?(Hash).should be_true
343     end
344     it '0の項目を含んでいる' do
345       r = SystemPicture.show_json_opt
346       r.should be_empty
347     end
348   end
349   
350   describe '作成・更新に於いて' do
351     before do
352       @sp = FactoryGirl.build :system_picture
353       @imager = ImagerTest.load "abc\ndef\nghi"
354     end
355     context '事前チェック' do
356       before do
357         #すべての処理が正常パターンで通過すれば、一番深い分岐まで通る。
358         #それで外部のメソッド呼び出しだけに着目してテストする。
359         SystemPicture.any_instance.stub(:save).with(any_args).and_return(true)
360         PictureIO.system_picture_io.stub(:put).with(any_args).and_return(true)
361       end
362       it '自身が保存されている' do
363         SystemPicture.any_instance.should_receive(:save).exactly(1)
364         r = @sp.store @imager
365       end
366       it 'PictureIoに画像データの保存を依頼している' do
367         PictureIO.system_picture_io.should_receive(:put).with(any_args).exactly(1)
368         r = @sp.store @imager
369       end
370     end
371     context 'つつがなく終わるとき' do
372       before do
373         #すべての処理を正常パターンで通過させ、保存機能をチェックする。
374         PictureIO.system_picture_io.stub(:put).with(any_args).and_return(true)
375       end
376       it 'システム画像モデルが作成されている' do
377         lambda {
378           r = @sp.store @imager
379         }.should change SystemPicture, :count
380       end
381       it 'Trueを返す' do
382         r = @sp.store @imager
383         r.should eq true
384       end
385     end
386     #以下から例外ケース。処理先頭から失敗させていく
387     context 'imagerが初期化に失敗したとき' do
388       before do
389       end
390       it 'falseを返す' do
391         @sp.store(false).should be_false
392       end
393       it '自身の保存は呼ばれていない' do
394         SystemPicture.any_instance.should_not_receive(:save)
395         @sp.store(false)
396       end
397       it '全体エラーメッセージがセットされている' do
398         lambda {
399           @sp.store(false)
400         }.should change(@sp.errors[:base], :count)
401       end
402     end
403     context '自身の保存に失敗したとき' do
404       before do
405         SystemPicture.any_instance.stub(:save).with(any_args).and_return(false)
406       end
407       it 'falseを返す' do
408         r = @sp.store @imager
409         r.should be_false
410       end
411       it '更新されていない' do
412         r = @sp.store @imager
413         @sp.should be_a_new SystemPicture
414       end
415       it '原画の保存は呼ばれていない' do
416         PictureIO.system_picture_io.should_not_receive(:put)
417         r = @sp.store @imager
418       end
419     end
420     context '画像データの保存に失敗したとき' do
421       before do
422         PictureIO.system_picture_io.stub(:put).with(any_args).and_raise(PictureIO::Error)
423       end
424       it 'falseを返す' do
425         r = @sp.store @imager
426         r.should be_false
427       end
428       it '更新されていない' do
429         r = @sp.store @imager
430         @sp.should be_a_new SystemPicture
431       end
432       it '全体エラーメッセージがセットされている' do
433         r = @sp.store @imager
434         @sp.errors[:base].should_not be_blank
435       end
436     end
437   end
438   
439   describe '置換に於いて' do
440     before do
441       @sp = FactoryGirl.create :system_picture
442       @imager = ImagerTest.load "abc\ndef\nghi"
443     end
444     context '事前チェック' do
445       before do
446         #すべての処理が正常パターンで通過すれば、一番深い分岐まで通る。
447         #それで外部のメソッド呼び出しだけに着目してテストする。
448         SystemPicture.stub(:modify_object).with(any_args).and_return(@sp)
449         SystemPicture.any_instance.stub(:store).with(@imager).and_return(true)
450       end
451       it 'データ更新準備を依頼する' do
452         SystemPicture.should_receive(:modify_object).with(any_args).exactly(1)
453         r = SystemPicture.store @imager
454       end
455       it '作成依頼している' do
456         SystemPicture.any_instance.should_receive(:store).with(@imager).exactly(1)
457         r = SystemPicture.store @imager
458       end
459     end
460     context 'つつがなく終わるとき' do
461       before do
462         SystemPicture.any_instance.stub(:store).with(any_args).and_return(true)
463       end
464       it '自身に属性をセットしている' do
465         r = SystemPicture.store @imager
466         r.width.should eq @imager.width
467         r.height.should eq @imager.height
468         r.ext.should eq @imager.ext
469         r.filesize.should eq @imager.filesize
470         r.md5.should eq @imager.md5
471       end
472       it 'オブジェクトを返す' do
473         r = SystemPicture.store @imager
474         r.is_a?(SystemPicture).should eq true
475       end
476     end
477     context '保存失敗のとき' do
478       before do
479         SystemPicture.any_instance.stub(:store).with(any_args).and_return(false)
480       end
481       it 'nilを返す' do
482         r = SystemPicture.store @imager
483         r.should eq nil
484       end
485     end
486   end
487 end