From 4a07277d3517672f4fc7a96af8cc2b2da54f3525 Mon Sep 17 00:00:00 2001 From: yasushiito Date: Sat, 24 Mar 2012 13:49:51 +0900 Subject: [PATCH] comic test ok --- Gemfile | 7 +- Gemfile.lock | 9 +- app/controllers/application_controller.rb | 18 + app/controllers/comics_controller.rb | 54 +-- app/models/comic.rb | 8 + config/application.rb | 4 + config/environments/production.rb | 3 +- config/routes.rb | 1 + db/schema.rb | 25 +- spec/controllers/comics_controller_spec.rb | 519 ++++++++++++++++++----------- spec/models/comic_spec.rb | 68 +++- spec/spec_helper.rb | 4 + 12 files changed, 469 insertions(+), 251 deletions(-) diff --git a/Gemfile b/Gemfile index 46c6d45d..99ee8a89 100644 --- a/Gemfile +++ b/Gemfile @@ -33,9 +33,9 @@ gem 'jquery-rails' # gem 'ruby-debug19', :require => 'ruby-debug' group :production do - gem 'pg' - gem 'therubyracer-heroku' - gem 'rmagick', '=2.12.0', :require => 'RMagick' +# gem 'pg' +# gem 'therubyracer-heroku' +# gem 'rmagick', '=2.12.0', :require => 'RMagick' end group :development, :test do @@ -47,4 +47,5 @@ group :development, :test do gem 'moro-miso' gem 'database_cleaner' gem 'rails-erd' + gem 'factory_girl' end diff --git a/Gemfile.lock b/Gemfile.lock index 30c6cd9a..2df19c26 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -71,6 +71,8 @@ GEM erubis (2.7.0) execjs (1.2.11) multi_json (~> 1.0) + factory_girl (2.6.4) + activesupport (>= 2.3.9) ffi (1.0.11-x86-mingw32) gherkin (2.6.9-x86-mingw32) json (>= 1.4.6) @@ -89,7 +91,6 @@ GEM multi_json (1.0.4) nokogiri (1.5.0-x86-mingw32) orm_adapter (0.0.5) - pg (0.11.0-x86-mingw32) polyglot (0.3.3) rack (1.3.5) rack-cache (1.1) @@ -122,7 +123,6 @@ GEM rake (0.9.2.2) rdoc (3.11) json (~> 1.4) - rmagick (2.12.0) rspec (2.7.0) rspec-core (~> 2.7.0) rspec-expectations (~> 2.7.0) @@ -155,7 +155,6 @@ GEM tilt (~> 1.1, != 1.3.0) sqlite3 (1.3.5-x86-mingw32) term-ansicolor (1.0.7) - therubyracer-heroku (0.8.1.pre3) thor (0.14.6) tilt (1.3.3) treetop (1.4.10) @@ -185,16 +184,14 @@ DEPENDENCIES cucumber-rails database_cleaner devise + factory_girl jquery-rails moro-miso - pg rails (= 3.1.1) rails-erd - rmagick (= 2.12.0) rspec rspec-rails sass-rails (~> 3.1.4) sqlite3 - therubyracer-heroku uglifier (>= 1.0.3) webrat diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 2be23256..978f3d97 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -21,4 +21,22 @@ class ApplicationController < ActionController::Base end end +=begin + rescue_from ActiveRecord::RecordNotFound, :with => :render_404 + + private + def render_404(exception = nil) + if exception + logger.info "Rendering 404: #{exception.message}" + end + respond_to do |format| + format.html { + render :file => "#{Rails.root}/public/404.html", :status => 404, :layout => false +} + format.json { + render :text => "404 found", :status => 404 +} + end + end +=end end diff --git a/app/controllers/comics_controller.rb b/app/controllers/comics_controller.rb index 1a1dc681..9d4e4454 100644 --- a/app/controllers/comics_controller.rb +++ b/app/controllers/comics_controller.rb @@ -1,5 +1,9 @@ class ComicsController < ApplicationController - before_filter :authenticate_user!, :only => [:top, :index, :show, :play, :create, :update, :destroy] + if Const.run_mode == 0 + before_filter :authenticate_user!, :only => [:new, :create, :edit, :update, :destroy] + else + before_filter :authenticate_user!, :only => [:top, :index, :show, :play, :new, :create, :edit, :update, :destroy] + end before_filter :authenticate_admin!, :only => [:list, :browse] private @@ -26,7 +30,9 @@ class ComicsController < ApplicationController # GET /comics # GET /comics.json def index - @comics = Comic.list({}, params[:page].to_i, params[:page_size].to_i) + @page = params[:page].to_i + @page_size = params[:page_size].to_i + @comics = Comic.list({}, @page, @page_size) respond_to do |format| format.html # index.html.erb format.json { render json: @comics.to_json(Comic.list_json_opt) } @@ -39,16 +45,19 @@ class ComicsController < ApplicationController @comic = Comic.show(params[:id]) respond_to do |format| - if @comic.own?(@author) or @comic.visible > 0 - format.html # show.html.erb - format.json { render json: @comic.to_json(Comic.show_json_opt) } - else - format.html { render :status => :forbidden, :file => '/403.html'} - format.json { head :forbidden } - end + raise ActiveRecord::Forbidden unless @comic.vis(@author) + format.html # show.html.erb + format.json { render json: @comic.to_json(Comic.show_json_opt) } end end + def count + @comic = {:count => Comic.visible_count} + respond_to do |format| + format.json { render json: @comic.to_json } + end + end + def play @comic = Comic.play(params[:id]) respond_to do |format| @@ -95,14 +104,11 @@ class ComicsController < ApplicationController # GET /comics/1.js/edit def edit @comic = Comic.find(params[:id]) + @comic.supply_default respond_to do |format| - if @comic.own? @author - format.html - format.js - else - format.html { render :status => :forbidden, :file => '/403.html'} - format.js { head :forbidden } - end + raise ActiveRecord::Forbidden unless @comic.own?(@author) + format.html + format.js end end @@ -130,17 +136,13 @@ class ComicsController < ApplicationController params[:comic].merge! author_id: @author.id @comic = Comic.find(params[:id]) respond_to do |format| - if @comic.own? @author - if @comic.update_attributes(params[:comic]) - format.html { redirect_to @comic, notice: 'Comic was successfully updated.' } - format.json { head :ok } - else - format.html { render action: "edit" } - format.json { render json: @comic.errors, status: :unprocessable_entity } - end + raise ActiveRecord::Forbidden unless @comic.own?(@author) + if @comic.update_attributes(params[:comic]) + format.html { redirect_to @comic, notice: 'Comic was successfully updated.' } + format.json { head :ok } else - format.html { render :status => :forbidden, :file => '/403.html'} - format.json { head :forbidden } + format.html { render action: "edit" } + format.json { render json: @comic.errors, status: :unprocessable_entity } end end end diff --git a/app/models/comic.rb b/app/models/comic.rb index 44b1f3b3..8dccc588 100644 --- a/app/models/comic.rb +++ b/app/models/comic.rb @@ -28,6 +28,10 @@ class Comic < ActiveRecord::Base self.author_id == author.id end + def vis author + self.own?(author) or self.visible > 0 + end + def disp_editable editable == 1 ? 'O' : 'X' end @@ -88,4 +92,8 @@ class Comic < ActiveRecord::Base self.to_json( :include => {:author => {}, :panels => {:methods => :panel_element}}) end + def self.visible_count + Comic.count 'visible > 0' + end + end diff --git a/config/application.rb b/config/application.rb index 35fd1cc2..5d6df701 100644 --- a/config/application.rb +++ b/config/application.rb @@ -51,5 +51,9 @@ config.assets.initialize_on_precompile = false # Version of your assets, change this if you want to expire all your assets config.assets.version = '1.0' + end end +class ActiveRecord::Forbidden < ActiveRecord::ActiveRecordError +end + diff --git a/config/environments/production.rb b/config/environments/production.rb index 25028e67..a063e948 100644 --- a/config/environments/production.rb +++ b/config/environments/production.rb @@ -1,4 +1,5 @@ -require 'RMagick' +#require 'RMagick' +require 'DMagick' Pettanr::Application.configure do # Settings specified here will take precedence over those in config/application.rb diff --git a/config/routes.rb b/config/routes.rb index 5ba523c9..a7b53418 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -136,6 +136,7 @@ Pettanr::Application.routes.draw do collection do get :index get :show + get :count post :create get :list get :browse diff --git a/db/schema.rb b/db/schema.rb index 6160bd67..6d51ff26 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -95,12 +95,12 @@ ActiveRecord::Schema.define(:version => 20120306231650) do add_index "balloons", ["panel_id"], :name => "index_balloons_on_panel_id" create_table "comics", :force => true do |t| - t.string "title" - t.integer "width", :default => 200, :null => false - t.integer "height", :default => 80, :null => false - t.integer "visible", :default => 0, :null => false - t.integer "editable", :default => 0, :null => false - t.integer "author_id" + t.string "title", :limit => 100, :null => false + t.integer "width", :null => false + t.integer "height", :null => false + t.integer "visible", :default => 0, :null => false + t.integer "editable", :default => 0, :null => false + t.integer "author_id", :null => false t.datetime "created_at" t.datetime "updated_at" end @@ -241,13 +241,14 @@ ActiveRecord::Schema.define(:version => 20120306231650) do add_index "speach_templates", ["balloon_template_id", "t"], :name => "speach_templates_idt", :unique => true create_table "speaches", :force => true do |t| - t.integer "balloon_id", :null => false + t.integer "balloon_id", :null => false + t.integer "speach_template_id", :null => false t.string "content" - t.integer "x", :null => false - t.integer "y", :null => false - t.integer "t", :default => 1, :null => false - t.integer "width", :null => false - t.integer "height", :null => false + t.integer "x", :null => false + t.integer "y", :null => false + t.integer "t", :default => 1, :null => false + t.integer "width", :null => false + t.integer "height", :null => false t.datetime "created_at" t.datetime "updated_at" end diff --git a/spec/controllers/comics_controller_spec.rb b/spec/controllers/comics_controller_spec.rb index 4cb92bf0..95dde5ac 100644 --- a/spec/controllers/comics_controller_spec.rb +++ b/spec/controllers/comics_controller_spec.rb @@ -1,16 +1,33 @@ +# -*- encoding: utf-8 -*- require 'spec_helper' describe ComicsController do # This should return the minimal set of attributes required to create a valid # Comic. As you add validations to Comic, be sure to # update the return value of this method accordingly. + before do + Factory :admin + @user = Factory :user_yas + @author = @user.author #ユーザ作成時に連動して作成される + end + def valid_attributes {:title => 'test comic', :width => 100, :height => 50, :visible => 0, :editable => 0} end describe '一覧表示に於いて' do before do - Pettanr.run_mode = 1 + Factory :normal_comic, :author_id => @user.author.id + Factory :visible_comic, :author_id => @user.author.id + Factory:editable_comic, :author_id => @user.author.id + Factory :hidden_comic, :author_id => @user.author.id + sign_in @user + end + context '事前チェックする' do + it '与えられたpageがセットされている' do + get :index, :page => 5 + assigns(:page).should eq 5 + end end context 'つつがなく終わるとき' do it 'ステータスコード200 OKを返す' do @@ -21,12 +38,6 @@ describe ComicsController do get :index assigns(:comics).should have_at_least(3).items end - context 'パラメータが' do - it 'indexテンプレートを描画する' do - get :index - response.should render_template("index") - end - end context 'html形式' do it 'indexテンプレートを描画する' do get :index @@ -36,37 +47,47 @@ describe ComicsController do context 'json形式' do it 'jsonデータを返す' do get :index, :format => :json - json = JSON.parse response.body - json.should match(/error/) + lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError) end it 'データがリスト構造になっている' do get :index, :format => :json - json = JSON.parse response.body + 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 - it 'ステータスコード401 Unauthorizedを返す' do - get :index - response.status.should eq 401 - end context 'html形式' do + it 'ステータスコード302 Foundを返す' do + sign_out @user + get :index + response.status.should eq 302 + end it 'サインインページへ遷移する' do + sign_out @user get :index - response.should redirect_to '/' + response.should redirect_to '/users/sign_in' end end context 'json形式' do - it '文字列errorが含まれたjsonデータを返す' do - response.body.should have_tag('json', 'error') + it 'ステータスコード401 Unauthorizedを返す' do + sign_out @user + get :index, :format => :json + response.status.should eq 401 end - end - context 'ただし、公開型のときだけは' do - it 'ステータスコード200 OKを返す' do - Pettanr.run_mode = 0 - get :index - response.should be_success + it 'jsonデータを返す' do + get :index, :format => :json + lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError) + end + it '応答メッセージにUnauthorizedを返す' do + sign_out @user + get :index, :format => :json + response.message.should match(/Unauthorized/) end end end @@ -74,80 +95,100 @@ describe ComicsController do describe '単体表示に於いて' do before do - Pettanr.run_mode = 1 + @comic = Factory :normal_comic, :author_id => @user.author.id + sign_in @user end context 'つつがなく終わるとき' do it 'ステータスコード200 OKを返す' do - get :show, :id => 1 - response.should be_success + get :show, :id => @comic.id + response.should be_success end it '@comicにアレを取得している' do - get :show, :id => 1 - assigns(:comic).should eq(comic) + get :show, :id => @comic.id + assigns(:comic).id.should eq(@comic.id) end context 'html形式' do it 'showテンプレートを描画する' do - get :show, :id => 1 + get :show, :id => @comic.id response.should render_template("show") end end context 'json形式' do it 'jsonデータを返す' do - get :show, :id => 1, :format => :json - json = JSON.parse response.body - json.should have_tag('json', 'error') + get :show, :id => @comic.id, :format => :json + lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError) end it 'データがアレになっている' do - get :show, :id => 1, :format => :json + get :show, :id => @comic.id, :format => :json json = JSON.parse response.body - json.should have_tag('json', 'error') + json["title"].should match(/normal/) end end end context '作家権限がないとき' do - it 'ステータスコード401 Unauthorizedを返す' do - get :show, :id => 1 - response.should eq 401 + it 'ステータスコード302 Foundを返す' do + sign_out @user + get :show, :id => @comic.id + response.status.should eq 302 end context 'html形式' do it 'サインインページへ遷移する' do - get :show, :id => 1 - response.should redirect_to '/' + sign_out @user + get :show, :id => @comic.id + response.body.should redirect_to '/users/sign_in' end end context 'json形式' do - it '文字列errorが含まれたjsonデータを返す' do - + it '応答メッセージにUnauthorizedを返す' do + sign_out @user + get :show, :id => @comic.id, :format => :json + response.message.should match(/Unauthorized/) end end - context 'ただし、公開型のときだけは' do - it 'ステータスコード200 OKを返す' do - Pettanr.run_mode = 0 - get :show, :id => 1 - response.should be_success + end + context '対象コミックがないとき' do + context 'html形式' do + it '例外404 not_foundを返す' do + lambda{ + get :show, :id => 0 + }.should raise_error(ActiveRecord::RecordNotFound) + end + end + context 'json形式' do + it '例外404 not_foundを返す' do + lambda{ + get :show, :id => 0, :format => :json + }.should raise_error(ActiveRecord::RecordNotFound) end end end context '非公開コミックを見ようとしたとき' do - it 'ステータスコード403 forbiddenを返す' do - get :show, :id => 1 - response.status.should eq 403 - end context 'html形式' do - it 'forbiddenページへ遷移する' do - get :show, :id => 1 - response.should redirect_to '/403.html' + it '例外403 forbiddenを返す' do + Comic.any_instance.stub(:vis).with(any_args()).and_return(false) + hidden = Factory :hidden_comic, :author_id => @author.id + lambda{ + get :show, :id => hidden + }.should raise_error(ActiveRecord::Forbidden) end end context 'json形式' do - it 'エラーメッセージを返す' do - + it '例外403 forbiddenを返す' do + Comic.any_instance.stub(:vis).with(any_args()).and_return(false) + hidden = Factory :hidden_comic, :author_id => @author.id + lambda{ + get :show, :id => hidden, :format => :json + }.should raise_error(ActiveRecord::Forbidden) 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 @@ -156,19 +197,21 @@ describe ComicsController do context 'json形式' do it 'jsonデータを返す' do get :count, :format => :json - json = JSON.parse response.body - json.should have_tag('json', 'error') + lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError) end - it 'データがリスト構造になっている' do + it 'データがHash構造になっていてコミック数が1である' do get :count, :format => :json json = JSON.parse response.body - json.should have_tag('json', 'error') + json["count"].should == 3 end end end end describe '新規作成フォーム表示に於いて' do + before do + sign_in @user + end context 'つつがなく終わるとき' do it 'ステータスコード200 OKを返す' do get :new @@ -178,69 +221,106 @@ describe ComicsController do get :new assigns(:comic).should be_a_new(Comic) end - it '@comicにデフォルト値editable=0, visible=0がセットされている' do + it '@comicにデフォルト値セットが施されている' 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 '部分フォームを返す' do - + it 'new.jsテンプレートを描画する' do + get :new, :format => :js + response.should render_template("new") end end end context '作家権限がないとき' do - it 'ステータスコード401 Unauthorizedを返す' do - + context 'html形式' do + it 'ステータスコード302 Foundを返す' do + sign_out @user + get :new + response.status.should eq 302 + end + it 'サインインページへ遷移する' do + sign_out @user + get :new + response.body.should redirect_to '/users/sign_in' + end end context 'js形式' do - it '文字列errorが含まれたjsonデータを返す' do - + it 'ステータスコード401 Unauthorizedを返す' do + sign_out @user + get :new, :format => :js + response.status.should eq 401 + end + it '応答メッセージにUnauthorizedを返す' do + sign_out @user + get :new, :format => :js + response.message.should match(/Unauthorized/) end end end end describe '新規作成に於いて' do + before do + sign_in @user + end context 'つつがなく終わるとき' do - it 'ステータスコード200 OKを返す' do - post :create - response.should be_success + it 'モデルに保存依頼する' do + Comic.any_instance.should_receive(:save).exactly(1) + post :create, :comic => Factory.attributes_for(:normal_comic) end - it '作成される' do - expect { - post :create, :comic => valid_attributes - }.to change(Comic, :count).by(1) + 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 "@comicに作成されたコミックを保持している" do - post :create, :comic => valid_attributes + 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 '作成されたコミックの表示ページへ遷移する' do - post :create, :comic => valid_attributes + post :create, :comic => Factory.attributes_for(:normal_comic) response.should redirect_to(Comic.last) end end context 'json形式' do 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 + it 'ステータスコード302 Foundを返す' do + sign_out @user + post :create, :comic => Factory.attributes_for(:normal_comic) + response.status.should eq 302 + end context 'html形式' do it 'サインインページへ遷移する' do - + sign_out @user + 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 - - end - it '文字列errorが含まれたjsonデータを返す' do - + it '応答メッセージにUnauthorizedを返す' do + sign_out @user + post :create, :comic => Factory.attributes_for(:normal_comic), :format => :json + response.message.should match(/Unauthorized/) end end end @@ -251,6 +331,11 @@ describe ComicsController do assigns(:comic).should be_a_new(Comic) end context 'html形式' do + it 'ステータスコード200 OKを返す' do + Comic.any_instance.stub(:save).and_return(false) + post :create, :comic => {} + response.status.should eq 200 + end it '新規ページを描画する' do Comic.any_instance.stub(:save).and_return(false) post :create, :comic => {} @@ -259,188 +344,242 @@ describe ComicsController do end context 'json形式' do it 'ステータスコード422 unprocessable_entity を返す' do - + Comic.any_instance.stub(:save).and_return(false) + post :create, :comic => {}, :format => :json + response.status.should eq 422 end - it '文字列errorが含まれたjsonデータを返す' do - + it 'jsonデータを返す' do + Comic.any_instance.stub(:save).and_return(false) + post :create, :comic => {}, :format => :json + lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError) + end + it 'データがアレになっている' do + Comic.any_instance.stub(:save).and_return(false) + post :create, :comic => {}, :format => :json + json = JSON.parse response.body + json.should eq( {}) end end end end describe '編集フォーム表示に於いて' do + before do + @comic = Factory :normal_comic, :author_id => @user.author.id + sign_in @user + end context 'つつがなく終わるとき' do - context 'js形式' do - it '部分フォームを返す' do - + it 'ステータスコード200 OKを返す' do + get :edit, :id => @comic.id + response.should be_success + end + it '@comicにデータを用意している' do + get :edit, :id => @comic.id + assigns(:comic).should eq @comic + end + it '@comicにデフォルト値セットが施されている' do + Comic.any_instance.should_receive(:supply_default).exactly(1) + get :edit, :id => @comic.id + end + context 'html形式' do + it 'editテンプレートを描画する' do + get :edit, :id => @comic.id + response.should render_template("edit") end - it '指定のデータが入っている' do - + 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 - it 'ステータスコード401 Unauthorizedを返す' do - + context 'html形式' do + it 'ステータスコード302 Foundを返す' do + sign_out @user + get :edit, :id => @comic.id + response.status.should eq 302 + end + it 'サインインページへ遷移する' do + sign_out @user + get :edit, :id => @comic.id + response.body.should redirect_to '/users/sign_in' + end end context 'js形式' do - it '文字列errorが含まれたjsonデータを返す' do - + it 'ステータスコード401 Unauthorizedを返す' do + sign_out @user + get :edit, :id => @comic.id, :format => :js + response.status.should eq 401 + end + it '応答メッセージにUnauthorizedを返す' do + sign_out @user + get :edit, :id => @comic.id, :format => :js + response.message.should match(/Unauthorized/) end end end context '対象コミックがないとき' do - it 'ステータスコード404 not_foundを返す' do - - end context 'html形式' do - it '404ページへ遷移する' do - + it '例外404 not_foundを返す' do + lambda{ + get :edit, :id => 0 + }.should raise_error(ActiveRecord::RecordNotFound) end end context 'js形式' do - it '文字列errorが含まれたjsonデータを返す' do - + it '例外404 not_foundを返す' do + lambda{ + get :edit, :id => 0, :format => :js + }.should raise_error(ActiveRecord::RecordNotFound) + end + end + end + context '自分のコミックでないとき' do + context 'html形式' do + it '例外403 forbiddenを返す' do + other = Factory :visible_comic, :author_id => 0 + lambda{ + get :edit, :id => other.id + }.should raise_error(ActiveRecord::Forbidden) + end + end + context 'json形式' do + it '例外403 forbiddenを返す' do + other = Factory :visible_comic, :author_id => 0 + lambda{ + get :edit, :id => other.id + }.should raise_error(ActiveRecord::Forbidden) end end end end describe '更新に於いて' do + before do + @comic = Factory :normal_comic, :author => @author + sign_in @user + end + context '事前チェックしておく' do + 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 - it '更新される' 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) + Comic.find(@comic.id).visible.should eq 0 end context 'html形式' do it '更新されたコミックの表示ページへ遷移する' do - + put :update, :id => @comic.id, :comic => Factory.attributes_for(:hidden_comic) + response.should redirect_to(@comic) end end context 'json形式' do - it '更新されたコミックをjsonデータで返す' do - + 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 - it 'ステータスコード401 Unauthorizedを返す' do - + it 'ステータスコード302 Foundを返す' do + sign_out @user + put :update, :id => @comic.id, :comic => Factory.attributes_for(:hidden_comic) + response.status.should eq 302 end context 'html形式' do it 'サインインページへ遷移する' do - + sign_out @user + 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 '文字列errorが含まれたjsonデータを返す' do - + it '応答メッセージにUnauthorizedを返す' do + sign_out @user + put :update, :id => @comic.id, :comic => Factory.attributes_for(:hidden_comic), :format => :json + response.message.should match(/Unauthorized/) end end end - context '他人のコミックを編集しようとしたとき' do - it 'ステータスコード403 forbiddenを返す' do - - end + context '対象コミックがないとき' do context 'html形式' do - it '403ページへ遷移する' do - + it '例外404 not_foundを返す' do + lambda{ + put :update, :id => 0, :comic => Factory.attributes_for(:hidden_comic) + }.should raise_error(ActiveRecord::RecordNotFound) end end context 'json形式' do - it '文字列errorが含まれたjsonデータを返す' do - + it '例外404 not_foundを返す' do + lambda{ + put :update, :id => 0, :comic => Factory.attributes_for(:hidden_comic), :format => :json + }.should raise_error(ActiveRecord::RecordNotFound) end end end - context '対象コミックがないとき' do - it 'ステータスコード404 not_foundを返す' do - - end + context '他人のコミックを編集しようとしたとき' do context 'html形式' do - it '404ページへ遷移する' do - + it '例外403 forbiddenを返す' do + other = Factory :visible_comic, :author_id => 0 + lambda{ + put :update, :id => other.id, :comic => Factory.attributes_for(:hidden_comic) + }.should raise_error(ActiveRecord::Forbidden) end end context 'json形式' do - it '文字列errorが含まれたjsonデータを返す' do - + it '例外403 forbiddenを返す' do + other = Factory :visible_comic, :author_id => 0 + lambda{ + put :update, :id => other.id, :comic => Factory.attributes_for(:hidden_comic), :format => :json + }.should raise_error(ActiveRecord::Forbidden) end end end context '検証、保存に失敗したとき' do context 'html形式' do it '編集ページを描画する' do - + Comic.any_instance.stub(:update_attributes).and_return(false) + 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 '文字列errorが含まれたjsonデータを返す' do - + it 'jsonデータを返す' do + Comic.any_instance.stub(:update_attributes).and_return(false) + put :update, :id => @comic.id, :comic => Factory.attributes_for(:hidden_comic), :format => :json + lambda{ + JSON.parse(response.body) + }.should_not raise_error(JSON::ParserError) + end + it 'データがアレになっている' do + Comic.any_instance.stub(:update_attributes).and_return(false) + put :update, :id => @comic.id, :comic => Factory.attributes_for(:hidden_comic), :format => :json + json = JSON.parse response.body + json.should eq( {}) end end end end - describe "PUT update" do - describe "with valid params" do - it "updates the requested comic" do - comic = Comic.create! valid_attributes - # Assuming there are no other comics in the database, this - # specifies that the Comic created on the previous line - # receives the :update_attributes message with whatever params are - # submitted in the request. - Comic.any_instance.should_receive(:update_attributes).with({'these' => 'params'}) - put :update, :id => comic.id, :comic => {'these' => 'params'} - end - - it "assigns the requested comic as @comic" do - comic = Comic.create! valid_attributes - put :update, :id => comic.id, :comic => valid_attributes - assigns(:comic).should eq(comic) - end - - it "redirects to the comic" do - comic = Comic.create! valid_attributes - put :update, :id => comic.id, :comic => valid_attributes - response.should redirect_to(comic) - end - end - - describe "with invalid params" do - it "assigns the comic as @comic" do - comic = Comic.create! valid_attributes - # Trigger the behavior that occurs when invalid params are submitted - Comic.any_instance.stub(:save).and_return(false) - put :update, :id => comic.id, :comic => {} - assigns(:comic).should eq(comic) - end - - it "re-renders the 'edit' template" do - comic = Comic.create! valid_attributes - # Trigger the behavior that occurs when invalid params are submitted - Comic.any_instance.stub(:save).and_return(false) - put :update, :id => comic.id, :comic => {} - response.should render_template("edit") - end - end - end - - describe "DELETE destroy" do - it "destroys the requested comic" do - comic = Comic.create! valid_attributes - expect { - delete :destroy, :id => comic.id - }.to change(Comic, :count).by(-1) - end - - it "redirects to the comics list" do - comic = Comic.create! valid_attributes - delete :destroy, :id => comic.id - response.should redirect_to(comics_url) - end - end end diff --git a/spec/models/comic_spec.rb b/spec/models/comic_spec.rb index 628f134b..a0cf2525 100644 --- a/spec/models/comic_spec.rb +++ b/spec/models/comic_spec.rb @@ -1,11 +1,31 @@ +# -*- encoding: utf-8 -*- require 'spec_helper' describe Comic do - pending "add some examples to (or delete) #{__FILE__}" before do + Factory :admin + @user = Factory( :user_yas) + @author = Factory( :author_yas, :user_id => @user.id) + @artist = Factory :artist_yas, :author_id => @author.id @comic = Comic.new end + + describe '検証に於いて' do + before do + @comic = Comic.new + end + + context 'titleを検証するとき' do + it '100文字以上なら失敗する' do + end + end + end + describe 'データ補充に於いて' do + before do + @comic = Comic.new + end + context '初期値を補充するとき' do it '空なら0が補充される' do @comic.supply_default @@ -22,23 +42,45 @@ describe Comic do end end end + describe '作者判定に於いて' do before do end - context '同一作家のとき' do - it 'trueを返す' do - @comic.own?(author).should == true - end + it '自作のコミックならyes' do + comic = Factory :normal_comic, :author_id => @author.id + comic.own?(@author).should == true end - context '同一作家でないとき' do - it 'falseを返す' do - @comic.own?(author).should == false - end + it '他人のコミックならno' do + comic = Factory :normal_comic, :author_id => @author.id - 1 + comic.own?(@author).should == false end - context 'パラメータが不正なとき' do - it 'falseを返す' do - @comic.own?(nil).should == false - end + it '作家が不明ならno' do + comic = Factory :normal_comic, :author_id => @author.id + comic.own?(nil).should == false + end + end + describe '閲覧許可に於いて' do + before do + end + it '自作の公開コミックを見るときは許可する' do + Comic.any_instance.stub('own?').and_return(true) + comic = Factory :normal_comic, :author_id => @author.id + comic.vis(@author).should == true + end + it '自作のコミックは非公開でも許可する' do + Comic.any_instance.stub('own?').and_return(true) + comic = Factory :hidden_comic, :author_id => @author.id + comic.vis(@author).should == true + end + it '他人のコミックでも公開コミックなら許可する' do + Comic.any_instance.stub('own?').and_return(false) + comic = Factory :normal_comic, :author_id => @author.id - 1 + comic.vis(@author).should == true + end + it '他人のコミックで非公開コミックなら許可しない' do + Comic.any_instance.stub('own?').and_return(false) + comic = Factory :hidden_comic, :author_id => @author.id - 1 + comic.vis(@author).should == false end end describe '保存検証に於いて' do diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 6d7f66d2..ff721422 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -7,6 +7,7 @@ require 'rspec/autorun' # Requires supporting ruby files with custom matchers and macros, etc, # in spec/support/ and its subdirectories. Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f} +require File.expand_path(File.dirname(__FILE__) + '/factories.rb') RSpec.configure do |config| # == Mock Framework @@ -30,4 +31,7 @@ RSpec.configure do |config| # automatically. This will be the default behavior in future versions of # rspec-rails. config.infer_base_class_for_anonymous_controllers = false + config.include Devise::TestHelpers, :type => :controller +# config.extend ControllerMacros, :type => :controller + end -- 2.11.0