OSDN Git Service

t#31223:add them contents list for author and artist
[pettanr/pettanr.git] / spec / controllers / artists_controller_spec.rb
index ea5123d..f36d754 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 ArtistsController do
-
-  # This should return the minimal set of attributes required to create a valid
-  # Artist. As you add validations to Artist, 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
+    @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
+    @artist = FactoryGirl.create :artist_yas, :author_id => @author.id
   end
-
-  describe "GET index" do
-    it "assigns all artists as @artists" do
-      artist = Artist.create! valid_attributes
-      get :index
-      assigns(:artists).should eq([artist])
+  
+if MagicNumber['run_mode'] == 1
+  describe '一覧表示に於いて' do
+    before do
+      Artist.stub(:list).and_return([@artist, @artist, @artist])
+      sign_in @user
+    end
+    context '事前チェックする' 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 Artist.default_page_size
+      end
+      it '最大を超えると@page_sizeにデフォルト最大値が入る' do
+        get :index, :page_size => 1500
+        assigns(:page_size).should eq Artist.max_page_size
+      end
+      it '不正な値が入ると@page_sizeにデフォルト最大値が入る' do
+        get :index, :page_size => 0
+        assigns(:page_size).should eq Artist.default_page_size
+      end
+    end
+    context 'つつがなく終わるとき' do
+      it 'ステータスコード200 OKを返す' do
+        get :index
+        response.should be_success 
+      end
+      it '絵師モデルに一覧を問い合わせている' do
+        Artist.should_receive(:list).exactly(1)
+        get :index
+      end
+      it '@artistsにリストを取得している' do
+        get :index
+        assigns(:artists).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
+          Artist.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
+        end
+      end
+    end
+    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
+    context 'ユーザだが作家登録していないとき' do
+      before do
+        @author.destroy
+      end
+      it 'ステータスコード200 OKを返す' do
+        get :index
+        response.should be_success 
+      end
     end
   end
-
-  describe "GET show" do
-    it "assigns the requested artist as @artist" do
-      artist = Artist.create! valid_attributes
-      get :show, :id => artist.id
-      assigns(:artist).should eq(artist)
+  
+  describe '閲覧に於いて' do
+    before do
+      Artist.stub(:show).and_return(@artist)
+      sign_in @user
     end
+    context 'つつがなく終わるとき' do
+      it 'ステータスコード200 OKを返す' do
+        get :show, :id => @artist.id
+        response.should be_success
+      end
+      it '絵師モデルに単体取得を問い合わせている' do
+        Artist.should_receive(:show).exactly(1)
+        get :show
+      end
+      it '@arにアレを取得している' do
+        get :show, :id => @artist.id
+        assigns(:ar).should eq(@artist)
+      end
+      context 'html形式' do
+        it 'showテンプレートを描画する' do
+          get :show, :id => @artist.id
+          response.should render_template("show")
+        end
+      end
+      context 'json形式' do
+        it 'jsonデータを返す' do
+          get :show, :id => @artist.id, :format => :json
+          lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
+        end
+        it '絵師モデルにjson単体出力オプションを問い合わせている' do
+          Artist.should_receive(:show_json_opt).exactly(1)
+          get :show, :id => @artist.id, :format => :json
+        end
+        it 'データがアレになっている' do
+          get :show, :id => @artist.id, :format => :json
+          json = JSON.parse response.body
+          json["name"].should match(/yas/)
+        end
+      end
+    end
+    context 'ユーザ権限がないとき' do
+      before do
+        sign_out @user
+      end
+      context 'html形式' do
+        it 'ステータスコード302 Foundを返す' do
+          get :show, :id => @artist.id
+          response.status.should eq 302
+        end
+        it 'サインインページへ遷移する' do
+          get :show, :id => @artist.id
+          response.body.should redirect_to '/users/sign_in'
+        end
+      end
+      context 'json形式' do
+        it 'ステータスコード401 Unauthorizedを返す' do
+          get :show, :id => @artist.id, :format => :json
+          response.status.should eq 401
+        end
+        it '応答メッセージにUnauthorizedを返す' do
+          get :show, :id => @artist.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 => @artist.id
+        response.should be_success 
+      end
+    end
+    context 'ユーザ権限はないが借手権限があるとき' do
+      before do
+        sign_out @user
+        sign_in @demand_user
+      end
+      it 'ステータスコード200 OKを返す' do
+        get :show, :id => @artist.id
+        response.should be_success 
+      end
+    end
+    context 'ユーザだが作家登録していないとき' do
+      before do
+        @author.destroy
+      end
+      it 'ステータスコード200 OKを返す' do
+        get :show, :id => @artist.id
+        response.should be_success 
+      end
+    end
+=begin
+    context '対象作家がないとき' do
+      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
+=end
   end
