OSDN Git Service

t#30473:fix authenticate
[pettanr/pettanr.git] / spec / controllers / system_pictures_controller_spec.rb
index 383f24d..0db0485 100644 (file)
+# -*- encoding: utf-8 -*-
+#システム画像
 require 'spec_helper'
 
-# This spec was generated by rspec-rails when you ran the scaffold generator.
-# It demonstrates how one might use RSpec to specify the controller code that
-# was generated by Rails when you ran the scaffold generator.
-#
-# It assumes that the implementation code is generated by the rails scaffold
-# generator.  If you are using any extension libraries to generate different
-# controller code, this generated spec may or may not pass.
-#
-# It only uses APIs available in rails and/or rspec-rails.  There are a number
-# of tools you can use to make these specs even more expressive, but we're
-# sticking to rails and rspec-rails APIs to keep things simple and stable.
-#
-# Compared to earlier versions of this generator, there is very limited use of
-# stubs and message expectations in this spec.  Stubs are only used when there
-# is no simpler way to get a handle on the object needed for the example.
-# Message expectations are only used when there is no simpler way to specify
-# that an instance is receiving a specific message.
-
 describe SystemPicturesController do
-
-  # This should return the minimal set of attributes required to create a valid
-  # SystemPicture. As you add validations to SystemPicture, be sure to
-  # update the return value of this method accordingly.
-  def valid_attributes
-    {}
+  before do
+    @admin = FactoryGirl.create :admin
+    @demand_user = FactoryGirl.create :demand_user
+    @user = FactoryGirl.create( :user_yas)
+    @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
+    @license = FactoryGirl.create :license, :license_group_id => @lg.id, :system_picture_id => @sp.id
   end
-
-  describe "GET index" do
-    it "assigns all system_pictures as @system_pictures" do
-      system_picture = SystemPicture.create! valid_attributes
-      get :index
-      assigns(:system_pictures).should eq([system_picture])
+  
+if MagicNumber['run_mode'] == 1
+  describe '一覧表示に於いて' do
+    before do
+      sign_in @user
+      SystemPicture.stub(:list).and_return([@sp, @sp, @sp])
     end
-  end
-
-  describe "GET show" do
-    it "assigns the requested system_picture as @system_picture" do
-      system_picture = SystemPicture.create! valid_attributes
-      get :show, :id => system_picture.id
-      assigns(:system_picture).should eq(system_picture)
+    context 'パラメータpageについて' do
+      it '@pageに値が入る' do
+        get :index, :page => 5
+        assigns(:page).should eq 5
+      end
+      it '省略されると@pageに1値が入る' do
+        get :index
+        assigns(:page).should eq 1
+      end
+      it '与えられたpage_sizeがセットされている' do
+        get :index, :page_size => 15
+        assigns(:page_size).should eq 15
+      end
+      it '省略されると@page_sizeにデフォルト値が入る' do
+        get :index
+        assigns(:page_size).should eq SystemPicture.default_page_size
+      end
+      it '最大を超えると@page_sizeにデフォルト最大値が入る' do
+        get :index, :page_size => 1500
+        assigns(:page_size).should eq SystemPicture.max_page_size
+      end
+      it '不正な値が入ると@page_sizeにデフォルト最大値が入る' do
+        get :index, :page_size => 0
+        assigns(:page_size).should eq SystemPicture.default_page_size
+      end
     end
-  end
-
-  describe "GET new" do
-    it "assigns a new system_picture as @system_picture" do
-      get :new
-      assigns(:system_picture).should be_a_new(SystemPicture)
+    context 'つつがなく終わるとき' do
+      it 'システム画像モデルに一覧を問い合わせている' do
+        SystemPicture.should_receive(:list).exactly(1)
+        get :index
+      end
+      it '@system_picturesにリストを取得している' do
+        get :index
+        assigns(:system_pictures).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
+          SystemPicture.should_receive(:list_json_opt).exactly(1)
+          get :index, :format => :json
+        end
+        it 'データがリスト構造になっている' do
+          get :index, :format => :json
+          json = JSON.parse response.body
+          json.should have_at_least(3).items
+        end
+        it 'リストの先頭くらいはシステム画像っぽいものであって欲しい' do
+          get :index, :format => :json
+          json = JSON.parse response.body
+          json.first.has_key?("ext").should be_true
+          json.first.has_key?("md5").should be_true
+        end
+      end
     end
