X-Git-Url: http://git.osdn.net/view?a=blobdiff_plain;f=spec%2Fcontrollers%2Fcomics_controller_spec.rb;h=449cbcbec14babba0fb428f9c0a7c64eef28c1b6;hb=0bdfae60dc58932c99849c31d36ee5d7715db782;hp=7d93e76ad68f005ecc197906432857738d0a4bdf;hpb=56f2b3cb59970e91bf7c9f94c53ca48c2b7054bf;p=pettanr%2Fpettanr.git diff --git a/spec/controllers/comics_controller_spec.rb b/spec/controllers/comics_controller_spec.rb index 7d93e76a..449cbcbe 100644 --- a/spec/controllers/comics_controller_spec.rb +++ b/spec/controllers/comics_controller_spec.rb @@ -1,650 +1,1301 @@ # -*- encoding: utf-8 -*- -require 'spec_helper' - -describe ComicsController do - before do - Factory :admin - @user = Factory :user_yas - @author = @user.author #ユーザ作成時に連動して作成される - end - - describe '一覧表示に於いて' do - before do - @comic = Factory :normal_comic, :author_id => @user.author.id - Comic.stub(:list).and_return([@comic, @comic, @comic]) - 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 +require 'spec_helper' +#コミック +describe ComicsController do + before do + @admin =FactoryGirl.create :admin + @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 + end + +if MagicNumber['run_mode'] == 1 + describe '一覧表示に於いて' do + before do + @comic = FactoryGirl.create :comic, :author_id => @user.author.id + Comic.stub(:list).and_return([@comic, @comic, @comic]) + 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 Comic.default_page_size + end + it '最大を超えると@page_sizeにデフォルト最大値が入る' do + get :index, :page_size => 1500 + assigns(:page_size).should eq Comic.max_page_size + end + it '不正な値が入ると@page_sizeにデフォルト最大値が入る' do + get :index, :page_size => 0 assigns(:page_size).should eq Comic.default_page_size - end - it '最大を超えると@page_sizeにデフォルト最大値が入る' do - get :index, :page_size => 1500 - assigns(:page_size).should eq Comic.max_page_size - end - it '不正な値が入ると@page_sizeにデフォルト最大値が入る' do - get :index, :page_size => 0 - assigns(:page_size).should eq Comic.default_page_size - end - end - context 'つつがなく終わるとき' do - it 'ステータスコード200 OKを返す' do - get :index - response.should be_success - end - it 'コミックモデルに一覧を問い合わせている' do + end + end + context 'つつがなく終わるとき' do + it 'ステータスコード200 OKを返す' do + get :index + response.should be_success + end + it 'コミックモデルに一覧を問い合わせている' do Comic.should_receive(:list).exactly(1) - get :index - end - it '@comicsにリストを取得している' do - get :index - assigns(:comics).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 'データがリスト構造になっている' do - get :index, :format => :json + get :index + end + it '@comicsにリストを取得している' do + get :index + assigns(:comics).should have_at_least(3).items + end + context 'html形式' do + it '@paginateにページ制御を取得している' do + get :index + assigns(:paginate).is_a?(Kaminari::PaginatableArray).should be_true + end + 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 + Comic.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.should have_at_least(3).items - end - it 'リストの先頭くらいはコミックっぽいものであって欲しい' do - get :index, :format => :json - json = JSON.parse response.body - json.first.has_key?("title").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 + json.first.has_key?("title").should be_true + json.first.has_key?("visible").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 - end - - describe '単体表示に於いて' do - before do - @comic = Factory :normal_comic, :author_id => @user.author.id - Comic.stub(:show).and_return(@comic) - sign_in @user - end - context 'つつがなく終わるとき' do - it 'ステータスコード200 OKを返す' do - get :show, :id => @comic.id - response.should be_success - end - it 'コミックモデルに単体取得を問い合わせている' do + 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 + @author.destroy + end + context 'html形式' do + it 'ステータスコード200 OKを返す' do + get :index + response.should be_success + end + end + end + end + + describe '単体表示に於いて' do + before do + @comic = FactoryGirl.create :comic, :author_id => @user.author.id, :title => 'normal' + Comic.stub(:show).and_return(@comic) + sign_in @user + end + context 'つつがなく終わるとき' do + it 'ステータスコード200 OKを返す' do + get :show, :id => @comic.id + response.should be_success + end + it 'コミックモデルに単体取得を問い合わせている' do Comic.should_receive(:show).exactly(1) - get :show - end - it '@comicにアレを取得している' do - get :show, :id => @comic.id - assigns(:comic).id.should eq(@comic.id) - end - context 'html形式' do - it 'showテンプレートを描画する' do - get :show, :id => @comic.id - response.should render_template("show") - end - end - context 'json形式' do - it 'jsonデータを返す' do - get :show, :id => @comic.id, :format => :json - lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError) - end - it 'データがアレになっている' do - get :show, :id => @comic.id, :format => :json - json = JSON.parse response.body - json["title"].should match(/normal/) - end - end - end - context '作家権限がないとき' do - before do - sign_out @user - end - context 'html形式' do - it 'ステータスコード302 Foundを返す' do - get :show, :id => @comic.id - response.status.should eq 302 - end - it 'サインインページへ遷移する' do - get :show, :id => @comic.id - response.body.should redirect_to '/users/sign_in' - end - end - context 'json形式' do - it 'ステータスコード401 Unauthorizedを返す' do - get :show, :id => @comic.id, :format => :json - response.status.should eq 401 - end - it '応答メッセージにUnauthorizedを返す' do - get :show, :id => @comic.id, :format => :json - response.message.should match(/Unauthorized/) - end - end - end -=begin - context '対象コミックがないとき' do - context 'html形式' do - it '例外404 not_foundを返す' do + get :show + end + it '@comicにアレを取得している' do + get :show, :id => @comic.id + assigns(:comic).id.should eq(@comic.id) + end + context 'html形式' do + it 'showテンプレートを描画する' do + get :show, :id => @comic.id + response.should render_template("show") + end + end + context 'json形式' do + it 'jsonデータを返す' do + get :show, :id => @comic.id, :format => :json + lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError) + end + it 'コミックモデルにjson単体出力オプションを問い合わせている' do + Comic.should_receive(:show_json_opt).exactly(1) + get :show, :id => @comic.id, :format => :json + end + it 'データがアレになっている' do + get :show, :id => @comic.id, :format => :json + json = JSON.parse response.body + json["title"].should match(/normal/) + json["visible"].should_not be_nil + end + end + end + context 'ユーザ権限がないとき' do + before do + sign_out @user + end + context 'html形式' do + it 'ステータスコード302 Foundを返す' do + get :show, :id => @comic.id + response.status.should eq 302 + end + it 'サインインページへ遷移する' do + get :show, :id => @comic.id + response.body.should redirect_to '/users/sign_in' + end + end + context 'json形式' do + it 'ステータスコード401 Unauthorizedを返す' do + get :show, :id => @comic.id, :format => :json + response.status.should eq 401 + end + it '応答メッセージにUnauthorizedを返す' do + get :show, :id => @comic.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 => @comic.id + response.should be_success + end + end + context 'ユーザだが作家登録していないとき' do + before do + @author.destroy + end + context 'html形式' do + it 'ステータスコード200 OKを返す' do + get :show, :id => @comic.id + response.should be_success + end + 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 + get :show, :id => 0 + }.should raise_error(ActiveRecord::RecordNotFound) + end + end + context 'json形式' do + it '例外404 not_foundを返す' do lambda{ - get :show, :id => 0, :format => :json - }.should raise_error(ActiveRecord::RecordNotFound) - end - end - end - context '非公開コミックを見ようとしたとき' do - context 'html形式' do - it '例外403 forbiddenを返す' do - Comic.any_instance.stub(:visible?).with(any_args()).and_return(false) - hidden = Factory :hidden_comic, :author_id => @author.id + get :show, :id => 0, :format => :json + }.should raise_error(ActiveRecord::RecordNotFound) + end + end + end + context '非公開コミックを見ようとしたとき' do + context 'html形式' do + it '例外403 forbiddenを返す' do + Comic.any_instance.stub(:visible?).with(any_args()).and_return(false) + hidden = FactoryGirl.create :hidden_comic, :author_id => @author.id lambda{ - get :show, :id => hidden - }.should raise_error(ActiveRecord::Forbidden) - end - end - context 'json形式' do - it '例外403 forbiddenを返す' do - Comic.any_instance.stub(:visible?).with(any_args()).and_return(false) - hidden = Factory :hidden_comic, :author_id => @author.id + get :show, :id => hidden + }.should raise_error(ActiveRecord::Forbidden) + end + end + context 'json形式' do + it '例外403 forbiddenを返す' do + Comic.any_instance.stub(:visible?).with(any_args()).and_return(false) + hidden = FactoryGirl.create :hidden_comic, :author_id => @author.id lambda{ - get :show, :id => hidden, :format => :json - }.should raise_error(ActiveRecord::Forbidden) - end - end - end -=end - end - describe '閲覧に於いて' do - before do - @comic = Factory :normal_comic, :author_id => @user.author.id - Comic.stub(:show).and_return(@comic) - sign_in @user - end - context '事前チェックする' do - before do - Panel.stub(:count).and_return(10) - end - it '与えられたoffsetがセットされている' do - get :play, :id => @comic.id, :offset => 7 - assigns(:offset).should eq 7 - end - it '省略されると@offsetに0値が入る' do - get :play, :id => @comic.id - assigns(:offset).should eq 0 - end - it 'コマ数以上が与えられるとコマ数-1が入る' do - get :play, :id => @comic.id, :offset => 10 - assigns(:offset).should eq 9 - end - it '負の値が与えられると末尾(コマ数)からさかのぼった値が入る' do - get :play, :id => @comic.id, :offset => -3 - assigns(:offset).should eq 7 - end - it 'コマ数以上の負の値が与えられると計算できないので0値が入る' do - get :play, :id => @comic.id, :offset => -13 - assigns(:offset).should eq 0 - end - it '与えられたcountがセットされている' do - get :play, :id => @comic.id, :count => 18 - assigns(:count).should eq 18 - end - it '省略されると@countにデフォルト値が入る' do - get :play, :id => @comic.id - assigns(:count).should eq Comic.default_panel_size - end - it '最大を超えると@countにデフォルト最大値が入る' do - get :play, :id => @comic.id, :count => 1500 - assigns(:count).should eq Comic.max_panel_size - end - it '不正な値が入ると@countにデフォルト最大値が入る' do - get :play, :id => @comic.id, :page_size => 0 - assigns(:count).should eq Comic.default_panel_size - end - end - context 'つつがなく終わるとき' do - it 'ステータスコード200 OKを返す' do - get :play, :id => @comic.id - response.should be_success - end - it 'コミックモデルに単体取得を問い合わせている' do - Comic.should_receive(:show).exactly(1) - get :play, :id => @comic.id - end - it '@comicにアレを取得している' do - get :play, :id => @comic.id - assigns(:comic).id.should eq(@comic.id) - end - context 'html形式' do - it 'playテンプレートを描画する' do - get :play, :id => @comic.id - response.should render_template("play") - end - end - context 'json形式' do - it 'jsonデータを返す' do - get :play, :id => @comic.id, :format => :json - lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError) - end - it 'データがアレになっている' do - get :play, :id => @comic.id, :format => :json - json = JSON.parse response.body - json["title"].should match(/normal/) - end - end - end - context '作家権限がないとき' do - before do - sign_out @user - end - context 'html形式' do - it 'ステータスコード302 Foundを返す' do - get :play, :id => @comic.id - response.status.should eq 302 - end - it 'サインインページへ遷移する' do - get :play, :id => @comic.id - response.body.should redirect_to '/users/sign_in' - end - end - context 'json形式' do - it 'ステータスコード401 Unauthorizedを返す' do - get :play, :id => @comic.id, :format => :json - response.status.should eq 401 - end - it '応答メッセージにUnauthorizedを返す' do - get :play, :id => @comic.id, :format => :json - response.message.should match(/Unauthorized/) - end - end - end - end - - describe 'コミック数取得に於いて' do - before do - Comic.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構造になっていてコミック数が1である' do - get :count, :format => :json - json = JSON.parse response.body - json["count"].should == 3 - end - end - end - end - - describe '新規作成フォーム表示に於いて' do - before do - sign_in @user - end - context 'つつがなく終わるとき' do - it 'ステータスコード200 OKを返す' do - get :new - response.should be_success - end - it '@comicに新規データを用意している' do - get :new - assigns(:comic).should be_a_new(Comic) - end - it 'コミックモデルにデフォルト値補充を依頼している' do + get :show, :id => hidden, :format => :json + }.should raise_error(ActiveRecord::Forbidden) + end + end + end +=end + end + describe 'コミック数取得に於いて' do + before do + Comic.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構造になっていてコミック数が1である' do + get :count, :format => :json + json = JSON.parse response.body + json["count"].should == 3 + end + end + end + end + + describe '新規作成フォーム表示に於いて' do + before do + sign_in @user + end + context 'つつがなく終わるとき' do + it 'ステータスコード200 OKを返す' do + get :new + response.should be_success + end + it '@comicに新規データを用意している' do + get :new + assigns(:comic).should be_a_new(Comic) + end + it 'コミックモデルにデフォルト値補充を依頼している' do Comic.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 - 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 - sign_in @user - end - context 'つつがなく終わるとき' do - it 'モデルに保存依頼する' do - Comic.any_instance.should_receive(:save).exactly(1) - post :create, :comic => Factory.attributes_for(:normal_comic) - end - it "@comicに作成されたコミックを保持していて、それがDBにある" do - post :create, :comic => Factory.attributes_for(:normal_comic) - assigns(:comic).should be_a(Comic) - assigns(:comic).should be_persisted - end - context 'html形式' do - it 'ステータスコード302 Foundを返す' do - Comic.any_instance.stub(:save).and_return(true) - post :create, :comic => Factory.attributes_for(:normal_comic) - response.status.should eq 302 - end - it '作成されたコミックの表示ページへ遷移する' do -# Comic.any_instance.stub(:save).and_return(true) - post :create, :comic => Factory.attributes_for(:normal_comic) - response.should redirect_to(Comic.last) - end - end - context 'json形式' do - it 'ステータスコード200 OKを返す' do -# Comic.any_instance.stub(:save).and_return(true) - post :create, :comic => Factory.attributes_for(:normal_comic), :format => :json - response.should be_success - end - it '作成されたコミックをjsonデータで返す' do - post :create, :comic => Factory.attributes_for(:normal_comic), :format => :json - lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError) - end - it 'データがアレになっている' do - post :create, :comic => Factory.attributes_for(:normal_comic), :format => :json - json = JSON.parse response.body - json["title"].should match(/normal/) - end - end - end - context '作家権限がないとき' do - before do - sign_out @user - end - context 'html形式' do - it 'ステータスコード302 Foundを返す' do - post :create, :comic => Factory.attributes_for(:normal_comic) - response.status.should eq 302 - end - it 'サインインページへ遷移する' do - post :create, :comic => Factory.attributes_for(:normal_comic) - response.body.should redirect_to '/users/sign_in' - end - end - context 'json形式' do - it 'ステータスコード401 Unauthorizedを返す' do - post :create, :comic => Factory.attributes_for(:normal_comic), :format => :json - response.status.should eq 401 - end - it '応答メッセージにUnauthorizedを返す' do - post :create, :comic => Factory.attributes_for(:normal_comic), :format => :json - response.message.should match(/Unauthorized/) - end - end - end - context '検証、保存に失敗した' do - before do - Comic.any_instance.stub(:save).and_return(false) - end - it "未保存のコミックを保持している" do - post :create, :comic => {} - assigns(:comic).should be_a_new(Comic) - end - context 'html形式' do - it 'ステータスコード200 OKを返す' do - post :create, :comic => {} - response.status.should eq 200 - end - it '新規ページを描画する' do - post :create, :comic => {} - response.should render_template("new") - end - end - context 'json形式' do - it 'ステータスコード422 unprocessable_entity を返す' do - post :create, :comic => {}, :format => :json - response.status.should eq 422 - end - it '応答メッセージUnprocessable Entityを返す' do - post :create, :comic => {}, :format => :json - response.message.should match(/Unprocessable/) - end - end - end - end - - describe '編集フォーム表示に於いて' do - before do - @comic = Factory :normal_comic, :author_id => @user.author.id - sign_in @user - Comic.stub(:show).and_return(@comic) - end - context 'つつがなく終わるとき' do - it 'ステータスコード200 OKを返す' do - get :edit, :id => @comic.id - response.should be_success - end - it 'コミックモデルに単体取得を問い合わせている' do - Comic.should_receive(:show).exactly(1) - get :edit, :id => @comic.id - end - it '@comicにデータを用意している' do - get :edit, :id => @comic.id - assigns(:comic).should eq @comic - end - context 'html形式' do - it 'editテンプレートを描画する' do - get :edit, :id => @comic.id - response.should render_template("edit") - end - end - context 'js形式' do - it 'edit.jsテンプレートを描画する' do - get :edit, :id => @comic.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 => @comic.id - response.status.should eq 302 - end - it 'サインインページへ遷移する' do - get :edit, :id => @comic.id - response.body.should redirect_to '/users/sign_in' - end - end - context 'js形式' do - it 'ステータスコード401 Unauthorizedを返す' do - get :edit, :id => @comic.id, :format => :js - response.status.should eq 401 - end - it '応答メッセージにUnauthorizedを返す' do - get :edit, :id => @comic.id, :format => :js - response.message.should match(/Unauthorized/) - end - end - end - end - - describe '更新に於いて' do + 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 + Comic.should_receive(:show_json_opt).exactly(1) + get :new, :format => :json + 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 + 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, @attr + response.status.should eq 302 + end + it '作家登録ページへ遷移する' do + get :new, @attr + response.body.should redirect_to new_author_path + end + end + end + end + + describe '新規作成に於いて' do before do - @comic = Factory :normal_comic, :author => @author - sign_in @user - end - context '事前チェックしておく' do - it 'コミックモデルに単体取得を問い合わせている' do - Comic.stub(:show).with(any_args()).and_return @comic - Comic.should_receive(:show).exactly(1) - put :update, :id => @comic.id, :comic => Factory.attributes_for(:normal_comic) - end - it 'モデルに更新を依頼する' do - Comic.any_instance.should_receive(:update_attributes).with(any_args) - put :update, :id => @comic.id, :comic => Factory.attributes_for(:normal_comic) - end - it '@comicにアレを取得している' do - put :update, :id => @comic.id, :comic => Factory.attributes_for(:normal_comic) - assigns(:comic).id.should eq(@comic.id) - end - end - context 'つつがなく終わるとき' do + sign_in @user + @attr = FactoryGirl.attributes_for(:comic, :author_id => @author.id, :title => 'normal') + end + context '事前チェックしておく' do + it 'コミックモデルにデフォルト値補充を依頼している' do + Comic.any_instance.should_receive(:supply_default).exactly(1) + post :create, :comic => @attr + end + it 'コミックモデルにカラム値復元を依頼している' do + Comic.any_instance.should_receive(:attributes=).exactly(1) + post :create, :comic => @attr + end + it 'コミックモデルに上書き補充を依頼している' do + Comic.any_instance.should_receive(:overwrite).exactly(1) + post :create, :comic => @attr + end + it 'モデルに保存依頼する' do + Comic.any_instance.should_receive(:save).exactly(1) + post :create, :comic => @attr + end + end + context 'つつがなく終わるとき' do + it "@comicに作成されたコミックを保持していて、それがDBにある" do + post :create, :comic => @attr + assigns(:comic).should be_a(Comic) + assigns(:comic).should be_persisted + end + context 'html形式' do + it 'ステータスコード302 Foundを返す' do + Comic.any_instance.stub(:save).and_return(true) + post :create, :comic => @attr + response.status.should eq 302 + end + it '作成されたコミックの表示ページへ遷移する' do +# Comic.any_instance.stub(:save).and_return(true) + post :create, :comic => @attr + response.should redirect_to(Comic.last) + end + end + context 'json形式' do + it 'ステータスコード200 OKを返す' do +# Comic.any_instance.stub(:save).and_return(true) + post :create, :comic => @attr, :format => :json + response.should be_success + end + it '作成されたコミックをjsonデータで返す' do + post :create, :comic => @attr, :format => :json + lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError) + end + it 'データがアレになっている' do + post :create, :comic => @attr, :format => :json + json = JSON.parse response.body + json["title"].should match(/normal/) + json["visible"].should_not be_nil + end + end + end + context 'ユーザ権限がないとき' do + before do + sign_out @user + end + context 'html形式' do + it 'ステータスコード302 Foundを返す' do + post :create, :comic => @attr + response.status.should eq 302 + end + it 'サインインページへ遷移する' do + post :create, :comic => @attr + response.body.should redirect_to '/users/sign_in' + end + end + context 'json形式' do + it 'ステータスコード401 Unauthorizedを返す' do + post :create, :comic => @attr, :format => :json + response.status.should eq 401 + end + it '応答メッセージにUnauthorizedを返す' do + post :create, :comic => @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, :comic => @attr + response.status.should eq 302 + end + it 'サインインページへ遷移する' do + post :create, :comic => @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, :comic => @attr + response.status.should eq 302 + end + it '作家登録ページへ遷移する' do + post :create, :comic => @attr + response.body.should redirect_to new_author_path + end + end + end + context '検証、保存に失敗した' do + before do + Comic.any_instance.stub(:save).and_return(false) + end + it "未保存のコミックを保持している" do + post :create, :comic => @attr + assigns(:comic).should be_a_new(Comic) + end + context 'html形式' do + it 'ステータスコード200 OKを返す' do + post :create, :comic => @attr + response.status.should eq 200 + end + it '新規ページを描画する' do + post :create, :comic => @attr + response.should render_template("new") + end + end + context 'json形式' do + it 'ステータスコード422 unprocessable_entity を返す' do + post :create, :comic => @attr, :format => :json + response.status.should eq 422 + end + it '応答メッセージUnprocessable Entityを返す' do + post :create, :comic => @attr, :format => :json + response.message.should match(/Unprocessable/) + end + end + end + end + + describe '編集フォーム表示に於いて' do + before do + @comic = FactoryGirl.create :comic, :author_id => @user.author.id + sign_in @user + Comic.stub(:edit).and_return(@comic) + end + context 'つつがなく終わるとき' do + it 'ステータスコード200 OKを返す' do + get :edit, :id => @comic.id + response.should be_success + end + it 'コミックモデルに編集取得を問い合わせている' do + Comic.should_receive(:edit).exactly(1) + get :edit, :id => @comic.id + end + it '@comicにデータを用意している' do + get :edit, :id => @comic.id + assigns(:comic).should eq @comic + end + context 'html形式' do + it 'editテンプレートを描画する' do + get :edit, :id => @comic.id + response.should render_template("edit") + end + end + context 'js形式' do + it 'edit.jsテンプレートを描画する' do + get :edit, :id => @comic.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 => @comic.id + response.status.should eq 302 + end + it 'サインインページへ遷移する' do + get :edit, :id => @comic.id + response.body.should redirect_to '/users/sign_in' + end + end + context 'js形式' do + it 'ステータスコード401 Unauthorizedを返す' do + get :edit, :id => @comic.id, :format => :js + response.status.should eq 401 + end + it '応答メッセージにUnauthorizedを返す' do + get :edit, :id => @comic.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 => @comic.id + response.status.should eq 302 + end + it 'サインインページへ遷移する' do + get :edit, :id => @comic.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 => @comic.id + response.status.should eq 302 + end + it '作家登録ページへ遷移する' do + get :edit, :id => @comic.id + response.body.should redirect_to new_author_path + end + end + end + end + + describe '更新に於いて' do + before do + @comic = FactoryGirl.create :comic, :author => @author + @attr = FactoryGirl.attributes_for(:comic, :author_id => @author.id, :title => 'updated title', :visible => 0) + sign_in @user + end + context '事前チェックしておく' do + it 'コミックモデルに編集取得を問い合わせている' do + Comic.stub(:edit).with(any_args()).and_return @comic + Comic.should_receive(:edit).exactly(1) + put :update, :id => @comic.id, :comic => @attr + end + it 'コミックモデルにカラム値復元を依頼している' do + Comic.any_instance.should_receive(:attributes=).exactly(1) + put :update, :id => @comic.id, :comic => @attr + end + it 'コミックモデルに上書き補充を依頼している' do + Comic.any_instance.should_receive(:overwrite).exactly(1) + put :update, :id => @comic.id, :comic => @attr + end + it 'モデルに更新を依頼する' do + Comic.any_instance.stub(:save).with(any_args).and_return true + Comic.any_instance.should_receive(:save).exactly(1) + put :update, :id => @comic.id, :comic => @attr + end + it '@comicにアレを取得している' do + put :update, :id => @comic.id, :comic => @attr + assigns(:comic).id.should eq(@comic.id) + end + end + context 'つつがなく終わるとき' do it '更新される' do - put :update, :id => @comic.id, :comic => Factory.attributes_for(:hidden_comic) - Comic.find(@comic.id).visible.should eq 0 - end - context 'html形式' do - it 'ステータスコード302 Foundを返す' do - Comic.any_instance.stub(:update_attributes).with(any_args()).and_return(true) - put :update, :id => @comic.id, :comic => Factory.attributes_for(:hidden_comic) - response.status.should eq 302 - end - it '更新されたコミックの表示ページへ遷移する' do - put :update, :id => @comic.id, :comic => Factory.attributes_for(:hidden_comic) - response.should redirect_to(@comic) - end - end - context 'json形式' do - it 'ステータスコード200 OKを返す' do - Comic.any_instance.stub(:update_attributes).with(any_args()).and_return(true) - put :update, :id => @comic.id, :comic => Factory.attributes_for(:hidden_comic), :format => :json - response.should be_success - end - it 'ページ本体は特に返さない' do - Comic.any_instance.stub(:update_attributes).with(any_args()).and_return(true) - put :update, :id => @comic.id, :comic => Factory.attributes_for(:hidden_comic), :format => :json - response.body.should match /./ - end - end - end - context '作家権限がないとき' do - before do - sign_out @user - end - it 'ステータスコード302 Foundを返す' do - put :update, :id => @comic.id, :comic => Factory.attributes_for(:hidden_comic) - response.status.should eq 302 - end - context 'html形式' do - it 'サインインページへ遷移する' do - put :update, :id => @comic.id, :comic => Factory.attributes_for(:hidden_comic) - response.body.should redirect_to '/users/sign_in' - end - end - context 'json形式' do - it '応答メッセージにUnauthorizedを返す' do - put :update, :id => @comic.id, :comic => Factory.attributes_for(:hidden_comic), :format => :json - response.message.should match(/Unauthorized/) - end - end - end - context '検証、保存に失敗したとき' do - before do - Comic.any_instance.stub(:update_attributes).and_return(false) - end - context 'html形式' do - it 'ステータスコード200 Okを返す' do - put :update, :id => @comic.id, :comic => Factory.attributes_for(:hidden_comic) - response.status.should eq 200 - end - it '編集ページを描画する' do - put :update, :id => @comic.id, :comic => Factory.attributes_for(:hidden_comic) - response.should render_template("edit") - end - end - context 'json形式' do - it 'ステータスコード422 unprocessable_entity を返す' do - Comic.any_instance.stub(:update_attributes).and_return(false) - put :update, :id => @comic.id, :comic => Factory.attributes_for(:hidden_comic), :format => :json - response.status.should eq 422 - end - it '応答メッセージUnprocessable Entityを返す' do - put :update, :id => @comic.id, :comic => Factory.attributes_for(:hidden_comic), :format => :json - response.message.should match(/Unprocessable/) - end - end - end - end - - -end + put :update, :id => @comic.id, :comic => @attr + Comic.find(@comic.id).visible.should eq 0 + end + context 'html形式' do + it 'ステータスコード302 Foundを返す' do + Comic.any_instance.stub(:save).with(any_args()).and_return(true) + put :update, :id => @comic.id, :comic => @attr + response.status.should eq 302 + end + it '更新されたコミックの表示ページへ遷移する' do + put :update, :id => @comic.id, :comic => @attr + response.should redirect_to(@comic) + end + end + context 'json形式' do + it 'ステータスコード200 OKを返す' do + Comic.any_instance.stub(:save).with(any_args()).and_return(true) + put :update, :id => @comic.id, :comic => @attr, :format => :json + response.should be_success + end + it 'ページ本体は特に返さない' do + Comic.any_instance.stub(:save).with(any_args()).and_return(true) + put :update, :id => @comic.id, :comic => @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 => @comic.id, :comic => @attr + response.status.should eq 302 + end + it 'サインインページへ遷移する' do + put :update, :id => @comic.id, :comic => @attr + response.body.should redirect_to '/users/sign_in' + end + end + context 'json形式' do + it '応答メッセージにUnauthorizedを返す' do + put :update, :id => @comic.id, :comic => @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 => @comic.id, :comic => @attr + response.status.should eq 302 + end + it 'サインインページへ遷移する' do + put :update, :id => @comic.id, :comic => @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 => @comic.id, :comic => @attr + response.status.should eq 302 + end + it '作家登録ページへ遷移する' do + put :update, :id => @comic.id, :comic => @attr + response.body.should redirect_to new_author_path + end + end + end + context '検証、保存に失敗したとき' do + before do + Comic.any_instance.stub(:save).and_return(false) + end + context 'html形式' do + it 'ステータスコード200 Okを返す' do + put :update, :id => @comic.id, :comic => @attr + response.status.should eq 200 + end + it '編集ページを描画する' do + put :update, :id => @comic.id, :comic => @attr + response.should render_template("edit") + end + end + context 'json形式' do + it 'ステータスコード422 unprocessable_entity を返す' do + Comic.any_instance.stub(:save).and_return(false) + put :update, :id => @comic.id, :comic => @attr, :format => :json + response.status.should eq 422 + end + it '応答メッセージUnprocessable Entityを返す' do + put :update, :id => @comic.id, :comic => @attr, :format => :json + response.message.should match(/Unprocessable/) + end + end + end + end + + describe '削除に於いて' do + before do + @comic = FactoryGirl.create :comic, :author => @author + sign_in @user + end + context '事前チェックしておく' do + before do + Comic.stub(:edit).with(any_args()).and_return @comic + Comic.any_instance.stub(:destroy_with_story).with(any_args()).and_return(true) + end + it 'コミックモデルに編集取得を問い合わせている' do + Comic.should_receive(:edit).exactly(1) + delete :destroy, :id => @comic.id + end + it 'モデルに削除を依頼する' do + Comic.any_instance.should_receive(:destroy_with_story).exactly(1) + delete :destroy, :id => @comic.id + end + it '@comicにアレを取得している' do + delete :destroy, :id => @comic.id + assigns(:comic).id.should eq(@comic.id) + end + end + context 'つつがなく終わるとき' do + it '削除される' do + lambda { + delete :destroy, :id => @comic.id + }.should change Comic, :count + end + context 'html形式' do + before do + Comic.any_instance.stub(:destroy_with_story).with(any_args()).and_return(true) + end + it 'ステータスコード302 Foundを返す' do + delete :destroy, :id => @comic.id + response.status.should eq 302 + end + it 'マイコミックの一覧ページへ遷移する' do + delete :destroy, :id => @comic.id + response.should redirect_to('/home/comics') + end + end + context 'json形式' do + before do + Comic.any_instance.stub(:destroy_with_story).with(any_args()).and_return(true) + end + it 'ステータスコード200 OKを返す' do + delete :destroy, :id => @comic.id, :format => :json + response.should be_success + end + it 'ページ本体は特に返さない' do + delete :destroy, :id => @comic.id, :format => :json + response.body.should match /./ + end + end + end + context 'ユーザ権限がないとき' do + before do + sign_out @user + end + context 'html形式' do + it 'ステータスコード302 Foundを返す' do + delete :destroy, :id => @comic.id + response.status.should eq 302 + end + it 'サインインページへ遷移する' do + delete :destroy, :id => @comic.id + response.body.should redirect_to '/users/sign_in' + end + end + context 'json形式' do + it '応答メッセージにUnauthorizedを返す' do + delete :destroy, :id => @comic.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 => @comic.id + response.status.should eq 302 + end + it 'サインインページへ遷移する' do + delete :destroy, :id => @comic.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 => @comic.id + response.status.should eq 302 + end + it '作家登録ページへ遷移する' do + delete :destroy, :id => @comic.id + response.body.should redirect_to new_author_path + end + end + end + context '削除に失敗したとき' do + before do + Comic.any_instance.stub(:destroy_with_story).and_return(false) + end + context 'html形式' do + it 'ステータスコード302 Foundを返す' do + delete :destroy, :id => @comic.id + response.status.should eq 302 + end + it 'そのコミックの詳細ページへ遷移する' do + delete :destroy, :id => @comic.id + response.should redirect_to(comic_path(@comic)) + end + end + context 'json形式' do + it 'ステータスコード422 unprocessable_entity を返す' do + delete :destroy, :id => @comic.id, :format => :json + response.status.should eq 422 + end + it '応答メッセージUnprocessable Entityを返す' do + delete :destroy, :id => @comic.id, :format => :json + response.message.should match(/Unprocessable/) + end + end + end + end + +else + describe '一覧表示に於いて' do + before do + @comic = FactoryGirl.create :comic, :author_id => @user.author.id + Comic.stub(:list).and_return([@comic, @comic, @comic]) + 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 + @comic = FactoryGirl.create :comic, :author_id => @user.author.id, :title => 'normal' + Comic.stub(:show).and_return(@comic) + sign_in @user + end + context 'つつがなく終わるとき' do + it 'ステータスコード200 OKを返す' do + get :show, :id => @comic.id + response.should be_success + end + context 'html形式' do + it 'showテンプレートを描画する' do + get :show, :id => @comic.id + response.should render_template("show") + end + end + context 'json形式' do + it 'jsonデータを返す' do + get :show, :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 + it 'ステータスコード200 OKを返す' do + get :show, :id => @comic.id + response.should be_success + end + context 'html形式' do + it 'showテンプレートを描画する' do + get :show, :id => @comic.id + response.should render_template("show") + end + end + context 'json形式' do + it 'jsonデータを返す' do + get :show, :id => @comic.id, :format => :json + lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError) + end + end + end + end + describe 'コミック数取得に於いて' do + before do + Comic.should_receive(:visible_count).and_return(3) + sign_out @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 + 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 + + describe '新規作成に於いて' do + before do + sign_in @user + @attr = FactoryGirl.attributes_for(:comic, :author_id => @author.id, :title => 'normal') + end + context 'つつがなく終わるとき' do + context 'html形式' do + it 'ステータスコード302 Foundを返す' do + Comic.any_instance.stub(:save).and_return(true) + post :create, :comic => @attr + response.status.should eq 302 + end + it '作成されたコミックの表示ページへ遷移する' do +# Comic.any_instance.stub(:save).and_return(true) + post :create, :comic => @attr + response.should redirect_to(Comic.last) + end + end + context 'json形式' do + it 'ステータスコード200 OKを返す' do +# Comic.any_instance.stub(:save).and_return(true) + post :create, :comic => @attr, :format => :json + response.should be_success + end + it '作成されたコミックをjsonデータで返す' do + post :create, :comic => @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, :comic => @attr + response.status.should eq 302 + end + it 'サインインページへ遷移する' do + post :create, :comic => @attr + response.body.should redirect_to '/users/sign_in' + end + end + context 'json形式' do + it 'ステータスコード401 Unauthorizedを返す' do + post :create, :comic => @attr, :format => :json + response.status.should eq 401 + end + it '応答メッセージにUnauthorizedを返す' do + post :create, :comic => @attr, :format => :json + response.message.should match(/Unauthorized/) + end + end + end + end + + describe '編集フォーム表示に於いて' do + before do + @comic = FactoryGirl.create :comic, :author_id => @user.author.id + sign_in @user + Comic.stub(:edit).and_return(@comic) + end + context 'つつがなく終わるとき' do + it 'ステータスコード200 OKを返す' do + get :edit, :id => @comic.id + response.should be_success + end + context 'html形式' do + it 'editテンプレートを描画する' do + get :edit, :id => @comic.id + response.should render_template("edit") + end + end + context 'js形式' do + it 'edit.jsテンプレートを描画する' do + get :edit, :id => @comic.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 => @comic.id + response.status.should eq 302 + end + it 'サインインページへ遷移する' do + get :edit, :id => @comic.id + response.body.should redirect_to '/users/sign_in' + end + end + context 'js形式' do + it 'ステータスコード401 Unauthorizedを返す' do + get :edit, :id => @comic.id, :format => :js + response.status.should eq 401 + end + it '応答メッセージにUnauthorizedを返す' do + get :edit, :id => @comic.id, :format => :js + response.message.should match(/Unauthorized/) + end + end + end + end + + describe '更新に於いて' do + before do + @comic = FactoryGirl.create :comic, :author => @author + @attr = FactoryGirl.attributes_for(:comic, :author_id => @author.id, :title => 'updated title', :visible => 0) + sign_in @user + end + context 'つつがなく終わるとき' do + context 'html形式' do + it 'ステータスコード302 Foundを返す' do + Comic.any_instance.stub(:save).with(any_args()).and_return(true) + put :update, :id => @comic.id, :comic => @attr + response.status.should eq 302 + end + it '更新されたコミックの表示ページへ遷移する' do + put :update, :id => @comic.id, :comic => @attr + response.should redirect_to(@comic) + end + end + context 'json形式' do + it 'ステータスコード200 OKを返す' do + Comic.any_instance.stub(:save).with(any_args()).and_return(true) + put :update, :id => @comic.id, :comic => @attr, :format => :json + response.should be_success + end + it 'ページ本体は特に返さない' do + Comic.any_instance.stub(:save).with(any_args()).and_return(true) + put :update, :id => @comic.id, :comic => @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 => @comic.id, :comic => @attr + response.status.should eq 302 + end + context 'html形式' do + it 'サインインページへ遷移する' do + put :update, :id => @comic.id, :comic => @attr + response.body.should redirect_to '/users/sign_in' + end + end + end + end + + describe '削除に於いて' do + before do + @comic = FactoryGirl.create :comic, :author => @author + sign_in @user + end + context 'つつがなく終わるとき' do + context 'html形式' do + before do + Comic.any_instance.stub(:destroy_with_story).with(any_args()).and_return(true) + end + it 'ステータスコード302 Foundを返す' do + delete :destroy, :id => @comic.id + response.status.should eq 302 + end + it 'マイコミックの一覧ページへ遷移する' do + delete :destroy, :id => @comic.id + response.should redirect_to('/home/comic') + end + end + context 'json形式' do + before do + Comic.any_instance.stub(:destroy_with_story).with(any_args()).and_return(true) + end + it 'ステータスコード200 OKを返す' do + delete :destroy, :id => @comic.id, :format => :json + response.should be_success + end + it 'ページ本体は特に返さない' do + delete :destroy, :id => @comic.id, :format => :json + response.body.should match /./ + end + end + end + context 'ユーザ権限がないとき' do + before do + sign_out @user + end + it 'ステータスコード302 Foundを返す' do + delete :destroy, :id => @comic.id + response.status.should eq 302 + end + context 'html形式' do + it 'サインインページへ遷移する' do + delete :destroy, :id => @comic.id + response.body.should redirect_to '/users/sign_in' + end + end + context 'json形式' do + it '応答メッセージにUnauthorizedを返す' do + delete :destroy, :id => @comic.id, :format => :json + response.message.should match(/Unauthorized/) + end + end + end + end + +end + +end