OSDN Git Service

t#30200:update i18n devise
[pettanr/pettanr.git] / spec / controllers / original_pictures_controller_spec.rb
index 5bc98f5..4a325ef 100644 (file)
@@ -4,9 +4,9 @@ require 'spec_helper'
 
 describe OriginalPicturesController do
   before do
-    FactoryGirl.create :admin
+    @admin = FactoryGirl.create :admin
     @user = FactoryGirl.create( :user_yas)
-    @author = @user.author
+    @author = FactoryGirl.create :author, :user_id => @user.id
     @artist = FactoryGirl.create :artist_yas, :author_id => @author.id
     @sp = FactoryGirl.create :system_picture
     @lg = FactoryGirl.create :license_group
@@ -167,16 +167,27 @@ describe OriginalPicturesController do
           get :show, :id => @pic.id, :format => :json
           lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
         end
+        it '原画モデルにjson単体出力オプションを問い合わせている' do
+          OriginalPicture.should_receive(:show_json_opt).exactly(1)
+          get :show, :id => @pic.id, :format => :json
+        end
         it 'データがアレになっている' do
           get :show, :id => @pic.id, :format => :json
           json = JSON.parse response.body
           json["ext"].should match(/png/)
+          json["md5"].should be_true
+          json["artist_id"].should be_true
+          json["width"].should be_true
         end
       end
       #画像送信では、send_dataにスタブをおいてテストしたいが、ここに噛ませると
       #renderが働かず、エラーとなってしまう。そこで、原画のファイル取得部分に
       #スタブをおいてsend_dataがデータを返す体裁でテストする。
       context 'png形式' do
+        before do
+          OriginalPicture.any_instance.stub(:mime_type).and_return('image/png')
+          OriginalPicture.any_instance.stub(:restore).and_return('aaa')
+        end
         it '画像モデルに画像データを問い合わせる' do
           OriginalPicture.any_instance.should_receive(:restore).exactly(1)
           get :show, :id => @pic.id, :format => :png
@@ -186,12 +197,15 @@ describe OriginalPicturesController do
           get :show, :id => @pic.id, :format => :png
         end
         it '画像を送信する' do
-          OriginalPicture.any_instance.stub(:restore).and_return('aaa')
           get :show, :id => @pic.id, :format => :png
           response.body.should eq 'aaa'
         end
       end
       context 'gif形式' do
+        before do
+          OriginalPicture.any_instance.stub(:mime_type).and_return('image/gif')
+          OriginalPicture.any_instance.stub(:restore).and_return('bbb')
+        end
         it '画像モデルに画像データを問い合わせる' do
           OriginalPicture.any_instance.should_receive(:restore).exactly(1)
           get :show, :id => @pic.id, :format => :gif
@@ -201,12 +215,15 @@ describe OriginalPicturesController do
           get :show, :id => @pic.id, :format => :gif
         end
         it '画像を送信する' do
-          OriginalPicture.any_instance.stub(:restore).and_return('bbb')
           get :show, :id => @pic.id, :format => :gif
           response.body.should eq 'bbb'
         end
       end
       context 'jpeg形式' do
+        before do
+          OriginalPicture.any_instance.stub(:mime_type).and_return('image/jpeg')
+          OriginalPicture.any_instance.stub(:restore).and_return('ccc')
+        end
         it '画像モデルに画像データを問い合わせる' do
           OriginalPicture.any_instance.should_receive(:restore).exactly(1)
           get :show, :id => @pic.id, :format => :jpeg
@@ -216,7 +233,6 @@ describe OriginalPicturesController do
           get :show, :id => @pic.id, :format => :jpeg
         end
         it '画像を送信する' do
-          OriginalPicture.any_instance.stub(:restore).and_return('ccc')
           get :show, :id => @pic.id, :format => :jpeg
           response.body.should eq 'ccc'
         end
@@ -332,7 +348,7 @@ describe OriginalPicturesController do
       end
       it '自身に履歴一覧を問い合わせている' do
         OriginalPicture.any_instance.should_receive(:history).exactly(1)
-        get :history
+        get :history, :id => @op.id
       end
       it '@historyにリストを取得している' do
         get :history, :id => @op.id
@@ -415,10 +431,6 @@ describe OriginalPicturesController do
       sign_in @user
     end
     context 'つつがなく終わるとき' do
