require 'spec_helper'
describe SystemPicturesController do
- describe "GET index" do
- it "assigns all system_pictures as @system_pictures" do
- system_picture = SystemPicture.create! valid_attributes
- get :index
- assigns(:system_pictures).should eq([system_picture])
+ before do
+ SpeechBalloonTemplate.delete_all
+ @admin = FactoryGirl.create :admin
+ @demand_user = FactoryGirl.create :demand_user
+ @user = FactoryGirl.create( :user_yas)
+ @author = FactoryGirl.create :author, :user_id => @user.id
+ @artist = FactoryGirl.create :artist_yas, :author_id => @author.id
+ @sp = FactoryGirl.create :system_picture
+ @lg = FactoryGirl.create :license_group
+ @license = FactoryGirl.create :license, :license_group_id => @lg.id, :system_picture_id => @sp.id
+ end
+
+if MagicNumber['run_mode'] == 1
+ describe '一覧表示に於いて' do
+ before do
+ sign_in @user
+ SystemPicture.stub(:list).and_return([@sp, @sp, @sp])
+ end
+ 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
+ it '与えられたpage_sizeがセットされている' do
+ get :index, :page_size => 15
+ assigns(:page_size).should eq 15
+ end
+ it '省略されると@page_sizeにデフォルト値が入る' do
+ get :index
+ assigns(:page_size).should eq SystemPicture.default_page_size
+ end
+ it '最大を超えると@page_sizeにデフォルト最大値が入る' do
+ get :index, :page_size => 1500
+ assigns(:page_size).should eq SystemPicture.max_page_size
+ end
+ it '不正な値が入ると@page_sizeにデフォルト最大値が入る' do
+ get :index, :page_size => 0
+ assigns(:page_size).should eq SystemPicture.default_page_size
+ end
+ end
+ context 'つつがなく終わるとき' do
+ it 'システム画像モデルに一覧を問い合わせている' do
+ SystemPicture.should_receive(:list).exactly(1)
+ get :index
+ end
+ it '@system_picturesにリストを取得している' do
+ get :index
+ assigns(:system_pictures).should have_at_least(3).items
+ end
+ context 'html形式' do
+ it 'ステータスコード200 OKを返す' do
+ get :index
+ response.should be_success
+ end
+ it 'indexテンプレートを描画する' do
+ get :index
+ response.should render_template("index")
+ end
+ end
+ context 'json形式' do
+ it 'ステータスコード200 OKを返す' do
+ get :index, :format => :json
+ response.should be_success
+ end
+ it 'jsonデータを返す' do
+ get :index, :format => :json
+ lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
+ end
+ it 'システム画像モデルにjson一覧出力オプションを問い合わせている' do
+ SystemPicture.should_receive(:list_json_opt).exactly(1)
+ get :index, :format => :json
+ 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
+ json.first.has_key?("md5").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
+ sign_out @user
+ sign_in @admin
+ end
+ it 'ステータスコード200 OKを返す' do
+ get :index
+ response.should be_success
+ end
+ end
+ context 'ユーザ権限はないが借手権限があるとき' do
+ before do
+ sign_out @user
+ sign_in @demand_user
+ end
+ it 'ステータスコード200 OKを返す' do
+ get :index
+ response.should be_success
+ end
end
end
+
+ describe '単体表示に於いて' do
+ before do
+ sign_in @user
+ SystemPicture.stub(:show).and_return(@sp)
+ end
+ context 'つつがなく終わるとき' do
+ it 'ステータスコード200 OKを返す' do
+ get :show, :id => @sp.id
+ response.should be_success
+ end
+ it 'システム画像モデルに単体取得を問い合わせている' do
+ SystemPicture.should_receive(:show).exactly(1)
+ get :show
+ end
+ it '@system_pictureにアレを取得している' do
+ get :show, :id => @sp.id
+ assigns(:system_picture).should eq @sp
+ end
+ context 'html形式' do
+ it 'showテンプレートを描画する' do
+ get :show, :id => @sp.id
+ response.should render_template("show")
+ end
+ end
+ context 'json形式' do
+ it 'jsonデータを返す' do
+ get :show, :id => @sp.id, :format => :json
+ lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
+ end
+ it 'システム画像モデルにjson単体出力オプションを問い合わせている' do
+ SystemPicture.should_receive(:show_json_opt).exactly(1)
+ get :show, :id => @sp.id, :format => :json
+ end
+ it 'データがアレになっている' do
+ get :show, :id => @sp.id, :format => :json
+ json = JSON.parse response.body
+ json["ext"].should match(/png/)
+ json["md5"].should be_true
+ json["width"].should be_true
+ end
+ end
+ #画像送信では、send_dataにスタブをおいてテストしたいが、ここに噛ませると
+ #renderが働かず、エラーとなってしまう。そこで、システム画像のファイル取得部分に
+ #スタブをおいてsend_dataがデータを返す体裁でテストする。
+ context 'png形式' do
+ before do
+ SystemPicture.any_instance.stub(:mime_type).and_return('image/png')
+ SystemPicture.any_instance.stub(:restore).and_return('aaa')
+ end
+ it '画像モデルに画像データを問い合わせる' do
+ SystemPicture.any_instance.should_receive(:restore).exactly(1)
+ get :show, :id => @sp.id, :format => :png
+ end
+ it '画像モデルにMimeTypeを問い合わせる' do
+ SystemPicture.any_instance.should_receive(:mime_type).exactly(1)
+ get :show, :id => @sp.id, :format => :png
+ end
+ it '画像を送信する' do
+ get :show, :id => @sp.id, :format => :png
+ response.body.should eq 'aaa'
+ end
+ end
+ context 'gif形式' do
+ before do
+ SystemPicture.any_instance.stub(:mime_type).and_return('image/gif')
+ SystemPicture.any_instance.stub(:restore).and_return('bbb')
+ end
+ it '画像モデルに画像データを問い合わせる' do
+ SystemPicture.any_instance.should_receive(:restore).exactly(1)
+ get :show, :id => @sp.id, :format => :gif
+ end
+ it '画像モデルにMimeTypeを問い合わせる' do
+ SystemPicture.any_instance.should_receive(:mime_type).exactly(1)
+ get :show, :id => @sp.id, :format => :gif
+ end
+ it '画像を送信する' do
+ get :show, :id => @sp.id, :format => :gif
+ response.body.should eq 'bbb'
+ end
+ end
+ context 'jpeg形式' do
+ before do
+ SystemPicture.any_instance.stub(:mime_type).and_return('image/jpeg')
+ SystemPicture.any_instance.stub(:restore).and_return('ccc')
+ end
+ it '画像モデルに画像データを問い合わせる' do
+ SystemPicture.any_instance.should_receive(:restore).exactly(1)
+ get :show, :id => @sp.id, :format => :jpeg
+ end
+ it '画像モデルにMimeTypeを問い合わせる' do
+ SystemPicture.any_instance.should_receive(:mime_type).exactly(1)
+ get :show, :id => @sp.id, :format => :jpeg
+ end
+ it '画像を送信する' do
+ get :show, :id => @sp.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 => @sp.id
+ response.status.should eq 302
+ end
+ it 'サインインページへ遷移する' do
+ get :show, :id => @sp.id
+ response.body.should redirect_to '/users/sign_in'
+ end
+ end
+ context 'json形式' do
+ it 'ステータスコード401 Unauthorizedを返す' do
+ get :show, :id => @sp.id, :format => :json
+ response.status.should eq 401
+ end
+ it '応答メッセージにUnauthorizedを返す' do
+ get :show, :id => @sp.id, :format => :json
+ response.message.should match(/Unauthorized/)
+ end
+ end
+ end
+ context 'ユーザ権限はないが管理者権限があるとき' do
+ before do
+ sign_out @user
+ sign_in @admin
+ end
+ it 'ステータスコード200 OKを返す' do
+ get :show, :id => @sp.id
+ response.should be_success
+ end
+ end
+ context 'ユーザ権限はないが借手権限があるとき' do
+ before do
+ sign_out @user
+ sign_in @demand_user
+ end
+ it 'ステータスコード200 OKを返す' do
+ get :show, :id => @sp.id
+ response.should be_success
+ end
+ end
+=begin
+ context '対象システム画像がないとき' do
+ before do
+ SystemPicture.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
+ SystemPicture.stub(:show).and_return(@sp)
+ SystemPicture.any_instance.stub(:own?).with(any_args()).and_return(false)
+ end
+ context 'html形式' do
+ it '例外403 forbiddenを返す' do
+ lambda{
+ get :show, :id => @sp.id
+ }.should raise_error(ActiveRecord::Forbidden)
+ end
+ end
+ context 'json形式' do
+ it '例外403 forbiddenを返す' do
+ lambda{
+ get :show, :id => @sp.id, :format => :json
+ }.should raise_error(ActiveRecord::Forbidden)
+ end
+ end
+ end
+=end
+ end
+
+else
+ describe '一覧表示に於いて' do
+ before do
+ sign_in @user
+ sign_in @admin
+ SystemPicture.stub(:list).and_return([@sp, @sp, @sp])
+ end
+ context 'つつがなく終わるとき' do
+ context 'html形式' do
+ it 'ステータスコード200 OKを返す' do
+ get :index
+ response.should be_success
+ end
+ it 'indexテンプレートを描画する' do
+ get :index
+ response.should render_template("index")
+ end
+ end
+ context 'json形式' do
+ it 'ステータスコード200 OKを返す' do
+ get :index, :format => :json
+ response.should be_success
+ end
+ it 'jsonデータを返す' do
+ get :index, :format => :json
+ lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
+ end
+ end
+ end
+ context 'ユーザ権限がないとき' do
+ before do
+ sign_out @user
+ end
+ context 'html形式' do
+ it 'ステータスコード200 OKを返す' do
+ get :index
+ response.should be_success
+ end
+ it 'indexテンプレートを描画する' do
+ get :index
+ response.should render_template("index")
+ end
+ end
+ context 'json形式' do
+ it 'ステータスコード200 OKを返す' do
+ get :index, :format => :json
+ response.should be_success
+ end
+ it 'jsonデータを返す' do
+ get :index, :format => :json
+ lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
+ end
+ end
+ end
+ end
+
+ describe '単体表示に於いて' do
+ before do
+ sign_in @user
+ SystemPicture.stub(:show).and_return(@sp)
+ end
+ context 'つつがなく終わるとき' do
+ it 'ステータスコード200 OKを返す' do
+ get :show, :id => @sp.id
+ response.should be_success
+ end
+ context 'html形式' do
+ it 'showテンプレートを描画する' do
+ get :show, :id => @sp.id
+ response.should render_template("show")
+ end
+ end
+ context 'json形式' do
+ it 'jsonデータを返す' do
+ get :show, :id => @sp.id, :format => :json
+ lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
+ end
+ end
+ end
+ context 'ユーザ権限がないとき' do
+ before do
+ sign_out @user
+ end
+ it 'ステータスコード200 OKを返す' do
+ get :show, :id => @sp.id
+ response.should be_success
+ end
+ context 'html形式' do
+ it 'showテンプレートを描画する' do
+ get :show, :id => @sp.id
+ response.should render_template("show")
+ end
+ end
+ context 'json形式' do
+ it 'jsonデータを返す' do
+ get :show, :id => @sp.id, :format => :json
+ lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
+ end
+ end
+ end
+ end
+end
end