From f20e087a523621b9e3efc6778509267fc25e5a70 Mon Sep 17 00:00:00 2001 From: yasushiito Date: Mon, 30 Jun 2014 11:31:40 +0900 Subject: [PATCH] fix js manifest --- app/controllers/application_controller.rb | 2 +- app/models/artist.rb | 2 +- app/models/scroll_panel.rb | 1 - ...30611092857_remove_index_t_on_panel_pictures.rb | 4 +- spec/controllers/scrolls_controller_spec.rb | 571 +++++++++------------ spec/factories/author.rb | 8 + spec/factories/panel.rb | 27 +- spec/factories/scroll.rb | 26 +- spec/factories/scroll_panel.rb | 72 +-- spec/factories/user.rb | 7 + spec/support/controller_macros.rb | 54 +- 11 files changed, 353 insertions(+), 421 deletions(-) diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index bb8f2fe9..98a8b8a9 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -289,7 +289,7 @@ class ApplicationController < ActionController::Base } end - def not_update_html_format format + def not_updated_html_format format format.html { flash[:notice] = I18n.t('flash.notice.not_updated', :model => @my_model_class.model_name.human) render_form diff --git a/app/models/artist.rb b/app/models/artist.rb index 3604e579..d3052924 100644 --- a/app/models/artist.rb +++ b/app/models/artist.rb @@ -7,7 +7,7 @@ class Artist < Peta::Owner validates :name, :presence => true, :length => {:maximum => 30} validates :user_id, :numericality => true, :existence => {:both => false} - validates :provider, :numericality => {:allow_nil => true} + #validates :provider, :numericality => {:allow_nil => true} def supply_default self.name = 'no name' if self.name.blank? diff --git a/app/models/scroll_panel.rb b/app/models/scroll_panel.rb index 412e803b..44885a61 100644 --- a/app/models/scroll_panel.rb +++ b/app/models/scroll_panel.rb @@ -6,7 +6,6 @@ class ScrollPanel < Peta::Leaf validates :scroll_id, :presence => true, :numericality => true, :existence => {:both => false} validates :panel_id, :presence => true, :numericality => true, :existence => {:both => false} - validates :author_id, :presence => true, :numericality => true, :existence => {:both => false} validates :t, :presence => true, :numericality => {:greater_than_or_equal_to => 0} def supply_default diff --git a/db/migrate/20130611092857_remove_index_t_on_panel_pictures.rb b/db/migrate/20130611092857_remove_index_t_on_panel_pictures.rb index ac44c9e4..19ddc807 100644 --- a/db/migrate/20130611092857_remove_index_t_on_panel_pictures.rb +++ b/db/migrate/20130611092857_remove_index_t_on_panel_pictures.rb @@ -1,9 +1,9 @@ class RemoveIndexTOnPanelPictures < ActiveRecord::Migration def up - remove_index "panel_pictures", :name => "altered_panel_pictures_idt" + #remove_index "panel_pictures", :name => "altered_panel_pictures_idt" end def down - add_index "panel_pictures", ["panel_id", "t"], :name => "altered_panel_pictures_idt", :unique => true + #add_index "panel_pictures", ["panel_id", "t"], :name => "altered_panel_pictures_idt", :unique => true end end diff --git a/spec/controllers/scrolls_controller_spec.rb b/spec/controllers/scrolls_controller_spec.rb index 78ad23ad..cb047c07 100644 --- a/spec/controllers/scrolls_controller_spec.rb +++ b/spec/controllers/scrolls_controller_spec.rb @@ -4,14 +4,15 @@ require 'spec_helper' describe ScrollsController do before do @admin =FactoryGirl.create :admin - @author_yas = FactoryGirl.create :yas_with_scrolls - @user_yas = @author_yas.user + @user_yas = FactoryGirl.create :user_yas_with_owner @user_rom = FactoryGirl.create :user_rom + @author_yas = @user_yas.author end if Manifest.manifest.magic_numbers['run_mode'] == 1 describe '一覧表示に於いて' do before do + factory_contents :scroll_hello, @author_yas end context '正常系の共通処理' do @@ -54,6 +55,12 @@ if Manifest.manifest.magic_numbers['run_mode'] == 1 it 'ステータス OKを返す' do return_ok_at_get :count, format: :json end + context 'html形式' do + it '必要ないので実装していない' do + get :count + expect(response.status).to eq(406) + end + end context 'json形式' do it 'jsonデータを返す' do get :count, format: :json @@ -79,31 +86,74 @@ if Manifest.manifest.magic_numbers['run_mode'] == 1 end describe '作家フィルタ一覧及びカウント' do before do + factory_contents :scroll_hello, @author_yas end + context '正常系の共通処理' do before do sign_in @user_yas end + it 'ステータス OKを返して、ファイラーまたはjsonデータを返す。' do + list_should_return_filer_or_json :by_author, id: @author_yas.id + end + it 'コントローラのマニフェストからScrollモデルを@my_model_classに取得している' do + get :by_author, id: @author_yas.id + expect(assigns(:my_model_class)).to be Scroll + end + it 'リストグループのマニフェストから新着リストの取得処理を@listに取得している' do + get :by_author, id: @author_yas.id + expect(assigns(:list).item_name).to eq('scroll') + expect(assigns(:list).list_name).to eq('by_author') + end it '@itemsにリストを取得している' do - get :by_author + get :by_author, id: @author_yas.id expect(assigns(:items).size).to be >= 3 end - it 'ステータスコード200 OKを返す' do - list_should_return_filer_or_json :by_author, id: @author_yas.id + context 'html形式' do + it '@filerにファイラーを取得している' do + get :by_author, id: @author_yas.id + expect(assigns(:filer).class).to be Locmare::Filer + end + end + context 'json形式' do + it '@filerにファイラーをセットしていない' do + get :by_author, id: @author_yas.id, format: :json + expect(assigns(:filer)).to be nil + end end end - context '権限' do - it 'ユーザ権限はないが管理者権限があるとき 200 OKを返す' do + context '件数取得' do + before do + sign_in @user_yas + end + it 'ステータス OKを返す' do + return_ok_at_get :count_by_author, id: @author_yas.id, format: :json + end + context 'html形式' do + it '必要ないので実装していない' do + get :count_by_author, id: @author_yas.id + expect(response.status).to eq(406) + end + end + context 'json形式' do + it 'jsonデータを返す' do + get :count_by_author, id: @author_yas.id, format: :json + expect {JSON.parse(response.body)}.not_to raise_error + end + end + end + context 'サインインの状態が例外的なとき' do + it 'ユーザではなく管理者でも通常通り応答する' do sign_in @admin return_ok_at_get :by_author, id: @author_yas.id return_ok_at_get :by_author, id: @author_yas.id, format: :json end - it 'ユーザだが作家登録していないとき 200 OKを返す' do + it '創作活動してないROM専の読者にも通常通り応答する' do sign_in @user_rom return_ok_at_get :by_author, id: @author_yas.id return_ok_at_get :by_author, id: @author_yas.id, format: :json end - it 'sign_inを返す' do + it 'ゲスト(サインインしていない)はサインインページに遷移する' do announce_sign_in_at_get :by_author, id: @author_yas.id end end @@ -111,7 +161,7 @@ if Manifest.manifest.magic_numbers['run_mode'] == 1 describe '単体表示に於いて' do before do - @item = @author_yas.scrolls.first + @item = FactoryGirl.create :scroll_hello, author: @author_yas @item_id = @item.id end context '正常系の共通処理' do @@ -173,7 +223,7 @@ if Manifest.manifest.magic_numbers['run_mode'] == 1 describe 'プレイリストに於いて' do before do - @item = @author_yas.scrolls.first + @item = FactoryGirl.create :scroll_hello_with_scroll_panels, author: @author_yas @item_id = @item.id end context '正常系の共通処理' do @@ -187,33 +237,33 @@ if Manifest.manifest.magic_numbers['run_mode'] == 1 it 'コントローラのマニフェストからScrollPanelモデルを@my_model_classに取得している' do # リクエストの対象はスクロールであるが、返されるのはスクコマのリストである。 # くれぐれも間違えないように。 - get :play, :id => @item_id + get :play, id: @item_id expect(assigns(:my_model_class)).to be ScrollPanel end it '@itemにリクエストしたスクロールを取得している' do - get :play, :id => @item_id + get :play, id: @item_id expect(assigns(:item)).to eq(@item) end it 'リストグループのマニフェストからプレイリストの取得処理を@listに取得している' do - get :play, :id => @item_id + get :play, id: @item_id expect(assigns(:list).item_name).to eq('scroll_panel') expect(assigns(:list).list_name).to eq('play') end it '@itemsにプレイリストを取得している' do - get :play, :id => @item_id + get :play, id: @item_id expect(assigns(:items).size).to be >= 3 expect(assigns(:count)).to be >= 3 - expect(assigns(:pager)).to be nil + expect(assigns(:pager)).not_to be nil end context 'html形式' do it 'playテンプレートを描画する' do - get :play, :id => @item_id + get :play, id: @item_id expect(response).to render_template("play") end end context 'json形式' do it 'jsonデータを返す' do - get :play, :id => @item_id, :format => :json + get :play, id: @item_id, format: :json expect {JSON.parse(response.body)}.not_to raise_error end end @@ -237,8 +287,6 @@ if Manifest.manifest.magic_numbers['run_mode'] == 1 describe '新規作成フォーム表示に於いて' do before do - @item = @author_yas.scrolls.first - @item_id = @item.id end context '正常系の共通処理' do before do @@ -254,15 +302,20 @@ if Manifest.manifest.magic_numbers['run_mode'] == 1 end it '@itemに新規データを用意している' do get :new - expect(assigns(:item)).to eq(@item) + @item = assigns(:item) + expect(@item.is_a?(Scroll)).to be true + expect(@item.new_record?).to be true end it '@formに入力フォーム(Bucket)を取得している' do get :new + @item = assigns(:item) expect(assigns(:form).item).to eq(@item) expect(assigns(:form).mounted).to eq(true) expect(assigns(:form).submit).to eq(true) end it 'スクロールモデルにデフォルト値補充とブーストを依頼している' do + allow_any_instance_of(Scroll).to receive(:supply_default) + allow_any_instance_of(Scroll).to receive(:boosts) expect_any_instance_of(Scroll).to receive(:supply_default) expect_any_instance_of(Scroll).to receive(:boosts) get :new @@ -282,410 +335,266 @@ if Manifest.manifest.magic_numbers['run_mode'] == 1 end context 'サインインの状態が例外的なとき' do before do - sign_in @user_rom end it '創作活動してないROM専の読者には作家登録ページへ遷移する' do + sign_in @user_rom get :new - expect(response.status).to eq(302) - expect(response).to redirect_to new_author_path - return_ok_at_get :play, id: @item_id, format: :json + announce_regist_author_for_html + announce_regist_author_for_json :get end it 'ゲスト(サインインしていない)はサインインページに遷移する' do - announce_sign_in_at_get :new, id: @item_id + announce_sign_in_at_get :new end it 'ユーザではなく管理者でもサインインページに遷移する' do sign_in @admin - announce_sign_in_at_get :new, id: @item_id - 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 + announce_sign_in_at_get :new end end end describe '新規作成に於いて' do before do - sign_in @user - @attr = FactoryGirl.attributes_for(:scroll, :author_id => @author.id, :title => 'normal') - end - context '事前チェックしておく' do - it 'スクロールモデルにデフォルト値補充を依頼している' do - Scroll.any_instance.should_receive(:supply_default).exactly(1) - post :create, :scroll => @attr - end - it 'スクロールモデルにカラム値復元を依頼している' do - Scroll.any_instance.should_receive(:attributes=).exactly(1) - post :create, :scroll => @attr - end - it 'スクロールモデルに上書き補充を依頼している' do - Scroll.any_instance.should_receive(:overwrite).exactly(1) - post :create, :scroll => @attr - end - it 'モデルに保存依頼する' do - Scroll.any_instance.should_receive(:save).exactly(1) - post :create, :scroll => @attr - end + @attributes = FactoryGirl.attributes_for(:scroll_hello, author_id: @author_yas.id) + @attr = {:scroll => @attributes} end context '正常系の共通処理' do - it "@scrollに作成されたスクロールを保持していて、それがDBにある" do - post :create, :scroll => @attr - assigns(:scroll).should be_a(Scroll) - assigns(:scroll).should be_persisted - end - context 'html形式' do - it 'ステータスコード302 Foundを返す' do - Scroll.any_instance.stub(:save).and_return(true) - post :create, :scroll => @attr - response.status.should eq 302 - end - it '作成されたスクロールの表示ページへ遷移する' do -# Scroll.any_instance.stub(:save).and_return(true) - post :create, :scroll => @attr - response.should redirect_to(Scroll.last) - end + before do + sign_in @user_yas end - context 'json形式' do - it 'ステータスコード200 OKを返す' do -# Scroll.any_instance.stub(:save).and_return(true) - post :create, :scroll => @attr, :format => :json - response.should be_success - end - it '作成されたスクロールをjsonデータで返す' do - post :create, :scroll => @attr, :format => :json - lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError) - end - it 'データがアレになっている' do - post :create, :scroll => @attr, :format => :json - json = JSON.parse response.body - json["title"].should match(/normal/) - json["visible"].should_not be_nil - end + it 'コントローラのマニフェストからScrollモデルを@my_model_classに取得している' do + allow_save Scroll + post_save @attr + expect(assigns(:my_model_class)).to be Scroll end - end - context 'ユーザ権限がないとき' do - before do - sign_out @user + it '@itemに新規アイテムを用意している' do + allow_save Scroll + post_save @attr + @item = assigns(:item) + expect(@item.is_a?(Scroll)).to be true end context 'html形式' do - it 'ステータスコード302 Foundを返す' do - post :create, :scroll => @attr - response.status.should eq 302 - end - it 'サインインページへ遷移する' do - post :create, :scroll => @attr - response.body.should redirect_to '/users/sign_in' + it 'ステータスコード302 Foundを返して, 作成されたアイテムの表示ページへ遷移する' do + post_save @attr + expect(response.status).to eq(302) + expect(response).to redirect_to Scroll.last end end context 'json形式' do - it 'ステータスコード401 Unauthorizedを返す' do - post :create, :scroll => @attr, :format => :json - response.status.should eq 401 - end - it '応答メッセージにUnauthorizedを返す' do - post :create, :scroll => @attr, :format => :json - response.message.should match(/Unauthorized/) + it 'ステータスコード200 OKを返して, 作成されたアイテムをjsonデータで返す' do + post_save @attr.merge(:format => :json) + expect(response).to be_success + expect {JSON.parse(response.body)}.not_to raise_error end end end - context 'ユーザ権限はないが管理者権限があるとき' do + context '検証、保存に失敗したとき' do before do - sign_out @user - sign_in @admin + sign_in @user_yas + reject_save Scroll end context 'html形式' do - it 'ステータスコード302 Foundを返す' do - post :create, :scroll => @attr - response.status.should eq 302 + it 'ステータスコード200 OKを返して, formテンプレートを描画する' do + post_save @attr + expect(response).to be_success + expect(response).to render_template("templates/r/form/form") end - it 'サインインページへ遷移する' do - post :create, :scroll => @attr - response.body.should redirect_to '/users/sign_in' + it '@formに入力フォーム(Bucket)を取得している' do + post_save @attr + @item = assigns(:item) + expect(assigns(:form).item).to eq(@item) + expect(assigns(:form).mounted).to eq(true) + expect(assigns(:form).submit).to eq(true) end end - end - context 'ユーザだが作家登録していないとき' do - before do - @author.destroy - end - context 'html形式' do - it 'ステータスコード302 Foundを返す' do - post :create, :scroll => @attr - response.status.should eq 302 - end - it '作家登録ページへ遷移する' do - post :create, :scroll => @attr - response.body.should redirect_to new_author_path + context 'json形式' do + it 'ステータスコード422 unprocessable_entityを返して, 応答メッセージUnprocessable Entityを返す' do + post_save @attr.merge(:format => :json) + expect(response.status).to eq(422) + expect(response.message).to match(/Unprocessable/) end end end - context '検証、保存に失敗した' do + context 'サインインの状態が例外的なとき' do before do - Scroll.any_instance.stub(:save).and_return(false) + allow_save Scroll end - it "未保存のスクロールを保持している" do - post :create, :scroll => @attr - assigns(:scroll).should be_a_new(Scroll) + it '創作活動してないROM専の読者には作家登録ページへ遷移する' do + sign_in @user_rom + post_save @attr + announce_regist_author_for_html + announce_regist_author_for_json :post, @attr end - context 'html形式' do - it 'ステータスコード200 OKを返す' do - post :create, :scroll => @attr - response.status.should eq 200 - end - it '新規ページを描画する' do - post :create, :scroll => @attr - response.should render_template("new") - end + it 'ゲスト(サインインしていない)はサインインページに遷移する' do + post_save @attr + announce_sign_in_for_html + post_save @attr.merge(:format => :json) + announce_sign_in_for_json end - context 'json形式' do - it 'ステータスコード422 unprocessable_entity を返す' do - post :create, :scroll => @attr, :format => :json - response.status.should eq 422 - end - it '応答メッセージUnprocessable Entityを返す' do - post :create, :scroll => @attr, :format => :json - response.message.should match(/Unprocessable/) - end + it 'ユーザではなく管理者でもサインインページに遷移する' do + sign_in @admin + post_save @attr + announce_sign_in_for_html + post_save @attr.merge(:format => :json) + announce_sign_in_for_json end end end describe '編集フォーム表示に於いて' do before do - @scroll = FactoryGirl.create :scroll, :author_id => @user.author.id - sign_in @user - Scroll.stub(:edit).and_return(@scroll) + @item = FactoryGirl.create :scroll_hello_with_scroll_panels, author: @author_yas + @item_id = @item.id end context '正常系の共通処理' do - it 'ステータスコード200 OKを返す' do - get :edit, :id => @scroll.id - response.should be_success + before do + sign_in @user_yas end - it 'スクロールモデルに編集取得を問い合わせている' do - Scroll.should_receive(:edit).exactly(1) - get :edit, :id => @scroll.id + it '[html, json]でステータス OKを返す' do + return_ok_at_get :edit, id: @item_id + return_ok_at_get :edit, id: @item_id, format: :json end - it '@scrollにデータを用意している' do - get :edit, :id => @scroll.id - assigns(:scroll).should eq @scroll + it 'コントローラのマニフェストからScrollモデルを@my_model_classに取得している' do + get :edit, id: @item_id + expect(assigns(:my_model_class)).to be Scroll end - context 'html形式' do - it 'editテンプレートを描画する' do - get :edit, :id => @scroll.id - response.should render_template("edit") - end + it '@itemにリクエストしたスクロールを取得している' do + get :edit, id: @item_id + @item = assigns(:item) + expect(@item.is_a?(Scroll)).to be true + expect(@item.new_record?).not_to be true end - context 'js形式' do - it 'edit.jsテンプレートを描画する' do - get :edit, :id => @scroll.id, :format => :js - response.should render_template("edit") - end + it '@formに入力フォーム(Bucket)を取得している' do + get :edit, id: @item_id + @item = assigns(:item) + expect(assigns(:form).item).to eq(@item) + expect(assigns(:form).mounted).to eq(true) + expect(assigns(:form).submit).to eq(true) end - end - context 'ユーザ権限がないとき' do - before do - sign_out @user + it 'スクロールモデルにデフォルト値補充とブーストを依頼している' do + allow_any_instance_of(Scroll).to receive(:boosts) + expect_any_instance_of(Scroll).to receive(:boosts) + get :edit, id: @item_id end context 'html形式' do - it 'ステータスコード302 Foundを返す' do - get :edit, :id => @scroll.id - response.status.should eq 302 - end - it 'サインインページへ遷移する' do - get :edit, :id => @scroll.id - response.body.should redirect_to '/users/sign_in' + it 'formテンプレートを描画する' do + get :edit, id: @item_id + expect(response).to render_template("templates/r/form/form") end end - context 'js形式' do - it 'ステータスコード401 Unauthorizedを返す' do - get :edit, :id => @scroll.id, :format => :js - response.status.should eq 401 - end - it '応答メッセージにUnauthorizedを返す' do - get :edit, :id => @scroll.id, :format => :js - response.message.should match(/Unauthorized/) + context 'json形式' do + it 'jsonデータを返す' do + get :edit, id: @item_id, format: :json + expect {JSON.parse(response.body)}.not_to raise_error end end end - context 'ユーザ権限はないが管理者権限があるとき' do + context 'サインインの状態が例外的なとき' do before do - sign_out @user - sign_in @admin + allow_save Scroll end - context 'html形式' do - it 'ステータスコード302 Foundを返す' do - get :edit, :id => @scroll.id - response.status.should eq 302 - end - it 'サインインページへ遷移する' do - get :edit, :id => @scroll.id - response.body.should redirect_to '/users/sign_in' - end + it '創作活動してないROM専の読者には作家登録ページへ遷移する' do + sign_in @user_rom + get :edit, id: @item_id + expect(response.status).to eq(302) + expect(response).to redirect_to new_author_path end - end - context 'ユーザだが作家登録していないとき' do - before do - @author.destroy + it 'ゲスト(サインインしていない)はサインインページに遷移する' do + announce_sign_in_at_get :new end - context 'html形式' do - it 'ステータスコード302 Foundを返す' do - get :edit, :id => @scroll.id - response.status.should eq 302 - end - it '作家登録ページへ遷移する' do - get :edit, :id => @scroll.id - response.body.should redirect_to new_author_path - end + it 'ユーザではなく管理者でもサインインページに遷移する' do + sign_in @admin + announce_sign_in_at_get :new end end end describe '更新に於いて' do before do - @scroll = FactoryGirl.create :scroll, :author => @author - @attr = FactoryGirl.attributes_for(:scroll, :author_id => @author.id, :title => 'updated title', :visible => 0) - sign_in @user + @item = FactoryGirl.create :scroll_hello_with_scroll_panels, author: @author_yas + @item_id = @item.id + @attributes = FactoryGirl.attributes_for(:scroll_hidden, author_id: @author_yas.id) + @attr = {:id => @item_id, :scroll => @attributes} end - context '事前チェックしておく' do - it 'スクロールモデルに編集取得を問い合わせている' do - Scroll.stub(:edit).with(any_args()).and_return @scroll - Scroll.should_receive(:edit).exactly(1) - put :update, :id => @scroll.id, :scroll => @attr - end - it 'スクロールモデルにカラム値復元を依頼している' do - Scroll.any_instance.should_receive(:attributes=).exactly(1) - put :update, :id => @scroll.id, :scroll => @attr - end - it 'スクロールモデルに上書き補充を依頼している' do - Scroll.any_instance.should_receive(:overwrite).exactly(1) - put :update, :id => @scroll.id, :scroll => @attr - end - it 'モデルに更新を依頼する' do - Scroll.any_instance.stub(:save).with(any_args).and_return true - Scroll.any_instance.should_receive(:save).exactly(1) - put :update, :id => @scroll.id, :scroll => @attr + context '正常系の共通処理' do + before do + sign_in @user_yas end - it '@scrollにアレを取得している' do - put :update, :id => @scroll.id, :scroll => @attr - assigns(:scroll).id.should eq(@scroll.id) + it 'コントローラのマニフェストからScrollモデルを@my_model_classに取得している' do + allow_save Scroll + put_save @attr + expect(assigns(:my_model_class)).to be Scroll end - end - context '正常系の共通処理' do - it '更新される' do - put :update, :id => @scroll.id, :scroll => @attr - Scroll.find(@scroll.id).visible.should eq 0 + it '@itemにリクエストしたアイテムを取得して, 更新している' do + allow_save Scroll + put_save @attr + @item = assigns(:item) + expect(@item.is_a?(Scroll)).to be true + expect(@item.visible).to eq(0) end context 'html形式' do - it 'ステータスコード302 Foundを返す' do - Scroll.any_instance.stub(:save).with(any_args()).and_return(true) - put :update, :id => @scroll.id, :scroll => @attr - response.status.should eq 302 - end - it '更新されたスクロールの表示ページへ遷移する' do - put :update, :id => @scroll.id, :scroll => @attr - response.should redirect_to(@scroll) + it 'ステータスコード302 Foundを返して, 作成されたアイテムの表示ページへ遷移する' do + put_save @attr + expect(response.status).to eq(302) + expect(response).to redirect_to @item end end context 'json形式' do - it 'ステータスコード200 OKを返す' do - Scroll.any_instance.stub(:save).with(any_args()).and_return(true) - put :update, :id => @scroll.id, :scroll => @attr, :format => :json - response.should be_success - end - it 'ページ本体は特に返さない' do - Scroll.any_instance.stub(:save).with(any_args()).and_return(true) - put :update, :id => @scroll.id, :scroll => @attr, :format => :json - response.body.should match /./ + it 'ステータスコード200 OKを返して, ページ本体は特に返さない' do + put_save @attr.merge(:format => :json) + expect(response).to be_success + expect(response.message).to match /./ end end end - context 'ユーザ権限がないとき' do + context '検証、保存に失敗したとき' do before do - sign_out @user + sign_in @user_yas + reject_save Scroll end context 'html形式' do - it 'ステータスコード302 Foundを返す' do - put :update, :id => @scroll.id, :scroll => @attr - response.status.should eq 302 + it 'ステータスコード200 OKを返して, formテンプレートを描画する' do + put_save @attr + expect(response).to be_success + expect(response).to render_template("templates/r/form/form") end - it 'サインインページへ遷移する' do - put :update, :id => @scroll.id, :scroll => @attr - response.body.should redirect_to '/users/sign_in' + it '@formに入力フォーム(Bucket)を取得している' do + put_save @attr + @item = assigns(:item) + expect(assigns(:form).item).to eq(@item) + expect(assigns(:form).mounted).to eq(true) + expect(assigns(:form).submit).to eq(true) end end context 'json形式' do - it '応答メッセージにUnauthorizedを返す' do - put :update, :id => @scroll.id, :scroll => @attr, :format => :json - response.message.should match(/Unauthorized/) + it 'ステータスコード422 unprocessable_entityを返して, 応答メッセージUnprocessable Entityを返す' do + put_save @attr.merge(:format => :json) + expect(response.status).to eq(422) + expect(response.message).to match(/Unprocessable/) end end end - context 'ユーザ権限はないが管理者権限があるとき' do - before do - sign_out @user - sign_in @admin - end - context 'html形式' do - it 'ステータスコード302 Foundを返す' do - put :update, :id => @scroll.id, :scroll => @attr - response.status.should eq 302 - end - it 'サインインページへ遷移する' do - put :update, :id => @scroll.id, :scroll => @attr - response.body.should redirect_to '/users/sign_in' - end - end - end - context 'ユーザだが作家登録していないとき' do + context 'サインインの状態が例外的なとき' do before do - @author.destroy + allow_save Scroll end - context 'html形式' do - it 'ステータスコード302 Foundを返す' do - put :update, :id => @scroll.id, :scroll => @attr - response.status.should eq 302 - end - it '作家登録ページへ遷移する' do - put :update, :id => @scroll.id, :scroll => @attr - response.body.should redirect_to new_author_path - end - end - end - context '検証、保存に失敗したとき' do - before do - Scroll.any_instance.stub(:save).and_return(false) + it '創作活動してないROM専の読者には作家登録ページへ遷移する' do + sign_in @user_rom + put_save @attr + announce_regist_author_for_html + announce_regist_author_for_json :put, @attr end - context 'html形式' do - it 'ステータスコード200 Okを返す' do - put :update, :id => @scroll.id, :scroll => @attr - response.status.should eq 200 - end - it '編集ページを描画する' do - put :update, :id => @scroll.id, :scroll => @attr - response.should render_template("edit") - end + it 'ゲスト(サインインしていない)はサインインページに遷移する' do + put_save @attr + announce_sign_in_for_html + put_save @attr.merge(:format => :json) + announce_sign_in_for_json end - context 'json形式' do - it 'ステータスコード422 unprocessable_entity を返す' do - Scroll.any_instance.stub(:save).and_return(false) - put :update, :id => @scroll.id, :scroll => @attr, :format => :json - response.status.should eq 422 - end - it '応答メッセージUnprocessable Entityを返す' do - put :update, :id => @scroll.id, :scroll => @attr, :format => :json - response.message.should match(/Unprocessable/) - end + it 'ユーザではなく管理者でもサインインページに遷移する' do + sign_in @admin + put_save @attr + announce_sign_in_for_html + put_save @attr.merge(:format => :json) + announce_sign_in_for_json end end end diff --git a/spec/factories/author.rb b/spec/factories/author.rb index 85f1ae0b..6274e251 100644 --- a/spec/factories/author.rb +++ b/spec/factories/author.rb @@ -11,7 +11,15 @@ FactoryGirl.define do create_list(:scroll_hello, evaluator.scrolls_count, author: author) end end + trait :with_contents do + after(:create) do |author, evaluator| + panel_hello = create(:panel_hello, author: author) + scroll_hello = create(:scroll_hello, author: author) + scroll_hello_panel_hello = create(:scroll_hello_panel_hello, scroll: scroll_hello, panel: panel_hello) + end + end factory :yas_with_scrolls, traits: [:with_scrolls] + factory :yas_with_contents , traits: [:with_contents] end factory :author_itojun, :class => Author do |i| i.name 'itojun' diff --git a/spec/factories/panel.rb b/spec/factories/panel.rb index ed319180..7d2538c1 100644 --- a/spec/factories/panel.rb +++ b/spec/factories/panel.rb @@ -8,13 +8,33 @@ FactoryGirl.define do i.publish 0 i.author_id 1 end - factory :panel002, :class => Panel do |i| + factory :panel_hello, :class => Panel do |i| i.width 514 i.height 329 i.border 2 - i.caption '' + i.caption 'hello' + i.publish 1 + end + factory :panel_my_name, :class => Panel do |i| + i.width 514 + i.height 329 + i.border 2 + i.caption 'my name is' + i.publish 1 + end + factory :panel_penjiro, :class => Panel do |i| + i.width 514 + i.height 329 + i.border 2 + i.caption 'penjiro' + i.publish 1 + end + factory :panel_enjoy, :class => Panel do |i| + i.width 514 + i.height 329 + i.border 2 + i.caption 'enjoy!' i.publish 1 - i.author_id 1 end factory :panel003, :class => Panel do |i| i.width 200 @@ -22,7 +42,6 @@ FactoryGirl.define do i.border 2 i.caption '' i.publish 0 - i.author_id 1 end factory :panel004, :class => Panel do |i| i.width 200 diff --git a/spec/factories/scroll.rb b/spec/factories/scroll.rb index 3b8face1..ba106f20 100644 --- a/spec/factories/scroll.rb +++ b/spec/factories/scroll.rb @@ -4,13 +4,25 @@ FactoryGirl.define do i.title 'おめざめ2' i.description '' i.visible 1 - association :author, factory: :author_yas - end - factory :scroll002, :class => Scroll do |i| - i.title '意味ないし。' - i.description '' - i.visible 1 - i.author_id 2 + #association :author, factory: :author_yas + trait :with_scroll_panels do + after(:create) do |scroll, evaluator| + #panel_hello = create(:panel_hello, author: scroll.author) + create(:scroll_panel_plain, t: 0, scroll: scroll, panel: create(:panel_hello, author: scroll.author)) + panel_my_name = create(:panel_hello, author: scroll.author) + create(:scroll_panel_plain, t: 1, scroll: scroll, panel: panel_my_name) + panel_penjiro = create(:panel_hello, author: scroll.author) + create(:scroll_panel_plain, t: 2, scroll: scroll, panel: panel_penjiro) + panel_enjoy = create(:panel_hello, author: scroll.author) + create(:scroll_panel_plain, t: 3, scroll: scroll, panel: panel_enjoy) + end + end + factory :scroll_hello_with_scroll_panels, traits: [:with_scroll_panels] + end + factory :scroll_hidden, :class => Scroll do |i| + i.title '非公開のスクロール' + i.description '非公開の状態で作成したり、公開スクロールを非公開に変えるときに使う。' + i.visible 0 end factory :scroll003, :class => Scroll do |i| i.title 'aaaaaaaa' diff --git a/spec/factories/scroll_panel.rb b/spec/factories/scroll_panel.rb index 8f578c3a..3e280924 100644 --- a/spec/factories/scroll_panel.rb +++ b/spec/factories/scroll_panel.rb @@ -1,75 +1,5 @@ # -*- encoding: utf-8 -*- FactoryGirl.define do - factory :scroll_panel001, :class => ScrollPanel do |i| - i.scroll_id 2 - i.panel_id 8 - i.t 0 - i.author_id 2 - end - factory :scroll_panel002, :class => ScrollPanel do |i| - i.scroll_id 1 - i.panel_id 8 - i.t 6 - i.author_id 1 - end - factory :scroll_panel003, :class => ScrollPanel do |i| - i.scroll_id 1 - i.panel_id 7 - i.t 5 - i.author_id 1 - end - factory :scroll_panel004, :class => ScrollPanel do |i| - i.scroll_id 1 - i.panel_id 5 - i.t 0 - i.author_id 1 - end - factory :scroll_panel005, :class => ScrollPanel do |i| - i.scroll_id 1 - i.panel_id 4 - i.t 1 - i.author_id 1 - end - factory :scroll_panel006, :class => ScrollPanel do |i| - i.scroll_id 1 - i.panel_id 6 - i.t 4 - i.author_id 1 - end - factory :scroll_panel007, :class => ScrollPanel do |i| - i.scroll_id 1 - i.panel_id 2 - i.t 2 - i.author_id 1 - end - factory :scroll_panel008, :class => ScrollPanel do |i| - i.scroll_id 1 - i.panel_id 1 - i.t 3 - i.author_id 1 - end - factory :scroll_panel009, :class => ScrollPanel do |i| - i.scroll_id 4 - i.panel_id 2 - i.t 1 - i.author_id 1 - end - factory :scroll_panel010, :class => ScrollPanel do |i| - i.scroll_id 3 - i.panel_id 6 - i.t 0 - i.author_id 1 - end - factory :scroll_panel011, :class => ScrollPanel do |i| - i.scroll_id 4 - i.panel_id 12 - i.t 0 - i.author_id 1 - end - factory :scroll_panel012, :class => ScrollPanel do |i| - i.scroll_id 6 - i.panel_id 11 - i.t 0 - i.author_id 1 + factory :scroll_panel_plain, :class => ScrollPanel do |i| end end diff --git a/spec/factories/user.rb b/spec/factories/user.rb index 440468f9..2536cd89 100644 --- a/spec/factories/user.rb +++ b/spec/factories/user.rb @@ -4,6 +4,13 @@ FactoryGirl.define do i.email "yas@jmail.com" i.password "user0100" i.password_confirmation "user0100" + trait :with_owner do + after(:create) do |user| + create(:author_yas, user: user) + create(:artist_yas, user: user) + end + end + factory :user_yas_with_owner, traits: [:with_owner] end factory :user_itojun, :class => User do |i| i.email "itojun@jmail.com" diff --git a/spec/support/controller_macros.rb b/spec/support/controller_macros.rb index 11440f4f..ef7dd7c2 100644 --- a/spec/support/controller_macros.rb +++ b/spec/support/controller_macros.rb @@ -14,23 +14,67 @@ module ControllerMacros expect {JSON.parse(response.body)}.not_to raise_error end - def announce_sign_in_at_get action, params = {} + # ゲストが画面からコンテンツにアクセスしようとした時の動作を確認する + def announce_sign_in_for_html # html形式: ステータスコード302 Foundを返す - get action, params expect(response.status).to eq(302) expect(response).to redirect_to '/users/sign_in' + end + + # ゲストがapiからコンテンツにアクセスしようとした時の動作を確認する + def announce_sign_in_for_json # json形式: ステータスコード401 Unauthorizedを返す - get action, params.merge(:format => :json) expect(response.status).to eq(401) expect(response.message).to match(/Unauthorized/) end + # ゲストがコンテンツにアクセスしようとしたとき、サインインページに遷移する。 + # ほとんどすべての機能から利用されるので、 htmlとjsonをまとめて処理している。 + # 作成と更新はメソッドが違うので、個別にチェックする。 + def announce_sign_in_at_get action, params = {} + get action, params + announce_sign_in_for_html + get action, params.merge(:format => :json) + announce_sign_in_for_json + end + + # 作家活動をしていないユーザーが画面から創作活動をしようとしたとき、作家登録ページに遷移する + def announce_regist_author_for_html + expect(response.status).to eq(302) + expect(response).to redirect_to new_author_path + end + + # 作家活動していないユーザーがAPIから創作活動をしようとしたとき、例外を発生させる。 + # 暫定的な仕様なので、良い方法があれば変えるかもしれない。メンテしやすいように切り分けてある。 + # リクエストして例外が発生したところとらえるので、リクエストと検証を同時に行う + def announce_regist_author_for_json method, params = {}, action = :create + expect {__send__(method, action, params.merge(:format => :json))}.to raise_error + end + def return_ok_at_get action, params = {} # ステータスコード200 OKを返す get action, params expect(response).to be_success end + def post_save params, action = :create + post action, params + end + + def put_save params, action = :update + put action, params + end + + # 保存処理を成功したことにしてスキップする + def allow_save my_model_class + allow_any_instance_of(my_model_class).to receive(:save).and_return(true) + end + + # 保存処理を失敗したことにしてスキップする + def reject_save my_model_class + allow_any_instance_of(my_model_class).to receive(:save).and_return(false) + end + def show_action_return_profiler item_id # prof形式: ステータスコード200 OKを返す get :show, :id => item_id, :format => :prof @@ -51,5 +95,9 @@ module ControllerMacros expect(response.message).to match(/Unauthorized/) end + def factory_contents factory_name, author, count = 5 + FactoryGirl.create_list(factory_name, count, author: author) + end + end -- 2.11.0