-      it 'ステータスコード200 OKを返す' do
-        get :new
-        response.should be_success 
-      end
       it '@original_pictureに新規データを用意している' do
         get :new
         assigns(:original_picture).should be_a_new(OriginalPicture)
@@ -428,18 +440,25 @@ describe OriginalPicturesController do
         get :new
       end
       context 'html形式' do
+        it 'ステータスコード200 OKを返す' do
+          get :new
+          response.should be_success 
+        end
         it 'ページテンプレートnewを描画する' do
           get :new
           response.should render_template("new")
         end
       end
       context 'js形式' do
+        it 'ステータスコード200 OKを返す' do
+          get :new, :format => :js
+          response.should be_success 
+        end
         it '部分テンプレートnew.jsを描画する' do
           get :new, :format => :js
           response.should render_template("new")
         end
       end
-    end
       context 'json形式' do
         it 'jsonデータを返す' do
           get :new, :format => :json
@@ -450,6 +469,7 @@ describe OriginalPicturesController do
           get :new, :format => :json
         end
       end
+    end
     context '作家権限がないとき' do
       before do
         sign_out @user
@@ -512,55 +532,73 @@ describe OriginalPicturesController do
   describe '新規作成に於いて' do
     before do
       sign_in @user
+      @attr = {:original_picture => {:file => "abc\ndef\nghi"}}
+      @imager = ImagerTest.load("abc\ndef\nghi")
+      OriginalPicturesController.any_instance.stub(:set_image).with(any_args()).and_return("abc\ndef\nghi")
     end
     context '事前チェックしておく' do
       before do
-        OriginalPicture.any_instance.stub(:store).with(any_args()).and_return(true)
+        PettanImager.stub(:load).with("abc\ndef\nghi").and_return(@imager)
+        OriginalPicture.any_instance.stub(:store).with(@imager).and_return(true)
       end
-      it 'モデルに保存依頼する' do
-        OriginalPicture.any_instance.should_receive(:store).exactly(1)
-        post :create, :original_picture => FactoryGirl.attributes_for(:original_picture)
+      it '画像ライブラリをロードする' do
+        PettanImager.should_receive(:load).with(any_args()).exactly(1)
+        post :create, @attr
       end
-      it "@original_pictureに作成された原画を保持している" do
-        post :create, :original_picture => FactoryGirl.attributes_for(:original_picture)
-        assigns(:original_picture).should be_a(OriginalPicture)
+      it '原画モデルにデフォルト値補充を依頼している' do
+        OriginalPicture.any_instance.should_receive(:supply_default).with(any_args()).exactly(1)
+        post :create, @attr
+      end
+      it '原画モデルに上書き補充を依頼している' do
+        OriginalPicture.any_instance.should_receive(:overwrite).with(@artist).exactly(1)
+        post :create, @attr
+      end
+      it 'モデルに保存依頼する' do
+        OriginalPicture.any_instance.should_receive(:store).with(@imager).exactly(1)
+        post :create, @attr
       end
     end
     context 'つつがなく終わるとき' do
       before do
-        OriginalPicture.any_instance.stub(:store).with(any_args()).and_return {
-          assigns(:original_picture).attributes = FactoryGirl.attributes_for(:original_picture, :artist_id => @artist.id, :ext => 'jpeg')
-          assigns(:original_picture).save
-          true
-        }
+        PettanImager.stub(:load).with("abc\ndef\nghi").and_return(@imager)
       end
-      it "作成された原画がDBにある" do
-        post :create, :original_picture => FactoryGirl.attributes_for(:original_picture)
-        assigns(:original_picture).should_not be_a_new OriginalPicture
+      it "@original_pictureに作成された原画を保持している" do
+        post :create, @attr
+        assigns(:original_picture).should be_a(OriginalPicture)
       end
       context 'html形式' do
         it 'ステータスコード302 Foundを返す' do
-          post :create, :original_picture => FactoryGirl.attributes_for(:original_picture)
+          post :create, @attr
           response.status.should eq 302
         end
         it '作成された原画の表示ページへ遷移する' do
-          post :create, :original_picture => FactoryGirl.attributes_for(:original_picture)
+          post :create, @attr
           response.should redirect_to(OriginalPicture.last)
         end
       end
       context 'json形式' do