-  end
-
-  describe "GET edit" do
-    it "assigns the requested system_picture as @system_picture" do
-      system_picture = SystemPicture.create! valid_attributes
-      get :edit, :id => system_picture.id
-      assigns(:system_picture).should eq(system_picture)
+    context '作家権限がないとき' do
+      before do
+        sign_out @user
+      end
+      context 'html形式' do
+        it 'ステータスコード302 Foundを返す' do
+          get :index
+          response.status.should eq 302
+        end
+        it 'サインインページへ遷移する' do
+          get :index
+          response.should redirect_to '/users/sign_in'
+        end
+      end
+      context 'json形式' do
+        it 'ステータスコード401 Unauthorizedを返す' do
+          get :index, :format => :json
+          response.status.should eq 401
+        end
+        it '応答メッセージにUnauthorizedを返す' do
+          get :index, :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 :index
+        response.should be_success 
+      end
+    end
+    context '作家権限はないが借手権限があるとき' do
+      before do
+        sign_out @user
+        sign_in @demand_user
+      end
+      it 'ステータスコード200 OKを返す' do
+        get :index
+        response.should be_success 
+      end
     end
   end
-
-  describe "POST create" do
-    describe "with valid params" do
-      it "creates a new SystemPicture" do
-        expect {
-          post :create, :system_picture => valid_attributes
-        }.to change(SystemPicture, :count).by(1)
+  
+  describe '単体表示に於いて' do
+    before do
+      sign_in @user
+      SystemPicture.stub(:show).and_return(@sp)
+    end
+    context 'つつがなく終わるとき' do
+      it 'ステータスコード200 OKを返す' do
+        get :show, :id => @sp.id
+        response.should be_success
       end
-
-      it "assigns a newly created system_picture as @system_picture" do
-        post :create, :system_picture => valid_attributes
-        assigns(:system_picture).should be_a(SystemPicture)
-        assigns(:system_picture).should be_persisted
+      it 'システム画像モデルに単体取得を問い合わせている' do
+        SystemPicture.should_receive(:show).exactly(1)
+        get :show
       end
-
-      it "redirects to the created system_picture" do
-        post :create, :system_picture => valid_attributes
-        response.should redirect_to(SystemPicture.last)
+      it '@system_pictureにアレを取得している' do
+        get :show, :id => @sp.id
+        assigns(:system_picture).should eq @sp
+      end
+      context 'html形式' do
+        it 'showテンプレートを描画する' do
+          get :show, :id => @sp.id
+          response.should render_template("show")
+        end
+      end
+      context 'json形式' do
+        it 'jsonデータを返す' do
+          get :show, :id => @sp.id, :format => :json
+          lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
+        end
+        it 'システム画像モデルにjson単体出力オプションを問い合わせている' do
+          SystemPicture.should_receive(:show_json_opt).exactly(1)
+          get :show, :id => @sp.id, :format => :json
+        end
+        it 'データがアレになっている' do
+          get :show, :id => @sp.id, :format => :json
+          json = JSON.parse response.body
+          json["ext"].should match(/png/)
+          json["md5"].should be_true
+          json["width"].should be_true
+        end
+      end
+      #画像送信では、send_dataにスタブをおいてテストしたいが、ここに噛ませると
+      #renderが働かず、エラーとなってしまう。そこで、システム画像のファイル取得部分に
+      #スタブをおいてsend_dataがデータを返す体裁でテストする。
+      context 'png形式' do
+        before do
+          SystemPicture.any_instance.stub(:mime_type).and_return('image/png')
+          SystemPicture.any_instance.stub(:restore).and_return('aaa')
+        end
+        it '画像モデルに画像データを問い合わせる' do
+          SystemPicture.any_instance.should_receive(:restore).exactly(1)
+          get :show, :id => @sp.id, :format => :png
+        end
+        it '画像モデルにMimeTypeを問い合わせる' do
+          SystemPicture.any_instance.should_receive(:mime_type).exactly(1)
+          get :show, :id => @sp.id, :format => :png
+        end
+        it '画像を送信する' do
+          get :show, :id => @sp.id, :format => :png
+          response.body.should eq 'aaa'
+        end
+      end
+      context 'gif形式' do
+        before do
+          SystemPicture.any_instance.stub(:mime_type).and_return('image/gif')
+          SystemPicture.any_instance.stub(:restore).and_return('bbb')
+        end
+        it '画像モデルに画像データを問い合わせる' do
+          SystemPicture.any_instance.should_receive(:restore).exactly(1)
+          get :show, :id => @sp.id, :format => :gif
+        end
+        it '画像モデルにMimeTypeを問い合わせる' do
+          SystemPicture.any_instance.should_receive(:mime_type).exactly(1)
+          get :show, :id => @sp.id, :format => :gif
+        end
+        it '画像を送信する' do
+          get :show, :id => @sp.id, :format => :gif
+          response.body.should eq 'bbb'
+        end
+      end
+      context 'jpeg形式' do
+        before do
+          SystemPicture.any_instance.stub(:mime_type).and_return('image/jpeg')
+          SystemPicture.any_instance.stub(:restore).and_return('ccc')
+        end
+        it '画像モデルに画像データを問い合わせる' do
+          SystemPicture.any_instance.should_receive(:restore).exactly(1)
+          get :show, :id => @sp.id, :format => :jpeg
+        end
+        it '画像モデルにMimeTypeを問い合わせる' do
+          SystemPicture.any_instance.should_receive(:mime_type).exactly(1)
+          get :show, :id => @sp.id, :format => :jpeg
+        end
+        it '画像を送信する' do
+          get :show, :id => @sp.id, :format => :jpeg
+          response.body.should eq 'ccc'
+        end
       end
     end
