X-Git-Url: http://git.osdn.net/view?a=blobdiff_plain;f=spec%2Fcontrollers%2Fpanels_controller_spec.rb;h=019ca87ba409a35ee15f5208a4ae25ac1fc47318;hb=8a42b6e27d6579a7e824cd3e67c156dc4c6e583a;hp=2a2ae43caeb85b30ff5c676cc988b185c86f87c9;hpb=ef9a33979c325f99b16263c4275379f4885f30ee;p=pettanr%2Fpettanr.git diff --git a/spec/controllers/panels_controller_spec.rb b/spec/controllers/panels_controller_spec.rb index 2a2ae43c..019ca87b 100644 --- a/spec/controllers/panels_controller_spec.rb +++ b/spec/controllers/panels_controller_spec.rb @@ -1,120 +1,130 @@ # -*- encoding: utf-8 -*- require 'spec_helper' - +#コマ describe PanelsController do before do - Factory :admin - @license = Factory :license - @user = Factory :user_yas - @author = @user.author #ユーザ作成時に連動して作成される + @admin =FactoryGirl.create :admin + @user_yas = FactoryGirl.create :user_yas_with_owner + @user_rom = FactoryGirl.create :user_rom + @author_yas = @user_yas.author + + @my_model_class = Panel #操作モデル end +if Manifest.manifest.magic_numbers['run_mode'] == 1 describe '一覧表示に於いて' do before do - @panel = Factory :panel, :author_id => @user.author.id - Panel.stub(:list).and_return([@panel, @panel, @panel]) - 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 Panel.default_page_size - end - it '最大を超えると@page_sizeにデフォルト最大値が入る' do - get :index, :page_size => 1500 - assigns(:page_size).should eq Panel.max_page_size - end - it '不正な値が入ると@page_sizeにデフォルト最大値が入る' do - get :index, :page_size => 0 - assigns(:page_size).should eq Panel.default_page_size - end + factory_contents :panel_hello, @author_yas + @query_play = { } + @query = {:mode => 'filer'} + @query_format_json = @query_play.merge(:format => :json) end - context 'つつがなく終わるとき' do - it 'ステータスコード200 OKを返す' do - get :index - response.should be_success - end - it 'コマモデルに一覧を問い合わせている' do - Panel.should_receive(:list).exactly(1) - get :index + + context '正常系' do + before do + sign_in @user_yas end - it '@panelsにリストを取得している' do - get :index - assigns(:panels).should have_at_least(3).items + context '共通処理' do + before do + get :index, @query + end + it 'コントローラのマニフェストから操作モデルを導出している' do + set_model? @my_model_class + end + it 'コントローラのマニフェストからリスト操作モデルを導出している' do + set_list_model? @my_model_class + end + it 'リストグループのマニフェストからリストgetter(新着リスト)を導出している' do + set_list? 'panel', 'public' + end + it '@itemsにリストを取得している' do + expect(assigns(:items).size).to be >= 3 + end end context 'html形式' do - it 'indexテンプレートを描画する' do - get :index - response.should render_template("index") + before do + get :index, @query + end + it 'ステータス OKを返して、ファイラーを描画している' do + return_ok? + render_filer? + end + it '@filerにファイラーを取得している' do + let_filer? end end context 'json形式' do - it 'jsonデータを返す' do - get :index, :format => :json - lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError) + before do + get :index, @query_format_json end - it 'データがリスト構造になっている' do - get :index, :format => :json - json = JSON.parse response.body - json.should have_at_least(3).items + it 'リストグループのマニフェストからリストgetter(新着リスト)を導出している' do + set_list? 'panel', 'public' end - it 'リストの先頭くらいはコマっぽいものであって欲しい' do - get :index, :format => :json - json = JSON.parse response.body - json.first.has_key?("comic_id").should be_true + it 'ステータス OKを返して、jsonデータを返している' do + return_ok? + render_json? + end + it '@filerにファイラーをセットしていない' do + expect(assigns(:filer)).to be nil end end end - context '作家権限がないとき' do - before do - sign_out @user - end + context '件数取得' do 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' + it '必要ないので実装していない' do + sign_in @user_yas + get :count, @query + expect(response.status).to eq(406) end end context 'json形式' do - it 'ステータスコード401 Unauthorizedを返す' do - get :index, :format => :json - response.status.should eq 401 + it 'ステータス OKを返して、jsonデータを返している' do + sign_in @user_yas + get :count, @query_format_json + return_ok? + render_json? end - it '応答メッセージにUnauthorizedを返す' do - get :index, :format => :json - response.message.should match(/Unauthorized/) + end + context 'ゲスト(サインインしていない)' do + it '通常通り応答する' do + get :count, @query_format_json + return_ok? + render_json? end end end + context 'サインインの状態が例外的なとき' do + it 'ユーザではなく管理者でも通常通り応答している' do + sign_in @admin + get :index, @query + return_ok? + get :index, @query_format_json + render_json? + end + it '創作活動してないROM専の読者にも通常通り応答している' do + sign_in @user_rom + get :index, @query + return_ok? + get :index, @query_format_json + render_json? + end + it 'ゲスト(サインインしていない)はサインインページに遷移している' do + get :index, @query + redirect? sign_in_url + get :index, @query_format_json + render_unauthorized? + end + end end describe '単体表示に於いて' do before do - @panel = Factory :panel, :author_id => @user.author.id - Panel.stub(:show).and_return(@panel) + @panel = FactoryGirl.create :panel, :author_id => @user.author.id + Panel.stub(:show).with(@panel.id.to_s, [@user, nil]).and_return(@panel) + Panel.stub(:show).with(@panel.id.to_s, [nil, @admin]).and_return(@panel) sign_in @user end context 'つつがなく終わるとき' do - it 'ステータスコード200 OKを返す' do - get :show, :id => @panel.id - response.should be_success - end it 'コマモデルに単体取得を問い合わせている' do Panel.should_receive(:show).exactly(1) get :show @@ -124,24 +134,36 @@ describe PanelsController do assigns(:panel).id.should eq(@panel.id) end context 'html形式' do + it 'ステータスコード200 OKを返す' do + get :show, :id => @panel.id + response.should be_success + end it 'showテンプレートを描画する' do get :show, :id => @panel.id response.should render_template("show") end end context 'json形式' do + it 'ステータスコード200 OKを返す' do + get :show, :id => @panel.id, :format => :json + response.should be_success + end it 'jsonデータを返す' do get :show, :id => @panel.id, :format => :json lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError) end + it 'コマモデルにコマのjson出力を問い合わせている' do + Panel.any_instance.should_receive(:panel_elements_as_json).exactly(1) + get :show, :id => @panel.id, :format => :json + end it 'データがアレになっている' do get :show, :id => @panel.id, :format => :json json = JSON.parse response.body - json["width"].should match(/438/) + json["width"].should eq 100 end end end - context '作家権限がないとき' do + context 'ユーザ権限がないとき' do before do sign_out @user end @@ -166,6 +188,25 @@ describe PanelsController do end end end + context 'ユーザ権限はないが管理者権限があるとき' do + before do + sign_out @user + sign_in @admin + end + it 'ステータスコード200 OKを返す' do + get :show, :id => @panel.id + response.should be_success + end + end + context 'ユーザだが作家登録していないとき' do + before do + @author.destroy + end + it 'ステータスコード200 OKを返す' do + get :show, :id => @panel.id + response.should be_success + end + end =begin context '対象コマがないとき' do context 'html形式' do @@ -187,7 +228,7 @@ describe PanelsController do context 'html形式' do it '例外403 forbiddenを返す' do Panel.any_instance.stub(:visible?).with(any_args()).and_return(false) - hidden = Factory :hidden_panel, :author_id => @author.id + hidden = FactoryGirl.create :hidden_panel, :author_id => @author.id lambda{ get :show, :id => hidden }.should raise_error(ActiveRecord::Forbidden) @@ -196,7 +237,7 @@ describe PanelsController do context 'json形式' do it '例外403 forbiddenを返す' do Panel.any_instance.stub(:visible?).with(any_args()).and_return(false) - hidden = Factory :hidden_panel, :author_id => @author.id + hidden = FactoryGirl.create :hidden_panel, :author_id => @author.id lambda{ get :show, :id => hidden, :format => :json }.should raise_error(ActiveRecord::Forbidden) @@ -235,10 +276,6 @@ describe PanelsController do sign_in @user end context 'つつがなく終わるとき' do - it 'ステータスコード200 OKを返す' do - get :new - response.should be_success - end it '@panelに新規データを用意している' do get :new assigns(:panel).should be_a_new(Panel) @@ -248,6 +285,10 @@ describe PanelsController do get :new end context 'html形式' do + it 'ステータスコード200 OKを返す' do + get :new + response.should be_success + end it 'newテンプレートを描画する' do get :new response.should render_template("new") @@ -259,8 +300,18 @@ describe PanelsController do 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 + Panel.any_instance.should_receive(:panel_elements_as_json).exactly(1) + get :new, :format => :json + end + end end - context '作家権限がないとき' do + context 'ユーザ権限がないとき' do before do sign_out @user end @@ -285,123 +336,193 @@ describe PanelsController do 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 describe '新規作成に於いて' do before do - @panel = Factory :normal_panel, :author_id => @user.author.id + @panel = FactoryGirl.build :panel, :author_id => @author.id + @attr = FactoryGirl.attributes_for(:panel, :author_id => @author.id) sign_in @user end context '事前チェックする' do before do - controller Panel.stub(:count).and_return(10) end it 'panelがパラメータにあれば、展開する' do - post :create, :panel => Factory.attributes_for(:panel, :panel_id => @panel.id) - assigns(:prm)['border'].to_i.should eq 1 + post :create, :panel => @attr + assigns(:prm)['border'].to_i.should eq @panel.border end it 'jsonがパラメータにあれば、展開する' do - post :create, :json => Factory.attributes_for(:panel, :panel_id => @panel.id, :border => 4).to_s + post :create, :json => FactoryGirl.attributes_for(:panel, :border => 4).to_json assigns(:prm)['border'].to_i.should eq 4 end it 'panel・json両パラメータがあれば、panelを優先して展開する' do post :create, { - :panel => Factory.attributes_for(:panel, :panel_id => @panel.id), - :json => Factory.attributes_for(:panel, :panel_id => @panel.id, :border => 4).to_s + :panel => FactoryGirl.attributes_for(:panel), + :json => FactoryGirl.attributes_for(:panel, :border => 4).to_json } - assigns(:prm)['border'].to_i.should eq 1 + assigns(:prm)['border'].to_i.should eq @panel.border + end + it 'コマモデルにデフォルト値補充を依頼している' do + Panel.any_instance.should_receive(:supply_default).exactly(1) + post :create, :panel => @attr end - end - context 'つつがなく終わるとき' do it 'モデルに保存依頼する' do - Panel.any_instance.should_receive(:save).exactly(1) - post :create, :panel => Factory.attributes_for(:panel, :author => @author.id) + Panel.any_instance.should_receive(:store).exactly(1) + post :create, :panel => @attr end + end + context 'つつがなく終わるとき' do it "@panelに作成されたコマを保持していて、それがDBにある" do - post :create, :panel => Factory.attributes_for(:panel, :author => @author.id) + post :create, :panel => @attr assigns(:panel).should be_a(Panel) assigns(:panel).should be_persisted end context 'html形式' do it 'ステータスコード302 Foundを返す' do - Panel.any_instance.stub(:save).and_return(true) - post :create, :panel => Factory.attributes_for(:panel, :author => @author.id) + Panel.any_instance.stub(:store).and_return(true) + post :create, :panel => @attr response.status.should eq 302 end it '作成されたコマの表示ページへ遷移する' do -# Panel.any_instance.stub(:save).and_return(true) - post :create, :panel => Factory.attributes_for(:panel, :author => @author.id) +# Panel.any_instance.stub(:store).and_return(true) + post :create, :panel => @attr response.should redirect_to(Panel.last) end end context 'json形式' do it 'ステータスコード200 OKを返す' do -# Panel.any_instance.stub(:save).and_return(true) - post :create, :panel => Factory.attributes_for(:panel, :author => @author.id), :format => :json +# Panel.any_instance.stub(:store).and_return(true) + post :create, :panel => @attr, :format => :json response.should be_success end it '作成されたコマをjsonデータで返す' do - post :create, :panel => Factory.attributes_for(:panel, :author => @author.id), :format => :json + post :create, :panel => @attr, :format => :json lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError) end + it 'コマモデルにコマのjson出力を問い合わせている' do + Panel.any_instance.should_receive(:panel_elements_as_json).exactly(1) + post :create, :panel => @attr, :format => :json + end it 'データがアレになっている' do - post :create, :panel => Factory.attributes_for(:panel, :author => @author.id), :format => :json + post :create, :panel => @attr, :format => :json json = JSON.parse response.body - json["title"].should match(/normal/) + json["width"].should eq @panel.width end end end - context '作家権限がないとき' do + context 'ユーザ権限がないとき' do before do sign_out @user end context 'html形式' do it 'ステータスコード302 Foundを返す' do - post :create, :panel => Factory.attributes_for(:panel, :author => @author.id) + post :create, :panel => @attr response.status.should eq 302 end it 'サインインページへ遷移する' do - post :create, :panel => Factory.attributes_for(:panel, :author => @author.id) + post :create, :panel => @attr response.body.should redirect_to '/users/sign_in' end end context 'json形式' do it 'ステータスコード401 Unauthorizedを返す' do - post :create, :panel => Factory.attributes_for(:panel, :author => @author.id), :format => :json + post :create, :panel => @attr, :format => :json response.status.should eq 401 end it '応答メッセージにUnauthorizedを返す' do - post :create, :panel => Factory.attributes_for(:panel, :author => @author.id), :format => :json + post :create, :panel => @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, :panel => @attr + response.status.should eq 302 + end + it 'サインインページへ遷移する' do + post :create, :panel => @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, :panel => @attr + response.status.should eq 302 + end + it '作家登録ページへ遷移する' do + post :create, :panel => @attr + response.body.should redirect_to new_author_path + end + end + end context '検証、保存に失敗した' do before do - Panel.any_instance.stub(:save).and_return(false) + Panel.any_instance.stub(:store).and_return(false) end it "未保存のコマを保持している" do - post :create, :panel => Factory.attributes_for(:panel, :author => @author.id) + post :create, :panel => @attr assigns(:panel).should be_a_new(Panel) end context 'html形式' do it 'ステータスコード200 OKを返す' do - post :create, :panel => Factory.attributes_for(:panel, :author => @author.id) + post :create, :panel => @attr response.status.should eq 200 end it '新規ページを描画する' do - post :create, :panel => Factory.attributes_for(:panel, :author => @author.id) + post :create, :panel => @attr response.should render_template("new") end end context 'json形式' do it 'ステータスコード422 unprocessable_entity を返す' do - post :create, :panel => Factory.attributes_for(:panel, :author => @author.id), :format => :json + post :create, :panel => @attr, :format => :json response.status.should eq 422 end it '応答メッセージUnprocessable Entityを返す' do - post :create, :panel => Factory.attributes_for(:panel, :author => @author.id), :format => :json + post :create, :panel => @attr, :format => :json response.message.should match(/Unprocessable/) end end @@ -410,17 +531,13 @@ describe PanelsController do describe '編集フォーム表示に於いて' do before do - @panel = Factory :panel, :author_id => @author.id + @panel = FactoryGirl.create :panel, :author_id => @author.id sign_in @user Panel.stub(:show).and_return(@panel) end context 'つつがなく終わるとき' do - it 'ステータスコード200 OKを返す' do - get :edit, :id => @panel.id - response.should be_success - end - it 'コマモデルに単体取得を問い合わせている' do - Panel.should_receive(:show).exactly(1) + it 'コマモデルに編集取得を問い合わせている' do + Panel.should_receive(:edit).exactly(1) get :edit, :id => @panel.id end it '@panelにデータを用意している' do @@ -428,19 +545,27 @@ describe PanelsController do assigns(:panel).should eq @panel end context 'html形式' do + it 'ステータスコード200 OKを返す' do + get :edit, :id => @panel.id + response.should be_success + end it 'editテンプレートを描画する' do get :edit, :id => @panel.id response.should render_template("edit") end end context 'js形式' do + it 'ステータスコード200 OKを返す' do + get :edit, :id => @panel.id, :format => :js + response.should be_success + end it 'edit.jsテンプレートを描画する' do get :edit, :id => @panel.id, :format => :js response.should render_template("edit") end end end - context '作家権限がないとき' do + context 'ユーザ権限がないとき' do before do sign_out @user end @@ -465,104 +590,730 @@ describe PanelsController do end end end + context 'ユーザ権限はないが管理者権限があるとき' do + before do + sign_out @user + sign_in @admin + end + context 'html形式' do + it 'ステータスコード302 Foundを返す' do + get :edit, :id => @panel.id + response.status.should eq 302 + end + it 'サインインページへ遷移する' do + get :edit, :id => @panel.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 => @panel.id + response.status.should eq 302 + end + it '作家登録ページへ遷移する' do + get :edit, :id => @panel.id + response.body.should redirect_to new_author_path + end + end + end end describe '更新に於いて' do before do - @panel = Factory :panel, :author => @author.id + @panel = FactoryGirl.create :panel, :author_id => @user.author.id + @attr = FactoryGirl.attributes_for(:panel, :author_id => @author.id) sign_in @user end - context '事前チェックしておく' do - it 'コマモデルに単体取得を問い合わせている' do - Panel.stub(:show).with(any_args()).and_return @panel - Panel.should_receive(:show).exactly(1) - put :update, :id => @panel.id, :panel => Factory.attributes_for(:panel, :author => @author.id) + context '事前チェックする' do + before do + controller + Panel.stub(:count).and_return(10) end - it 'モデルに更新を依頼する' do - Panel.any_instance.should_receive(:update_attributes).with(any_args) - put :update, :id => @panel.id, :panel => Factory.attributes_for(:panel, :author => @author.id) + it 'panelがパラメータにあれば、展開する' do + put :update, :id => @panel.id, :panel => @attr + assigns(:prm)['border'].to_i.should eq @panel.border end - it '@panelにアレを取得している' do - put :update, :id => @panel.id, :panel => Factory.attributes_for(:panel, :author => @author.id) - assigns(:panel).id.should eq(@panel.id) + it 'jsonがパラメータにあれば、展開する' do + put :update, :id => @panel.id, :json => FactoryGirl.attributes_for(:panel, :border => 4).to_json + assigns(:prm)['border'].to_i.should eq 4 + end + it 'panel・json両パラメータがあれば、panelを優先して展開する' do + put :update, { + :id => @panel.id, + :panel => FactoryGirl.attributes_for(:panel), + :json => FactoryGirl.attributes_for(:panel, :border => 4).to_json + } + assigns(:prm)['border'].to_i.should eq @panel.border end end context 'つつがなく終わるとき' do - it '更新される' do - put :update, :id => @panel.id, :panel => Factory.attributes_for(:panel, :author => @author.id) - Panel.find(@panel.id).visible.should eq 0 + it 'モデルに編集取得依頼する' do + Panel.stub(:edit).with(any_args).and_return(@panel) + Panel.should_receive(:edit).exactly(1) + put :update, :id => @panel.id, :panel => @attr + end + it 'モデルに保存依頼する' do + Panel.any_instance.should_receive(:store).exactly(1) + put :update, :id => @panel.id, :panel => @attr + end + it "@panelに作成されたコマを保持していて、それがDBにある" do + put :update, :id => @panel.id, :panel => @attr + assigns(:panel).should be_a(Panel) + assigns(:panel).should be_persisted end context 'html形式' do it 'ステータスコード302 Foundを返す' do - Panel.any_instance.stub(:update_attributes).with(any_args()).and_return(true) - put :update, :id => @panel.id, :panel => Factory.attributes_for(:panel, :author => @author.id) + Panel.any_instance.stub(:store).and_return(true) + put :update, :id => @panel.id, :panel => @attr response.status.should eq 302 end - it '更新されたコマの表示ページへ遷移する' do - put :update, :id => @panel.id, :panel => Factory.attributes_for(:panel, :author => @author.id) - response.should redirect_to(@panel) + it '作成されたコマの表示ページへ遷移する' do +# Panel.any_instance.stub(:store).and_return(true) + put :update, :id => @panel.id, :panel => @attr + response.should redirect_to(Panel.last) end end context 'json形式' do it 'ステータスコード200 OKを返す' do - Panel.any_instance.stub(:update_attributes).with(any_args()).and_return(true) - put :update, :id => @panel.id, :panel => Factory.attributes_for(:panel, :author => @author.id), :format => :json +# Panel.any_instance.stub(:store).and_return(true) + put :update, :id => @panel.id, :panel => @attr, :format => :json response.should be_success end it 'ページ本体は特に返さない' do - Panel.any_instance.stub(:update_attributes).with(any_args()).and_return(true) - put :update, :id => @panel.id, :panel => Factory.attributes_for(:panel, :author => @author.id), :format => :json + Panel.any_instance.stub(:save).with(any_args()).and_return(true) + put :update, :id => @panel.id, :panel => @attr, :format => :json response.body.should match /./ end end end - context '作家権限がないとき' do + context 'ユーザ権限がないとき' do before do sign_out @user end - it 'ステータスコード302 Foundを返す' do - put :update, :id => @panel.id, :panel => Factory.attributes_for(:panel, :author => @author.id) - response.status.should eq 302 - end context 'html形式' do + it 'ステータスコード302 Foundを返す' do + put :update, :id => @panel.id, :panel => @attr + response.status.should eq 302 + end it 'サインインページへ遷移する' do - put :update, :id => @panel.id, :panel => Factory.attributes_for(:panel, :author => @author.id) + put :update, :id => @panel.id, :panel => @attr response.body.should redirect_to '/users/sign_in' end end context 'json形式' do + it 'ステータスコード401 Unauthorizedを返す' do + put :update, :id => @panel.id, :panel => @attr, :format => :json + response.status.should eq 401 + end it '応答メッセージにUnauthorizedを返す' do - put :update, :id => @panel.id, :panel => Factory.attributes_for(:panel, :author => @author.id), :format => :json + put :update, :id => @panel.id, :panel => @attr, :format => :json response.message.should match(/Unauthorized/) end end end - context '検証、保存に失敗したとき' do + context 'ユーザ権限はないが管理者権限があるとき' do + before do + sign_out @user + sign_in @admin + end + context 'html形式' do + it 'ステータスコード302 Foundを返す' do + put :update, :id => @panel.id, :panel => @attr + response.status.should eq 302 + end + it 'サインインページへ遷移する' do + put :update, :id => @panel.id, :panel => @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 => @panel.id, :panel => @attr + response.status.should eq 302 + end + it '作家登録ページへ遷移する' do + put :update, :id => @panel.id, :panel => @attr + response.body.should redirect_to new_author_path + end + end + end + context '検証、保存に失敗した' do before do - Panel.any_instance.stub(:update_attributes).and_return(false) + Panel.any_instance.stub(:store).and_return(false) + end + it "指定のコマを保持している" do + put :update, :id => @panel.id, :panel => @attr + assigns(:panel).should eq @panel end context 'html形式' do - it 'ステータスコード200 Okを返す' do - put :update, :id => @panel.id, :panel => Factory.attributes_for(:panel, :author => @author.id) + it 'ステータスコード200 OKを返す' do + put :update, :id => @panel.id, :panel => @attr response.status.should eq 200 end it '編集ページを描画する' do - put :update, :id => @panel.id, :panel => Factory.attributes_for(:panel, :author => @author.id) + put :update, :id => @panel.id, :panel => @attr response.should render_template("edit") end end context 'json形式' do it 'ステータスコード422 unprocessable_entity を返す' do - Panel.any_instance.stub(:update_attributes).and_return(false) - put :update, :id => @panel.id, :panel => Factory.attributes_for(:panel, :author => @author.id), :format => :json + put :update, :id => @panel.id, :panel => @attr, :format => :json response.status.should eq 422 end it '応答メッセージUnprocessable Entityを返す' do - put :update, :id => @panel.id, :panel => Factory.attributes_for(:panel, :author => @author.id), :format => :json + put :update, :id => @panel.id, :panel => @attr, :format => :json response.message.should match(/Unprocessable/) end end end end - -end + + describe '削除に於いて' do + before do + @panel = FactoryGirl.create :panel, :author_id => @user.author.id + Panel.stub(:edit).and_return(@panel) + sign_in @user + end + context 'つつがなく終わるとき' do + it 'コマモデルに編集取得を問い合わせている' do + Panel.should_receive(:edit).exactly(1) + delete :destroy, :id => @panel.id + end + it '@panelにアレを取得している' do + delete :destroy, :id => @panel.id + assigns(:panel).id.should eq(@panel.id) + end + it 'そのコマを削除する' do + lambda { + delete :destroy, :id => @panel.id + }.should change(Panel, :count) + end + context 'html形式' do + it 'ステータスコード302 Foundを返す' do + delete :destroy, :id => @panel.id + response.status.should eq 302 + end + it 'マイコマ一覧ページへ遷移する' do + delete :destroy, :id => @panel.id + response.should redirect_to('/home/panels') + end + end + context 'json形式' do + it 'ステータスコード200 OKを返す' do + delete :destroy, :id => @panel.id, :format => :json + response.should be_success + end + end + end + context 'ユーザ権限がないとき' do + before do + sign_out @user + end + context 'html形式' do + it 'ステータスコード302 Foundを返す' do + delete :destroy, :id => @panel.id + response.status.should eq 302 + end + it 'サインインページへ遷移する' do + delete :destroy, :id => @panel.id + response.body.should redirect_to '/users/sign_in' + end + end + context 'json形式' do + it 'ステータスコード401 Unauthorizedを返す' do + delete :destroy, :id => @panel.id, :format => :json + response.status.should eq 401 + end + it '応答メッセージにUnauthorizedを返す' do + delete :destroy, :id => @panel.id, :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 + delete :destroy, :id => @panel.id + response.status.should eq 302 + end + it 'サインインページへ遷移する' do + delete :destroy, :id => @panel.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 + delete :destroy, :id => @panel.id + response.status.should eq 302 + end + it '作家登録ページへ遷移する' do + delete :destroy, :id => @panel.id + response.body.should redirect_to new_author_path + end + end + end + context '削除に失敗したとき' do + before do + Panel.any_instance.stub(:destroy_with_elements).and_return(false) + end + context 'html形式' do + it 'ステータスコード302 Foundを返す' do + delete :destroy, :id => @panel.id + response.status.should eq 302 + end + it 'そのコマの詳細ページへ遷移する' do + delete :destroy, :id => @panel.id + response.should redirect_to(panel_path(@panel)) + end + end + context 'json形式' do + it 'ステータスコード422 unprocessable_entity を返す' do + delete :destroy, :id => @panel.id, :format => :json + response.status.should eq 422 + end + it '応答メッセージUnprocessable Entityを返す' do + delete :destroy, :id => @panel.id, :format => :json + response.message.should match(/Unprocessable/) + end + end + end +=begin + context '対象コマがないとき' do + end + context '他人のコマだったとき' do + end +=end + end + +else + describe '一覧表示に於いて' do + before do + @panel = FactoryGirl.create :panel, :author_id => @author.id + Panel.stub(:list).and_return([@panel, @panel, @panel]) + sign_in @user + 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 + @panel = FactoryGirl.create :panel, :author_id => @user.author.id + Panel.stub(:show).with(@panel.id.to_s, [nil, nil]).and_return(@panel) + Panel.stub(:show).with(@panel.id.to_s, [@user, nil]).and_return(@panel) + Panel.stub(:show).with(@panel.id.to_s, [nil, @admin]).and_return(@panel) + sign_in @user + end + context 'つつがなく終わるとき' do + context 'html形式' do + it 'ステータスコード200 OKを返す' do + get :show, :id => @panel.id + response.should be_success + end + it 'showテンプレートを描画する' do + get :show, :id => @panel.id + response.should render_template("show") + end + end + context 'json形式' do + it 'ステータスコード200 OKを返す' do + get :show, :id => @panel.id, :format => :json + response.should be_success + end + it 'jsonデータを返す' do + get :show, :id => @panel.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 => @panel.id + response.should be_success + end + it 'showテンプレートを描画する' do + get :show, :id => @panel.id + response.should render_template("show") + end + end + context 'json形式' do + it 'ステータスコード200 OKを返す' do + get :show, :id => @panel.id, :format => :json + response.should be_success + end + it 'jsonデータを返す' do + get :show, :id => @panel.id, :format => :json + lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError) + end + end + end + end + + describe 'コマ数取得に於いて' do + before do + Panel.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 '新規作成フォーム表示に於いて' do + before do + sign_in @user + end + context 'つつがなく終わるとき' do + context 'html形式' do + it 'ステータスコード200 OKを返す' do + get :new + response.should be_success + end + 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 + end + end + + describe '新規作成に於いて' do + before do + @panel = FactoryGirl.build :panel, :author_id => @author.id + @attr = FactoryGirl.attributes_for(:panel, :author_id => @author.id) + sign_in @user + end + context 'つつがなく終わるとき' do + context 'html形式' do + it 'ステータスコード302 Foundを返す' do + Panel.any_instance.stub(:store).and_return(true) + post :create, :panel => @attr + response.status.should eq 302 + end + it '作成されたコマの表示ページへ遷移する' do +# Panel.any_instance.stub(:store).and_return(true) + post :create, :panel => @attr + response.should redirect_to(Panel.last) + end + end + context 'json形式' do + it 'ステータスコード200 OKを返す' do +# Panel.any_instance.stub(:store).and_return(true) + post :create, :panel => @attr, :format => :json + response.should be_success + end + it '作成されたコマをjsonデータで返す' do + post :create, :panel => @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, :panel => @attr + response.status.should eq 302 + end + it 'サインインページへ遷移する' do + post :create, :panel => @attr + response.body.should redirect_to '/users/sign_in' + end + end + context 'json形式' do + it 'ステータスコード401 Unauthorizedを返す' do + post :create, :panel => @attr, :format => :json + response.status.should eq 401 + end + it '応答メッセージにUnauthorizedを返す' do + post :create, :panel => @attr, :format => :json + response.message.should match(/Unauthorized/) + end + end + end + end + + describe '編集フォーム表示に於いて' do + before do + @panel = FactoryGirl.create :panel, :author_id => @author.id + sign_in @user + Panel.stub(:show).and_return(@panel) + end + context 'つつがなく終わるとき' do + context 'html形式' do + it 'ステータスコード200 OKを返す' do + get :edit, :id => @panel.id + response.should be_success + end + it 'editテンプレートを描画する' do + get :edit, :id => @panel.id + response.should render_template("edit") + end + end + context 'js形式' do + it 'ステータスコード200 OKを返す' do + get :edit, :id => @panel.id, :format => :js + response.should be_success + end + it 'edit.jsテンプレートを描画する' do + get :edit, :id => @panel.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 => @panel.id + response.status.should eq 302 + end + it 'サインインページへ遷移する' do + get :edit, :id => @panel.id + response.body.should redirect_to '/users/sign_in' + end + end + context 'js形式' do + it 'ステータスコード401 Unauthorizedを返す' do + get :edit, :id => @panel.id, :format => :js + response.status.should eq 401 + end + it '応答メッセージにUnauthorizedを返す' do + get :edit, :id => @panel.id, :format => :js + response.message.should match(/Unauthorized/) + end + end + end + end + + describe '更新に於いて' do + before do + @panel = FactoryGirl.create :panel, :author_id => @user.author.id + @attr = FactoryGirl.attributes_for(:panel, :author_id => @author.id) + sign_in @user + end + context 'つつがなく終わるとき' do + context 'html形式' do + it 'ステータスコード302 Foundを返す' do + Panel.any_instance.stub(:store).and_return(true) + put :update, :id => @panel.id, :panel => @attr + response.status.should eq 302 + end + it '作成されたコマの表示ページへ遷移する' do +# Panel.any_instance.stub(:store).and_return(true) + put :update, :id => @panel.id, :panel => @attr + response.should redirect_to(Panel.last) + end + end + context 'json形式' do + it 'ステータスコード200 OKを返す' do +# Panel.any_instance.stub(:store).and_return(true) + put :update, :id => @panel.id, :panel => @attr, :format => :json + response.should be_success + end + it 'ページ本体は特に返さない' do + Panel.any_instance.stub(:save).with(any_args()).and_return(true) + put :update, :id => @panel.id, :panel => @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 => @panel.id, :panel => @attr + response.status.should eq 302 + end + it 'サインインページへ遷移する' do + put :update, :id => @panel.id, :panel => @attr + response.body.should redirect_to '/users/sign_in' + end + end + context 'json形式' do + it 'ステータスコード401 Unauthorizedを返す' do + put :update, :id => @panel.id, :panel => @attr, :format => :json + response.status.should eq 401 + end + it '応答メッセージにUnauthorizedを返す' do + put :update, :id => @panel.id, :panel => @attr, :format => :json + response.message.should match(/Unauthorized/) + end + end + end + end + + describe '削除に於いて' do + before do + @panel = FactoryGirl.create :panel, :author_id => @user.author.id + Panel.stub(:edit).and_return(@panel) + sign_in @user + end + context 'つつがなく終わるとき' do + context 'html形式' do + it 'ステータスコード302 Foundを返す' do + delete :destroy, :id => @panel.id + response.status.should eq 302 + end + it 'マイコマ一覧ページへ遷移する' do + delete :destroy, :id => @panel.id + response.should redirect_to('/home/panel') + end + end + context 'json形式' do + it 'ステータスコード200 OKを返す' do + delete :destroy, :id => @panel.id, :format => :json + response.should be_success + end + end + end + context 'ユーザ権限がないとき' do + before do + sign_out @user + end + context 'html形式' do + it 'ステータスコード302 Foundを返す' do + delete :destroy, :id => @panel.id + response.status.should eq 302 + end + it 'サインインページへ遷移する' do + delete :destroy, :id => @panel.id + response.body.should redirect_to '/users/sign_in' + end + end + context 'json形式' do + it 'ステータスコード401 Unauthorizedを返す' do + delete :destroy, :id => @panel.id, :format => :json + response.status.should eq 401 + end + it '応答メッセージにUnauthorizedを返す' do + delete :destroy, :id => @panel.id, :format => :json + response.message.should match(/Unauthorized/) + end + end + end + end + +end +end +