From 3f0ce2cf0fdb97034e05ce91dfbc6bf818c7ea89 Mon Sep 17 00:00:00 2001 From: yasushiito Date: Sat, 31 Mar 2012 10:21:21 +0900 Subject: [PATCH] picture test --- app/controllers/original_pictures_controller.rb | 93 +-- app/models/original_picture.rb | 55 ++ config/application.rb | 4 +- config/environments/test.rb | 9 + .../original_pictures_controller_spec.rb | 856 ++++++++++++++++++--- spec/factories.rb | 2 +- 6 files changed, 850 insertions(+), 169 deletions(-) diff --git a/app/controllers/original_pictures_controller.rb b/app/controllers/original_pictures_controller.rb index 694f57d7..efcd36e8 100644 --- a/app/controllers/original_pictures_controller.rb +++ b/app/controllers/original_pictures_controller.rb @@ -1,7 +1,8 @@ class OriginalPicturesController < ApplicationController before_filter :authenticate_user!, :only => [:index, :show, :new, :edit, :create, :update, :destroy] before_filter :authenticate_admin!, :only => [:list, :browse] - + before_filter :authenticate_artist, :only => [:index, :show, :new, :edit, :create, :update, :destroy] + private def set_image(prm) @@ -30,39 +31,56 @@ class OriginalPicturesController < ApplicationController img end + def authenticate_artist + if @author.artist? + true + else + respond_to do |format| + format.html { redirect_to new_artist_path, :status => :found } + format.js { render "artists/new" } + format.json { + raise ActiveRecord::Forbidden + } + end + false + end + end + public # GET /original_pictures # GET /original_pictures.json def index - @original_pictures = OriginalPicture.find(:all, - :include => [:artist, :license, :resource_picture], :order => 'updated_at', :limit => 20 - ) + @page = OriginalPicture.page params[:page] + @page_size = OriginalPicture.page_size params[:page_size] + @original_pictures = OriginalPicture.list(@artist.id, {}, @page, @page_size) respond_to do |format| format.html # index.html.erb - format.json { render :json => @original_pictures.to_json( - :include => [:resource_picture, :artist, :license] - ) } + format.json { render json: @original_pictures.to_json(OriginalPicture.list_json_opt) } end end # GET /original_pictures/1 # GET /original_pictures/1.json def show - @original_picture = OriginalPicture.find(params[:id]) + @original_picture = OriginalPicture.show(params[:id]) # if params[:subdir] == 'refresh' # refresh # return # end respond_to do |format| - opt = {:type => @original_picture.mime_type, :disposition=>"inline"} - format.png { send_data(@original_picture.restore, opt ) } - format.gif { send_data(@original_picture.restore, opt ) } - format.jpeg { send_data(@original_picture.restore, opt ) } - format.html # show.html.erb - format.json { render json: @original_picture} + if @original_picture.own?(@author) + opt = {:type => @original_picture.mime_type, :disposition=>"inline"} + format.png { send_data(@original_picture.restore, opt ) } + format.gif { send_data(@original_picture.restore, opt ) } + format.jpeg { send_data(@original_picture.restore, opt ) } + format.html # show.html.erb + format.json { render json: @original_picture} + else + raise ActiveRecord::Forbidden + end end end @@ -97,6 +115,7 @@ class OriginalPicturesController < ApplicationController # GET /original_pictures/new.json def new @original_picture = OriginalPicture.new + @original_picture.supply_default @artist respond_to do |format| format.html # new.html.erb @@ -107,19 +126,17 @@ class OriginalPicturesController < ApplicationController # GET /original_pictures/1/edit def edit - @original_picture = OriginalPicture.find(params[:id]) + @original_picture = OriginalPicture.show(params[:id]) + raise ActiveRecord::Forbidden unless @original_picture.own?(@author) + respond_to do |format| + format.html + format.js + end end # POST /original_pictures # POST /original_pictures.json def create - unless @artist - respond_to do |format| - format.html { redirect_to @original_picture, notice: 'Failed! author does not artist.' } - format.json { render json: @original_picture.errors, status: :unprocessable_entity } - end - return - end @original_picture = OriginalPicture.new img = set_image params @original_picture.artist_id = @author.artist.id @@ -146,21 +163,8 @@ class OriginalPicturesController < ApplicationController # PUT /original_pictures/1 # PUT /original_pictures/1.json def update - unless @artist - respond_to do |format| - format.html { redirect_to @original_picture, notice: 'Failed! author does not artist.' } - format.json { render json: @original_picture.errors, status: :unprocessable_entity } - end - return - end - @original_picture = OriginalPicture.find(params[:id]) - unless @original_picture.own? @author - respond_to do |format| - format.html { redirect_to @original_picture, notice: 'Failed! ' } - format.json { render json: @original_picture.errors, status: :unprocessable_entity } - end - return - end + @original_picture = OriginalPicture.show(params[:id]) + raise ActiveRecord::Forbidden unless @original_picture.own?(@author) img = set_image params respond_to do |format| @@ -184,21 +188,8 @@ class OriginalPicturesController < ApplicationController # DELETE /original_pictures/1 # DELETE /original_pictures/1.json def destroy - unless @artist - respond_to do |format| - format.html { redirect_to @original_picture, notice: 'Failed! author does not artist.' } - format.json { render json: @original_picture.errors, status: :unprocessable_entity } - end - return - end @original_picture = OriginalPicture.find(params[:id]) - unless @original_picture.own? @author - respond_to do |format| - format.html { redirect_to @original_picture, notice: 'Failed! ' } - format.json { render json: @original_picture.errors, status: :unprocessable_entity } - end - return - end + raise ActiveRecord::Forbidden unless @original_picture.own?(@author) OriginalPicture.transaction do @original_picture.destroy end diff --git a/app/models/original_picture.rb b/app/models/original_picture.rb index 47ec7c8f..90679f44 100644 --- a/app/models/original_picture.rb +++ b/app/models/original_picture.rb @@ -9,6 +9,61 @@ class OriginalPicture < ActiveRecord::Base errors.add(:filesize, 'size over(1MB)') if self.filesize > 1000000 end + def supply_default art + self.license_id = art.default_license_id if self.license_id.blank? + end + + def self.default_page_size + 25 + end + + def self.max_page_size + 100 + end + + def self.page prm = nil + page = prm.to_i + page = 1 if page < 1 + page + end + + def self.page_size prm = self.default_page_size + page_size = prm.to_i + page_size = self.max_page_size if page_size > self.max_page_size + page_size = self.default_page_size if page_size < 1 + page_size + end + + def self.list artist_id, opt = {}, page = 1, page_size = self.default_page_size + opt.merge!(self.list_opt) unless opt[:include] + opt.merge!({:conditions => ['artist_id = ?', artist_id], + :order => 'updated_at desc', :limit => page_size, :offset => (page -1) * page_size} + ) + OriginalPicture.find(:all, opt) + end + + def self.list_opt + {:include => [:artist, :license, :resource_picture]} + end + + def self.list_json_opt + {:include => [:resource_picture, :artist, :license]} + end + + def self.show cid, opt = {} + Comic.find(cid, :include => self.show_include_opt(opt)) + end + + def self.show_include_opt opt = {} + res = [:author] + res.push(opt[:include]) if opt[:include] + res + end + + def self.show_json_include_opt + {:include => :author} + end + def destroy_with_file PictureIO.original_picture_io.delete self.filename self.resource_picture.destroy diff --git a/config/application.rb b/config/application.rb index b4c8ec57..1f18627a 100644 --- a/config/application.rb +++ b/config/application.rb @@ -55,6 +55,8 @@ config.assets.initialize_on_precompile = false config.autoload_paths += %W(#{config.root}/lib/validators) end end -class ActiveRecord::Forbidden < ActiveRecord::ActiveRecordError +module ActiveRecord + class Forbidden < ActiveRecordError + end end diff --git a/config/environments/test.rb b/config/environments/test.rb index a9b849b4..ce3efa42 100644 --- a/config/environments/test.rb +++ b/config/environments/test.rb @@ -1,3 +1,5 @@ +require 'DMagick' + Pettanr::Application.configure do # Settings specified here will take precedence over those in config/application.rb @@ -37,3 +39,10 @@ Pettanr::Application.configure do # Print deprecation notices to the stderr config.active_support.deprecation = :stderr end + +require 'local_picture' +PictureIO.setup do |config| + config.original_picture_io = PictureIO::LocalPicture.new '/sites/original/pettanr/' + config.resource_picture_io = PictureIO::LocalPicture.new '/sites/resource/pettanr/' + config.system_picture_io = PictureIO::LocalPicture.new '/sites/system/pettanr/' +end diff --git a/spec/controllers/original_pictures_controller_spec.rb b/spec/controllers/original_pictures_controller_spec.rb index b60ecdf7..b13ab7b6 100644 --- a/spec/controllers/original_pictures_controller_spec.rb +++ b/spec/controllers/original_pictures_controller_spec.rb @@ -1,156 +1,780 @@ +# -*- encoding: utf-8 -*- require 'spec_helper' -# This spec was generated by rspec-rails when you ran the scaffold generator. -# It demonstrates how one might use RSpec to specify the controller code that -# was generated by Rails when you ran the scaffold generator. -# -# It assumes that the implementation code is generated by the rails scaffold -# generator. If you are using any extension libraries to generate different -# controller code, this generated spec may or may not pass. -# -# It only uses APIs available in rails and/or rspec-rails. There are a number -# of tools you can use to make these specs even more expressive, but we're -# sticking to rails and rspec-rails APIs to keep things simple and stable. -# -# Compared to earlier versions of this generator, there is very limited use of -# stubs and message expectations in this spec. Stubs are only used when there -# is no simpler way to get a handle on the object needed for the example. -# Message expectations are only used when there is no simpler way to specify -# that an instance is receiving a specific message. - describe OriginalPicturesController do - - # This should return the minimal set of attributes required to create a valid - # OriginalPicture. As you add validations to OriginalPicture, be sure to - # update the return value of this method accordingly. - def valid_attributes - {} + before do + Factory :admin + @user = Factory( :user_yas) + @author = @user.author + @artist = Factory :artist_yas, :author_id => @author.id end - describe "GET index" do - it "assigns all original_pictures as @original_pictures" do - original_picture = OriginalPicture.create! valid_attributes - get :index - assigns(:original_pictures).should eq([original_picture]) + describe '一覧表示に於いて' do + before do + @op = Factory :original_picture, :artist_id => @artist.id + sign_in @user + OriginalPicture.stub(:list).and_return([@op, @op, @op]) end - end - - describe "GET show" do - it "assigns the requested original_picture as @original_picture" do - original_picture = OriginalPicture.create! valid_attributes - get :show, :id => original_picture.id - assigns(:original_picture).should eq(original_picture) + context 'パラメータpageについて' 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 + end + context 'つつがなく終わるとき' do + it 'ステータスコード200 OKを返す' do + get :index + response.should be_success + end + it '原画モデルに一覧を問い合わせている' do + OriginalPicture.should_receive(:list).exactly(1) + get :index + end + it '@original_picturesにリストを取得している' do + get :index + assigns(:original_pictures).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 + 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?("ext").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 + context '作家が絵師でないとき' do + before do + Author.any_instance.stub(:artist?).and_return(false) + end + context 'html形式' do + it 'ステータスコード302 Foundを返す' do + get :index + response.status.should eq 302 + end + it '絵師登録ページへ遷移する' do + get :index + response.should redirect_to new_artist_path + end + end + context 'json形式' do + it '例外403 forbiddenを返す' do + lambda{ + get :index, :format => :json + }.should raise_error(ActiveRecord::Forbidden) + end + end end end - - describe "GET new" do - it "assigns a new original_picture as @original_picture" do - get :new - assigns(:original_picture).should be_a_new(OriginalPicture) + + describe '単体表示に於いて' do + before do + @pic = Factory :original_picture, :artist_id => @artist.id + sign_in @user + OriginalPicture.stub(:show).and_return(@pic) + end + context 'つつがなく終わるとき' do + it 'ステータスコード200 OKを返す' do + get :show, :id => @pic.id + response.should be_success + end + it '原画モデルに単体取得を問い合わせている' do + OriginalPicture.should_receive(:show).exactly(1) + get :show + end + it '@original_pictureにアレを取得している' do + get :show, :id => @pic.id + assigns(:original_picture).id.should eq(@pic.id) + end + context 'html形式' do + it 'showテンプレートを描画する' do + get :show, :id => @pic.id + response.should render_template("show") + end + end + context 'json形式' do + it 'jsonデータを返す' do + get :show, :id => @pic.id, :format => :json + lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError) + end + it 'データがアレになっている' do + get :show, :id => @pic.id, :format => :json + json = JSON.parse response.body + json["ext"].should match(/png/) + end + end + #画像送信では、send_dataにスタブをおいてテストしたいが、ここに噛ませると + #renderが働かず、エラーとなってしまう。そこで、原画のファイル取得部分に + #スタブをおいてsend_dataがデータを返す体裁でテストする。 + context 'png形式' do + it '画像モデルに画像データを問い合わせる' do + OriginalPicture.any_instance.should_receive(:restore).exactly(1) + get :show, :id => @pic.id, :format => :png + end + it '画像を送信する' do + OriginalPicture.any_instance.stub(:restore).and_return('aaa') + get :show, :id => @pic.id, :format => :png + response.body.should eq 'aaa' + end + end + context 'gif形式' do + it '画像モデルに画像データを問い合わせる' do + OriginalPicture.any_instance.should_receive(:restore).exactly(1) + get :show, :id => @pic.id, :format => :gif + end + it '画像を送信する' do + OriginalPicture.any_instance.stub(:restore).and_return('bbb') + get :show, :id => @pic.id, :format => :gif + response.body.should eq 'bbb' + end + end + context 'jpeg形式' do + it '画像モデルに画像データを問い合わせる' do + OriginalPicture.any_instance.should_receive(:restore).exactly(1) + get :show, :id => @pic.id, :format => :jpeg + end + it '画像を送信する' do + OriginalPicture.any_instance.stub(:restore).and_return('ccc') + get :show, :id => @pic.id, :format => :jpeg + response.body.should eq 'ccc' + end + end + end + context '作家権限がないとき' do + before do + sign_out @user + end + context 'html形式' do + it 'ステータスコード302 Foundを返す' do + get :show, :id => @pic.id + response.status.should eq 302 + end + it 'サインインページへ遷移する' do + get :show, :id => @pic.id + response.body.should redirect_to '/users/sign_in' + end + end + context 'json形式' do + it 'ステータスコード401 Unauthorizedを返す' do + get :show, :id => @pic.id, :format => :json + response.status.should eq 401 + end + it '応答メッセージにUnauthorizedを返す' do + get :show, :id => @pic.id, :format => :json + response.message.should match(/Unauthorized/) + end + end + end + context '作家が絵師でないとき' do + before do + Author.any_instance.stub(:artist?).and_return(false) + end + context 'html形式' do + it 'ステータスコード302 Foundを返す' do + get :show, :id => @pic.id + response.status.should eq 302 + end + it '絵師登録ページへ遷移する' do + get :show, :id => @pic.id + response.should redirect_to new_artist_path + end + end + context 'json形式' do + it '例外403 forbiddenを返す' do + lambda{ + get :show, :id => @pic.id, :format => :json + }.should raise_error(ActiveRecord::Forbidden) + end + end + end + context '対象原画がないとき' do + before do + OriginalPicture.unstub(:show) + end + 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 + before do + OriginalPicture.stub(:show).and_return(@pic) + OriginalPicture.any_instance.stub(:own?).with(any_args()).and_return(false) + end + context 'html形式' do + it '例外403 forbiddenを返す' do + lambda{ + get :show, :id => @pic.id + }.should raise_error(ActiveRecord::Forbidden) + end + end + context 'json形式' do + it '例外403 forbiddenを返す' do + lambda{ + get :show, :id => @pic.id, :format => :json + }.should raise_error(ActiveRecord::Forbidden) + end + end end end - describe "GET edit" do - it "assigns the requested original_picture as @original_picture" do - original_picture = OriginalPicture.create! valid_attributes - get :edit, :id => original_picture.id - assigns(:original_picture).should eq(original_picture) + describe '新規作成フォーム表示に於いて' do + before do + sign_in @user + end + context 'つつがなく終わるとき' do + it 'ステータスコード200 OKを返す' do + get :new + response.should be_success + end + it '@original_pictureに新規データを用意している' do + get :new + assigns(:original_picture).should be_a_new(OriginalPicture) + end + it '原画モデルにデフォルト値補充を依頼している' do + OriginalPicture.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 + context '作家が絵師でないとき' do + before do + Author.any_instance.stub(:artist?).and_return(false) + end + context 'html形式' do + it 'ステータスコード302 Foundを返す' do + get :new + response.status.should eq 302 + end + it '絵師登録ページへ遷移する' do + get :new + response.should redirect_to new_artist_path + end + end + context 'js形式' do + it 'ステータスコード200 Okを返す' do + get :new, :format => :js + response.status.should eq 200 + end + it '絵師登録部分テンプレートartists/new.jsを描画する' do + get :new, :format => :js + response.should render_template("artists/new") + end + end end end - describe "POST create" do - describe "with valid params" do - it "creates a new OriginalPicture" do - expect { - post :create, :original_picture => valid_attributes - }.to change(OriginalPicture, :count).by(1) + describe '新規作成に於いて' do + before do + sign_in @user + end + context 'つつがなく終わるとき' do + it 'モデルに保存依頼する' do + OriginalPicture.any_instance.should_receive(:save).exactly(1) + post :create, :original_picture => Factory.attributes_for(:original_picture) end - - it "assigns a newly created original_picture as @original_picture" do - post :create, :original_picture => valid_attributes + it "@original_pictureに作成された原画を保持していて、それがDBにある" do + post :create, :original_picture => Factory.attributes_for(:original_picture) assigns(:original_picture).should be_a(OriginalPicture) assigns(:original_picture).should be_persisted end - - it "redirects to the created original_picture" do - post :create, :original_picture => valid_attributes - response.should redirect_to(OriginalPicture.last) + context 'html形式' do + it 'ステータスコード302 Foundを返す' do + OriginalPicture.any_instance.stub(:save).with(any_args()).and_return(true) + OriginalPicture.any_instance.stub(:store).with(any_args()).and_return(true) + post :create, :original_picture => Factory.attributes_for(:original_picture) + response.status.should eq 302 + end + it '作成された原画の表示ページへ遷移する' do + OriginalPicture.any_instance.stub(:save).with(any_args()).and_return(true) + OriginalPicture.any_instance.stub(:store).with(any_args()).and_return(true) + post :create, :original_picture => Factory.attributes_for(:original_picture) + response.should redirect_to(OriginalPicture.last) + end + end + context 'json形式' do + it 'ステータスコード200 OKを返す' do + OriginalPicture.any_instance.stub(:save).with(any_args()).and_return(true) + OriginalPicture.any_instance.stub(:store).with(any_args()).and_return(true) + post :create, :original_picture => Factory.attributes_for(:original_picture), :format => :json + response.should be_success + end + it '作成された原画をjsonデータで返す' do + OriginalPicture.any_instance.stub(:save).with(any_args()).and_return(true) + OriginalPicture.any_instance.stub(:store).with(any_args()).and_return(true) + post :create, :original_picture => Factory.attributes_for(:original_picture), :format => :json + lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError) + end + it 'データがアレになっている' do + OriginalPicture.any_instance.stub(:save).with(any_args()).and_return(true) + OriginalPicture.any_instance.stub(:store).with(any_args()).and_return(true) + post :create, :original_picture => Factory.attributes_for(:original_picture), :format => :json + json = JSON.parse response.body + json["ext"].should match(/png/) + end end end - - describe "with invalid params" do - it "assigns a newly created but unsaved original_picture as @original_picture" do - # Trigger the behavior that occurs when invalid params are submitted + context '作家権限がないとき' do + before do + sign_out @user + end + context 'html形式' do + it 'ステータスコード302 Foundを返す' do + post :create, :original_picture => Factory.attributes_for(:original_picture) + response.status.should eq 302 + end + it 'サインインページへ遷移する' do + post :create, :original_picture => Factory.attributes_for(:original_picture) + response.body.should redirect_to '/users/sign_in' + end + end + context 'json形式' do + it 'ステータスコード401 Unauthorizedを返す' do + post :create, :original_picture => Factory.attributes_for(:original_picture), :format => :json + response.status.should eq 401 + end + it '応答メッセージにUnauthorizedを返す' do + post :create, :original_picture => Factory.attributes_for(:original_picture), :format => :json + response.message.should match(/Unauthorized/) + end + end + end + context '作家が絵師でないとき' do + before do + Author.any_instance.stub(:artist?).and_return(false) + end + context 'html形式' do + it 'ステータスコード302 Foundを返す' do + post :create, :original_picture => Factory.attributes_for(:original_picture) + response.status.should eq 302 + end + it '絵師登録ページへ遷移する' do + post :create, :original_picture => Factory.attributes_for(:original_picture) + response.should redirect_to new_artist_path + end + end + context 'json形式' do + it '例外403 forbiddenを返す' do + lambda{ + post :create, :original_picture => Factory.attributes_for(:original_picture), :format => :json + }.should raise_error(ActiveRecord::Forbidden) + end + end + end + context '検証、保存に失敗した' do + before do OriginalPicture.any_instance.stub(:save).and_return(false) - post :create, :original_picture => {} + end + it "未保存のコミックを保持している" do + post :create, :original_picture => Factory.attributes_for(:original_picture) assigns(:original_picture).should be_a_new(OriginalPicture) end - - it "re-renders the 'new' template" do - # Trigger the behavior that occurs when invalid params are submitted - OriginalPicture.any_instance.stub(:save).and_return(false) - post :create, :original_picture => {} - response.should render_template("new") + context 'html形式' do + it 'ステータスコード200 OKを返す' do + post :create, :original_picture => Factory.attributes_for(:original_picture) + response.status.should eq 200 + end + it '新規ページを描画する' do + post :create, :original_picture => Factory.attributes_for(:original_picture) + response.should render_template("new") + end + end + context 'json形式' do + it 'ステータスコード422 unprocessable_entity を返す' do + post :create, :original_picture => Factory.attributes_for(:original_picture), :format => :json + response.status.should eq 422 + end + it '応答メッセージUnprocessable Entityを返す' do + post :create, :original_picture => Factory.attributes_for(:original_picture), :format => :json + response.message.should match(/Unprocessable/) + end end end end - describe "PUT update" do - describe "with valid params" do - it "updates the requested original_picture" do - original_picture = OriginalPicture.create! valid_attributes - # Assuming there are no other original_pictures in the database, this - # specifies that the OriginalPicture created on the previous line - # receives the :update_attributes message with whatever params are - # submitted in the request. - OriginalPicture.any_instance.should_receive(:update_attributes).with({'these' => 'params'}) - put :update, :id => original_picture.id, :original_picture => {'these' => 'params'} + describe '編集フォーム表示に於いて' do + before do + @pic = Factory :original_picture, :artist_id => @artist.id + sign_in @user + OriginalPicture.stub(:show).and_return(@pic) + end + context 'つつがなく終わるとき' do + it 'ステータスコード200 OKを返す' do + get :edit, :id => @pic.id + response.should be_success end - - it "assigns the requested original_picture as @original_picture" do - original_picture = OriginalPicture.create! valid_attributes - put :update, :id => original_picture.id, :original_picture => valid_attributes - assigns(:original_picture).should eq(original_picture) + it '原画モデルに単体取得を問い合わせている' do + OriginalPicture.should_receive(:show).exactly(1) + get :show end - - it "redirects to the original_picture" do - original_picture = OriginalPicture.create! valid_attributes - put :update, :id => original_picture.id, :original_picture => valid_attributes - response.should redirect_to(original_picture) + it '@original_pictureにデータを用意している' do + get :edit, :id => @pic.id + assigns(:original_picture).should eq @pic + end + context 'html形式' do + it 'ページテンプレートeditを描画する' do + get :edit, :id => @pic.id + response.should render_template("edit") + end + end + context 'js形式' do + it '部分テンプレートedit.jsを描画する' do + get :edit, :id => @pic.id, :format => :js + response.should render_template("edit") + end end end - - describe "with invalid params" do - it "assigns the original_picture as @original_picture" do - original_picture = OriginalPicture.create! valid_attributes - # Trigger the behavior that occurs when invalid params are submitted - OriginalPicture.any_instance.stub(:save).and_return(false) - put :update, :id => original_picture.id, :original_picture => {} - assigns(:original_picture).should eq(original_picture) + context '作家権限がないとき' do + before do + sign_out @user end - - it "re-renders the 'edit' template" do - original_picture = OriginalPicture.create! valid_attributes - # Trigger the behavior that occurs when invalid params are submitted - OriginalPicture.any_instance.stub(:save).and_return(false) - put :update, :id => original_picture.id, :original_picture => {} - response.should render_template("edit") + context 'html形式' do + it 'ステータスコード302 Foundを返す' do + get :edit, :id => @pic.id + response.status.should eq 302 + end + it 'サインインページへ遷移する' do + get :edit, :id => @pic.id + response.body.should redirect_to '/users/sign_in' + end + end + context 'js形式' do + it 'ステータスコード401 Unauthorizedを返す' do + get :edit, :id => @pic.id, :format => :js + response.status.should eq 401 + end + it '応答メッセージにUnauthorizedを返す' do + get :edit, :id => @pic.id, :format => :js + response.message.should match(/Unauthorized/) + end + end + end + context '作家が絵師でないとき' do + before do + Author.any_instance.stub(:artist?).and_return(false) + end + context 'html形式' do + it 'ステータスコード302 Foundを返す' do + get :edit, :id => @pic.id + response.status.should eq 302 + end + it '絵師登録ページへ遷移する' do + get :edit, :id => @pic.id + response.should redirect_to new_artist_path + end + end + context 'js形式' do + it 'ステータスコード200 Okを返す' do + get :edit, :id => @pic.id, :format => :js + response.status.should eq 200 + end + it '絵師登録部分テンプレートartists/new.jsを描画する' do + get :edit, :id => @pic.id, :format => :js + response.should render_template("artists/new") + end + end + end + context '対象コミックがないとき' do + before do + OriginalPicture.unstub(:show) + end + context 'html形式' do + it '例外404 not_foundを返す' do + lambda{ + get :edit, :id => 0 + }.should raise_error(ActiveRecord::RecordNotFound) + end + end + context 'js形式' do + it '例外404 not_foundを返す' do + lambda{ + get :edit, :id => 0, :format => :js + }.should raise_error(ActiveRecord::RecordNotFound) + end + end + end + context '他人の原画を見ようとしたとき' do + before do + OriginalPicture.any_instance.stub(:own?).with(any_args()).and_return(false) + end + context 'html形式' do + it '例外403 forbiddenを返す' do + lambda{ + get :edit, :id => @pic.id + }.should raise_error(ActiveRecord::Forbidden) + end + end + context 'js形式' do + it '例外403 forbiddenを返す' do + lambda{ + get :edit, :id => @pic.id, :format => :js + }.should raise_error(ActiveRecord::Forbidden) + end end end end - describe "DELETE destroy" do - it "destroys the requested original_picture" do - original_picture = OriginalPicture.create! valid_attributes - expect { - delete :destroy, :id => original_picture.id - }.to change(OriginalPicture, :count).by(-1) + describe '更新に於いて' do + before do + @pic = Factory :original_picture, :artist_id => @artist.id + sign_in @user end - - it "redirects to the original_pictures list" do - original_picture = OriginalPicture.create! valid_attributes - delete :destroy, :id => original_picture.id - response.should redirect_to(original_pictures_url) + context '事前チェックしておく' do + it '原画モデルに単体取得を問い合わせている' do + OriginalPicture.should_receive(:show).exactly(1) + put :update, :id => @pic.id, :original_picture => Factory.attributes_for(:original_picture) + end + it 'モデルに更新を依頼する' do + OriginalPicture.any_instance.stub(:own?).with(any_args()).and_return(true) + OriginalPicture.any_instance.stub(:store).with(any_args()).and_return(true) + OriginalPicture.any_instance.should_receive(:save).exactly(1) + put :update, :id => @pic.id, :original_picture => Factory.attributes_for(:original_picture) + end + it '@original_pictureにアレを取得している' do + OriginalPicture.any_instance.stub(:own?).with(any_args()).and_return(true) + OriginalPicture.any_instance.stub(:save).with(any_args()).and_return(true) + OriginalPicture.any_instance.stub(:store).with(any_args()).and_return(true) + put :update, :id => @pic.id, :original_picture => Factory.attributes_for(:original_picture) + assigns(:original_picture).id.should eq(@pic.id) + end + end + context 'つつがなく終わるとき' do + it '更新される' do + OriginalPicture.any_instance.stub(:own?).with(any_args()).and_return(true) + OriginalPicture.any_instance.stub(:store).with(any_args()).and_return(true) + put :update, :id => @pic.id, :original_picture => Factory.attributes_for(:original_picture, :ext => 'jpeg') + OriginalPicture.find(@pic.id).ext.should eq 'jpeg' + end + context 'html形式' do + it 'ステータスコード302 Foundを返す' do + OriginalPicture.any_instance.stub(:own?).with(any_args()).and_return(true) + OriginalPicture.any_instance.stub(:save).with(any_args()).and_return(true) + OriginalPicture.any_instance.stub(:store).with(any_args()).and_return(true) + put :update, :id => @pic.id, :original_picture => Factory.attributes_for(:original_picture) + response.status.should eq 302 + end + it '更新された原画の表示ページへ遷移する' do + OriginalPicture.any_instance.stub(:own?).with(any_args()).and_return(true) + OriginalPicture.any_instance.stub(:save).with(any_args()).and_return(true) + OriginalPicture.any_instance.stub(:store).with(any_args()).and_return(true) + put :update, :id => @pic.id, :original_picture => Factory.attributes_for(:original_picture) + response.should redirect_to(@pic) + end + end + context 'json形式' do + it 'ステータスコード200 OKを返す' do + OriginalPicture.any_instance.stub(:own?).with(any_args()).and_return(true) + OriginalPicture.any_instance.stub(:save).with(any_args()).and_return(true) + OriginalPicture.any_instance.stub(:store).with(any_args()).and_return(true) + put :update, :id => @pic.id, :original_picture => Factory.attributes_for(:original_picture), :format => :json + response.should be_success + end + it 'ページ本体は特に返さない' do + OriginalPicture.any_instance.stub(:own?).with(any_args()).and_return(true) + OriginalPicture.any_instance.stub(:save).with(any_args()).and_return(true) + OriginalPicture.any_instance.stub(:store).with(any_args()).and_return(true) + put :update, :id => @pic.id, :original_picture => Factory.attributes_for(:original_picture), :format => :json + response.body.should match /./ + end + end + end + context '作家権限がないとき' do + before do + sign_out @user + end + context 'html形式' do + it 'ステータスコード302 Foundを返す' do + post :create, :original_picture => Factory.attributes_for(:original_picture) + response.status.should eq 302 + end + it 'サインインページへ遷移する' do + post :create, :original_picture => Factory.attributes_for(:original_picture) + response.body.should redirect_to '/users/sign_in' + end + end + context 'json形式' do + it 'ステータスコード401 Unauthorizedを返す' do + post :create, :original_picture => Factory.attributes_for(:original_picture), :format => :json + response.status.should eq 401 + end + it '応答メッセージにUnauthorizedを返す' do + post :create, :original_picture => Factory.attributes_for(:original_picture), :format => :json + response.message.should match(/Unauthorized/) + end + end + end + context '作家が絵師でないとき' do + before do + Author.any_instance.stub(:artist?).and_return(false) + end + context 'html形式' do + it 'ステータスコード302 Foundを返す' do + post :create, :original_picture => Factory.attributes_for(:original_picture) + response.status.should eq 302 + end + it '絵師登録ページへ遷移する' do + post :create, :original_picture => Factory.attributes_for(:original_picture) + response.should redirect_to new_artist_path + end + end + context 'json形式' do + it '例外403 forbiddenを返す' do + lambda{ + post :create, :original_picture => Factory.attributes_for(:original_picture), :format => :json + }.should raise_error(ActiveRecord::Forbidden) + end + end + end + context '対象原画がないとき' do + before do + end + context 'html形式' do + it '例外404 not_foundを返す' do + lambda{ + put :update, :id => 0, :original_picture => Factory.attributes_for(:original_picture) + }.should raise_error(ActiveRecord::RecordNotFound) + end + end + context 'json形式' do + it '例外404 not_foundを返す' do + lambda{ + put :update, :id => 0, :original_picture => Factory.attributes_for(:original_picture), :format => :json + }.should raise_error(ActiveRecord::RecordNotFound) + end + end + end + context '他人の原画を見ようとしたとき' do + before do + OriginalPicture.any_instance.stub(:own?).with(any_args()).and_return(false) + end + context 'html形式' do + it '例外403 forbiddenを返す' do + lambda{ + put :update, :id => @pic.id, :original_picture => Factory.attributes_for(:original_picture) + }.should raise_error(ActiveRecord::Forbidden) + end + end + context 'json形式' do + it '例外403 forbiddenを返す' do + lambda{ + put :update, :id => @pic.id, :original_picture => Factory.attributes_for(:original_picture), :format => :json + }.should raise_error(ActiveRecord::Forbidden) + end + end + end + context '検証、保存に失敗した' do + before do + OriginalPicture.any_instance.stub(:save).and_return(false) + end + context 'html形式' do + it 'ステータスコード200 Okを返す' do + put :update, :id => @pic.id, :original_picture => Factory.attributes_for(:original_picture) + response.status.should eq 200 + end + it '編集ページを描画する' do + put :update, :id => @pic.id, :original_picture => Factory.attributes_for(:original_picture) + response.should render_template("edit") + end + end + context 'json形式' do + it 'ステータスコード422 unprocessable_entity を返す' do + put :update, :id => @pic.id, :original_picture => Factory.attributes_for(:original_picture), :format => :json + response.status.should eq 422 + end + it '応答メッセージUnprocessable Entityを返す' do + put :update, :id => @pic.id, :original_picture => Factory.attributes_for(:original_picture), :format => :json + response.message.should match(/Unprocessable/) + end + end end end diff --git a/spec/factories.rb b/spec/factories.rb index f8c1941c..bd61d1f7 100644 --- a/spec/factories.rb +++ b/spec/factories.rb @@ -60,7 +60,7 @@ Factory.define :original_picture, :class => OriginalPicture do |op| op.ext 'png' op.width 222 op.height 111 - op.fileseze 100000 + op.filesize 100000 op.artist_id 1 op.license_id 1 end -- 2.11.0