OSDN Git Service

t#29400:itr3?
[pettanr/pettanr.git] / spec / models / system_picture_spec.rb
index ead2846..41523ba 100644 (file)
@@ -8,21 +8,29 @@ describe SystemPicture do
   
   describe '検証に於いて' do
     before do
-    end
-    
-    it 'オーソドックスなデータなら通る' do
       @sp = FactoryGirl.build :system_picture
-      @sp.should be_valid
     end
     
-    context 'extを検証するとき' do
-      before do
-        @sp = FactoryGirl.build :system_picture
+    context 'オーソドックスなデータのとき' do
+      it '下限データが通る' do
+        @sp.ext = 'png' #リストにない拡張子は通らないし
+        @sp.width = 1
+        @sp.height = 1
+        @sp.filesize = 1
+        @sp.md5 = 'a'*32
+        @sp.should be_valid
       end
-      it 'テストデータの確認' do
+      it '上限データが通る' do
         @sp.ext = 'jpeg'
+        @sp.width = 99999
+        @sp.height = 99999
+        @sp.filesize = 2000000
+        @sp.md5 = 'a'*32
         @sp.should be_valid
       end
+    end
+    
+    context 'extを検証するとき' do
       it 'nullなら失敗する' do
         @sp.ext = ''
         @sp.should_not be_valid
@@ -37,13 +45,6 @@ describe SystemPicture do
       end
     end
     context 'widthを検証するとき' do
-      before do
-        @sp = FactoryGirl.build :system_picture
-      end
-      it 'テストデータの確認' do
-        @sp.width = 1
-        @sp.should be_valid
-      end
       it 'nullなら失敗する' do
         @sp.width = nil
         @sp.should_not be_valid
@@ -62,13 +63,6 @@ describe SystemPicture do
       end
     end
     context 'heightを検証するとき' do
-      before do
-        @sp = FactoryGirl.build :system_picture
-      end
-      it 'テストデータの確認' do
-        @sp.height = 1
-        @sp.should be_valid
-      end
       it 'nullなら失敗する' do
         @sp.height = nil
         @sp.should_not be_valid
@@ -87,13 +81,6 @@ describe SystemPicture do
       end
     end
     context 'filesizeを検証するとき' do
-      before do
-        @sp = FactoryGirl.build :system_picture
-      end
-      it 'テストデータの確認' do
-        @sp.filesize = 1
-        @sp.should be_valid
-      end
       it 'nullなら失敗する' do
         @sp.filesize = nil
         @sp.should_not be_valid
@@ -112,119 +99,320 @@ describe SystemPicture do
       end
     end
     context 'md5を検証するとき' do
-      before do
-        @sp = FactoryGirl.build :system_picture
-      end
-      it 'テストデータの確認' do
-        @sp.md5 = 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'
-        @sp.should be_valid
-      end
       it 'nullなら失敗する' do
         @sp.md5 = ''
         @sp.should_not be_valid
       end
-      it '32文字以上なら失敗する' do
+      it '31文字なら失敗する' do
+        @sp.md5 = 'a'*31
+        @sp.should_not be_valid
+      end
+      it '33文字なら失敗する' do
         @sp.md5 = 'a'*33
         @sp.should_not be_valid
       end
     end
   end
   
-  describe 'データ補充に於いて' do
+  describe 'デフォルト値補充に於いて' do
+    it 'defined' do
+      @sp = FactoryGirl.build :system_picture
+      @sp.supply_default
+    end
+  end
+  
+  describe '上書き補充に於いて' do
+    it 'defined' do
+      @sp = FactoryGirl.build :system_picture
+      @sp.overwrite
+    end
+  end
+  
+  describe '所持判定に於いて' do
     before do
