OSDN Git Service

fix test
[pettanr/pettanr.git] / spec / controllers / speech_balloon_templates_controller_spec.rb
index 3a215b2..5173dff 100644 (file)
@@ -4,27 +4,25 @@ require 'spec_helper'
 describe SpeechBalloonTemplatesController do
 
   before do
-    @admin = Factory :admin
-    @sp = Factory :system_picture
-    @lg = Factory :license_group
-    @license = Factory :license, :license_group_id => @lg.id, :system_picture_id => @sp.id
-    @user = Factory( :user_yas)
-    @author = @user.author
-    @sbt = Factory :speech_balloon_template
+    SpeechBalloonTemplate.delete_all
+    @admin = FactoryGirl.create :admin
+    @sp = FactoryGirl.create :system_picture
+    @lg = FactoryGirl.create :license_group
+    @license = FactoryGirl.create :license, :license_group_id => @lg.id, :system_picture_id => @sp.id
+    @user = FactoryGirl.create( :user_yas)
+    @author = FactoryGirl.create :author, :user_id => @user.id
+    @sbt = FactoryGirl.create :speech_balloon_template
   end
 
+if MagicNumber['run_mode'] == 1
   describe '一覧表示に於いて' do
     before do
       sign_in @user
-      SpeechBalloonTemplate.stub(:list).and_return([@sbt, @sbt, @sbt])
+      SpeechBalloonTemplate.stub(:enable_list).and_return([@sbt, @sbt, @sbt])
     end
     context 'つつがなく終わるとき' do
-      it 'ステータスコード200 OKを返す' do
-        get :index
-        response.should be_success 
-      end
       it 'フキダシテンプレートモデルに一覧を問い合わせている' do
-        SpeechBalloonTemplate.should_receive(:list).exactly(1)
+        SpeechBalloonTemplate.should_receive(:enable_list).exactly(1)
         get :index
       end
       it '@speech_balloon_templatesにリストを取得している' do
@@ -32,16 +30,28 @@ describe SpeechBalloonTemplatesController do
         assigns(:speech_balloon_templates).should have_at_least(3).items
       end
       context 'html形式' do
+        it 'ステータスコード200 OKを返す' do
+          get :index
+          response.should be_success 
+        end
         it 'indexテンプレートを描画する' do
           get :index
           response.should render_template("index")
         end
       end
       context 'json形式' do
+        it 'ステータスコード200 OKを返す' do
+          get :index, :format => :json
+          response.should be_success 
+        end
         it 'jsonデータを返す' do
           get :index, :format => :json
           lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
         end
+        it 'フキダシテンプレートモデルにjson一覧出力オプションを問い合わせている' do
+          SpeechBalloonTemplate.should_receive(:list_json_opt).exactly(1)
+          get :index, :format => :json
+        end
         it 'データがリスト構造になっている' do
           get :index, :format => :json
           json = JSON.parse response.body
@@ -50,11 +60,13 @@ describe SpeechBalloonTemplatesController do
         it 'リストの先頭くらいはフキダシテンプレートっぽいものであって欲しい' do
           get :index, :format => :json
           json = JSON.parse response.body
+          json.first.has_key?("name").should be_true
           json.first.has_key?("classname").should be_true
+          json.first.has_key?("t").should be_true
         end
       end
     end
-    context 'ä½\9c家権é\99\90ã\81\8cã\81ªã\81\84ã\81¨ã\81\8d' do
+    context 'ã\83¦ã\83¼ã\82¶æ¨©é\99\90ã\81\8cã\81ªã\81\84ã\81¨ã\81\8d' do
       before do
         sign_out @user
       end
@@ -79,37 +91,65 @@ describe SpeechBalloonTemplatesController do
         end
       end
     end
+    context 'ユーザ権限はないが管理者権限があるとき' do
+      before do
+        sign_out @user
+        sign_in @admin
+      end
+      it 'ステータスコード200 OKを返す' do
+        get :index
+        response.should be_success 
+      end
+    end
+    context 'ユーザだが作家登録していないとき' do
+      before do
+        @author.destroy
+      end
+      it 'ステータスコード200 OKを返す' do
+        get :index
+        response.should be_success 
+      end
+    end
   end
   
   describe '単体表示に於いて' do
     before do
       sign_in @user
-      SpeechBalloonTemplate.stub(:show).and_return(@sbt)
+      SpeechBalloonTemplate.stub(:show).with(@sbt.id.to_s, [@user, nil]).and_return(@sbt)
+      SpeechBalloonTemplate.stub(:show).with(@sbt.id.to_s, [nil, @admin]).and_return(@sbt)
     end
     context 'つつがなく終わるとき' do
-      it 'ステータスコード200 OKを返す' do
-        get :show, :id => @sbt.id
-        response.should be_success
-      end
       it 'フキダシテンプレートモデルに単体取得を問い合わせている' do
         SpeechBalloonTemplate.should_receive(:show).exactly(1)
         get :show
       end
       it '@speech_balloon_templateにアレを取得している' do
         get :show, :id => @sbt.id
-        assigns(:speech_balloon_template).id.should eq(@sbt.id)
+        assigns(:speech_balloon_template).should eq(@sbt)
       end
       context 'html形式' do
+        it 'ステータスコード200 OKを返す' do
+          get :show, :id => @sbt.id
+          response.should be_success
+        end
         it 'showテンプレートを描画する' do
           get :show, :id => @sbt.id
           response.should render_template("show")
         end
       end
       context 'json形式' do
