OSDN Git Service

Merge branch 'v06' of git.sourceforge.jp:/gitroot/pettanr/pettanr into v06
[pettanr/pettanr.git] / spec / models / picture_spec.rb
index e37eac7..5ce8081 100644 (file)
@@ -201,6 +201,49 @@ describe Picture do
     end
   end
   
+  describe '文字コード検証に於いて' do
+    before do
+      @p = FactoryGirl.build :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @artist.id
+    end
+    
+    context 'artist_nameを検証するとき' do
+      it 'Shift JISなら失敗する' do
+        @p.artist_name = "\x83G\x83r\x83]\x83D"
+        lambda{
+          @p.valid_encode
+        }.should raise_error(Pettanr::BadRequest)
+      end
+    end
+    
+    context 'classnameを検証するとき' do
+      it 'Shift JISなら失敗する' do
+        @p.classname = "\x83G\x83r\x83]\x83D"
+        lambda{
+          @p.valid_encode
+        }.should raise_error(Pettanr::BadRequest)
+      end
+    end
+    
+    context 'creditを検証するとき' do
+      it 'Shift JISなら失敗する' do
+        @p.credit = "\x83G\x83r\x83]\x83D"
+        lambda{
+          @p.valid_encode
+        }.should raise_error(Pettanr::BadRequest)
+      end
+    end
+    
+    context 'settingsを検証するとき' do
+      it 'Shift JISなら失敗する' do
+        @p.settings = "\x83G\x83r\x83]\x83D"
+        lambda{
+          @p.valid_encode
+        }.should raise_error(Pettanr::BadRequest)
+      end
+    end
+    
+  end
+  
   describe 'デフォルト値補充に於いて' do
     it 'defined' do
       @p = FactoryGirl.build :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @artist.id
@@ -250,28 +293,36 @@ describe Picture do
     before do
       @p = FactoryGirl.build :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @artist.id
     end
-    context 'パラメータが作家のとき' do
-      it '自作の実素材ならyes' do
-        @p.own?(@author).should == true
-      end
-      it '他人のならno' do
-        @p.own?(@other_author).should == false
+    context '事前チェックする' do
+      it '自身にロールリストからの絵師取得を依頼している' do
+        Picture.should_receive(:get_artist_from_roles).with(any_args).exactly(1)
+        r = @p.own?([@artist])
       end
     end
-    context 'パラメータが絵師のとき' do
-      it '自作の実素材ならyes' do
-        @p.own?(@artist).should == true
+    context 'ロール内絵師が取得できるとき' do
+      before do
+      end
+      it 'ロール内絵師のidが自身の絵師idと一致するなら許可する' do
+        Picture.stub(:get_artist_from_roles).with(any_args).and_return(@artist)
+        r = @p.own?([@artist])
+        r.should be_true
       end
-      it '他人のならno' do
-        @p.own?(@other_artist).should == false
+      it 'ロール内絵師のidが自身の絵師idと一致しないならno' do
+        Picture.stub(:get_artist_from_roles).with(any_args).and_return(@other_artist)
+        @p.own?(@other_artist).should be_false
       end
     end
-    context 'それ以外のとき' do
-      it 'no' do
-        @p.own?(nil).should == false
+    context 'ロール内絵師が取得できないとき' do
+      before do
+        Picture.stub(:get_artist_from_roles).with(any_args).and_return(nil)
+      end
+      it 'Falseを返す' do
+        r = @p.own?([@artist])
+        r.should be_false
       end
     end
   end
+  
   describe '閲覧許可に於いて' do
     before do
       @p = FactoryGirl.build :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @artist.id
@@ -286,6 +337,15 @@ describe Picture do
     before do
       @p = FactoryGirl.build :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @artist.id
     end
+    context '自身に原画がリンクしていないとき' do
+      before do
+        Picture.any_instance.stub(:original_picture).with(any_args).and_return(nil)
+      end
+      it 'Falseを返す' do
+        r = @p.showable?(@author)
+        r.should be_false
+      end
+    end
     it '自作の実素材ならyes' do
       Picture.any_instance.stub(:own?).with(any_args).and_return(true)
       @p.showable?(@artist).should == true
@@ -852,6 +912,55 @@ describe Picture do
     
   end
   
+  describe '墨塗に於いて' do
+    before do
+      @p = FactoryGirl.create :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @artist.id, :ext => 'png'
+    end
+    context '事前チェック' do
+      before do
+        @imager = ImagerTest.load "abc\ndef\nghi"
+        Picture.any_instance.stub(:store).with(any_args).and_return(true)
+        PettanImager.stub(:load).with(any_args).and_return(@imager)
+      end
+      it '画像ライブラリにロードを依頼している' do
+        PettanImager.should_receive(:load).with(any_args).exactly(1)
+        r = @p.unpublish
+      end
+      it '自身に作成を依頼している' do
+        Picture.any_instance.should_receive(:store).with(any_args).exactly(1)
+        r = @p.unpublish
+      end
+    end
+    context 'つつがなく終わるとき' do
+      before do
+        Picture.any_instance.stub(:store).with(any_args).and_return(true)
+      end
+      it 'Trueを返す' do
+        r = @p.unpublish
+        r.should be_true
+      end
+    end
+    #例外ケース
+    context '画像ライブラリのロードに失敗したとき' do
+      before do
+        PettanImager.stub(:load).and_return(false)
+      end
+      it 'Falseを返す' do
+        r = @p.unpublish
+        r.should be_false
+      end
+    end
+    context '作成に失敗したとき' do
+      before do
+        Picture.any_instance.stub(:store).with(any_args).and_return(false)
+      end
+      it 'Falseを返す' do
+        r = @p.unpublish
+        r.should be_false
+      end
+    end
+  end
+  
   describe 'フラグ展開に於いて' do
     before do
       @p = FactoryGirl.build :picture, :original_picture_id => @op.id, :license_id => @license.id,