+      @sp = FactoryGirl.build :system_picture
+    end
+    it '管理者のアクセス権はすべて等しいので、Trueを返す' do
+      @sp.own?(@admin).should == true
+    end
+  end
+  
+  describe '閲覧許可に於いて' do
+    before do
+      @sp = FactoryGirl.build :system_picture
+    end
+    it '必ず許可となる' do
+      r = @sp.visible?(@admin)
+      r.should == true
+    end
+  end
+  
+  describe 'ファイル名に於いて' do
+    before do
+      @sp = FactoryGirl.create :system_picture
+    end
+    it 'id+拡張子のフォーマットで返す' do
+      r = @sp.filename
+      r.should eq "#{@sp.id}.png"
+    end
+  end
+  
+  describe 'MimeTypeに於いて' do
+    before do
+      @sp = FactoryGirl.create :system_picture
+    end
+    it 'image/拡張子のフォーマットで返す' do
+      r = @sp.mime_type
+      r.should eq "image/png"
+    end
+  end
+  
+  describe 'ファイルのurlに於いて' do
+    before do
+      @sp = FactoryGirl.create :system_picture
+      SystemPicture.any_instance.stub(:filename).and_return('3.gif')
+    end
+    it 'ファイル名取得を依頼している' do
+      SystemPicture.any_instance.should_receive(:filename).exactly(1)
+      @sp.url
+    end
+    it '/original_pictures/3.gifのフォーマットで返す' do
+      r = @sp.url
+      r.should eq "/system_pictures/3.gif"
+    end
+  end
+  
+  describe '一覧取得に於いて' do
+    before do
+      @sp = FactoryGirl.create :system_picture
+    end
+    context 'page補正について' do
+      it '文字列から数値に変換される' do
+        SystemPicture.page('8').should eq 8
+      end
+      it 'nilの場合は1になる' do
+        SystemPicture.page().should eq 1
+      end
+      it '0以下の場合は1になる' do
+        SystemPicture.page('0').should eq 1
+      end
+    end
+    context 'page_size補正について' do
+      it '文字列から数値に変換される' do
+        SystemPicture.page_size('7').should eq 7
+      end
+      it 'nilの場合はSystemPicture.default_page_sizeになる' do
+        SystemPicture.page_size().should eq SystemPicture.default_page_size
+      end
+      it '0以下の場合はSystemPicture.default_page_sizeになる' do
+        SystemPicture.page_size('0').should eq SystemPicture.default_page_size
+      end
+      it 'SystemPicture.max_page_sizeを超えた場合はSystemPicture.max_page_sizeになる' do
+        SystemPicture.page_size('1000').should eq SystemPicture.max_page_size
+      end
+    end
+    context 'つつがなく終わるとき' do
+      it '一覧取得オプションを利用している' do
+        SystemPicture.stub(:list_opt).with(any_args).and_return({})
+        SystemPicture.should_receive(:list_opt).with(any_args).exactly(1)
+        r = SystemPicture.list
+      end
+    end
+    it 'リストを返す' do
+      r = SystemPicture.list
+      r.should eq [@sp]
+    end
+    it '時系列で並んでいる' do
+      ni = FactoryGirl.create :system_picture, :updated_at => Time.now + 100
+      r = SystemPicture.list
+      r.should eq [ni, @sp]
+    end
+    context 'DBに5件あって1ページの件数を2件に変えたとして' do
+      before do
+        @sp2 = FactoryGirl.create :system_picture, :updated_at => Time.now + 100
+        @sp3 = FactoryGirl.create :system_picture, :updated_at => Time.now + 200
+        @sp4 = FactoryGirl.create :system_picture, :updated_at => Time.now + 300
+        @sp5 = FactoryGirl.create :system_picture, :updated_at => Time.now + 400
+        SystemPicture.stub(:default_page_size).and_return(2)\r
+      end
+      it '通常は2件を返す' do
+        r = SystemPicture.list
+        r.should have(2).items 
+      end
+      it 'page=1なら末尾2件を返す' do
+        #時系列で並んでいる
+        r = SystemPicture.list 1, 2
+        r.should eq [@sp5, @sp4]
+      end
+      it 'page=2なら中間2件を返す' do
+        r = SystemPicture.list 2, 2
+        r.should eq [@sp3, @sp2]
+      end
+      it 'page=3なら先頭1件を返す' do
+        r = SystemPicture.list 3, 2
+        r.should eq [@sp]
+      end
+    end
+    context 'DBに5件あって1ページの件数を0件に変えたとして' do
+      before do
+        @sp2 = FactoryGirl.create :system_picture, :updated_at => Time.now + 100
+        @sp3 = FactoryGirl.create :system_picture, :updated_at => Time.now + 200
+        @sp4 = FactoryGirl.create :system_picture, :updated_at => Time.now + 300
+        @sp5 = FactoryGirl.create :system_picture, :updated_at => Time.now + 400
+        SystemPicture.stub(:default_page_size).and_return(2)\r
+      end
+      it '件数0は全件(5件)を返す' do
+        r = SystemPicture.list 5, 0
+        r.should have(5).items 
+      end
+    end
+  end
+  describe '一覧出力オプションに於いて' do
+    it 'Hashを返す' do
+      r = SystemPicture.list_opt
+      r.is_a?(Hash).should be_true
+    end
+    it '0の項目を含んでいる' do
+      r = SystemPicture.list_opt
+      r.should be_empty
+    end
+  end
+  describe 'json一覧出力オプションに於いて' do
+    it 'Hashを返す' do
+      r = SystemPicture.list_json_opt
+      r.is_a?(Hash).should be_true
+    end
+    it '0の項目を含んでいる' do
+      r = SystemPicture.list_json_opt
+      r.should be_empty
+    end
+  end
+  
+  describe '単体取得に於いて' do
+    before do
+      @sp = FactoryGirl.create :system_picture
+    end
+    context 'つつがなく終わるとき' do
+      it '単体取得オプションを利用している' do
+        SystemPicture.stub(:show_opt).with(any_args).and_return({})
+        SystemPicture.should_receive(:show_opt).with(any_args).exactly(1)
+        r = SystemPicture.show @sp.id, @admin
+      end
+      it '閲覧許可を問い合わせている' do
+        SystemPicture.any_instance.stub(:visible?).with(any_args).and_return(true)
+        SystemPicture.any_instance.should_receive(:visible?).with(any_args).exactly(1)
+        r = SystemPicture.show @sp.id, @admin
+      end
+    end
+    it '指定のシステム画像を返す' do
+      r = SystemPicture.show @sp.id, @admin
+      r.should eq @sp
+    end
+    context '他人のシステム画像を開こうとしたとき' do
+      it '403Forbidden例外を返す' do
+        SystemPicture.any_instance.stub(:visible?).and_return(false)
+        lambda{
+          r = SystemPicture.show @sp.id, @admin
+        }.should raise_error(ActiveRecord::Forbidden)
+      end
+    end
+    context '存在しないシステム画像を開こうとしたとき' do
+      it '404RecordNotFound例外を返す' do
+        lambda{
+          r = SystemPicture.show 0, @admin
+        }.should raise_error(ActiveRecord::RecordNotFound)
+      end
+    end
+  end
+  describe '単体出力オプションに於いて' do
+    it 'Hashを返す' do
+      r = SystemPicture.show_opt
+      r.is_a?(Hash).should be_true
+    end
+    it '0の項目を含んでいる' do
+      r = SystemPicture.show_opt
+      r.should be_empty
+    end
+  end
+  describe 'json単体出力オプションに於いて' do
+    it 'Hashを返す' do
+      r = SystemPicture.show_json_opt
+      r.is_a?(Hash).should be_true
+    end
+    it '0の項目を含んでいる' do
+      r = SystemPicture.show_json_opt
+      r.should be_empty
     end