-
-  describe "GET new" do
-    it "assigns a new artist as @artist" do
-      get :new
-      assigns(:artist).should be_a_new(Artist)
+  
+  describe '対象絵師の素材一覧表示に於いて' do
+    before do
+      @other_user = FactoryGirl.create( :user_yas)
+      @other_author = FactoryGirl.create :author, :user_id => @other_user.id
+      @other_artist = FactoryGirl.create :artist_yas, :author_id => @other_author.id
+      @op = FactoryGirl.create :original_picture, :artist_id => @other_artist.id
+      @p = FactoryGirl.create :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @other_artist.id
+      @rp = FactoryGirl.create :resource_picture, :artist_id => @other_artist.id, :license_id => @license.id, :original_picture_id => @op.id, :picture_id => @p.id
+      ResourcePicture.stub(:mylist).and_return([@rp, @rp, @rp])
+      sign_in @user
+    end
+    context 'パラメータpageについて' do
+      it '@pageに値が入る' do
+        get :resource_pictures, :id => @other_artist.id, :page => 5
+        assigns(:page).should eq 5
+      end
+      it '省略されると@pageに1値が入る' do
+        get :resource_pictures, :id => @other_artist.id
+        assigns(:page).should eq 1
+      end
+      it '与えられたpage_sizeがセットされている' do
+        get :resource_pictures, :id => @other_artist.id, :page_size => 15
+        assigns(:page_size).should eq 15
+      end
+      it '省略されると@page_sizeにデフォルト値が入る' do
+        get :resource_pictures, :id => @other_artist.id
+        assigns(:page_size).should eq Author.default_resource_picture_page_size
+      end
+      it '最大を超えると@page_sizeにデフォルト最大値が入る' do
+        get :resource_pictures, :id => @other_artist.id, :page_size => 1500
+        assigns(:page_size).should eq Author.resource_picture_max_page_size
+      end
+      it '不正な値が入ると@page_sizeにデフォルト最大値が入る' do
+        get :resource_pictures, :id => @other_artist.id, :page_size => 0
+        assigns(:page_size).should eq Author.default_resource_picture_page_size
+      end
+    end
+    context 'つつがなく終わるとき' do
+      it 'ステータスコード200 OKを返す' do
+        get :resource_pictures, :id => @other_artist.id
+        response.should be_success 
+      end
+      it '絵師モデルに単体取得を問い合わせている' do
+        Artist.should_receive(:show).exactly(1)
+        get :resource_pictures, :id => @other_artist.id
+      end
+      it '素材モデルに一覧を問い合わせている' do
+        ResourcePicture.should_receive(:mylist).exactly(1)
+        get :resource_pictures, :id => @other_artist.id
+      end
+      it '@resource_picturesにリストを取得している' do
+        get :resource_pictures, :id => @other_artist.id
+        assigns(:resource_pictures).should have_at_least(3).items
+      end
+      context 'html形式' do
+        it 'resource_pictureテンプレートを描画する' do
+          get :resource_pictures, :id => @other_artist.id
+          response.should render_template("resource_pictures")
+        end
+      end
+      context 'json形式' do
+        it 'jsonデータを返す' do
+          get :resource_pictures, :id => @other_artist.id, :format => :json
+          lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
+        end
+        it '素材モデルにコマリストのjson出力を問い合わせている' do
+          ResourcePicture.should_receive(:list_json_opt).exactly(1)
+          get :resource_pictures, :id => @other_artist.id, :format => :json
+        end
+        it 'データがリスト構造になっている' do
+          get :resource_pictures, :id => @other_artist.id, :format => :json
+          json = JSON.parse response.body
+          json.should have_at_least(3).items
+        end
+        it 'リストの先頭くらいは素材っぽいものであって欲しい' do
+          get :resource_pictures, :id => @other_artist.id, :format => :json
+          json = JSON.parse response.body
+          json.first.has_key?("original_picture_id").should be_true
+          json.first.has_key?("license_id").should be_true
+        end
+      end
+    end
+    context 'ユーザ権限がないとき' do
+      before do
+        sign_out @user
+      end
+      context 'html形式' do
+        it 'ステータスコード302 Foundを返す' do
+          get :resource_pictures, :id => @other_artist.id
+          response.status.should eq 302
+        end
+        it 'サインインページへ遷移する' do
+          get :resource_pictures, :id => @other_artist.id
+          response.should redirect_to '/users/sign_in'
+        end
+      end
+      context 'json形式' do
+        it 'ステータスコード401 Unauthorizedを返す' do
+          get :resource_pictures, :id => @other_artist.id, :format => :json
+          response.status.should eq 401
+        end
+        it '応答メッセージにUnauthorizedを返す' do
+          get :resource_pictures, :id => @other_artist.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 :resource_pictures, :id => @other_artist.id
+        response.should be_success 
+      end
+    end
+    context 'ユーザだが作家登録していないとき' do
+      before do
+        @artist.destroy
+      end
+      it 'ステータスコード200 OKを返す' do
+        get :resource_pictures, :id => @other_artist.id
+        response.should be_success 
+      end
     end
   end
