OSDN Git Service

top page moved
[pettanr/pettanr.git] / spec / controllers / licenses_controller_spec.rb
index f12c704..3d36e6b 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 LicensesController do
-
-  # This should return the minimal set of attributes required to create a valid
-  # License. As you add validations to License, be sure to
-  # update the return value of this method accordingly.
-  def valid_attributes
-    {}
+  before do
+    @admin = FactoryGirl.create :admin
+    @lg = FactoryGirl.create :license_group
+    @sp = FactoryGirl.create :system_picture
+    @user = FactoryGirl.create( :user_yas)
+    @author = FactoryGirl.create :author, :user_id => @user.id
+    @artist = FactoryGirl.create :artist_yas, :author_id => @author.id
   end
 
-  describe "GET index" do
-    it "assigns all licenses as @licenses" do
-      license = License.create! valid_attributes
-      get :index
-      assigns(:licenses).should eq([license])
+if MagicNumber['run_mode'] == 1
+  describe '一覧表示に於いて' do
+    before do
+      @l = FactoryGirl.create :license, :license_group_id => @lg.id, :system_picture_id => @sp.id
+      sign_in @user
+      License.stub(:list).and_return([@l, @l, @l])
     end
-  end
-
-  describe "GET show" do
-    it "assigns the requested license as @license" do
-      license = License.create! valid_attributes
-      get :show, :id => license.id
-      assigns(:license).should eq(license)
+    context 'つつがなく終わるとき' do
+      it 'ステータスコード200 OKを返す' do
+        get :index
+        response.should be_success 
+      end
+      it 'ライセンスモデルに一覧を問い合わせている' do
+        License.should_receive(:list).exactly(1)
+        get :index
+      end
+      it '@licensesにリストを取得している' do
+        get :index
+        assigns(:licenses).should have_at_least(3).items
+      end
+      context 'html形式' do
+        it 'indexテンプレートを描画する' do
+          get :index
+          response.should render_template("index")
+        end
+      end
+      context 'json形式' do
+        it 'jsonデータを返す' do
+          get :index, :format => :json
+          lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
+        end
+        it 'ライセンスモデルにjson一覧出力オプションを問い合わせている' do
+          License.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?("name").should be_true
+          json.first.has_key?("caption").should be_true
+          json.first.has_key?("url").should be_true
+        end
+      end
     end
-  end
-
-  describe "GET new" do
-    it "assigns a new license as @license" do
-      get :new
-      assigns(:license).should be_a_new(License)
+    context '作家権限がないとき' do
+      before do
+        sign_out @user
+      end
+      it 'ステータスコード200 okを返す' do
+        get :index
+        response.status.should eq 200
+      end
     end
   end
-
-  describe "GET edit" do
-    it "assigns the requested license as @license" do
-      license = License.create! valid_attributes
-      get :edit, :id => license.id
-      assigns(:license).should eq(license)
+  
+  describe '単体表示に於いて' do
+    before do
+      sign_in @user
+      @l = FactoryGirl.create :license, :license_group_id => @lg.id, :system_picture_id => @sp.id
+      License.stub(:show).and_return(@l)
+    end
+    context 'つつがなく終わるとき' do
+      it 'ステータスコード200 OKを返す' do
+        get :show, :id => @l.id
+        response.should be_success
+      end
+      it 'ライセンスモデルに単体取得を問い合わせている' do
+        License.should_receive(:show).exactly(1)
+        get :show
+      end
+      it '@licenseにアレを取得している' do
+        get :show, :id => @l.id
+        assigns(:license).id.should eq(@l.id)
+      end
+      context 'html形式' do
+        it 'showテンプレートを描画する' do
+          get :show, :id => @l.id
+          response.should render_template("show")
+        end
+      end
+      context 'json形式' do
+        it 'jsonデータを返す' do
+          get :show, :id => @l.id, :format => :json
+          lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
+        end
+        it 'ライセンスモデルにjson単体出力オプションを問い合わせている' do
+          License.should_receive(:show_json_opt).exactly(1)
+          get :show, :id => @l.id, :format => :json
+        end
+        it 'データがアレになっている' do
+          get :show, :id => @l.id, :format => :json
+          json = JSON.parse response.body
+          json["name"].should match(/peta/)
+          json["caption"].should_not be_nil
+          json["url"].should_not be_nil
+        end
+      end
+    end
+    context '作家権限がないとき' do
+      before do
+        sign_out @user
+      end
+      it 'ステータスコード200 okを返す' do
+        get :show, :id => @l.id
+        response.status.should eq 200
+      end
     end
   end
-
-  describe "POST create" do
-    describe "with valid params" do
-      it "creates a new License" do
-        expect {
-          post :create, :license => valid_attributes
-        }.to change(License, :count).by(1)
+  
+  describe '管理名検索の一覧に於いて' do
+    before do
+      @l = FactoryGirl.create :license, :license_group_id => @lg.id, :system_picture_id => @sp.id
+      sign_in @user
+      License.stub(:list_by_name).with(any_args).and_return([@l])
+    end
+    context 'つつがなく終わるとき' do
+      it 'ライセンスモデルに管理名検索を問い合わせている' do
+        License.should_receive(:list_by_name).exactly(1)
+        get :search, :name => @l.name
       end
-
-      it "assigns a newly created license as @license" do
-        post :create, :license => valid_attributes
-        assigns(:license).should be_a(License)
-        assigns(:license).should be_persisted
+      it '@licensesにリストを取得している' do
+        get :search, :name => @l.name
+        assigns(:licenses).should have_at_least(1).items
       end
