OSDN Git Service

t#31470:create pager
[pettanr/pettanr.git] / spec / models / artist_spec.rb
index 3ea2d45..e93b28f 100644 (file)
@@ -49,6 +49,22 @@ describe Artist do
     end
   end
   
+  describe '文字コード検証に於いて' do
+    before do
+      @artist = FactoryGirl.build :artist, :author_id => @author.id
+    end
+    
+    context 'nameを検証するとき' do
+      it 'Shift JISなら失敗する' do
+        @artist.name = "\x83G\x83r\x83]\x83D"
+        lambda{
+          @artist.valid_encode
+        }.should raise_error(Pettanr::BadRequest)
+      end
+    end
+    
+  end
+  
   describe 'デフォルト値補充に於いて' do
     it '名前がno nameになっている' do
       @artist = FactoryGirl.build :artist, :name => nil
@@ -70,14 +86,33 @@ describe Artist do
       @artist = FactoryGirl.create :artist, :author_id => @author.id
       @other_artist = FactoryGirl.create :artist_yas, :author_id => @other_author.id
     end
-    it '自分の絵師ならyes' do
-      @artist.own?(@author).should == true
+    context '事前チェックする' do
+      it '自身にロールリストからの作家取得を依頼している' do
+        Artist.should_receive(:get_author_from_roles).with(any_args).exactly(1)
+        r = @artist.own?([@author])
+      end
     end
-    it '他人の絵師ならno' do
-      @artist.own?(@other_author).should == false
+    context 'ロール内作家が取得できるとき' do
+      before do
+      end
+      it 'ロール内作家のidが自身の作家idと一致するなら許可する' do
+        Artist.stub(:get_author_from_roles).with(any_args).and_return(@author)
+        r = @artist.own?([@author])
+        r.should be_true
+      end
+      it 'ロール内作家のidが自身の作家idと一致しないならno' do
+        Artist.stub(:get_author_from_roles).with(any_args).and_return(@other_author)
+        @artist.own?(@other_author).should be_false
+      end
     end
-    it 'パラメータが作家でないならならno' do
-      @artist.own?(nil).should == false
+    context 'ロール内作家が取得できないとき' do
+      before do
+        Artist.stub(:get_author_from_roles).with(any_args).and_return(nil)
+      end
+      it 'Falseを返す' do
+        r = @artist.own?([@author])
+        r.should be_false
+      end
     end
   end
   
@@ -85,38 +120,46 @@ describe Artist do
     before do
       @artist = FactoryGirl.create :artist, :author_id => @author.id
     end
-    context '検査対象がnil(ゲスト)のとき' do
-      context 'クローズドモードのとき' do
-        before do
-          MagicNumber['run_mode'] = 1
-        end
-        it '不許可を返す。' do
-          r = @artist.visible?(nil)
-          r.should be_false
-        end
-      end
-      context 'オープンモードのとき' do
-        before do
-          MagicNumber['run_mode'] = 0
-        end
-        it '許可する' do
-          r = @artist.visible?(nil)
-          r.should be_true
-        end
-      end
-    end
-    context '検査対象が作家のとき' do
-      it '許可する' do
-        r = @artist.visible?(@author)
-        r.should == true
+    context 'オープンモードのとき' do
+      before do
+        MagicNumber['run_mode'] = 0
+      end
+      it '自身にゲスト用ロールチェックを問い合わせしている' do
+        Artist.any_instance.stub(:guest_role_check).and_return(true)
+        Artist.any_instance.should_receive(:guest_role_check).with(any_args).exactly(1)
+        r = @artist.visible?([@author])
+      end
+      it 'ゲスト用ロールチェックが失敗したとき、falseを返す' do
+        Artist.any_instance.stub(:guest_role_check).and_return(false)
+        r = @artist.visible?([@author])
+        r.should be_false
       end
     end