+        before do
+          @attr.merge!({:format => :json})
+        end
         it 'ステータスコード200 OKを返す' do
-          post :create, :original_picture => FactoryGirl.attributes_for(:original_picture), :format => :json
+          post :create, @attr
           response.should be_success 
         end
         it '作成された原画をjsonデータで返す' do
-          post :create, :original_picture => FactoryGirl.attributes_for(:original_picture), :format => :json
+          post :create, @attr
           lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
         end
+        it '原画モデルにjson単体出力オプションを問い合わせている' do
+          OriginalPicture.should_receive(:show_json_opt).exactly(1)
+          post :create, @attr
+        end
         it 'データがアレになっている' do
-          post :create, :original_picture => FactoryGirl.attributes_for(:original_picture, :ext => 'jpeg'), :format => :json
+          post :create, @attr
           json = JSON.parse response.body
-          json["ext"].should match(/jpeg/)
+          json["ext"].should match(/png/)
+          json["md5"].should be_true
+          json["artist_id"].should be_true
+          json["width"].should be_true
         end
       end
     end
@@ -570,21 +608,24 @@ describe OriginalPicturesController do
       end
       context 'html形式' do
         it 'ステータスコード302 Foundを返す' do
-          post :create, :original_picture => FactoryGirl.attributes_for(:original_picture)
+          post :create, @attr
           response.status.should eq 302
         end
         it 'サインインページへ遷移する' do
-          post :create, :original_picture => FactoryGirl.attributes_for(:original_picture)
+          post :create, @attr
           response.body.should redirect_to '/users/sign_in'
         end
       end
       context 'json形式' do
+        before do
+          @attr.merge!({:format => :json})
+        end
         it 'ステータスコード401 Unauthorizedを返す' do
-          post :create, :original_picture => FactoryGirl.attributes_for(:original_picture), :format => :json
+          post :create, @attr
           response.status.should eq 401
         end
         it '応答メッセージにUnauthorizedを返す' do
-          post :create, :original_picture => FactoryGirl.attributes_for(:original_picture), :format => :json
+          post :create, @attr
           response.message.should match(/Unauthorized/)
         end
       end
@@ -595,47 +636,54 @@ describe OriginalPicturesController do
       end
       context 'html形式' do
         it 'ステータスコード302 Foundを返す' do
-          post :create, :original_picture => FactoryGirl.attributes_for(:original_picture)
+          post :create, @attr
           response.status.should eq 302
         end
         it '絵師登録ページへ遷移する' do
-          post :create, :original_picture => FactoryGirl.attributes_for(:original_picture)
+          post :create, @attr
           response.should redirect_to new_artist_path
         end
       end
       context 'json形式' do
+        before do
+          @attr.merge!({:format => :json})
+        end
         it '例外403 forbiddenを返す' do
           lambda{
-            post :create, :original_picture => FactoryGirl.attributes_for(:original_picture), :format => :json
+            post :create, @attr
           }.should raise_error(ActiveRecord::Forbidden)
         end
       end
     end
     context '検証、保存に失敗した' do
       before do
-        OriginalPicture.any_instance.stub(:store).and_return(false)
+        PettanImager.stub(:load).with("abc\ndef\nghi").and_return(@imager)
+        OriginalPicture.any_instance.stub(:store).with(@imager).and_return(false)
       end
-      it "未保存のコミックを保持している" do
-        post :create, :original_picture => FactoryGirl.attributes_for(:original_picture)
+      it "未保存の原画を保持している" do
+        post :create, @attr
         assigns(:original_picture).should be_a_new(OriginalPicture)
       end
       context 'html形式' do
         it 'ステータスコード200 OKを返す' do
-          post :create, :original_picture => FactoryGirl.attributes_for(:original_picture)
+          post :create, @attr
           response.status.should eq 200
         end
         it '新規ページを描画する' do
-          post :create, :original_picture => FactoryGirl.attributes_for(:original_picture)
+          post :create, @attr
           response.should render_template("new")
         end
       end
       context 'json形式' do
+        before do
+          @attr.merge!({:format => :json})
+        end
         it 'ステータスコード422 unprocessable_entity を返す' do