-
-    describe "with invalid params" do
-      it "assigns a newly created but unsaved system_picture as @system_picture" do
-        # Trigger the behavior that occurs when invalid params are submitted
-        SystemPicture.any_instance.stub(:save).and_return(false)
-        post :create, :system_picture => {}
-        assigns(:system_picture).should be_a_new(SystemPicture)
+    context '作家権限がないとき' do
+      before do
+        sign_out @user
       end
-
-      it "re-renders the 'new' template" do
-        # Trigger the behavior that occurs when invalid params are submitted
-        SystemPicture.any_instance.stub(:save).and_return(false)
-        post :create, :system_picture => {}
-        response.should render_template("new")
+      context 'html形式' do
+        it 'ステータスコード302 Foundを返す' do
+          get :show, :id => @sp.id
+          response.status.should eq 302
+        end
+        it 'サインインページへ遷移する' do
+          get :show, :id => @sp.id
+          response.body.should redirect_to '/users/sign_in'
+        end
+      end
+      context 'json形式' do
+        it 'ステータスコード401 Unauthorizedを返す' do
+          get :show, :id => @sp.id, :format => :json
+          response.status.should eq 401
+        end
+        it '応答メッセージにUnauthorizedを返す' do
+          get :show, :id => @sp.id, :format => :json
+          response.message.should match(/Unauthorized/)
+        end
       end
     end
-  end
-
-  describe "PUT update" do
-    describe "with valid params" do
-      it "updates the requested system_picture" do
-        system_picture = SystemPicture.create! valid_attributes
-        # Assuming there are no other system_pictures in the database, this
-        # specifies that the SystemPicture created on the previous line
-        # receives the :update_attributes message with whatever params are
-        # submitted in the request.
-        SystemPicture.any_instance.should_receive(:update_attributes).with({'these' => 'params'})
-        put :update, :id => system_picture.id, :system_picture => {'these' => 'params'}
+    context '作家権限はないが管理者権限があるとき' do
+      before do
+        sign_out @user
+        sign_in @admin
       end
-
-      it "assigns the requested system_picture as @system_picture" do
-        system_picture = SystemPicture.create! valid_attributes
-        put :update, :id => system_picture.id, :system_picture => valid_attributes
-        assigns(:system_picture).should eq(system_picture)
+      it 'ステータスコード200 OKを返す' do
+        get :show, :id => @sp.id
+        response.should be_success 
       end