-    context '検査対象がそれ以外のとき' do
-      it '不許可を返す。' do
-        r = @artist.visible?(@admin)
+    context 'クローズドモードのとき' do
+      before do
+        MagicNumber['run_mode'] = 1
+      end
+      it '自身に素材読者用ロールチェックを問い合わせしている' do
+        Artist.any_instance.stub(:resource_reader_role_check).and_return(true)
+        Artist.any_instance.should_receive(:resource_reader_role_check).with(any_args).exactly(1)
+        r = @artist.visible?([@author])
+      end
+      it '素材読者用ロールチェックが失敗したとき、falseを返す' do
+        Artist.any_instance.stub(:resource_reader_role_check).and_return(false)
+        r = @artist.visible?([@author])
         r.should be_false
       end
     end
+    context 'つつがなく終わるとき' do
+      before do
+        MagicNumber['run_mode'] = 1
+        Artist.any_instance.stub(:resource_reader_role_check).and_return(true)
+      end
+      it '許可する' do
+        r = @artist.visible?([@author])
+        r.should be_true
+      end
+    end
   end
   
   describe '一覧取得に於いて' do
@@ -195,31 +238,35 @@ describe Artist do
         r.should eq [@artist]
       end
     end
-    context 'DBに5件あって1ページの件数を0件に変えたとして' do
-      before do
-        @artist2 = FactoryGirl.create :artist, :author_id => @author.id, :name => 'artist2', :created_at => Time.now + 100
-        @artist3 = FactoryGirl.create :artist, :author_id => @author.id, :name => 'artist3', :created_at => Time.now + 200
-        @artist4 = FactoryGirl.create :artist, :author_id => @author.id, :name => 'artist4', :created_at => Time.now + 300
-        @artist5 = FactoryGirl.create :artist, :author_id => @author.id, :name => 'artist5', :created_at => Time.now + 400
-        Artist.stub(:default_page_size).and_return(2)
-      end
-      it '通常は全件(5件)を返す' do
-        r = Artist.list 5, 0
-        r.should have(5).items 
-      end
+  end
+  
+  describe '一覧ページ制御に於いて' do
+    before do
+      Artist.stub(:count).with(any_args).and_return(100)
+    end
+    it 'ページ制御を返す' do
+      r = Artist.list_paginate
+      r.is_a?(Kaminari::PaginatableArray).should be_true
+    end
+    it '一覧の取得条件を利用している' do
+      Artist.stub(:list_where).with(any_args).and_return('')
+      Artist.should_receive(:list_where).with(any_args).exactly(1)
+      r = Artist.list_paginate
+    end
+    it 'ページ件数10のとき、3ページ目のオフセットは20から始まる' do
+      r = Artist.list_paginate 3, 10
+      r.limit_value.should eq 10
+      r.offset_value.should eq 20
     end
   end
+  
   describe '一覧取得オプションに於いて' do
-    it 'includeキーを含んでいる' do
-      r = Artist.list_opt
-      r.has_key?(:include).should be_true
-    end
     it '1つの項目を含んでいる' do
-      r = Artist.list_opt[:include]
+      r = Artist.list_opt
       r.should have(1).items
     end
     it '作家を含んでいる' do
-      r = Artist.list_opt[:include]
+      r = Artist.list_opt
       r.has_key?(:author).should be_true
     end
   end
@@ -341,5 +388,22 @@ describe Artist do
       r.should eq 2
     end
   end
+  
+  describe 'エクスポートに於いて' do
+    before do
+      @artist = FactoryGirl.create :artist, :author_id => @author.id
+      @artist2 = FactoryGirl.create :artist, :author_id => @other_author.id, :updated_at => Time.now - 3000
+      @artist3 = FactoryGirl.create :artist, :author_id => nil
+    end
+    it '開始日時が省略された場合はすべての内絵師を返す' do
+      r = Artist.export 
+      r.should eq [@artist, @artist2]
+    end
+    it '開始日時以降に更新された内絵師を返す' do
+      r = Artist.export @artist.updated_at - 100
+      r.should eq [@artist]
+    end
+  end
+  
 
 end