1 # -*- encoding: utf-8 -*-
4 describe SystemPicture do
6 @admin = FactoryGirl.create :admin
11 @sp = FactoryGirl.build :system_picture
14 context 'オーソドックスなデータのとき' do
16 @sp.ext = 'png' #リストにない拡張子は通らないし
27 @sp.filesize = 2000000
33 context 'extを検証するとき' do
36 @sp.should_not be_valid
40 @sp.should_not be_valid
42 it 'png,gif,jpeg以外なら失敗する' do
44 @sp.should_not be_valid
47 context 'widthを検証するとき' do
50 @sp.should_not be_valid
54 @sp.should_not be_valid
58 @sp.should_not be_valid
62 @sp.should_not be_valid
65 context 'heightを検証するとき' do
68 @sp.should_not be_valid
72 @sp.should_not be_valid
76 @sp.should_not be_valid
80 @sp.should_not be_valid
83 context 'filesizeを検証するとき' do
86 @sp.should_not be_valid
90 @sp.should_not be_valid
94 @sp.should_not be_valid
97 @sp.filesize = 2000000+1
98 @sp.should_not be_valid
101 context 'md5を検証するとき' do
104 @sp.should_not be_valid
108 @sp.should_not be_valid
112 @sp.should_not be_valid
117 describe 'デフォルト値補充に於いて' do
119 @sp = FactoryGirl.build :system_picture
124 describe '上書き補充に於いて' do
126 @sp = FactoryGirl.build :system_picture
131 describe '所持判定に於いて' do
133 @sp = FactoryGirl.build :system_picture
135 it '管理者のアクセス権はすべて等しいので、Trueを返す' do
136 @sp.own?(@admin).should == true
140 describe '閲覧許可に於いて' do
142 @sp = FactoryGirl.build :system_picture
145 r = @sp.visible?(@admin)
150 describe 'ファイル名に於いて' do
152 @sp = FactoryGirl.create :system_picture
154 it 'id+拡張子のフォーマットで返す' do
156 r.should eq "#{@sp.id}.png"
160 describe 'MimeTypeに於いて' do
162 @sp = FactoryGirl.create :system_picture
164 it 'image/拡張子のフォーマットで返す' do
166 r.should eq "image/png"
170 describe 'ファイルのurlに於いて' do
172 @sp = FactoryGirl.create :system_picture
173 SystemPicture.any_instance.stub(:filename).and_return('3.gif')
175 it 'ファイル名取得を依頼している' do
176 SystemPicture.any_instance.should_receive(:filename).exactly(1)
179 it '/original_pictures/3.gifのフォーマットで返す' do
181 r.should eq "/system_pictures/3.gif"
185 describe '一覧取得に於いて' do
187 @sp = FactoryGirl.create :system_picture
189 context 'page補正について' do
190 it '文字列から数値に変換される' do
191 SystemPicture.page('8').should eq 8
194 SystemPicture.page().should eq 1
197 SystemPicture.page('0').should eq 1
200 context 'page_size補正について' do
201 it '文字列から数値に変換される' do
202 SystemPicture.page_size('7').should eq 7
204 it 'nilの場合はSystemPicture.default_page_sizeになる' do
205 SystemPicture.page_size().should eq SystemPicture.default_page_size
207 it '0以下の場合はSystemPicture.default_page_sizeになる' do
208 SystemPicture.page_size('0').should eq SystemPicture.default_page_size
210 it 'SystemPicture.max_page_sizeを超えた場合はSystemPicture.max_page_sizeになる' do
211 SystemPicture.page_size('1000').should eq SystemPicture.max_page_size
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
222 r = SystemPicture.list
226 ni = FactoryGirl.create :system_picture, :updated_at => Time.now + 100
227 r = SystemPicture.list
228 r.should eq [ni, @sp]
230 context 'DBに5件あって1ページの件数を2件に変えたとして' 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)
239 r = SystemPicture.list
240 r.should have(2).items
242 it 'page=1なら末尾2件を返す' do
244 r = SystemPicture.list 1, 2
245 r.should eq [@sp5, @sp4]
247 it 'page=2なら中間2件を返す' do
248 r = SystemPicture.list 2, 2
249 r.should eq [@sp3, @sp2]
251 it 'page=3なら先頭1件を返す' do
252 r = SystemPicture.list 3, 2
256 context 'DBに5件あって1ページの件数を0件に変えたとして' 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)
264 it '件数0は全件(5件)を返す' do
265 r = SystemPicture.list 5, 0
266 r.should have(5).items
270 describe '一覧出力オプションに於いて' do
272 r = SystemPicture.list_opt
273 r.is_a?(Hash).should be_true
276 r = SystemPicture.list_opt
280 describe 'json一覧出力オプションに於いて' do
282 r = SystemPicture.list_json_opt
283 r.is_a?(Hash).should be_true
286 r = SystemPicture.list_json_opt
291 describe '単体取得に於いて' do
293 @sp = FactoryGirl.create :system_picture
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
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
308 r = SystemPicture.show @sp.id, @admin
311 context '他人のシステム画像を開こうとしたとき' do
312 it '403Forbidden例外を返す' do
313 SystemPicture.any_instance.stub(:visible?).and_return(false)
315 r = SystemPicture.show @sp.id, @admin
316 }.should raise_error(ActiveRecord::Forbidden)
319 context '存在しないシステム画像を開こうとしたとき' do
320 it '404RecordNotFound例外を返す' do
322 r = SystemPicture.show 0, @admin
323 }.should raise_error(ActiveRecord::RecordNotFound)
327 describe '単体出力オプションに於いて' do
329 r = SystemPicture.show_opt
330 r.is_a?(Hash).should be_true
333 r = SystemPicture.show_opt
337 describe 'json単体出力オプションに於いて' do
339 r = SystemPicture.show_json_opt
340 r.is_a?(Hash).should be_true
343 r = SystemPicture.show_json_opt
348 describe '作成・更新に於いて' do
350 @sp = FactoryGirl.build :system_picture
351 @imager = ImagerTest.load "abc\ndef\nghi"
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)
361 SystemPicture.any_instance.should_receive(:save).exactly(1)
362 r = @sp.store @imager
364 it 'PictureIoに画像データの保存を依頼している' do
365 PictureIO.system_picture_io.should_receive(:put).with(any_args).exactly(1)
366 r = @sp.store @imager
369 context 'つつがなく終わるとき' do
371 #すべての処理を正常パターンで通過させ、保存機能をチェックする。
372 PictureIO.system_picture_io.stub(:put).with(any_args).and_return(true)
374 it 'システム画像モデルが作成されている' do
376 r = @sp.store @imager
377 }.should change SystemPicture, :count
380 r = @sp.store @imager
384 #以下から例外ケース。処理先頭から失敗させていく
385 context 'imagerが初期化に失敗したとき' do
389 @sp.store(false).should be_false
391 it '自身の保存は呼ばれていない' do
392 SystemPicture.any_instance.should_not_receive(:save)
395 it '全体エラーメッセージがセットされている' do
398 }.should change(@sp.errors[:base], :count)
401 context '自身の保存に失敗したとき' do
403 SystemPicture.any_instance.stub(:save).with(any_args).and_return(false)
406 r = @sp.store @imager
410 r = @sp.store @imager
411 @sp.should be_a_new SystemPicture
413 it '原画の保存は呼ばれていない' do
414 PictureIO.system_picture_io.should_not_receive(:put)
415 r = @sp.store @imager
418 context '画像データの保存に失敗したとき' do
420 PictureIO.system_picture_io.stub(:put).with(any_args).and_raise(PictureIO::Error)
423 r = @sp.store @imager
427 r = @sp.store @imager
428 @sp.should be_a_new SystemPicture
430 it '全体エラーメッセージがセットされている' do
431 r = @sp.store @imager
432 @sp.errors[:base].should_not be_blank
439 @sp = FactoryGirl.create :system_picture
440 @imager = ImagerTest.load "abc\ndef\nghi"
444 #すべての処理が正常パターンで通過すれば、一番深い分岐まで通る。
445 #それで外部のメソッド呼び出しだけに着目してテストする。
446 SystemPicture.stub(:modify_object).with(any_args).and_return(@sp)
447 SystemPicture.any_instance.stub(:store).with(@imager).and_return(true)
450 SystemPicture.should_receive(:modify_object).with(any_args).exactly(1)
451 r = SystemPicture.store @imager
454 SystemPicture.any_instance.should_receive(:store).with(@imager).exactly(1)
455 r = SystemPicture.store @imager
458 context 'つつがなく終わるとき' do
460 SystemPicture.any_instance.stub(:store).with(any_args).and_return(true)
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
471 r = SystemPicture.store @imager
472 r.is_a?(SystemPicture).should eq true
477 SystemPicture.any_instance.stub(:store).with(any_args).and_return(false)
480 r = SystemPicture.store @imager