@@ -1023,4 +1132,67 @@ describe Picture do
     end
   end
   
+  describe 'エクスポートに於いて' do
+    before do
+      @op = FactoryGirl.create :original_picture, :artist_id => @artist.id
+      @p = FactoryGirl.create :picture, :artist_id => @artist.id, :original_picture_id => @op.id, :license_id => @license.id, :revision => 0
+      @rp = FactoryGirl.create :resource_picture, :artist_id => @artist.id, :original_picture_id => @op.id, :license_id => @license.id, :picture_id => @p.id
+      @p2 = FactoryGirl.create :picture, :artist_id => @artist.id, :original_picture_id => @op.id, :license_id => @license.id, :revision => 1, :updated_at => Time.now + 1000
+      #開始日時以前の実素材
+      @old_op = FactoryGirl.create :original_picture, :artist_id => @artist.id
+      @old_p = FactoryGirl.create :picture, :artist_id => @artist.id, :original_picture_id => @old_op.id, :license_id => @license.id, :revision => 0, :updated_at => Time.now - 1000
+      @old_rp = FactoryGirl.create :resource_picture, :artist_id => @artist.id, :original_picture_id => @old_op.id, :license_id => @license.id, :picture_id => @old_p.id
+      #他人の実素材
+      @other_op = FactoryGirl.create :original_picture, :artist_id => @other_artist.id
+      @other_p = FactoryGirl.create :picture, :artist_id => @other_artist.id, :original_picture_id => @other_op.id, :license_id => @license.id, :revision => 0, :updated_at => Time.now + 100
+      @other_rp = FactoryGirl.create :resource_picture, :artist_id => @other_artist.id, :original_picture_id => @other_op.id, :license_id => @license.id, :picture_id => @other_p.id
+      #貸手からの実素材排除
+      @outer_artist = FactoryGirl.create :artist_yas, :author_id => nil
+      @outer_op = FactoryGirl.create :original_picture, :artist_id => @outer_artist.id
+      @outer_p = FactoryGirl.create :picture, :artist_id => @outer_artist.id, :original_picture_id => @outer_op.id, :license_id => @license.id, :revision => 0
+      @outer_rp = FactoryGirl.create :resource_picture, :artist_id => @outer_artist.id, :original_picture_id => @outer_op.id, :license_id => @license.id, :picture_id => @outer_p.id
+    end
+    context 'つつがなく終わるとき' do
+      it '開始日時が省略された場合はすべての内実素材を返す' do
+        r = Picture.export 
+        r.should eq [@p2, @other_p, @p, @old_p]
+      end
+      it '開始日時以降に更新された内実素材を返す' do
+        r = Picture.export @p.updated_at - 100
+        r.should eq [@p2, @other_p, @p]
+      end
+    end
+  end
+  
+  describe 'リストのjson化に於いて' do
+    before do
+      @op = FactoryGirl.create :original_picture, :artist_id => @artist.id
+      @p = FactoryGirl.create :picture, :artist_id => @artist.id, :original_picture_id => @op.id, :license_id => @license.id, :revision => 0
+      @rp = FactoryGirl.create :resource_picture, :artist_id => @artist.id, :original_picture_id => @op.id, :license_id => @license.id, :picture_id => @p.id
+      @other_op = FactoryGirl.create :original_picture, :artist_id => @other_artist.id
+      @other_p = FactoryGirl.create :picture, :artist_id => @other_artist.id, :original_picture_id => @other_op.id, :license_id => @license.id, :revision => 0, :updated_at => Time.now + 100
+      @other_rp = FactoryGirl.create :resource_picture, :artist_id => @other_artist.id, :original_picture_id => @other_op.id, :license_id => @license.id, :picture_id => @other_p.id
+      Picture.any_instance.stub(:restore).with(any_args).and_return('picture binary data')
+    end
+    context 'つつがなく終わるとき' do
+      it 'json文字列を返す' do
+        r = Picture.list_as_json_text Picture.all 
+        lambda {
+          j = JSON.parse r
+        }.should_not raise_error(JSON::ParserError)
+      end
+      it '実素材リストを返す' do
+        r = Picture.list_as_json_text Picture.all 
+        j = JSON.parse r
+        j.size.should eq 2
+      end
+      it '各実素材に画像データを添えて返す' do
+        r = Picture.list_as_json_text Picture.all 
+        j = JSON.parse r
+        j.first['picture_data'].should_not be_nil
+        j.last['picture_data'].should_not be_nil
+      end
+    end
+  end
+  
 end