OSDN Git Service

add OriginalPictureLicenseGroup
[pettanr/pettanr.git] / spec / controllers / original_picture_license_groups_controller_spec.rb
diff --git a/spec/controllers/original_picture_license_groups_controller_spec.rb b/spec/controllers/original_picture_license_groups_controller_spec.rb
new file mode 100644 (file)
index 0000000..eb55c72
--- /dev/null
@@ -0,0 +1,276 @@
+# -*- encoding: utf-8 -*-
+#原画ライセンスグループ
+require 'spec_helper'
+
+describe OriginalPictureLicenseGroupsController do
+  before do
+    Factory :admin
+    @user = Factory( :user_yas)
+    @author = @user.author
+    @artist = Factory :artist_yas, :author_id => @author.id
+    @sp = Factory :system_picture
+    @lg = Factory :license_group, :classname => 'OriginalPicture'
+    @license = Factory :license, :license_group_id => @lg.id, :system_picture_id => @sp.id
+    @op = Factory :original_picture, :artist_id => @artist.id
+  end
+
+  describe '新規作成フォーム表示に於いて' do
+    before do
+      sign_in @user
+    end
+    context 'つつがなく終わるとき' do
+      before do
+        @attr = {:original_picture_id => @op.id}
+        @attrj = @attr.merge({:format => :js})
+      end
+      it 'ステータスコード200 OKを返す' do
+        get :new, @attr
+        response.should be_success 
+      end
+      it '原画モデルに単体取得を問い合わせている' do
+        OriginalPicture.stub(:show).with(any_args()).and_return(@op)
+        OriginalPicture.should_receive(:show).with(any_args()).exactly(1)\r
+        get :new, @attr
+      end
+      it '@original_pictureにデータを用意している' do
+        get :new, @attr
+        assigns(:original_picture).should eq @op
+      end
+      it '@original_picture_license_groupに新規データを用意している' do
+        get :new, @attr
+        assigns(:original_picture_license_group).should be_a_new(OriginalPictureLicenseGroup)
+      end
+      it '原画ライセンスグループモデルにデフォルト値補充を依頼している' do
+        OriginalPictureLicenseGroup.any_instance.should_receive(:supply_default).exactly(1)\r
+        get :new, @attr
+      end
+      context 'html形式' do
+        it 'ページテンプレートnewを描画する' do
+          get :new, @attr
+          response.should render_template("new")
+        end
+      end
+      context 'js形式' do
+        it '部分テンプレートnew.jsを描画する' do
+          get :new, @attrj
+          response.should render_template("new")
+        end
+      end
+    end
+    context '作家権限がないとき' do
+      before do
+        sign_out @user
+        @attr = {:original_picture_id => @op.id}
+        @attrj = @attr.merge({:format => :js})
+      end
+      context 'html形式' do
+        it 'ステータスコード302 Foundを返す' do
+          get :new, @attr
+          response.status.should eq 302
+        end
+        it 'サインインページへ遷移する' do
+          get :new, @attr
+          response.body.should redirect_to '/users/sign_in'
+        end
+      end
+      context 'js形式' do
+        it 'ステータスコード401 Unauthorizedを返す' do
+          get :new, @attrj
+          response.status.should eq 401
+        end
+        it '応答メッセージにUnauthorizedを返す' do
+          get :new, @attrj
+          response.message.should match(/Unauthorized/)
+        end
+      end
+    end
+    context '作家が絵師でないとき' do
+      before do
+        @attr = {:original_picture_id => @op.id}
+        @attrj = @attr.merge({:format => :js})
+        Author.any_instance.stub(:artist?).and_return(false)
+      end
+      context 'html形式' do
+        it 'ステータスコード302 Foundを返す' do
+          get :new, @attr
+          response.status.should eq 302
+        end
+        it '絵師登録ページへ遷移する' do
+          get :new, @attr
+          response.should redirect_to new_artist_path
+        end
+      end
+      context 'js形式' do
+        it 'ステータスコード200 Okを返す' do
+          get :new, @attrj
+          response.status.should eq 200
+        end
+        it '絵師登録部分テンプレートartists/new.jsを描画する' do
+          get :new, @attrj
+          response.should render_template("artists/new")
+        end
+      end
+    end
+  end
+
+  describe '新規作成に於いて' do
+    before do
+      sign_in @user
+    end
+    context '事前チェックしておく' do
+      before do
+        OriginalPictureLicenseGroup.any_instance.stub(:valid?).with(any_args()).and_return(true)
+        @attr = {:original_picture_id => @op.id, :original_picture_license_group => {:original_picture_id => @op.id, :license_group_id => @lg.id}}
+        @attrj = @attr.merge({:format => :js})
+      end
+      it '原画モデルに単体取得を問い合わせている' do
+        OriginalPicture.stub(:show).with(any_args()).and_return(@op)
+        OriginalPicture.should_receive(:show).exactly(1)\r
+        post :create, @attr
+      end
+      it '@original_pictureにデータを用意している' do
+        post :create, @attr
+        assigns(:original_picture).should eq @op
+      end
+      it 'ライセンスグループモデルに単体取得を問い合わせている' do
+        LicenseGroup.stub(:show).with(any_args()).and_return(@lg)
+        LicenseGroup.should_receive(:show).exactly(1)\r
+        post :create, @attr
+      end
+      it '@license_groupにデータを用意している' do
+        post :create, @attr
+        assigns(:license_group).should eq @lg
+      end
+      it '@ctlにコントローラを用意している' do
+        post :create, @attr
+        assigns(:ctl).should eq 'original_pictures'
+      end
+      it 'モデルに検証依頼する' do
+        OriginalPictureLicenseGroup.any_instance.stub(:valid?).with(any_args()).and_return(true)
+        OriginalPictureLicenseGroup.any_instance.should_receive(:valid?).exactly(1)
+        post :create, @attr
+      end
+    end
+    context 'つつがなく終わるとき' do
+      before do
+        @attr = {:original_picture_id => @op.id, :original_picture_license_group => {:original_picture_id => @op.id, :license_group_id => @lg.id}}
+        @attrj = @attr.merge({:format => :js})
+      end
+      it "検証が通る" do
+        post :create, @attr
+        assigns(:original_picture_license_group).valid?.should eq true
+      end
+      it "DBに変化がない" do
+        lambda {
+          post :create, @attr
+        }.should_not change OriginalPictureLicenseGroup, :count
+      end
+      context 'html形式' do
+        it 'ステータスコード200 OKを返す' do
+          post :create, @attr
+          response.should be_success
+        end
+        it 'ライセンスクラスの新規作成テンプレートnewを描画する' do
+          post :create, @attr
+          response.should render_template("original_pictures/new")
+        end
+      end
+      context 'js形式' do
+        it 'ステータスコード200 OKを返す' do
+          post :create, @attrj
+          response.should be_success 
+        end
+        it 'ライセンスクラスの新規作成部分テンプレートnew.jsを描画する' do
+          post :create, @attrj
+          response.should render_template("original_pictures/new")
+        end
+      end
+    end
+    context '作家権限がないとき' do
+      before do
+        sign_out @user
+        @attr = {:original_picture_id => @op.id, :original_picture_license_group => {:original_picture_id => @op.id, :license_group_id => @lg.id}}
+        @attrj = @attr.merge({:format => :js})
+      end
+      context 'html形式' do
+        it 'ステータスコード302 Foundを返す' do
+          post :create, @attr
+          response.status.should eq 302
+        end
+        it 'サインインページへ遷移する' do
+          post :create, @attr
+          response.body.should redirect_to '/users/sign_in'
+        end
+      end
+      context 'json形式' do
+        it 'ステータスコード401 Unauthorizedを返す' do
+          post :create, @attrj
+          response.status.should eq 401
+        end
+        it '応答メッセージにUnauthorizedを返す' do
+          post :create, @attrj
+          response.message.should match(/Unauthorized/)
+        end
+      end
+    end
+    context '作家が絵師でないとき' do
+      before do
+        Author.any_instance.stub(:artist?).and_return(false)
+        @attr = {:original_picture_id => @op.id, :original_picture_license_group => {:original_picture_id => @op.id, :license_group_id => @lg.id}}
+        @attrj = @attr.merge({:format => :js})
+      end
+      context 'html形式' do
+        it 'ステータスコード302 Foundを返す' do
+          post :create, @attr
+          response.status.should eq 302
+        end
+        it '絵師登録ページへ遷移する' do
+          post :create, @attr
+          response.should redirect_to new_artist_path
+        end
+      end
+      context 'js形式' do
+        it 'ステータスコード200 OKを返す' do
+          post :create, @attrj
+          response.status.should eq 200
+        end
+        it '絵師登録ページへ遷移する' do
+          post :create, @attrj
+          response.should render_template("artists/new")
+        end
+      end
+    end
+    context '検証、保存に失敗した' do
+      before do
+        OriginalPictureLicenseGroup.any_instance.stub(:valid?).and_return(false)
+        @attr = {:original_picture_id => @op.id, :original_picture_license_group => {:original_picture_id => @op.id, :license_group_id => @lg.id}}
+        @attrj = @attr.merge({:format => :js})
+      end
+      it "未保存の原画ライセンスグループを保持している" do
+        post :create, @attr
+        assigns(:original_picture_license_group).should be_a_new(OriginalPictureLicenseGroup)
+      end
+      context 'html形式' do
+        it 'ステータスコード200 OKを返す' do
+          post :create, @attr
+          response.status.should eq 200
+        end
+        it '新規ページを描画する' do
+          post :create, @attr
+          response.should render_template("new")
+        end
+      end
+      context 'js形式' do
+        it 'ステータスコード200 OKを返す' do
+          post :create, @attrj
+          response.status.should eq 200
+        end
+        it '新規ページを描画する' do
+          post :create, @attrj
+          response.should render_template("new")
+        end
+      end
+    end
+  end
+
+end