-
-  describe "GET edit" do
-    it "assigns the requested artist as @artist" do
-      artist = Artist.create! valid_attributes
-      get :edit, :id => artist.id
-      assigns(:artist).should eq(artist)
+  
+  describe '絵師数取得に於いて' do
+    before do
+      Artist.should_receive(:visible_count).and_return(3)
+#      sign_in @user
+    end
+    context 'つつがなく終わるとき' do
+      it 'ステータスコード200 OKを返す' do
+        get :count, :format => :json
+        response.should be_success 
+      end
+      context 'json形式' do
+        it 'jsonデータを返す' do
+          get :count, :format => :json
+          lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
+        end
+        it 'データがHash構造になっていて絵師数が3である' do
+          get :count, :format => :json
+          json = JSON.parse response.body
+          json["count"].should == 3
+        end
+      end
     end
   end
 
-  describe "POST create" do
-    describe "with valid params" do
-      it "creates a new Artist" do
-        expect {
-          post :create, :artist => valid_attributes
-        }.to change(Artist, :count).by(1)
+  describe '新規作成フォーム表示に於いて' do
+    before do
+      sign_in @user
+    end
+    context 'つつがなく終わるとき' do
+      it 'ステータスコード200 OKを返す' do
+        get :new
+        response.should be_success 
       end
-
-      it "assigns a newly created artist as @artist" do
-        post :create, :artist => valid_attributes
-        assigns(:artist).should be_a(Artist)
-        assigns(:artist).should be_persisted
+      it '@artistに新規データを用意している' do
+        get :new
+        assigns(:ar).should be_a_new(Artist)
       end
-
-      it "redirects to the created artist" do
-        post :create, :artist => valid_attributes
-        response.should redirect_to(Artist.last)
+      it '絵師モデルにデフォルト値補充を依頼している' do
+        Artist.any_instance.should_receive(:supply_default).exactly(1)
+        get :new
+      end
+      context 'html形式' do
+        it 'newテンプレートを描画する' do
+          get :new
+          response.should render_template("new")
+        end
+      end
+      context 'js形式' do
+        it 'new.jsテンプレートを描画する' do
+          get :new, :format => :js
+          response.should render_template("new")
+        end
+      end
+      context 'json形式' do
+        it 'jsonデータを返す' do
+          get :new, :format => :json
+          lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
+        end
+        it '絵師モデルにjson単体出力オプションを問い合わせている' do
+          Artist.should_receive(:show_json_opt).exactly(1)
+          get :new, :format => :json
+        end
       end
     end
-
-    describe "with invalid params" do
-      it "assigns a newly created but unsaved artist as @artist" do
-        # Trigger the behavior that occurs when invalid params are submitted
-        Artist.any_instance.stub(:save).and_return(false)
-        post :create, :artist => {}
-        assigns(:artist).should be_a_new(Artist)
+    context 'ユーザ権限がないとき' do
+      before do
+        sign_out @user
+      end
+      context 'html形式' do
+        it 'ステータスコード302 Foundを返す' do
+          get :new
+          response.status.should eq 302
+        end
+        it 'サインインページへ遷移する' do
+          get :new
+          response.body.should redirect_to '/users/sign_in'
+        end
       end
+      context 'js形式' do
+        it 'ステータスコード401 Unauthorizedを返す' do
+          get :new, :format => :js
+          response.status.should eq 401
+        end
+        it '応答メッセージにUnauthorizedを返す' do
+          get :new, :format => :js
+          response.message.should match(/Unauthorized/)
+        end
+      end
+      context 'json形式' do
+        it 'ステータスコード401 Unauthorizedを返す' do
+          get :new, :format => :json
+          response.status.should eq 401
+        end
+        it '応答メッセージにUnauthorizedを返す' do
+          get :new, :format => :json
+          response.message.should match(/Unauthorized/)
+        end
+      end
+    end
+    context 'ユーザ権限はないが管理者権限があるとき' do
+      before do
+        sign_out @user
+        sign_in @admin
+      end
+      context 'html形式' do
+        it 'ステータスコード302 Foundを返す' do
+          get :new
+          response.status.should eq 302
+        end
+        it 'サインインページへ遷移する' do
+          get :new
+          response.body.should redirect_to '/users/sign_in'
+        end
+      end
+    end
+    context 'ユーザだが作家登録していないとき' do
+      before do
+        @author.destroy
+      end
+      context 'html形式' do
+        it 'ステータスコード302 Foundを返す' do
+          get :new
+          response.status.should eq 302
+        end
+        it '作家登録ページへ遷移する' do
+          get :new
+          response.body.should redirect_to new_author_path
+        end
+      end
+    end
+  end
 
