+# -*- 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)\r
+ 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)\r
+ 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)\r
+ get :show, :id => @pic.id, :format => :png
+ end
+ it '画像を送信する' do
+ OriginalPicture.any_instance.stub(:restore).and_return('aaa')\r
+ 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)\r
+ get :show, :id => @pic.id, :format => :gif
+ end
+ it '画像を送信する' do
+ OriginalPicture.any_instance.stub(:restore).and_return('bbb')\r
+ 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)\r
+ get :show, :id => @pic.id, :format => :jpeg
+ end
+ it '画像を送信する' do
+ OriginalPicture.any_instance.stub(:restore).and_return('ccc')\r
+ 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)\r
+ 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)\r
+ 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)\r
+ 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