-          post :create, :original_picture => FactoryGirl.attributes_for(:original_picture), :format => :json
+          post :create, @attr
           response.status.should eq 422
         end
         it '応答メッセージUnprocessable Entityを返す' do
-          post :create, :original_picture => FactoryGirl.attributes_for(:original_picture), :format => :json
+          post :create, @attr
           response.message.should match(/Unprocessable/)
         end
       end
@@ -728,56 +776,60 @@ describe OriginalPicturesController do
 
   describe '更新に於いて' do
     before do
-      @pic = FactoryGirl.create :original_picture, :artist_id => @artist.id
-      OriginalPicture.stub(:edit).with(any_args()).and_return(@pic)
+      @op = FactoryGirl.create :original_picture, :artist_id => @artist.id
+      OriginalPicture.stub(:edit).with(@op.id.to_s, @artist).and_return(@op)
+      OriginalPicturesController.any_instance.stub(:set_image).with(any_args()).and_return("abc\ndef\nghi")
       sign_in @user
+      @attr = {:file => "abc\ndef\nghi"}
+      @imager = ImagerTest.load("abc\ndef\nghi")
     end
     context '事前チェックしておく' do
       before do
-        OriginalPicture.any_instance.stub(:store).with(any_args()).and_return(true)
+        PettanImager.stub(:load).with("abc\ndef\nghi").and_return(@imager)
+        OriginalPicture.any_instance.stub(:store).with(@imager).and_return(true)
       end
-      it '原画モデルに単体取得を問い合わせている' do
-        OriginalPicture.should_receive(:edit).exactly(1)
-        put :update, :id => @pic.id, :original_picture => FactoryGirl.attributes_for(:original_picture)
+      it '画像ライブラリをロードする' do
+        PettanImager.should_receive(:load).with(any_args()).exactly(1)
+        put :update, :id => @op.id, :original_picture => @attr
       end
-      it 'モデルに更新を依頼する' do
-        OriginalPicture.any_instance.should_receive(:store).exactly(1)
-        put :update, :id => @pic.id, :original_picture => FactoryGirl.attributes_for(:original_picture)
+      it '原画モデルに編集取得を問い合わせている' do
+        OriginalPicture.should_receive(:edit).with(@op.id.to_s, @artist).exactly(1)
+        put :update, :id => @op.id, :original_picture => @attr
       end
-      it '@original_pictureにアレを取得している' do
-        put :update, :id => @pic.id, :original_picture => FactoryGirl.attributes_for(:original_picture)
-        assigns(:original_picture).should eq(@pic)
+      it '原画モデルに上書き補充を依頼している' do
+        OriginalPicture.any_instance.should_receive(:overwrite).with(@artist).exactly(1)
+        put :update, :id => @op.id, :original_picture => @attr
+      end
+      it 'モデルに更新を依頼する' do
+        OriginalPicture.any_instance.should_receive(:store).with(@imager).exactly(1)
+        put :update, :id => @op.id, :original_picture => @attr
       end
     end
     context 'つつがなく終わるとき' do
       before do
-        OriginalPicture.any_instance.stub(:store).with(any_args()).and_return {
-          assigns(:original_picture).attributes = FactoryGirl.attributes_for(:original_picture, :artist_id => @artist.id, :ext => 'jpeg')
-          assigns(:original_picture).save
-          true
-        }
+        PettanImager.stub(:load).with("abc\ndef\nghi").and_return(@imager)
       end
-      it '更新される' do
-        put :update, :id => @pic.id, :original_picture => FactoryGirl.attributes_for(:original_picture, :ext => 'jpeg')
-        OriginalPicture.find(@pic.id).ext.should eq 'jpeg'
+      it '@original_pictureにアレを取得している' do
+        put :update, :id => @op.id, :original_picture => @attr
+        assigns(:original_picture).should eq(@op)
       end
       context 'html形式' do
         it 'ステータスコード302 Foundを返す' do
-          put :update, :id => @pic.id, :original_picture => FactoryGirl.attributes_for(:original_picture)
+          put :update, :id => @op.id, :original_picture => @attr
           response.status.should eq 302
         end
         it '更新された原画の表示ページへ遷移する' do