-
-      it "redirects to the system_picture" do
-        system_picture = SystemPicture.create! valid_attributes
-        put :update, :id => system_picture.id, :system_picture => valid_attributes
-        response.should redirect_to(system_picture)
+    end
+    context '作家権限はないが借手権限があるとき' do
+      before do
+        sign_out @user
+        sign_in @demand_user
+      end
+      it 'ステータスコード200 OKを返す' do
+        get :show, :id => @sp.id
+        response.should be_success 
       end
     end
-
-    describe "with invalid params" do
-      it "assigns the system_picture as @system_picture" do
-        system_picture = SystemPicture.create! valid_attributes
-        # Trigger the behavior that occurs when invalid params are submitted
-        SystemPicture.any_instance.stub(:save).and_return(false)
-        put :update, :id => system_picture.id, :system_picture => {}
-        assigns(:system_picture).should eq(system_picture)
+=begin
+    context '対象システム画像がないとき' do
+      before do
+        SystemPicture.unstub(:show)
       end
-
-      it "re-renders the 'edit' template" do
-        system_picture = SystemPicture.create! valid_attributes
-        # Trigger the behavior that occurs when invalid params are submitted
-        SystemPicture.any_instance.stub(:save).and_return(false)
-        put :update, :id => system_picture.id, :system_picture => {}
-        response.should render_template("edit")
+      context 'html形式' do
+        it '例外404 not_foundを返す' do
+          lambda{
+            get :show, :id => 0
+          }.should raise_error(ActiveRecord::RecordNotFound)
+        end
+      end
+      context 'json形式' do
+        it '例外404 not_foundを返す' do
+          lambda{ 
+            get :show, :id => 0, :format => :json
+          }.should raise_error(ActiveRecord::RecordNotFound)
+        end
       end
     end
+    context '他人のシステム画像を見ようとしたとき' do
+      before do
+        SystemPicture.stub(:show).and_return(@sp)
+        SystemPicture.any_instance.stub(:own?).with(any_args()).and_return(false)
+      end
+      context 'html形式' do
+        it '例外403 forbiddenを返す' do
+          lambda{
+            get :show, :id => @sp.id
+          }.should raise_error(ActiveRecord::Forbidden)
+        end
+      end
+      context 'json形式' do
+        it '例外403 forbiddenを返す' do
+          lambda{
+            get :show, :id => @sp.id, :format => :json
+          }.should raise_error(ActiveRecord::Forbidden)
+        end
+      end
+    end
+=end
   end
-
-  describe "DELETE destroy" do
-    it "destroys the requested system_picture" do
-      system_picture = SystemPicture.create! valid_attributes
-      expect {
-        delete :destroy, :id => system_picture.id
-      }.to change(SystemPicture, :count).by(-1)
+  
+else
+  describe '一覧表示に於いて' do
+    before do
+      sign_in @user
+      sign_in @admin
+      SystemPicture.stub(:list).and_return([@sp, @sp, @sp])
     end
-
-    it "redirects to the system_pictures list" do
-      system_picture = SystemPicture.create! valid_attributes
-      delete :destroy, :id => system_picture.id
-      response.should redirect_to(system_pictures_url)
+    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
+      SystemPicture.stub(:show).and_return(@sp)
+    end
+    context 'つつがなく終わるとき' do
+      it 'ステータスコード200 OKを返す' do
+        get :show, :id => @sp.id
+        response.should be_success
+      end
+      context 'html形式' do
+        it 'showテンプレートを描画する' do
+          get :show, :id => @sp.id
+          response.should render_template("show")
+        end
+      end
+      context 'json形式' do
+        it 'jsonデータを返す' do
+          get :show, :id => @sp.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
+      it 'ステータスコード200 OKを返す' do
+        get :show, :id => @sp.id
+        response.should be_success
+      end
+      context 'html形式' do
+        it 'showテンプレートを描画する' do
+          get :show, :id => @sp.id
+          response.should render_template("show")
+        end
+      end
+      context 'json形式' do
+        it 'jsonデータを返す' do
+          get :show, :id => @sp.id, :format => :json
+          lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
+        end
+      end
+    end
+  end
+end
 end