X-Git-Url: http://git.osdn.net/view?a=blobdiff_plain;f=spec%2Fcontrollers%2Fstories_controller_spec.rb;h=0268c4ec261d275c90237036e51a498a14b941b5;hb=135599effaad9a0b15ad52e46355e885ec0ddc94;hp=2b325edd9b4a59424849072036a2e26564218b53;hpb=418be292310a1adaa5113d2eeaf668aa97fce0b1;p=pettanr%2Fpettanr.git diff --git a/spec/controllers/stories_controller_spec.rb b/spec/controllers/stories_controller_spec.rb index 2b325edd..0268c4ec 100644 --- a/spec/controllers/stories_controller_spec.rb +++ b/spec/controllers/stories_controller_spec.rb @@ -13,6 +13,7 @@ describe StoriesController do @panel = FactoryGirl.create :panel, :author_id => @author.id end +if MagicNumber['run_mode'] == 1 describe '一覧表示に於いて' do before do sign_in @user @@ -87,7 +88,7 @@ describe StoriesController do end end end - context '作家権限がないとき' do + context 'ユーザ権限がないとき' do before do sign_out @user end @@ -112,18 +113,39 @@ describe StoriesController do end end end + context 'ユーザ権限はないが管理者権限があるとき' do + before do + sign_out @user + sign_in @admin + end + it 'ステータスコード200 OKを返す' do + get :index + response.should be_success + end + end + context 'ユーザだが作家登録していないとき' do + before do + @author.destroy + end + it 'ステータスコード200 OKを返す' do + get :index + response.should be_success + end + end end describe '単体表示に於いて' do before do sign_in @user @story = FactoryGirl.create :story, :t => 0, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id - Comic.stub(:show).with(@comic.id.to_s, @author).and_return(@comic) - Story.stub(:show).with(@story.id.to_s, @author).and_return(@story) + Comic.stub(:show).with(@comic.id.to_s, [@user, nil]).and_return(@comic) + Comic.stub(:show).with(@comic.id.to_s, [nil, @admin]).and_return(@comic) + Story.stub(:show).with(@story.id.to_s, [@user, nil]).and_return(@story) + Story.stub(:show).with(@story.id.to_s, [nil, @admin]).and_return(@story) end context 'つつがなく終わるとき' do it 'ストーリーモデルに単体取得を問い合わせている' do - Story.should_receive(:show).with(@story.id.to_s, @author).exactly(1) + Story.should_receive(:show).with(@story.id.to_s, [@user, nil]).exactly(1) get :show, :id => @story.id end it '@storyにアレを取得している' do @@ -162,7 +184,7 @@ describe StoriesController do end end end - context '作家権限がないとき' do + context 'ユーザ権限がないとき' do before do sign_out @user end @@ -187,12 +209,32 @@ describe StoriesController do end end end + context 'ユーザ権限はないが管理者権限があるとき' do + before do + sign_out @user + sign_in @admin + end + it 'ステータスコード200 OKを返す' do + get :show, :id => @story.id + response.should be_success + end + end + context 'ユーザだが作家登録していないとき' do + before do + @author.destroy + end + it 'ステータスコード200 OKを返す' do + get :show, :id => @story.id + response.should be_success + end + end end describe '閲覧に於いて' do before do @story = FactoryGirl.create :story, :t => 0, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id - Comic.stub(:show).with(@comic.id.to_s, @author).and_return(@comic) + Comic.stub(:show).with(@comic.id.to_s, [@user, nil]).and_return(@comic) + Comic.stub(:show).with(@comic.id.to_s, [nil, @admin]).and_return(@comic) Story.stub(:count).and_return(10) Story.stub(:play_list).with(any_args).and_return([@story, @story, @story]) sign_in @user @@ -225,7 +267,7 @@ describe StoriesController do end context '事前チェックする' do it 'コミックモデルに単体取得を問い合わせている' do - Comic.should_receive(:show).with(@comic.id.to_s, @author).exactly(1) + Comic.should_receive(:show).with(@comic.id.to_s, [@user, nil]).exactly(1) get :comic, :id => @comic.id end it 'ストーリーモデルにプレイリスト取得を問い合わせている' do @@ -275,7 +317,7 @@ describe StoriesController do end end end - context '作家権限がないとき' do + context 'ユーザ権限がないとき' do before do sign_out @user end @@ -300,6 +342,25 @@ describe StoriesController do end end end + context 'ユーザ権限はないが管理者権限があるとき' do + before do + sign_out @user + sign_in @admin + end + it 'ステータスコード200 OKを返す' do + get :comic, :id => @comic.id + response.should be_success + end + end + context 'ユーザだが作家登録していないとき' do + before do + @author.destroy + end + it 'ステータスコード200 OKを返す' do + get :comic, :id => @comic.id + response.should be_success + end + end end describe '新規作成フォーム表示に於いて' do @@ -357,7 +418,7 @@ describe StoriesController do end end end - context '作家権限がないとき' do + context 'ユーザ権限がないとき' do before do sign_out @user end @@ -382,6 +443,37 @@ describe StoriesController 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 @@ -454,7 +546,7 @@ describe StoriesController do end end end - context '作家権限がないとき' do + context 'ユーザ権限がないとき' do before do sign_out @user end @@ -479,6 +571,37 @@ describe StoriesController do end end end + context 'ユーザ権限はないが管理者権限があるとき' do + before do + sign_out @user + sign_in @admin + end + context 'html形式' do + it 'ステータスコード302 Foundを返す' do + post :create, :story => @attr + response.status.should eq 302 + end + it 'サインインページへ遷移する' do + post :create, :story => @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, :story => @attr + response.status.should eq 302 + end + it '作家登録ページへ遷移する' do + post :create, :story => @attr + response.body.should redirect_to new_author_path + end + end + end context '検証、保存に失敗した' do before do Story.any_instance.stub(:store).and_return(false) @@ -546,7 +669,7 @@ describe StoriesController do end end end - context '作家権限がないとき' do + context 'ユーザ権限がないとき' do before do sign_out @user end @@ -571,6 +694,37 @@ describe StoriesController 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 => @story.id + response.status.should eq 302 + end + it 'サインインページへ遷移する' do + get :edit, :id => @story.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 => @story.id + response.status.should eq 302 + end + it '作家登録ページへ遷移する' do + get :edit, :id => @story.id + response.body.should redirect_to new_author_path + end + end + end end describe '更新に於いて' do @@ -623,7 +777,7 @@ describe StoriesController do end end end - context '作家権限がないとき' do + context 'ユーザ権限がないとき' do before do sign_out @user end @@ -648,6 +802,37 @@ describe StoriesController do end end end + context 'ユーザ権限はないが管理者権限があるとき' do + before do + sign_out @user + sign_in @admin + end + context 'html形式' do + it 'ステータスコード302 Foundを返す' do + put :update, :id => @story.id, :story => @attr + response.status.should eq 302 + end + it 'サインインページへ遷移する' do + put :update, :id => @story.id, :story => @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 => @story.id, :story => @attr + response.status.should eq 302 + end + it '作家登録ページへ遷移する' do + put :update, :id => @story.id, :story => @attr + response.body.should redirect_to new_author_path + end + end + end context '検証、保存に失敗した' do before do Story.any_instance.stub(:store).and_return(false) @@ -685,28 +870,36 @@ describe StoriesController do sign_in @user Story.stub(:edit).and_return(@story) end - context 'つつがなく終わるとき' do + context '事前チェックしておく' do + before do + Story.stub(:edit).with(any_args()).and_return @story + Story.any_instance.stub(:destroy_and_shorten).with(any_args()).and_return(true) + end it 'ストーリーモデルに編集取得を問い合わせている' do Story.should_receive(:edit).exactly(1) delete :destroy, :id => @story.id end + it 'モデルに削除を依頼する' do + Story.any_instance.should_receive(:destroy_and_shorten).exactly(1) + delete :destroy, :id => @story.id + end it '@storyにアレを取得している' do delete :destroy, :id => @story.id assigns(:story).id.should eq(@story.id) end - it 'そのストーリーを一つのトランザクションで削除する' do - lambda { - delete :destroy, :id => @story.id - }.should change(Story, :count) + end + context 'つつがなく終わるとき' do + before do + Story.any_instance.stub(:destroy_and_shorten).with(any_args()).and_return(true) end context 'html形式' do it 'ステータスコード302 Foundを返す' do delete :destroy, :id => @story.id response.status.should eq 302 end - it 'ストーリー一覧ページへ遷移する' do + it '閲覧ページへ遷移する' do delete :destroy, :id => @story.id - response.should redirect_to(story_path(@story.comic_id)) + response.should redirect_to(:controller => 'stories', :action => :comic, :id => @story.comic_id) end end context 'json形式' do @@ -716,7 +909,7 @@ describe StoriesController do end end end - context '作家権限がないとき' do + context 'ユーザ権限がないとき' do before do sign_out @user end @@ -741,6 +934,62 @@ describe StoriesController do end end end + context 'ユーザ権限はないが管理者権限があるとき' do + before do + sign_out @user + sign_in @admin + end + context 'html形式' do + it 'ステータスコード302 Foundを返す' do + delete :destroy, :id => @story.id + response.status.should eq 302 + end + it 'サインインページへ遷移する' do + delete :destroy, :id => @story.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 => @story.id + response.status.should eq 302 + end + it '作家登録ページへ遷移する' do + delete :destroy, :id => @story.id + response.body.should redirect_to new_author_path + end + end + end + context '削除に失敗したとき' do + before do + Story.any_instance.stub(:destroy_and_shorten).with(any_args()).and_return(false) + end + context 'html形式' do + it 'ステータスコード302 Foundを返す' do + delete :destroy, :id => @story.id + response.status.should eq 302 + end + it 'そのコミックの詳細ページへ遷移する' do + delete :destroy, :id => @story.id + response.should redirect_to(story_path(@story)) + end + end + context 'json形式' do + it 'ステータスコード422 unprocessable_entity を返す' do + delete :destroy, :id => @story.id, :format => :json + response.status.should eq 422 + end + it '応答メッセージUnprocessable Entityを返す' do + delete :destroy, :id => @story.id, :format => :json + response.message.should match(/Unprocessable/) + end + end + end =begin context '対象ストーリーがないとき' do end @@ -749,4 +998,455 @@ describe StoriesController do =end end +else + describe '一覧表示に於いて' do + before do + sign_in @user + @story = FactoryGirl.create :story, :t => 0, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id + Story.stub(:list).and_return([@story, @story, @story]) + 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 + sign_in @user + @story = FactoryGirl.create :story, :t => 0, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id + Comic.stub(:show).with(@comic.id.to_s, [nil, nil]).and_return(@comic) + Comic.stub(:show).with(@comic.id.to_s, [@user, nil]).and_return(@comic) + Comic.stub(:show).with(@comic.id.to_s, [nil, @admin]).and_return(@comic) + Story.stub(:show).with(@story.id.to_s, [nil, nil]).and_return(@story) + Story.stub(:show).with(@story.id.to_s, [@user, nil]).and_return(@story) + Story.stub(:show).with(@story.id.to_s, [nil, @admin]).and_return(@story) + end + context 'つつがなく終わるとき' do + context 'html形式' do + it 'ステータスコード200 OKを返す' do + get :show, :id => @story.id + response.should be_success + end + it 'showテンプレートを描画する' do + get :show, :id => @story.id + response.should render_template("show") + end + end + context 'json形式' do + it 'ステータスコード200 OKを返す' do + get :show, :id => @story.id, :format => :json + response.should be_success + end + it 'jsonデータを返す' do + get :show, :id => @story.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 => @story.id + response.should be_success + end + it 'showテンプレートを描画する' do + get :show, :id => @story.id + response.should render_template("show") + end + end + context 'json形式' do + it 'ステータスコード200 OKを返す' do + get :show, :id => @story.id, :format => :json + response.should be_success + end + it 'jsonデータを返す' do + get :show, :id => @story.id, :format => :json + lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError) + end + end + end + end + + describe '閲覧に於いて' do + before do + @story = FactoryGirl.create :story, :t => 0, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id + Comic.stub(:show).with(@comic.id.to_s, [nil, nil]).and_return(@comic) + Comic.stub(:show).with(@comic.id.to_s, [@user, nil]).and_return(@comic) + Comic.stub(:show).with(@comic.id.to_s, [nil, @admin]).and_return(@comic) + Story.stub(:count).and_return(10) + Story.stub(:play_list).with(any_args).and_return([@story, @story, @story]) + sign_in @user + end + context 'つつがなく終わるとき' do + context 'html形式' do + it 'ステータスコード200 OKを返す' do + get :comic, :id => @comic.id + response.should be_success + end + it 'comicテンプレートを描画する' do + get :comic, :id => @comic.id + response.should render_template("comic") + end + end + context 'json形式' do + it 'ステータスコード200 OKを返す' do + get :comic, :id => @comic.id, :format => :json + response.should be_success + end + it 'jsonデータを返す' do + get :comic, :id => @comic.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 :comic, :id => @comic.id + response.should be_success + end + it 'comicテンプレートを描画する' do + get :comic, :id => @comic.id + response.should render_template("comic") + end + end + context 'json形式' do + it 'ステータスコード200 OKを返す' do + get :comic, :id => @comic.id, :format => :json + response.should be_success + end + it 'jsonデータを返す' do + get :comic, :id => @comic.id, :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 'ステータスコード200 OKを返す' do + get :new, :format => :js + response.should be_success + end + it 'new.jsテンプレートを描画する' do + get :new, :format => :js + response.should render_template("new") + end + end + context 'json形式' do + it 'ステータスコード200 OKを返す' do + get :new, :format => :json + response.should be_success + end + 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 '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 + + describe '新規作成に於いて' do + before do + @attr = FactoryGirl.attributes_for(:story, :t => nil, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id) + sign_in @user + end + context 'つつがなく終わるとき' do + context 'html形式' do + it 'ステータスコード302 Foundを返す' do + Story.any_instance.stub(:store).and_return(true) + post :create, :story => @attr + response.status.should eq 302 + end + it 'コミックのストーリー表示へ遷移する' do +# Story.any_instance.stub(:store).and_return(true) + post :create, :story => @attr + response.should redirect_to(:action => :comic, :id => @attr[:comic_id]) + end + end + context 'json形式' do + it 'ステータスコード200 OKを返す' do +# Story.any_instance.stub(:store).and_return(true) + post :create, :story => @attr, :format => :json + response.should be_success + end + it '作成されたコマをjsonデータで返す' do + post :create, :story => @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, :story => @attr + response.status.should eq 302 + end + it 'サインインページへ遷移する' do + post :create, :story => @attr + response.body.should redirect_to '/users/sign_in' + end + end + context 'json形式' do + it 'ステータスコード401 Unauthorizedを返す' do + post :create, :story => @attr, :format => :json + response.status.should eq 401 + end + it '応答メッセージにUnauthorizedを返す' do + post :create, :story => @attr, :format => :json + response.message.should match(/Unauthorized/) + end + end + end + end + + describe '編集フォーム表示に於いて' do + before do + @story = FactoryGirl.create :story, :author_id => @author.id + sign_in @user + Story.stub(:show).and_return(@story) + end + context 'つつがなく終わるとき' do + context 'html形式' do + it 'ステータスコード200 OKを返す' do + get :edit, :id => @story.id + response.should be_success + end + it 'editテンプレートを描画する' do + get :edit, :id => @story.id + response.should render_template("edit") + end + end + context 'js形式' do + it 'ステータスコード200 OKを返す' do + get :edit, :id => @story.id, :format => :js + response.should be_success + end + it 'edit.jsテンプレートを描画する' do + get :edit, :id => @story.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 => @story.id + response.status.should eq 302 + end + it 'サインインページへ遷移する' do + get :edit, :id => @story.id + response.body.should redirect_to '/users/sign_in' + end + end + context 'js形式' do + it 'ステータスコード401 Unauthorizedを返す' do + get :edit, :id => @story.id, :format => :js + response.status.should eq 401 + end + it '応答メッセージにUnauthorizedを返す' do + get :edit, :id => @story.id, :format => :js + response.message.should match(/Unauthorized/) + end + end + end + end + + describe '更新に於いて' do + before do + @story = FactoryGirl.create :story, :author_id => @user.author.id + @attr = FactoryGirl.attributes_for(:story, :author_id => @author.id) + sign_in @user + end + context 'つつがなく終わるとき' do + context 'html形式' do + it 'ステータスコード302 Foundを返す' do + Story.any_instance.stub(:store).and_return(true) + put :update, :id => @story.id, :story => @attr + response.status.should eq 302 + end + it 'ストーリー表示へ遷移する' do +# Story.any_instance.stub(:store).and_return(true) + put :update, :id => @story.id, :story => @attr + response.should redirect_to(:action => :comic, :id => @attr[:comic_id]) + end + end + context 'json形式' do + it 'ステータスコード200 OKを返す' do +# Story.any_instance.stub(:store).and_return(true) + put :update, :id => @story.id, :story => @attr, :format => :json + response.should be_success + end + end + end + context 'ユーザ権限がないとき' do + before do + sign_out @user + end + context 'html形式' do + it 'ステータスコード302 Foundを返す' do + put :update, :id => @story.id, :story => @attr + response.status.should eq 302 + end + it 'サインインページへ遷移する' do + put :update, :id => @story.id, :story => @attr + response.body.should redirect_to '/users/sign_in' + end + end + context 'json形式' do + it 'ステータスコード401 Unauthorizedを返す' do + put :update, :id => @story.id, :story => @attr, :format => :json + response.status.should eq 401 + end + it '応答メッセージにUnauthorizedを返す' do + put :update, :id => @story.id, :story => @attr, :format => :json + response.message.should match(/Unauthorized/) + end + end + end + end + + describe '削除に於いて' do + before do + @story = FactoryGirl.create :story, :author_id => @author.id + sign_in @user + Story.stub(:edit).and_return(@story) + end + context 'つつがなく終わるとき' do + before do + Story.any_instance.stub(:destroy_and_shorten).with(any_args()).and_return(true) + end + context 'html形式' do + it 'ステータスコード302 Foundを返す' do + delete :destroy, :id => @story.id + response.status.should eq 302 + end + it '閲覧ページへ遷移する' do + delete :destroy, :id => @story.id + response.should redirect_to(:controller => 'stories', :action => :comic, :id => @story.comic_id) + end + end + context 'json形式' do + it 'ステータスコード200 OKを返す' do + delete :destroy, :id => @story.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 => @story.id + response.status.should eq 302 + end + it 'サインインページへ遷移する' do + delete :destroy, :id => @story.id + response.body.should redirect_to '/users/sign_in' + end + end + context 'json形式' do + it 'ステータスコード401 Unauthorizedを返す' do + delete :destroy, :id => @story.id, :format => :json + response.status.should eq 401 + end + it '応答メッセージにUnauthorizedを返す' do + delete :destroy, :id => @story.id, :format => :json + response.message.should match(/Unauthorized/) + end + end + end + end + +end end