-    
   end
   
   describe '作成・更新に於いて' do
     before do
       @sp = FactoryGirl.build :system_picture
-      #RMagickのスタブをおいておく
-      class Mgk ; class Image ; end ; end
-      @filesize = 76543
-      Mgk::Image.stub(:from_blob).with(any_args).and_return([Mgk.new])
-      Mgk.any_instance.stub(:format).with(any_args).and_return('png')
-      Mgk.any_instance.stub(:rows).with(any_args).and_return(200)
-      Mgk.any_instance.stub(:columns).with(any_args).and_return(100)
-      Mgk.any_instance.stub(:filesize).with(any_args).and_return(@filesize)
-      Mgk.any_instance.stub(:to_blob).with(any_args).and_return('data')
+      @imager = ImagerTest.load "abc\ndef\nghi"
     end
     context '事前チェック' do
       before do
         #すべての処理が正常パターンで通過すれば、一番深い分岐まで通る。
         #それで外部のメソッド呼び出しだけに着目してテストする。
-        SystemPicture.any_instance.stub(:data_to_mgk).with(any_args).and_return(Mgk.new)
         SystemPicture.any_instance.stub(:save).with(any_args).and_return(true)
-        PictureIO::LocalPicture.any_instance.stub(:put).with(any_args).and_return(true)
-      end
-      it 'RMagick変換を依頼している' do
-        SystemPicture.any_instance.should_receive(:data_to_mgk).exactly(1)
-        @sp.store 'bindata'
-      end
-      it '自身に属性をセットしている' do
-        SystemPicture.any_instance.should_receive(:attributes=).exactly(1)
-        @sp.store 'bindata'
+        PictureIO.system_picture_io.stub(:put).with(any_args).and_return(true)
       end
       it '自身が保存されている' do
         SystemPicture.any_instance.should_receive(:save).exactly(1)
