OSDN Git Service

t#31470:create pager
[pettanr/pettanr.git] / spec / models / artist_spec.rb
index 42bb737..e93b28f 100644 (file)
@@ -4,65 +4,167 @@ require 'spec_helper'
 
 describe Artist do
   before do
-    Factory :admin
-    @user = Factory( :user_yas)
-    @author = @user.author
-    @license = Factory :license
+    @admin = FactoryGirl.create :admin
+    @user = FactoryGirl.create( :user_yas)
+    @author = FactoryGirl.create :author, :user_id => @user.id
+    @other_user = FactoryGirl.create( :user_yas)
+    @other_author = FactoryGirl.create :author, :user_id => @other_user.id
+    @sp = FactoryGirl.create :system_picture
+    @lg = FactoryGirl.create :license_group
+    @license = FactoryGirl.create :license, :license_group_id => @lg.id, :system_picture_id => @sp.id
   end
 
   describe '検証に於いて' do
     before do
+      @artist = FactoryGirl.build :artist, :author_id => @author.id
     end
     
-    it 'オーソドックスなデータなら通る' do
-      @artist = Factory.build :artist, :author_id => @author.id, :default_license_id => @license.id
-      @artist.should be_valid
+    context 'オーソドックスなデータのとき' do
+      it '下限データが通る' do
+        @artist.name = 'a'
+        @artist.should be_valid
+      end
+      it '上限データが通る' do
+        @artist.name = 'a'*30
+        @artist.should be_valid
+      end
     end
     
     context 'nameを検証するとき' do
       it 'nullなら失敗する' do
-        @artist = Factory.build :artist, :author_id => @author.id, :default_license_id => @license.id, :name => nil
+        @artist.name = nil
         @artist.should_not be_valid
       end
       it '30文字以上なら失敗する' do
-        @artist = Factory.build :artist, :author_id => @author.id, :default_license_id => @license.id, :name => 'a'*31
+        @artist.name = 'a'*31
         @artist.should_not be_valid
       end
     end
+    context 'author_idを検証するとき' do
+      #絵師は独立絵師があるので数値チェックだけ
+      it '数値でなければ失敗する' do
+        @artist.author_id = 'a'
+        @artist.should_not be_valid
+      end
+    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
+  describe 'デフォルト値補充に於いて' do
     it '名前がno nameになっている' do
-      @artist = Factory.build :artist, :name => nil
+      @artist = FactoryGirl.build :artist, :name => nil
       @artist.supply_default
       @artist.name.should eq 'no name'
     end
   end
   
-  describe '単体取得に於いて' do
+  describe '上書き補充に於いて' do
+    it '作家idが設定されている' do
+      @artist = FactoryGirl.build :artist
+      @artist.overwrite @author
+      @artist.author_id.should eq @author.id
+    end
+  end
+  
+  describe '所持判定に於いて' do
     before do
-      @artist = Factory :artist, :author_id => @author.id, :default_license_id => @license.id
+      @artist = FactoryGirl.create :artist, :author_id => @author.id
+      @other_artist = FactoryGirl.create :artist_yas, :author_id => @other_author.id
     end
-    it '指定の絵師を返す' do
-      a = Artist.show @artist.id
-      a.should eq @artist
+    context '事前チェックする' do
+      it '自身にロールリストからの作家取得を依頼している' do
+        Artist.should_receive(:get_author_from_roles).with(any_args).exactly(1)
+        r = @artist.own?([@author])
+      end
+    end
+    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
+    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
+  
+  describe '閲覧許可に於いて' do
+    before do
+      @artist = FactoryGirl.create :artist, :author_id => @author.id
+    end
+    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.show_include_opt
-        r.should eq [:author]
+    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
-      it '作家データと素材データを含んでいる' do
-        r = Artist.show_include_opt(:include => :resource_pictures)
-        r.should eq [:author, :resource_pictures]
+    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
     before do
-      @artist = Factory :artist, :author_id => @author.id, :default_license_id => @license.id
+      @artist = FactoryGirl.create :artist, :author_id => @author.id
     end
     context 'page補正について' do
       it '文字列から数値に変換される' do
@@ -89,40 +191,219 @@ describe Artist do
         Artist.page_size('1000').should eq Artist.max_page_size
       end
     end
+    context 'つつがなく終わるとき' do
+      it '一覧取得オプションを利用している' do
+        Artist.stub(:list_opt).with(any_args).and_return({})
+        Artist.should_receive(:list_opt).with(any_args).exactly(1)
+        r = Artist.list
+      end
+    end
     it 'リストを返す' do
-      c = Artist.list
-      c.should eq [@artist]
+      r = Artist.list
+      r.should eq [@artist]
     end
-    it '時系列で並んでいる' do
-      n = Factory :artist, :author_id => @author.id, :name => 'artist'
+    it '作成時系列で並んでいる' do
+      n = FactoryGirl.create :artist, :author_id => @author.id, :name => 'artist', :created_at => Time.now + 100
       l = Artist.list
       l.should eq [n, @artist]
     end
+    it 'ただし、内絵師(作家idが空)だけとする' do
+      n = FactoryGirl.create :artist, :author_id => nil
+      l = Artist.list
+      l.should eq [@artist]
+    end
     context 'DBに5件あって1ページの件数を2件に変えたとして' do
       before do
