1 # -*- encoding: utf-8 -*-
4 describe SystemPicture do
6 SpeechBalloonTemplate.delete_all
7 SystemPicture.delete_all
8 @admin = FactoryGirl.create :admin
13 @sp = FactoryGirl.build :system_picture
16 context 'オーソドックスなデータのとき' do
18 @sp.ext = 'png' #リストにない拡張子は通らないし
29 @sp.filesize = 2000000
35 context 'extを検証するとき' do
38 @sp.should_not be_valid
42 @sp.should_not be_valid
44 it 'png,gif,jpeg以外なら失敗する' do
46 @sp.should_not be_valid
49 context 'widthを検証するとき' do
52 @sp.should_not be_valid
56 @sp.should_not be_valid
60 @sp.should_not be_valid
64 @sp.should_not be_valid
67 context 'heightを検証するとき' do
70 @sp.should_not be_valid
74 @sp.should_not be_valid
78 @sp.should_not be_valid
82 @sp.should_not be_valid
85 context 'filesizeを検証するとき' do
88 @sp.should_not be_valid
92 @sp.should_not be_valid
96 @sp.should_not be_valid
99 @sp.filesize = 2000000+1
100 @sp.should_not be_valid
103 context 'md5を検証するとき' do
106 @sp.should_not be_valid
110 @sp.should_not be_valid
114 @sp.should_not be_valid
119 describe 'デフォルト値補充に於いて' do
121 @sp = FactoryGirl.build :system_picture
126 describe '上書き補充に於いて' do
128 @sp = FactoryGirl.build :system_picture
133 describe '所持判定に於いて' do
135 @sp = FactoryGirl.build :system_picture
137 it '管理者のアクセス権はすべて等しいので、Trueを返す' do
138 @sp.own?(@admin).should == true
142 describe '閲覧許可に於いて' do
144 @sp = FactoryGirl.build :system_picture
147 r = @sp.visible?(@admin)
152 describe 'ファイル名に於いて' do
154 @sp = FactoryGirl.create :system_picture
156 it 'id+拡張子のフォーマットで返す' do
158 r.should eq "#{@sp.id}.png"
162 describe 'MimeTypeに於いて' do
164 @sp = FactoryGirl.create :system_picture
166 it 'image/拡張子のフォーマットで返す' do
168 r.should eq "image/png"
172 describe 'ファイルのurlに於いて' do
174 @sp = FactoryGirl.create :system_picture
175 SystemPicture.any_instance.stub(:filename).and_return('3.gif')
177 it 'ファイル名取得を依頼している' do
178 SystemPicture.any_instance.should_receive(:filename).exactly(1)
181 it '/original_pictures/3.gifのフォーマットで返す' do
183 r.should eq "/system_pictures/3.gif"
187 describe '一覧取得に於いて' do
189 @sp = FactoryGirl.create :system_picture
191 context 'page補正について' do
192 it '文字列から数値に変換される' do
193 SystemPicture.page('8').should eq 8
196 SystemPicture.page().should eq 1
199 SystemPicture.page('0').should eq 1
202 context 'page_size補正について' do
203 it '文字列から数値に変換される' do
204 SystemPicture.page_size('7').should eq 7
206 it 'nilの場合はSystemPicture.default_page_sizeになる' do
207 SystemPicture.page_size().should eq SystemPicture.default_page_size
209 it '0以下の場合はSystemPicture.default_page_sizeになる' do
210 SystemPicture.page_size('0').should eq SystemPicture.default_page_size
212 it 'SystemPicture.max_page_sizeを超えた場合はSystemPicture.max_page_sizeになる' do
213 SystemPicture.page_size('1000').should eq SystemPicture.max_page_size
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
224 r = SystemPicture.list
228 ni = FactoryGirl.create :system_picture, :updated_at => Time.now + 100
229 r = SystemPicture.list
230 r.should eq [ni, @sp]
232 context 'DBに5件あって1ページの件数を2件に変えたとして' 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)
241 r = SystemPicture.list
242 r.should have(2).items
244 it 'page=1なら末尾2件を返す' do
246 r = SystemPicture.list 1, 2
247 r.should eq [@sp5, @sp4]
249 it 'page=2なら中間2件を返す' do
250 r = SystemPicture.list 2, 2
251 r.should eq [@sp3, @sp2]
253 it 'page=3なら先頭1件を返す' do
254 r = SystemPicture.list 3, 2
258 context 'DBに5件あって1ページの件数を0件に変えたとして' 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)
266 it '件数0は全件(5件)を返す' do
267 r = SystemPicture.list 5, 0
268 r.should have(5).items
272 describe '一覧出力オプションに於いて' do
274 r = SystemPicture.list_opt
275 r.is_a?(Hash).should be_true
278 r = SystemPicture.list_opt
282 describe 'json一覧出力オプションに於いて' do
284 r = SystemPicture.list_json_opt
285 r.is_a?(Hash).should be_true
288 r = SystemPicture.list_json_opt
293 describe '単体取得に於いて' do
295 @sp = FactoryGirl.create :system_picture
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
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
310 r = SystemPicture.show @sp.id, @admin
313 context '他人のシステム画像を開こうとしたとき' do
314 it '403Forbidden例外を返す' do
315 SystemPicture.any_instance.stub(:visible?).and_return(false)
317 r = SystemPicture.show @sp.id, @admin
318 }.should raise_error(ActiveRecord::Forbidden)
321 context '存在しないシステム画像を開こうとしたとき' do
322 it '404RecordNotFound例外を返す' do
324 r = SystemPicture.show 0, @admin
325 }.should raise_error(ActiveRecord::RecordNotFound)
329 describe '単体出力オプションに於いて' do
331 r = SystemPicture.show_opt
332 r.is_a?(Hash).should be_true
335 r = SystemPicture.show_opt
339 describe 'json単体出力オプションに於いて' do
341 r = SystemPicture.show_json_opt
342 r.is_a?(Hash).should be_true
345 r = SystemPicture.show_json_opt
350 describe '作成・更新に於いて' do
352 @sp = FactoryGirl.build :system_picture
353 @imager = ImagerTest.load "abc\ndef\nghi"
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)
363 SystemPicture.any_instance.should_receive(:save).exactly(1)
364 r = @sp.store @imager
366 it 'PictureIoに画像データの保存を依頼している' do
367 PictureIO.system_picture_io.should_receive(:put).with(any_args).exactly(1)
368 r = @sp.store @imager
371 context 'つつがなく終わるとき' do
373 #すべての処理を正常パターンで通過させ、保存機能をチェックする。
374 PictureIO.system_picture_io.stub(:put).with(any_args).and_return(true)
376 it 'システム画像モデルが作成されている' do
378 r = @sp.store @imager
379 }.should change SystemPicture, :count
382 r = @sp.store @imager
386 #以下から例外ケース。処理先頭から失敗させていく
387 context 'imagerが初期化に失敗したとき' do
391 @sp.store(false).should be_false
393 it '自身の保存は呼ばれていない' do
394 SystemPicture.any_instance.should_not_receive(:save)
397 it '全体エラーメッセージがセットされている' do
400 }.should change(@sp.errors[:base], :count)
403 context '自身の保存に失敗したとき' do
405 SystemPicture.any_instance.stub(:save).with(any_args).and_return(false)
408 r = @sp.store @imager
412 r = @sp.store @imager
413 @sp.should be_a_new SystemPicture
415 it '原画の保存は呼ばれていない' do
416 PictureIO.system_picture_io.should_not_receive(:put)
417 r = @sp.store @imager
420 context '画像データの保存に失敗したとき' do
422 PictureIO.system_picture_io.stub(:put).with(any_args).and_raise(PictureIO::Error)
425 r = @sp.store @imager
429 r = @sp.store @imager
430 @sp.should be_a_new SystemPicture
432 it '全体エラーメッセージがセットされている' do
433 r = @sp.store @imager
434 @sp.errors[:base].should_not be_blank
441 @sp = FactoryGirl.create :system_picture
442 @imager = ImagerTest.load "abc\ndef\nghi"
446 #すべての処理が正常パターンで通過すれば、一番深い分岐まで通る。
447 #それで外部のメソッド呼び出しだけに着目してテストする。
448 SystemPicture.stub(:modify_object).with(any_args).and_return(@sp)
449 SystemPicture.any_instance.stub(:store).with(@imager).and_return(true)
452 SystemPicture.should_receive(:modify_object).with(any_args).exactly(1)
453 r = SystemPicture.store @imager
456 SystemPicture.any_instance.should_receive(:store).with(@imager).exactly(1)
457 r = SystemPicture.store @imager
460 context 'つつがなく終わるとき' do
462 SystemPicture.any_instance.stub(:store).with(any_args).and_return(true)
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
473 r = SystemPicture.store @imager
474 r.is_a?(SystemPicture).should eq true
479 SystemPicture.any_instance.stub(:store).with(any_args).and_return(false)
482 r = SystemPicture.store @imager