-        @sp.store 'bindata'
+        r = @sp.store @imager
       end
       it 'PictureIoに画像データの保存を依頼している' do
-        PictureIO::LocalPicture.any_instance.should_receive(:put).with(any_args).exactly(1)
-        @sp.store 'bindata'
+        PictureIO.system_picture_io.should_receive(:put).with(any_args).exactly(1)
+        r = @sp.store @imager
       end
     end
     context 'つつがなく終わるとき' do
       before do
         #すべての処理を正常パターンで通過させ、保存機能をチェックする。
-        SystemPicture.any_instance.stub(:data_to_mgk).with(any_args).and_return(Mgk.new)
-#        SystemPicture.any_instance.stub(:save).with(any_args).and_return(true)
-        PictureIO::LocalPicture.any_instance.stub(:put).with(any_args).and_return(true)
-      end
-      it '自身に属性をセットしている' do
-        lambda {
-          @sp.store 'bindata'
-        }.should change @sp, :filesize
+        PictureIO.system_picture_io.stub(:put).with(any_args).and_return(true)
       end
       it 'システム画像モデルが作成されている' do
         lambda {
-          @sp.store 'bindata'
+          r = @sp.store @imager
         }.should change SystemPicture, :count
       end
-      it 'システム画像が保存されている' do
-        @sp.store 'bindata'
-        SystemPicture.find(@sp).should_not be_nil
-      end
       it 'Trueを返す' do
-        @sp.store('bindata').should eq true
+        r = @sp.store @imager
+        r.should eq true
       end
     end
     #以下から例外ケース。処理先頭から失敗させていく
-    context 'RMagick変換が失敗したとき' do
+    context 'imagerが初期化に失敗したとき' do
       before do
-        SystemPicture.any_instance.stub(:data_to_mgk).with(any_args).and_return(false)
       end
       it 'falseを返す' do
-        @sp.store('bindata').should be_false
+        @sp.store(false).should be_false
       end
       it '自身の保存は呼ばれていない' do
         SystemPicture.any_instance.should_not_receive(:save)
+        @sp.store(false)
+      end
+      it '全体エラーメッセージがセットされている' do
+        lambda {
+          @sp.store(false)
+        }.should change(@sp.errors[:base], :count)
       end
     end
     context '自身の保存に失敗したとき' do
       before do