-      it "re-renders the 'new' template" do
-        # Trigger the behavior that occurs when invalid params are submitted
+  describe '新規作成に於いて' do
+    before do
+      sign_in @user
+      @attr = FactoryGirl.attributes_for(:artist, :author_id => @author.id, :name => 'ken')
+    end
+    context '事前チェックしておく' do
+      it '絵師モデルにデフォルト値補充を依頼している' do
+        Artist.any_instance.should_receive(:supply_default).exactly(1)
+        post :create, :artist => @attr
+      end
+      it '絵師モデルにカラム値復元を依頼している' do
+        Artist.any_instance.should_receive(:attributes=).exactly(1)
+        post :create, :artist => @attr
+      end
+      it '絵師モデルに上書き補充を依頼している' do
+        Artist.any_instance.should_receive(:overwrite).exactly(1)
+        post :create, :artist => @attr
+      end
+      it 'モデルに保存依頼する' do
+        Artist.any_instance.should_receive(:save).exactly(1)
+        post :create, :artist => @attr
+      end
+    end
+    context 'つつがなく終わるとき' do
+      it "@arに作成された絵師を保持していて、それがDBにある" do
+        post :create, :artist => @attr
+        assigns(:ar).should be_a(Artist)
+        assigns(:ar).should be_persisted
+      end
+      context 'html形式' do
+        it 'ステータスコード302 Foundを返す' do
+          Artist.any_instance.stub(:save).and_return(true)
+          post :create, :artist => @attr
+          response.status.should eq 302
+        end
+        it 'トップページへ遷移する' do
+#          Artist.any_instance.stub(:save).and_return(true)
+          post :create, :artist => @attr
+          response.should redirect_to(root_path)
+        end
+      end
+      context 'json形式' do
+        it 'ステータスコード200 OKを返す' do
+#          Artist.any_instance.stub(:save).and_return(true)
+          post :create, :artist => @attr, :format => :json
+          response.should be_success 
+        end
+        it '作成された絵師をjsonデータで返す' do
+          post :create, :artist => @attr, :format => :json
+          lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
+        end
+        it '絵師モデルにjson単体出力オプションを問い合わせている' do
+          Artist.should_receive(:show_json_opt).exactly(1)
+          post :create, :artist => @attr, :format => :json
+        end
+        it 'データがアレになっている' do
+          post :create, :artist => @attr, :format => :json
+          json = JSON.parse response.body
+          json["name"].should match(/ken/)
+        end
+      end
+    end
+    context 'ユーザ権限がないとき' do
+      before do
+        sign_out @user
+      end
+      context 'html形式' do
+        it 'ステータスコード302 Foundを返す' do
+          post :create, :artist => @attr
+          response.status.should eq 302
+        end
+        it 'サインインページへ遷移する' do
+          post :create, :artist => @attr
+          response.body.should redirect_to '/users/sign_in'
+        end
+      end
+      context 'json形式' do
+        it 'ステータスコード401 Unauthorizedを返す' do
+          post :create, :artist => @attr, :format => :json
+          response.status.should eq 401
+        end
+        it '応答メッセージにUnauthorizedを返す' do
+          post :create, :artist => @attr, :format => :json
+          response.message.should match(/Unauthorized/)
+        end
+      end
+    end
+    context 'ユーザ権限はないが管理者権限があるとき' do
+      before do
+        sign_out @user
+        sign_in @admin
+      end
+      context 'html形式' do
+        it 'ステータスコード302 Foundを返す' do
+          post :create, :artist => @attr
+          response.status.should eq 302
+        end
+        it 'サインインページへ遷移する' do
+          post :create, :artist => @attr
+          response.body.should redirect_to '/users/sign_in'
+        end
+      end
+    end
+    context 'ユーザだが作家登録していないとき' do
+      before do
+        @author.destroy
+      end
+      context 'html形式' do
+        it 'ステータスコード302 Foundを返す' do
+          post :create, :artist => @attr
+          response.status.should eq 302
+        end
+        it '作家登録ページへ遷移する' do
+          post :create, :artist => @attr
+          response.body.should redirect_to new_author_path
+        end
+      end
+    end
+    context '検証、保存に失敗した' do
+      before do
         Artist.any_instance.stub(:save).and_return(false)
-        post :create, :artist => {}
-        response.should render_template("new")
+      end
+      it "未保存の絵師を保持している" do
+        post :create, :artist => @attr
+        assigns(:ar).should be_a_new(Artist)
+      end
+      context 'html形式' do
+        it 'ステータスコード200 OKを返す' do
+          post :create, :artist => @attr
+          response.status.should eq 200
+        end
+        it '新規ページを描画する' do
+          post :create, :artist => @attr
+          response.should render_template("new")
+        end
+      end
+      context 'json形式' do
+        it 'ステータスコード422 unprocessable_entity を返す' do
+          post :create, :artist => @attr, :format => :json
+          response.status.should eq 422
+        end
+        it '応答メッセージUnprocessable Entityを返す' do
+          post :create, :artist => @attr, :format => :json
+          response.message.should match(/Unprocessable/)
+        end
       end
     end
   end
 
