OSDN Git Service

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