-
-      it "redirects to the created license" do
-        post :create, :license => valid_attributes
-        response.should redirect_to(License.last)
+      context 'html形式' do
+        it 'ステータスコード200 OKを返す' do
+          get :search, :name => @l.name
+          response.should be_success 
+        end
+        it 'searchテンプレートを描画する' do
+          get :search, :name => @l.name
+          response.should render_template("search")
+        end
+      end
+      context 'json形式' do
+        it 'ステータスコード200 OKを返す' do
+          get :search, :name => @l.name, :format => :json
+          response.should be_success 
+        end
+        it 'jsonデータを返す' do
+          get :search, :name => @l.name, :format => :json
+          lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
+        end
+        it 'データがリスト構造になっている' do
+          get :search, :name => @l.name, :format => :json
+          json = JSON.parse response.body
+          json.should have_at_least(1).items
+        end
+        it 'リストの先頭くらいはライセンスっぽいものであって欲しい' do
+          get :search, :name => @l.name, :format => :json
+          json = JSON.parse response.body
+          json.first.has_key?("name").should be_true
+          json.first.has_key?("caption").should be_true
+          json.first.has_key?("url").should be_true
+        end
       end
     end
-
-    describe "with invalid params" do
-      it "assigns a newly created but unsaved license as @license" do
-        # Trigger the behavior that occurs when invalid params are submitted
-        License.any_instance.stub(:save).and_return(false)
-        post :create, :license => {}
-        assigns(:license).should be_a_new(License)
+    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
-        License.any_instance.stub(:save).and_return(false)
-        post :create, :license => {}
-        response.should render_template("new")
+      it 'ステータスコード200 okを返す' do
+        get :index
+        response.status.should eq 200
       end
     end
   end
-
-  describe "PUT update" do
-    describe "with valid params" do
-      it "updates the requested license" do
-        license = License.create! valid_attributes
-        # Assuming there are no other licenses in the database, this
-        # specifies that the License created on the previous line
-        # receives the :update_attributes message with whatever params are
-        # submitted in the request.
-        License.any_instance.should_receive(:update_attributes).with({'these' => 'params'})
-        put :update, :id => license.id, :license => {'these' => 'params'}
+  
+else
+  describe '一覧表示に於いて' do
+    before do
+      @l = FactoryGirl.create :license, :license_group_id => @lg.id, :system_picture_id => @sp.id
+      sign_in @user
+      License.stub(:list).and_return([@l, @l, @l])
+    end
+      context 'つつがなく終わるとき' do
+      it 'ステータスコード200 OKを返す' do
+        get :index
+        response.should be_success 
       end
-
-      it "assigns the requested license as @license" do
-        license = License.create! valid_attributes
-        put :update, :id => license.id, :license => valid_attributes
-        assigns(:license).should eq(license)
+      context 'html形式' do
+        it 'indexテンプレートを描画する' do
+          get :index
+          response.should render_template("index")
+        end
       end
-
-      it "redirects to the license" do
-        license = License.create! valid_attributes
-        put :update, :id => license.id, :license => valid_attributes
-        response.should redirect_to(license)
+      context 'json形式' do
+        it 'jsonデータを返す' do
+          get :index, :format => :json
+          lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
+        end
       end
     end
-
-    describe "with invalid params" do
-      it "assigns the license as @license" do
-        license = License.create! valid_attributes
-        # Trigger the behavior that occurs when invalid params are submitted
-        License.any_instance.stub(:save).and_return(false)
-        put :update, :id => license.id, :license => {}
-        assigns(:license).should eq(license)
+    context '作家権限がないとき' do
+      before do
+        sign_out @user
       end
-
-      it "re-renders the 'edit' template" do
-        license = License.create! valid_attributes
-        # Trigger the behavior that occurs when invalid params are submitted
-        License.any_instance.stub(:save).and_return(false)
-        put :update, :id => license.id, :license => {}
-        response.should render_template("edit")
+      it 'ステータスコード200 okを返す' do
+        get :index
+        response.status.should eq 200
+      end
+      context 'html形式' do
+        it 'indexテンプレートを描画する' do
+          get :index
+          response.should render_template("index")
+        end
+      end
+      context 'json形式' do
+        it 'jsonデータを返す' do
+          get :index, :format => :json
+          lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
+        end
       end
     end
   end
-
-  describe "DELETE destroy" do
-    it "destroys the requested license" do
-      license = License.create! valid_attributes
-      expect {
-        delete :destroy, :id => license.id
-      }.to change(License, :count).by(-1)
+  
+  describe '単体表示に於いて' do
+    before do
+      sign_in @user
+      @l = FactoryGirl.create :license, :license_group_id => @lg.id, :system_picture_id => @sp.id
+      License.stub(:show).and_return(@l)
     end
-
-    it "redirects to the licenses list" do
-      license = License.create! valid_attributes
-      delete :destroy, :id => license.id
-      response.should redirect_to(licenses_url)
+    context 'つつがなく終わるとき' do
+      it 'ステータスコード200 OKを返す' do
+        get :show, :id => @l.id
+        response.should be_success
+      end
+      context 'html形式' do
+        it 'showテンプレートを描画する' do
+          get :show, :id => @l.id
+          response.should render_template("show")
+        end
+      end
+      context 'json形式' do
+        it 'jsonデータを返す' do
+          get :show, :id => @l.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 => @l.id
+        response.status.should eq 200
+      end
+      context 'html形式' do
+        it 'showテンプレートを描画する' do
+          get :show, :id => @l.id
+          response.should render_template("show")
+        end
+      end
+      context 'json形式' do
+        it 'jsonデータを返す' do
+          get :show, :id => @l.id, :format => :json
+          lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
+        end
+      end
     end
   end
-
+  
+end
 end