-          put :update, :id => @pic.id, :original_picture => FactoryGirl.attributes_for(:original_picture)
+          put :update, :id => @op.id, :original_picture => @attr
           response.should redirect_to(@pic)
         end
       end
       context 'json形式' do
         it 'ステータスコード200 OKを返す' do
-          put :update, :id => @pic.id, :original_picture => FactoryGirl.attributes_for(:original_picture), :format => :json
+          put :update, :id => @op.id, :original_picture => @attr, :format => :json
           response.should be_success 
         end
         it 'ページ本体は特に返さない' do
-          put :update, :id => @pic.id, :original_picture => FactoryGirl.attributes_for(:original_picture), :format => :json
+          put :update, :id => @op.id, :original_picture => @attr, :format => :json
           response.body.should match /./
         end
       end
@@ -788,21 +840,21 @@ describe OriginalPicturesController do
       end
       context 'html形式' do
         it 'ステータスコード302 Foundを返す' do
-          post :create, :original_picture => FactoryGirl.attributes_for(:original_picture)
+          put :update, :id => @op.id, :original_picture => @attr
           response.status.should eq 302
         end
         it 'サインインページへ遷移する' do
-          post :create, :original_picture => FactoryGirl.attributes_for(:original_picture)
+          put :update, :id => @op.id, :original_picture => @attr
           response.body.should redirect_to '/users/sign_in'
         end
       end
       context 'json形式' do
         it 'ステータスコード401 Unauthorizedを返す' do
-          post :create, :original_picture => FactoryGirl.attributes_for(:original_picture), :format => :json
+          put :update, :id => @op.id, :original_picture => @attr, :format => :json
           response.status.should eq 401
         end
         it '応答メッセージにUnauthorizedを返す' do
-          post :create, :original_picture => FactoryGirl.attributes_for(:original_picture), :format => :json
+          put :update, :id => @op.id, :original_picture => @attr, :format => :json
           response.message.should match(/Unauthorized/)
         end
       end
@@ -813,43 +865,44 @@ describe OriginalPicturesController do
       end
       context 'html形式' do
         it 'ステータスコード302 Foundを返す' do
-          post :create, :original_picture => FactoryGirl.attributes_for(:original_picture)
+          put :update, :id => @op.id, :original_picture => @attr
           response.status.should eq 302
         end
         it '絵師登録ページへ遷移する' do
-          post :create, :original_picture => FactoryGirl.attributes_for(:original_picture)
+          put :update, :id => @op.id, :original_picture => @attr
           response.should redirect_to new_artist_path
         end
       end
       context 'json形式' do
         it '例外403 forbiddenを返す' do
           lambda{
-            post :create, :original_picture => FactoryGirl.attributes_for(:original_picture), :format => :json
+            put :update, :id => @op.id, :original_picture => @attr, :format => :json
           }.should raise_error(ActiveRecord::Forbidden)
         end
       end
     end
     context '検証、保存に失敗した' do
       before do
-        OriginalPicture.any_instance.stub(:store).and_return(false)
+        PettanImager.stub(:load).with("abc\ndef\nghi").and_return(@imager)
+        OriginalPicture.any_instance.stub(:store).with(@imager).and_return(false)
       end
       context 'html形式' do
         it 'ステータスコード200 Okを返す' do
-          put :update, :id => @pic.id, :original_picture => FactoryGirl.attributes_for(:original_picture)
+          put :update, :id => @op.id, :original_picture => @attr
           response.status.should eq 200
         end
         it '編集ページを描画する' do
-          put :update, :id => @pic.id, :original_picture => FactoryGirl.attributes_for(:original_picture)
+          put :update, :id => @op.id, :original_picture => @attr
           response.should render_template("edit")
         end
       end
       context 'json形式' do
         it 'ステータスコード422 unprocessable_entity を返す' do
-          put :update, :id => @pic.id, :original_picture => FactoryGirl.attributes_for(:original_picture), :format => :json
+          put :update, :id => @op.id, :original_picture => @attr, :format => :json
           response.status.should eq 422
         end
         it '応答メッセージUnprocessable Entityを返す' do
-          put :update, :id => @pic.id, :original_picture => FactoryGirl.attributes_for(:original_picture), :format => :json
+          put :update, :id => @op.id, :original_picture => @attr, :format => :json
           response.message.should match(/Unprocessable/)
         end
       end