-  describe "PUT update" do
-    describe "with valid params" do
-      it "updates the requested artist" do
-        artist = Artist.create! valid_attributes
-        # Assuming there are no other artists in the database, this
-        # specifies that the Artist created on the previous line
-        # receives the :update_attributes message with whatever params are
-        # submitted in the request.
-        Artist.any_instance.should_receive(:update_attributes).with({'these' => 'params'})
-        put :update, :id => artist.id, :artist => {'these' => 'params'}
+  describe '編集フォーム表示に於いて' do
+    before do
+      sign_in @user
+      Artist.stub(:edit).and_return(@artist)
+    end
+    context 'つつがなく終わるとき' do
+      it 'ステータスコード200 OKを返す' do
+        get :edit, :id => @artist.id
+        response.should be_success 
+      end
+      it '絵師モデルに編集取得を問い合わせている' do
+        Artist.should_receive(:edit).exactly(1)
+        get :edit, :id => @artist.id
+      end
+      it '@artistにデータを用意している' do
+        get :edit, :id => @artist.id
+        assigns(:artist).should eq @artist
+      end
+      context 'html形式' do
+        it 'editテンプレートを描画する' do
+          get :edit, :id => @artist.id
+          response.should render_template("edit")
+        end
+      end
+      context 'js形式' do
+        it 'edit.jsテンプレートを描画する' do
+          get :edit, :id => @artist.id, :format => :js
+          response.should render_template("edit")
+        end
+      end
+    end
+    context 'ユーザ権限がないとき' do
+      before do
+        sign_out @user
+      end
+      context 'html形式' do
+        it 'ステータスコード302 Foundを返す' do
+          get :edit, :id => @artist.id
+          response.status.should eq 302
+        end
+        it 'サインインページへ遷移する' do
+          get :edit, :id => @artist.id
+          response.body.should redirect_to '/users/sign_in'
+        end
+      end
+      context 'js形式' do
+        it 'ステータスコード401 Unauthorizedを返す' do
+          get :edit, :id => @artist.id, :format => :js
+          response.status.should eq 401
+        end
+        it '応答メッセージにUnauthorizedを返す' do
+          get :edit, :id => @artist.id, :format => :js
+          response.message.should match(/Unauthorized/)
+        end
+      end
+    end
+    context 'ユーザ権限はないが管理者権限があるとき' do
+      before do
+        sign_out @user
+        sign_in @admin
+      end
+      context 'html形式' do
+        it 'ステータスコード302 Foundを返す' do
+          get :edit, :id => @artist.id
+          response.status.should eq 302
+        end
+        it 'サインインページへ遷移する' do
+          get :edit, :id => @artist.id
+          response.body.should redirect_to '/users/sign_in'
+        end
       end
+    end
+    context 'ユーザだが作家登録していないとき' do
+      before do
+        @author.destroy
+      end
+      context 'html形式' do
+        it 'ステータスコード302 Foundを返す' do
+          get :edit, :id => @artist.id
+          response.status.should eq 302
+        end
+        it '作家登録ページへ遷移する' do
+          get :edit, :id => @artist.id
+          response.body.should redirect_to new_author_path
+        end
+      end
+    end
+  end
 