-        SystemPicture.any_instance.stub(:data_to_mgk).with(any_args).and_return(Mgk.new)
         SystemPicture.any_instance.stub(:save).with(any_args).and_return(false)
       end
       it 'falseを返す' do
-        @sp.store('bindata').should be_false
+        r = @sp.store @imager
+        r.should be_false
       end
       it '更新されていない' do
-        @sp.store('bindata')
+        r = @sp.store @imager
         @sp.should be_a_new SystemPicture
       end
       it '原画の保存は呼ばれていない' do
-        PictureIO::LocalPicture.any_instance.should_not_receive(:put)
+        PictureIO.system_picture_io.should_not_receive(:put)
+        r = @sp.store @imager
       end
     end
   end
@@ -232,50 +420,47 @@ describe SystemPicture do
   describe '置換に於いて' do
     before do
       @sp = FactoryGirl.create :system_picture
+      @imager = ImagerTest.load "abc\ndef\nghi"
     end
-    context '新規のとき' do
+    context '事前チェック' do
       before do
-        SystemPicture.stub(:find_by_md5).with(any_args).and_return(nil)
-        SystemPicture.any_instance.stub(:store).with(any_args).and_return(true)
+        #すべての処理が正常パターンで通過すれば、一番深い分岐まで通る。
+        #それで外部のメソッド呼び出しだけに着目してテストする。
+        SystemPicture.stub(:modify_object).with(any_args).and_return(@sp)
+        SystemPicture.any_instance.stub(:store).with(@imager).and_return(true)
       end
-      it '新規オブジェクト生成している' do
-        r = SystemPicture.store 'bindata'
-        r.should be_a_new SystemPicture
+      it 'データ更新準備を依頼する' do
+        SystemPicture.should_receive(:modify_object).with(any_args).exactly(1)
+        r = SystemPicture.store @imager
       end
       it '作成依頼している' do
-        SystemPicture.any_instance.should_receive(:store).with('bindata').exactly(1)
-        SystemPicture.store 'bindata'
-      end
-      it '保存された行を返す' do
-        r = SystemPicture.store 'bindata'
-        r.should_not eq @sp
+        SystemPicture.any_instance.should_receive(:store).with(@imager).exactly(1)
+        r = SystemPicture.store @imager
       end
     end
-    context '既存のとき' do
+    context 'つつがなく終わるとき' do
       before do
-        SystemPicture.stub(:find_by_md5).with(any_args).and_return(@sp)
         SystemPicture.any_instance.stub(:store).with(any_args).and_return(true)
       end
-      it '新規オブジェクト生成していない' do
-        SystemPicture.should_receive(:new).with(any_args).exactly(0)
-        SystemPicture.store 'bindata'
-      end
-      it '作成依頼している' do
-        SystemPicture.any_instance.should_receive(:store).with('bindata').exactly(1)
-        SystemPicture.store 'bindata'
+      it '自身に属性をセットしている' do
+        r = SystemPicture.store @imager
+        r.width.should eq @imager.width
+        r.height.should eq @imager.height
+        r.ext.should eq @imager.ext
+        r.filesize.should eq @imager.filesize
+        r.md5.should eq @imager.md5
       end
-      it '保存された行を返す' do
-        r = SystemPicture.store 'bindata'
-        r.should eq @sp
+      it 'オブジェクトを返す' do
+        r = SystemPicture.store @imager
+        r.is_a?(SystemPicture).should eq true
       end
     end
     context '保存失敗のとき' do
       before do
-        SystemPicture.stub(:find_by_md5).with(any_args).and_return(@sp)
         SystemPicture.any_instance.stub(:store).with(any_args).and_return(false)
       end
       it 'nilを返す' do
-        r = SystemPicture.store 'bindata'
+        r = SystemPicture.store @imager
         r.should eq nil
       end
     end