-        @artist2 = Factory :artist, :author_id => @author.id, :name => 'artist2'
-        @artist3 = Factory :artist, :author_id => @author.id, :name => 'artist3'
-        @artist4 = Factory :artist, :author_id => @author.id, :name => 'artist4'
-        @artist5 = Factory :artist, :author_id => @author.id, :name => 'artist5'
+        @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 '通常は2件を返す' do
-        c = Artist.list
-        c.should have(2).items 
+        r = Artist.list
+        r.should have(2).items 
       end
       it 'page=1なら末尾2件を返す' do
         #時系列で並んでいる
-        c = Artist.list({}, 1)
-        c.should eq [@artist5, @artist4]
+        r = Artist.list(1)
+        r.should eq [@artist5, @artist4]
       end
       it 'page=2なら中間2件を返す' do
-        c = Artist.list({}, 2)
-        c.should eq [@artist3, @artist2]
+        r = Artist.list(2)
+        r.should eq [@artist3, @artist2]
       end
       it 'page=3なら先頭1件を返す' do
-        c = Artist.list({}, 3)
-        c.should eq [@artist]
+        r = Artist.list(3)
+        r.should eq [@artist]
       end
     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 '1つの項目を含んでいる' do
+      r = Artist.list_opt
+      r.should have(1).items
+    end
+    it '作家を含んでいる' do
+      r = Artist.list_opt
+      r.has_key?(:author).should be_true
+    end
+  end
+  describe 'json一覧出力オプションに於いて' do
+    before do
+      @artist = FactoryGirl.create :artist, :author_id => @author.id
+    end
+    it '作家を含んでいる' do
+      r = Artist.list.to_json Artist.list_json_opt
+      j = JSON.parse r
+      i = j.first
+      i.has_key?('author').should be_true
+    end
+  end
+  
+  describe '単体取得に於いて' do
+    before do
+      @artist = FactoryGirl.create :artist, :author_id => @author.id
+    end
+    context 'つつがなく終わるとき' do
+      it '単体取得オプションを利用している' do
+        Artist.stub(:show_opt).with(any_args).and_return({})
+        Artist.should_receive(:show_opt).with(any_args).exactly(1)
+        r = Artist.show @artist.id, @author
+      end
+      it '閲覧許可を問い合わせている' do
+        Artist.any_instance.stub(:visible?).with(any_args).and_return(true)
+        Artist.any_instance.should_receive(:visible?).with(any_args).exactly(1)
+        r = Artist.show @artist.id, @author
+      end
+    end
+    it '指定の絵師を返す' do
+      a = Artist.show @artist.id, @author
+      a.should eq @artist
+    end
+    context '閲覧許可が出なかったとき' do
+      it '403Forbidden例外を返す' do
+        Artist.any_instance.stub(:visible?).and_return(false)
+        lambda{
+          Artist.show @artist.id, @author
+        }.should raise_error(ActiveRecord::Forbidden)
+      end
+    end
+    context '存在しない絵師を開こうとしたとき' do
+      it '404RecordNotFound例外を返す' do
+        lambda{
+          Artist.show 110, @author
+        }.should raise_error(ActiveRecord::RecordNotFound)
+      end
+    end
+  end
+  describe '編集取得に於いて' do
+    before do
+      @artist = FactoryGirl.create :artist, :author_id => @author.id
+    end
+    context 'つつがなく終わるとき' do
+      it '単体取得オプションを利用している' do
+        Artist.stub(:show_opt).with(any_args).and_return({})
+        Artist.should_receive(:show_opt).with(any_args).exactly(1)
+        r = Artist.edit @artist.id, @author
+      end
+      it '所持判定を問い合わせている' do
+        Artist.any_instance.stub(:own?).with(any_args).and_return(true)
+        Artist.any_instance.should_receive(:own?).with(any_args).exactly(1)
+        r = Artist.edit @artist.id, @author
+      end
+    end
+    it '指定の絵師を返す' do
+      Artist.any_instance.stub(:own?).and_return(true)
+      r = Artist.edit @artist.id, @author.id
+      r.should eq @artist
+    end
+    context '他人の絵師を開こうとしたとき' do
+      it '403Forbidden例外を返す' do
+        Artist.any_instance.stub(:own?).and_return(false)
+        lambda{
+          Artist.edit @artist.id, @author
+        }.should raise_error(ActiveRecord::Forbidden)
+      end
+    end
+    context '存在しない絵師を開こうとしたとき' do
+      it '404RecordNotFound例外を返す' do
+        lambda{
+          Artist.edit 110, @author
+        }.should raise_error(ActiveRecord::RecordNotFound)
+      end
+    end
+  end
+  describe '単体取得オプションに於いて' do
+    it 'includeキーを含んでいる' do
+      r = Artist.show_opt
+      r.has_key?(:include).should be_true
+    end
+    it '1つの項目を含んでいる' do
+      r = Artist.show_opt[:include]
+      r.should have(1).items
+    end
+  end
+  describe 'json単体出力オプションに於いて' do
+    before do
+      @artist = FactoryGirl.create :artist, :author_id => @author.id
+    end
+    it '作家を含んでいる' do
+      r = Artist.show(@artist.id, @author).to_json Artist.show_json_opt
+      j = JSON.parse r
+      i = j
+      i.has_key?('author').should be_true
+    end
+  end
+  
+  describe '有効絵師数に於いて' do
+    before do
+      @artist = FactoryGirl.create :artist, :author_id => @author.id
+      @artist = FactoryGirl.create :artist, :author_id => @other_author.id
+      @artist = FactoryGirl.create :artist, :author_id => nil
+    end
+    it '内絵師数を返す' do
+      r = Artist.visible_count
+      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