+        it 'ステータスコード200 OKを返す' do
+          get :show, :id => @sbt.id, :format => :json
+          response.should be_success
+        end
         it 'jsonデータを返す' do
           get :show, :id => @sbt.id, :format => :json
           lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
         end
+        it 'フキダシテンプレートモデルにjson単体出力オプションを問い合わせている' do
+          SpeechBalloonTemplate.should_receive(:show_json_opt).exactly(1)
+          get :show, :id => @sbt.id, :format => :json
+        end
         it 'データがアレになっている' do
           get :show, :id => @sbt.id, :format => :json
           json = JSON.parse response.body
@@ -117,32 +157,163 @@ describe SpeechBalloonTemplatesController do
         end
       end
     end
-    context 'ä½\9c家権é\99\90ã\81\8cã\81ªã\81\84ã\81¨ã\81\8d' do
+    context 'ã\83¦ã\83¼ã\82¶æ¨©é\99\90ã\81\8cã\81ªã\81\84ã\81¨ã\81\8d' do
       before do
         sign_out @user
       end
       context 'html形式' do
         it 'ステータスコード302 Foundを返す' do
-          get :index
+          get :show, :id => @sbt.id
           response.status.should eq 302
         end
         it 'サインインページへ遷移する' do
-          get :index
+          get :show, :id => @sbt.id
           response.should redirect_to '/users/sign_in'
         end
       end
       context 'json形式' do
         it 'ステータスコード401 Unauthorizedを返す' do
-          get :index, :format => :json
+          get :show, :id => @sbt.id, :format => :json
           response.status.should eq 401
         end
         it '応答メッセージにUnauthorizedを返す' do
-          get :index, :format => :json
+          get :show, :id => @sbt.id, :format => :json
           response.message.should match(/Unauthorized/)
         end
       end
     end
+    context 'ユーザ権限はないが管理者権限があるとき' do
+      before do
+        sign_out @user
+        sign_in @admin
+      end
+      it 'ステータスコード200 OKを返す' do
+        get :show, :id => @sbt.id
+        response.should be_success 
+      end
+    end
+    context 'ユーザだが作家登録していないとき' do
+      before do
+        @author.destroy
+      end
+      it 'ステータスコード200 OKを返す' do
+        get :show, :id => @sbt.id
+        response.should be_success 
+      end
+    end
+  end
+  
+else
+  describe '一覧表示に於いて' do
+    before do
+      sign_in @user
+      SpeechBalloonTemplate.stub(:list).and_return([@sbt, @sbt, @sbt])
+    end
+    context 'つつがなく終わるとき' do
+      context 'html形式' do
+        it 'ステータスコード200 OKを返す' do
+          get :index
+          response.should be_success 
+        end
+        it 'indexテンプレートを描画する' do
+          get :index
+          response.should render_template("index")
+        end
+      end
+      context 'json形式' do
+        it 'ステータスコード200 OKを返す' do
+          get :index, :format => :json
+          response.should be_success 
+        end
+        it 'jsonデータを返す' do
+          get :index, :format => :json
+          lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
+        end
+      end
+    end
+    context 'ユーザ権限がないとき' do
+      before do
+        sign_out @user
+      end
+      context 'html形式' do
+        it 'ステータスコード200 OKを返す' do
+          get :index
+          response.should be_success 
+        end
+        it 'indexテンプレートを描画する' do
+          get :index
+          response.should render_template("index")
+        end
+      end
+      context 'json形式' do
+        it 'ステータスコード200 OKを返す' do
+          get :index, :format => :json
+          response.should be_success 
+        end
+        it 'jsonデータを返す' do
+          get :index, :format => :json
+          lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
+        end
+      end
+    end
+  end
+  
+  describe '単体表示に於いて' do
+    before do
+      sign_in @user
+      SpeechBalloonTemplate.stub(:show).with(@sbt.id.to_s, [nil, nil]).and_return(@sbt)
+      SpeechBalloonTemplate.stub(:show).with(@sbt.id.to_s, [@user, nil]).and_return(@sbt)
+      SpeechBalloonTemplate.stub(:show).with(@sbt.id.to_s, [nil, @admin]).and_return(@sbt)
+    end
+    context 'つつがなく終わるとき' do
+      context 'html形式' do
+        it 'ステータスコード200 OKを返す' do
+          get :show, :id => @sbt.id
+          response.should be_success
+        end
+        it 'showテンプレートを描画する' do
+          get :show, :id => @sbt.id
+          response.should render_template("show")
+        end
+      end
+      context 'json形式' do
+        it 'ステータスコード200 OKを返す' do
+          get :show, :id => @sbt.id, :format => :json
+          response.should be_success
+        end
+        it 'jsonデータを返す' do
+          get :show, :id => @sbt.id, :format => :json
+          lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
+        end
+      end
+    end
+    context 'ユーザ権限がないとき' do
+      before do
+        sign_out @user
+      end
+      context 'html形式' do
+        it 'ステータスコード200 OKを返す' do
+          get :show, :id => @sbt.id
+          response.should be_success
+        end
+        it 'showテンプレートを描画する' do
+          get :show, :id => @sbt.id
+          response.should render_template("show")
+        end
+      end
+      context 'json形式' do
+        it 'ステータスコード200 OKを返す' do
+          get :show, :id => @sbt.id, :format => :json
+          response.should be_success
+        end
+        it 'jsonデータを返す' do
+          get :show, :id => @sbt.id, :format => :json
+          lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
+        end
+      end
+    end
   end
   
+end
 
 end