-      it "assigns the requested artist as @artist" do
-        artist = Artist.create! valid_attributes
-        put :update, :id => artist.id, :artist => valid_attributes
-        assigns(:artist).should eq(artist)
+  describe '更新に於いて' do
+    before do
+      @attr = FactoryGirl.attributes_for(:artist, :author_id => @author.id, :name => 'ryu')
+      sign_in @user
+    end
+    context '事前チェックしておく' do
+      it '絵師モデルに編集取得を問い合わせている' do
+        Artist.stub(:edit).with(any_args()).and_return @artist
+        Artist.should_receive(:edit).exactly(1)
+        put :update, :id => @artist.id, :artist => @attr
+      end
+      it '絵師モデルにカラム値復元を依頼している' do
+        Artist.any_instance.should_receive(:attributes=).exactly(1)
+        put :update, :id => @artist.id, :artist => @attr
+      end
+      it '絵師モデルに上書き補充を依頼している' do
+        Artist.any_instance.should_receive(:overwrite).exactly(1)
+        put :update, :id => @artist.id, :artist => @attr
+      end
+      it 'モデルに更新を依頼する' do
+        Artist.any_instance.stub(:save).with(any_args).and_return true
+        Artist.any_instance.should_receive(:save).exactly(1)
+        put :update, :id => @artist.id, :artist => @attr
+      end
+      it '@arにアレを取得している' do
+        put :update, :id => @artist.id, :artist => @attr
+        assigns(:ar).should eq @artist
+      end
+    end
+    context 'つつがなく終わるとき' do
+      it '更新される' do
+        put :update, :id => @artist.id, :artist => @attr
+        Artist.find(@artist.id).name.should eq 'ryu'
+      end
+      context 'html形式' do
+        it 'ステータスコード302 Foundを返す' do
+          Artist.any_instance.stub(:save).with(any_args()).and_return(true)
+          put :update, :id => @artist.id, :artist => @attr
+          response.status.should eq 302
+        end
+        it '設定ページへ遷移する' do
+          put :update, :id => @artist.id, :artist => @attr
+          response.should redirect_to('/home/configure')
+        end
+      end
+      context 'json形式' do
+        it 'ステータスコード200 OKを返す' do
+          Artist.any_instance.stub(:save).with(any_args()).and_return(true)
+          put :update, :id => @artist.id, :artist => @attr, :format => :json
+          response.should be_success 
+        end
+        it 'ページ本体は特に返さない' do
+          Artist.any_instance.stub(:save).with(any_args()).and_return(true)
+          put :update, :id => @artist.id, :artist => @attr, :format => :json
+          response.body.should match /./
+        end
+      end
+    end
+    context 'ユーザ権限がないとき' do
+      before do
+        sign_out @user
+      end
+      context 'html形式' do
+        it 'ステータスコード302 Foundを返す' do
+          put :update, :id => @artist.id, :artist => @attr
+          response.status.should eq 302
+        end
+        it 'サインインページへ遷移する' do
+          put :update, :id => @artist.id, :artist => @attr
+          response.body.should redirect_to '/users/sign_in'
+        end
+      end
+      context 'json形式' do
+        it '応答メッセージにUnauthorizedを返す' do
+          put :update, :id => @artist.id, :artist => @attr, :format => :json
+          response.message.should match(/Unauthorized/)
+        end
+      end
+    end
+    context 'ユーザ権限はないが管理者権限があるとき' do
+      before do
+        sign_out @user
+        sign_in @admin
+      end
+      context 'html形式' do
+        it 'ステータスコード302 Foundを返す' do
+          put :update, :id => @artist.id, :artist => @attr
+          response.status.should eq 302
+        end
+        it 'サインインページへ遷移する' do
+          put :update, :id => @artist.id, :artist => @attr
+          response.body.should redirect_to '/users/sign_in'
+        end
+      end
+    end
+    context 'ユーザだが作家登録していないとき' do
+      before do
+        @author.destroy
       end
+      context 'html形式' do
+        it 'ステータスコード302 Foundを返す' do
+          put :update, :id => @artist.id, :artist => @attr
+          response.status.should eq 302
+        end
+        it '作家登録ページへ遷移する' do
+          put :update, :id => @artist.id, :artist => @attr
+          response.body.should redirect_to new_author_path
+        end
+      end
+    end
+    context '検証、保存に失敗したとき' do
+      before do
+        Artist.any_instance.stub(:save).and_return(false)
+      end
+      context 'html形式' do
+        it 'ステータスコード200 Okを返す' do
+          put :update, :id => @artist.id, :artist => @attr
+          response.status.should eq 200
+        end
+        it '編集ページを描画する' do
+          put :update, :id => @artist.id, :artist => @attr
+          response.should render_template("edit")
+        end
+      end
+      context 'json形式' do
+        it 'ステータスコード422 unprocessable_entity を返す' do
+          Artist.any_instance.stub(:save).and_return(false)
+          put :update, :id => @artist.id, :artist => @attr, :format => :json
+          response.status.should eq 422
+        end
+        it '応答メッセージUnprocessable Entityを返す' do
+          put :update, :id => @artist.id, :artist => @attr, :format => :json
+          response.message.should match(/Unprocessable/)
+        end
+      end
+    end
+  end
 
