OSDN Git Service

t#28985:add credit
[pettanr/pettanr.git] / spec / controllers / pictures_controller_spec.rb
index d0335da..b98e86e 100644 (file)
@@ -1,5 +1,237 @@
+# -*- encoding: utf-8 -*-
+#実素材
 require 'spec_helper'
 
 describe PicturesController do
+  before do
+    Factory :admin
+    @sp = Factory :system_picture
+    @lg = Factory :license_group
+    @license = Factory :license, :license_group_id => @lg.id, :system_picture_id => @sp.id
+    @user = Factory( :user_yas)
+    @author = @user.author
+    @artist = Factory :artist_yas, :author_id => @author.id
+    @op = Factory :original_picture, :artist_id => @artist.id
+  end
+
+  describe '単体表示に於いて' do
+    before do
+      @p = Factory :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @artist.id
+      sign_in @user
+      Picture.stub(:show).and_return(@p)
+    end
+    context 'つつがなく終わるとき as json' do
+      it 'ステータスコード200 OKを返す' do
+        get :show, :id => @p.id, :format => :json
+        response.should be_success
+      end
+      it '実素材モデルに単体取得を問い合わせている' do
+        Picture.should_receive(:show).exactly(1)
+        get :show, :id => @p.id, :format => :json
+      end
+      it '@pictureにアレを取得している' do
+        get :show, :id => @p.id, :format => :json
+        assigns(:picture).id.should eq(@p.id)
+      end
+      context 'json形式' do
+        it 'jsonデータを返す' do
+          get :show, :id => @p.id, :format => :json
+          lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
+        end
+        it 'データがアレになっている' do
+          get :show, :id => @p.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
+          Picture.any_instance.should_receive(:restore).exactly(1)
+          get :show, :id => @p.id, :format => :png
+        end
+        it '画像を送信する' do
+          Picture.any_instance.stub(:restore).and_return('aaa')
+          get :show, :id => @p.id, :format => :png
+          response.body.should eq 'aaa'
+        end
+      end
+      context 'gif形式' do
+        it '画像モデルに画像データを問い合わせる' do
+          Picture.any_instance.should_receive(:restore).exactly(1)
+          get :show, :id => @p.id, :format => :gif
+        end
+        it '画像を送信する' do
+          Picture.any_instance.stub(:restore).and_return('bbb')
+          get :show, :id => @p.id, :format => :gif
+          response.body.should eq 'bbb'
+        end
+      end
+      context 'jpeg形式' do
+        it '画像モデルに画像データを問い合わせる' do
+          Picture.any_instance.should_receive(:restore).exactly(1)
+          get :show, :id => @p.id, :format => :jpeg
+        end
+        it '画像を送信する' do
+          Picture.any_instance.stub(:restore).and_return('ccc')
+          get :show, :id => @p.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 => @p.id
+          response.status.should eq 302
+        end
+        it 'サインインページへ遷移する' do
+          get :show, :id => @p.id
+          response.body.should redirect_to '/users/sign_in'
+        end
+      end
+      context 'json形式' do
+        it 'ステータスコード401 Unauthorizedを返す' do
+          get :show, :id => @p.id, :format => :json
+          response.status.should eq 401
+        end
+        it '応答メッセージにUnauthorizedを返す' do
+          get :show, :id => @p.id, :format => :json
+          response.message.should match(/Unauthorized/)
+        end
+      end
+    end
+=begin
+    context '対象素材がないとき' do
+      before do
+        Picture.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
+        Picture.stub(:show).and_return(@p)
+        Picture.any_instance.stub(:own?).with(any_args()).and_return(false)
+      end
+      context 'html形式' do
+        it '例外403 forbiddenを返す' do
+          lambda{
+            get :show, :id => @p.id
+          }.should raise_error(ActiveRecord::Forbidden)
+        end
+      end
+      context 'json形式' do
+        it '例外403 forbiddenを返す' do
+          lambda{
+            get :show, :id => @p.id, :format => :json
+          }.should raise_error(ActiveRecord::Forbidden)
+        end
+      end
+    end
+=end
+  end
+
+  describe 'クレジット表示に於いて' do\r
+    before do\r
+      @p = Factory :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @artist.id
+      sign_in @user
+      Picture.stub(:show).and_return(@p)
+    end\r
+    context 'つつがなく終わるとき' do\r
+      it 'ステータスコード200 OKを返す' do\r
+        get :credit, :id => @p.id\r
+        response.should be_success\r
+      end\r
+      it '実素材モデルに単体取得を問い合わせている' do\r
+        Picture.should_receive(:show).exactly(1)\r
+        get :credit, :id => @p.id\r
+      end\r
+      it '@pictureにアレを取得している' do\r
+        get :credit, :id => @p.id\r
+        assigns(:picture).id.should eq(@p.id)\r
+      end\r
+      context 'html形式' do\r
+        it 'creditテンプレートを描画する' do\r
+          get :credit, :id => @p.id\r
+          response.should render_template("credit")\r
+        end\r
+      end\r
+      context 'json形式' do\r
+        it 'jsonデータを返す' do\r
+          get :credit, :id => @p.id, :format => :json\r
+          lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)\r
+        end\r
+        it 'データがアレになっている' do\r
+          get :credit, :id => @p.id, :format => :json\r
+          json = JSON.parse response.body\r
+          json["ext"].should match(/png/)\r
+        end\r
+      end\r
+    end\r
+    context '作家権限がないとき' do\r
+      before do\r
+        sign_out @user\r
+      end\r
+      context 'html形式' do\r
+        it 'ステータスコード302 Foundを返す' do\r
+          get :credit, :id => @p.id\r
+          response.status.should eq 302\r
+        end\r
+        it 'サインインページへ遷移する' do\r
+          get :credit, :id => @p.id\r
+          response.body.should redirect_to '/users/sign_in'\r
+        end\r
+      end\r
+      context 'json形式' do\r
+        it 'ステータスコード401 Unauthorizedを返す' do\r
+          get :credit, :id => @p.id, :format => :json\r
+          response.status.should eq 401\r
+        end\r
+        it '応答メッセージにUnauthorizedを返す' do\r
+          get :credit, :id => @p.id, :format => :json\r
+          response.message.should match(/Unauthorized/)\r
+        end\r
+      end\r
+    end\r
+=begin\r
+    context '対象素材がないとき' do\r
+      before do\r
+        Picture.unstub(:show)\r
+      end\r
+      context 'html形式' do\r
+        it '例外404 not_foundを返す' do\r
+          lambda{\r
+            get :show, :id => 0\r
+          }.should raise_error(ActiveRecord::RecordNotFound)\r
+        end\r
+      end\r
+      context 'json形式' do\r
+        it '例外404 not_foundを返す' do\r
+          lambda{ \r
+            get :show, :id => 0, :format => :json\r
+          }.should raise_error(ActiveRecord::RecordNotFound)\r
+        end\r
+      end\r
+    end\r
+=end\r
+  end\r
 
 end