+ describe '更新に於いて' do
+ before do
+ @op = FactoryGirl.create :original_picture, :artist_id => @artist.id
+ OriginalPicture.stub(:edit).with(@op.id.to_s, @artist).and_return(@op)
+ OriginalPicturesController.any_instance.stub(:set_image).with(any_args()).and_return("abc\ndef\nghi")
+ sign_in @user
+ @attr = {:file => "abc\ndef\nghi"}
+ @imager = ImagerTest.load("abc\ndef\nghi")
+ end
+ context '事前チェックしておく' do
+ before do
+ PettanImager.stub(:load).with("abc\ndef\nghi").and_return(@imager)
+ OriginalPicture.any_instance.stub(:store).with(@imager).and_return(true)
+ end
+ it '画像ライブラリをロードする' do
+ PettanImager.should_receive(:load).with(any_args()).exactly(1)
+ put :update, :id => @op.id, :original_picture => @attr
+ end
+ it '原画モデルに編集取得を問い合わせている' do
+ OriginalPicture.should_receive(:edit).with(@op.id.to_s, @artist).exactly(1)
+ put :update, :id => @op.id, :original_picture => @attr
+ end
+ it '原画モデルに上書き補充を依頼している' do
+ OriginalPicture.any_instance.should_receive(:overwrite).with(@artist).exactly(1)
+ put :update, :id => @op.id, :original_picture => @attr
+ end
+ it 'モデルに更新を依頼する' do
+ OriginalPicture.any_instance.should_receive(:store).with(@imager).exactly(1)
+ put :update, :id => @op.id, :original_picture => @attr
+ end
+ end
+ context 'つつがなく終わるとき' do
+ before do
+ PettanImager.stub(:load).with("abc\ndef\nghi").and_return(@imager)
+ end
+ it '@original_pictureにアレを取得している' do
+ put :update, :id => @op.id, :original_picture => @attr
+ assigns(:original_picture).should eq(@op)
+ end
+ context 'html形式' do
+ it 'ステータスコード302 Foundを返す' do
+ put :update, :id => @op.id, :original_picture => @attr
+ response.status.should eq 302
+ end
+ it '更新された原画の表示ページへ遷移する' do
+ put :update, :id => @op.id, :original_picture => @attr
+ response.should redirect_to(@pic)
+ end
+ end
+ context 'json形式' do
+ it 'ステータスコード200 OKを返す' do
+ put :update, :id => @op.id, :original_picture => @attr, :format => :json
+ response.should be_success
+ end
+ it 'ページ本体は特に返さない' do
+ put :update, :id => @op.id, :original_picture => @attr, :format => :json
+ response.body.should match /./
+ end
+ end
+ end
+ context 'ユーザ権限がないとき' do
+ before do
+ sign_out @user
+ end
+ context 'html形式' do
+ it 'ステータスコード302 Foundを返す' do
+ put :update, :id => @op.id, :original_picture => @attr
+ response.status.should eq 302
+ end
+ it 'サインインページへ遷移する' do
+ put :update, :id => @op.id, :original_picture => @attr
+ response.body.should redirect_to '/users/sign_in'
+ end
+ end
+ context 'json形式' do
+ it 'ステータスコード401 Unauthorizedを返す' do
+ put :update, :id => @op.id, :original_picture => @attr, :format => :json
+ response.status.should eq 401
+ end
+ it '応答メッセージにUnauthorizedを返す' do
+ put :update, :id => @op.id, :original_picture => @attr, :format => :json
+ response.message.should match(/Unauthorized/)
+ end
+ end
+ end
+ context 'ユーザ権限はないが管理者権限があるとき' do
+ before do
+ sign_out @user
+ sign_in @admin
+ end
+ context 'html形式' do
+ it 'ステータスコード302 Foundを返す' do
+ put :update, :id => @op.id, :original_picture => @attr
+ response.status.should eq 302
+ end
+ it 'サインインページへ遷移する' do
+ put :update, :id => @op.id, :original_picture => @attr
+ response.body.should redirect_to '/users/sign_in'
+ end
+ end
+ end
+ context 'ユーザが絵師でないとき' do
+ before do
+ @artist.destroy
+ end
+ context 'html形式' do
+ it 'ステータスコード302 Foundを返す' do
+ put :update, :id => @op.id, :original_picture => @attr
+ response.status.should eq 302
+ end
+ it '絵師登録ページへ遷移する' do
+ put :update, :id => @op.id, :original_picture => @attr
+ response.should redirect_to new_artist_path
+ end
+ end
+ context 'json形式' do
+ it '例外403 forbiddenを返す' do
+ lambda{
+ put :update, :id => @op.id, :original_picture => @attr, :format => :json
+ }.should raise_error(ActiveRecord::Forbidden)
+ end
+ end
+ end
+ context '検証、保存に失敗した' do
+ before do
+ PettanImager.stub(:load).with("abc\ndef\nghi").and_return(@imager)
+ OriginalPicture.any_instance.stub(:store).with(@imager).and_return(false)
+ end
+ context 'html形式' do
+ it 'ステータスコード200 Okを返す' do
+ put :update, :id => @op.id, :original_picture => @attr
+ response.status.should eq 200
+ end
+ it '編集ページを描画する' do
+ put :update, :id => @op.id, :original_picture => @attr
+ response.should render_template("edit")
+ end
+ end
+ context 'json形式' do
+ it 'ステータスコード422 unprocessable_entity を返す' do
+ put :update, :id => @op.id, :original_picture => @attr, :format => :json
+ response.status.should eq 422
+ end
+ it '応答メッセージUnprocessable Entityを返す' do
+ put :update, :id => @op.id, :original_picture => @attr, :format => :json
+ response.message.should match(/Unprocessable/)
+ end
+ end
+ end
+ context 'ファイルが指定されていないとき' do
+ before do
+ @attr = {}
+ PettanImager.stub(:load).and_return(nil)
+ end
+ context 'html形式' do
+ it 'ステータスコード200 Okを返す' do
+ put :update, :id => @op.id, :original_picture => @attr
+ response.status.should eq 200
+ end
+ it '編集ページを描画する' do
+ put :update, :id => @op.id, :original_picture => @attr
+ response.should render_template("edit")
+ end
+ end
+ context 'json形式' do
+ it 'ステータスコード422 unprocessable_entity を返す' do
+ put :update, :id => @op.id, :original_picture => @attr, :format => :json
+ response.status.should eq 422
+ end
+ it '応答メッセージUnprocessable Entityを返す' do
+ put :update, :id => @op.id, :original_picture => @attr, :format => :json
+ response.message.should match(/Unprocessable/)
+ end