-      it "redirects to the artist" do
-        artist = Artist.create! valid_attributes
-        put :update, :id => artist.id, :artist => valid_attributes
-        response.should redirect_to(artist)
+else
+  describe '一覧表示に於いて' do
+    before do
+      Artist.stub(:list).and_return([@artist, @artist, @artist])
+      sign_in @user
+    end
+    context 'つつがなく終わるとき' do
+      it 'ステータスコード200 OKを返す' do
+        get :index
+        response.should be_success 
+      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
+    context 'ユーザ権限がないとき' do
+      before do
+        sign_out @user
+      end
+      it 'ステータスコード200 OKを返す' do
+        get :index
+        response.should be_success 
+      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 '閲覧に於いて' do
+    before do
+      Artist.stub(:show).and_return(@artist)
+      sign_in @user
+    end
+    context 'つつがなく終わるとき' do
+      it 'ステータスコード200 OKを返す' do
+        get :show, :id => @artist.id
+        response.should be_success
+      end
+      context 'html形式' do
+        it 'showテンプレートを描画する' do
+          get :show, :id => @artist.id
+          response.should render_template("show")
+        end
+      end
+      context 'json形式' do
+        it 'jsonデータを返す' do
+          get :show, :id => @artist.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 => @artist.id
+        response.should be_success
+      end
+      context 'html形式' do
+        it 'showテンプレートを描画する' do
+          get :show, :id => @artist.id
+          response.should render_template("show")
+        end
+      end
+      context 'json形式' do
+        it 'jsonデータを返す' do
+          get :show, :id => @artist.id, :format => :json
+          lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
+        end
+      end
+    end
+  end
+  
+  describe '対象絵師の素材一覧表示に於いて' do
+    before do
+      @other_user = FactoryGirl.create( :user_yas)
+      @other_author = FactoryGirl.create :author, :user_id => @other_user.id
+      @other_artist = FactoryGirl.create :artist_yas, :author_id => @other_author.id
+      @op = FactoryGirl.create :original_picture, :artist_id => @other_artist.id
+      @p = FactoryGirl.create :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @other_artist.id
+      @rp = FactoryGirl.create :resource_picture, :artist_id => @other_artist.id, :license_id => @license.id, :original_picture_id => @op.id, :picture_id => @p.id
+      ResourcePicture.stub(:mylist).and_return([@rp, @rp, @rp])
+      sign_in @user
+    end
+    context 'つつがなく終わるとき' do
+      it 'ステータスコード200 OKを返す' do
+        get :resource_pictures, :id => @other_artist.id
+        response.should be_success 
+      end
+      context 'html形式' do
+        it 'resource_pictureテンプレートを描画する' do
+          get :resource_pictures, :id => @other_artist.id
+          response.should render_template("resource_pictures")
+        end
+      end
+      context 'json形式' do
+        it 'jsonデータを返す' do
+          get :resource_pictures, :id => @other_artist.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 :resource_pictures, :id => @other_artist.id
+        response.should be_success 
+      end
+      context 'html形式' do
+        it 'resource_pictureテンプレートを描画する' do
+          get :resource_pictures, :id => @other_artist.id
+          response.should render_template("resource_pictures")
+        end
+      end
+      context 'json形式' do
+        it 'jsonデータを返す' do
+          get :resource_pictures, :id => @other_artist.id, :format => :json
+          lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
+        end
+      end
+    end
+  end
+  
+  describe '絵師数取得に於いて' do
+    before do
+      Artist.should_receive(:visible_count).and_return(3)
+#      sign_in @user
+    end
+    context 'つつがなく終わるとき' do
+      it 'ステータスコード200 OKを返す' do
+        get :count, :format => :json
+        response.should be_success 
+      end
+      context 'json形式' do
+        it 'jsonデータを返す' do
+          get :count, :format => :json
+          lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
+        end
+      end
+    end
+  end
 
-    describe "with invalid params" do
-      it "assigns the artist as @artist" do
-        artist = Artist.create! valid_attributes
-        # Trigger the behavior that occurs when invalid params are submitted
-        Artist.any_instance.stub(:save).and_return(false)
-        put :update, :id => artist.id, :artist => {}
-        assigns(:artist).should eq(artist)
+  describe '新規作成フォーム表示に於いて' do
+    before do
+      sign_in @user
+    end
+    context 'つつがなく終わるとき' do
+      it 'ステータスコード200 OKを返す' do
+        get :new
+        response.should be_success 
+      end
+      context 'html形式' do
+        it 'newテンプレートを描画する' do
+          get :new
+          response.should render_template("new")
+        end
+      end
+      context 'js形式' do
+        it 'new.jsテンプレートを描画する' do
+          get :new, :format => :js
+          response.should render_template("new")
+        end
+      end
+      context 'json形式' do
+        it 'jsonデータを返す' do
+          get :new, :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 'ステータスコード302 Foundを返す' do
+          get :new
+          response.status.should eq 302
+        end
+        it 'サインインページへ遷移する' do
+          get :new
+          response.body.should redirect_to '/users/sign_in'
+        end
       end
+      context 'js形式' do
+        it 'ステータスコード401 Unauthorizedを返す' do
+          get :new, :format => :js
+          response.status.should eq 401
+        end
+        it '応答メッセージにUnauthorizedを返す' do
+          get :new, :format => :js
+          response.message.should match(/Unauthorized/)
+        end
+      end
+      context 'json形式' do
+        it 'ステータスコード401 Unauthorizedを返す' do
+          get :new, :format => :json
+          response.status.should eq 401
+        end
+        it '応答メッセージにUnauthorizedを返す' do
+          get :new, :format => :json
+          response.message.should match(/Unauthorized/)
+        end
+      end
+    end
+  end
 
-      it "re-renders the 'edit' template" do
-        artist = Artist.create! valid_attributes
-        # Trigger the behavior that occurs when invalid params are submitted
-        Artist.any_instance.stub(:save).and_return(false)
-        put :update, :id => artist.id, :artist => {}
-        response.should render_template("edit")
+  describe '新規作成に於いて' do
+    before do
+      sign_in @user
+      @attr = FactoryGirl.attributes_for(:artist, :author_id => @author.id, :name => 'ken')
+    end
+    context 'つつがなく終わるとき' do
+      context 'html形式' do
+        it 'ステータスコード302 Foundを返す' do
+          Artist.any_instance.stub(:save).and_return(true)
+          post :create, :artist => @attr
+          response.status.should eq 302
+        end
+        it 'トップページへ遷移する' do
+#          Artist.any_instance.stub(:save).and_return(true)
+          post :create, :artist => @attr
+          response.should redirect_to(root_path)
+        end
+      end
+      context 'json形式' do
+        it 'ステータスコード200 OKを返す' do
+#          Artist.any_instance.stub(:save).and_return(true)
+          post :create, :artist => @attr, :format => :json
+          response.should be_success 
+        end
+        it '作成された絵師をjsonデータで返す' do
+          post :create, :artist => @attr, :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 'ステータスコード302 Foundを返す' do
+          post :create, :artist => @attr
+          response.status.should eq 302
+        end
+        it 'サインインページへ遷移する' do
+          post :create, :artist => @attr
+          response.body.should redirect_to '/users/sign_in'
+        end
+      end
+      context 'json形式' do
+        it 'ステータスコード401 Unauthorizedを返す' do
+          post :create, :artist => @attr, :format => :json
+          response.status.should eq 401
+        end
+        it '応答メッセージにUnauthorizedを返す' do
+          post :create, :artist => @attr, :format => :json
+          response.message.should match(/Unauthorized/)
+        end
       end
     end
   end
 
-  describe "DELETE destroy" do
-    it "destroys the requested artist" do
-      artist = Artist.create! valid_attributes
-      expect {
-        delete :destroy, :id => artist.id
-      }.to change(Artist, :count).by(-1)
+  describe '編集フォーム表示に於いて' do
+    before do
+      sign_in @user
+      Artist.stub(:edit).and_return(@artist)
+    end
+    context 'つつがなく終わるとき' do
+      it 'ステータスコード200 OKを返す' do
+        get :edit, :id => @artist.id
+        response.should be_success 
+      end
+      context 'html形式' do
+        it 'editテンプレートを描画する' do
+          get :edit, :id => @artist.id
+          response.should render_template("edit")
+        end
+      end
+      context 'js形式' do
+        it 'edit.jsテンプレートを描画する' do
+          get :edit, :id => @artist.id, :format => :js
+          response.should render_template("edit")
+        end
+      end
     end
+    context 'ユーザ権限がないとき' do
+      before do
+        sign_out @user
+      end
+      context 'html形式' do
+        it 'ステータスコード302 Foundを返す' do
+          get :edit, :id => @artist.id
+          response.status.should eq 302
+        end
+        it 'サインインページへ遷移する' do
+          get :edit, :id => @artist.id
+          response.body.should redirect_to '/users/sign_in'
+        end
+      end
+      context 'js形式' do
+        it 'ステータスコード401 Unauthorizedを返す' do
+          get :edit, :id => @artist.id, :format => :js
+          response.status.should eq 401
+        end
+        it '応答メッセージにUnauthorizedを返す' do
+          get :edit, :id => @artist.id, :format => :js
+          response.message.should match(/Unauthorized/)
+        end
+      end
+    end
+  end
 
-    it "redirects to the artists list" do
-      artist = Artist.create! valid_attributes
-      delete :destroy, :id => artist.id
-      response.should redirect_to(artists_url)
+  describe '更新に於いて' do
+    before do
+      @attr = FactoryGirl.attributes_for(:artist, :author_id => @author.id, :name => 'ryu')
+      sign_in @user
+    end
+    context 'つつがなく終わるとき' do
+      context 'html形式' do
+        it 'ステータスコード302 Foundを返す' do
+          Artist.any_instance.stub(:save).with(any_args()).and_return(true)
+          put :update, :id => @artist.id, :artist => @attr
+          response.status.should eq 302
+        end
+        it '設定ページへ遷移する' do
+          put :update, :id => @artist.id, :artist => @attr
+          response.should redirect_to('/home/configure')
+        end
+      end
+      context 'json形式' do
+        it 'ステータスコード200 OKを返す' do
+          Artist.any_instance.stub(:save).with(any_args()).and_return(true)
+          put :update, :id => @artist.id, :artist => @attr, :format => :json
+          response.should be_success 
+        end
+        it 'ページ本体は特に返さない' do
+          Artist.any_instance.stub(:save).with(any_args()).and_return(true)
+          put :update, :id => @artist.id, :artist => @attr, :format => :json
+          response.body.should match /./
+        end
+      end
+    end
+    context 'ユーザ権限がないとき' do
+      before do
+        sign_out @user
+      end
+      it 'ステータスコード302 Foundを返す' do
+        put :update, :id => @artist.id, :artist => @attr
+        response.status.should eq 302
+      end
+      context 'html形式' do
+        it 'サインインページへ遷移する' do
+          put :update, :id => @artist.id, :artist => @attr
+          response.body.should redirect_to '/users/sign_in'
+        end
+      end
+      context 'json形式' do
+        it '応答メッセージにUnauthorizedを返す' do
+          put :update, :id => @artist.id, :artist => @attr, :format => :json
+          response.message.should match(/Unauthorized/)
+        end
+      end
     end
   end
 
 end
+end