require 'spec_helper'
describe BalloonsController do
+ before do
+ @admin = FactoryGirl.create :admin
+ @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
+
+ @speech_balloon_template = FactoryGirl.create :speech_balloon_template
+ @panel = FactoryGirl.create :panel, :author_id => @author.id
+ end
+
+ describe '一覧表示に於いて' do
+ before do
+ @sb = FactoryGirl.create :speech_balloon, :panel_id => @panel.id, :speech_balloon_template_id => @speech_balloon_template.id
+ @balloon = FactoryGirl.create :balloon, :speech_balloon_id => @sb.id, :system_picture_id => @sp.id
+ @speech = FactoryGirl.create :speech, :speech_balloon_id => @sb.id
+ sign_in @user
+ Balloon.stub(:list).and_return([@balloon, @balloon, @balloon])
+ 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 Balloon.default_page_size
+ end
+ it '最大を超えると@page_sizeにデフォルト最大値が入る' do
+ get :index, :page_size => 1500
+ assigns(:page_size).should eq Balloon.max_page_size
+ end
+ it '不正な値が入ると@page_sizeにデフォルト最大値が入る' do
+ get :index, :page_size => 0
+ assigns(:page_size).should eq Balloon.default_page_size
+ end
+ end
+ context 'つつがなく終わるとき' do
+ it 'ステータスコード200 OKを返す' do
+ get :index
+ response.should be_success
+ end
+ it 'フキダシ枠モデルに一覧を問い合わせている' do
+ Balloon.should_receive(:list).exactly(1)
+ get :index
+ end
+ it '@balloonsにリストを取得している' do
+ get :index
+ assigns(:balloons).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 'フキダシ枠モデルにjson一覧出力オプションを問い合わせている' do
+ Balloon.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?("speech_balloon_id").should be_true
+ json.first.has_key?("x").should be_true
+ json.first.has_key?("system_picture_id").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
+ end
+
+ describe '単体表示に於いて' do
+ before do
+ sign_in @user
+ @sb = FactoryGirl.create :speech_balloon, :panel_id => @panel.id, :speech_balloon_template_id => @speech_balloon_template.id
+ @balloon = FactoryGirl.create :balloon, :speech_balloon_id => @sb.id, :system_picture_id => @sp.id
+ @speech = FactoryGirl.create :speech, :speech_balloon_id => @sb.id
+ Balloon.stub(:show).and_return(@balloon)
+ end
+ context 'つつがなく終わるとき' do
+ it 'ステータスコード200 OKを返す' do
+ get :show, :id => @balloon.id
+ response.should be_success
+ end
+ it 'フキダシ枠モデルに単体取得を問い合わせている' do
+ Balloon.should_receive(:show).exactly(1)
+ get :show
+ end
+ it '@balloonにアレを取得している' do
+ get :show, :id => @balloon.id
+ assigns(:balloon).should eq(@balloon)
+ end
+ context 'html形式' do
+ it 'showテンプレートを描画する' do
+ get :show, :id => @balloon.id
+ response.should render_template("show")
+ end
+ end
+ context 'json形式' do
+ it 'jsonデータを返す' do
+ get :show, :id => @balloon.id, :format => :json
+ lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
+ end
+ it 'フキダシ枠モデルにjson単体出力オプションを問い合わせている' do
+ Balloon.should_receive(:show_json_opt).exactly(1)
+ get :show, :id => @balloon.id, :format => :json
+ end
+ it 'データがアレになっている' do
+ get :show, :id => @balloon.id, :format => :json
+ json = JSON.parse response.body
+ json["speech_balloon_id"].should_not be_nil
+ json["x"].should_not be_nil
+ json["system_picture_id"].should_not be_nil
+ end
+ end
+ end
+ context '作家権限がないとき' do
+ before do
+ sign_out @user
+ end
+ context 'html形式' do
+ it 'ステータスコード302 Foundを返す' do
+ get :show, :id => @balloon.id
+ response.status.should eq 302
+ end
+ it 'サインインページへ遷移する' do
+ get :show, :id => @balloon.id
+ response.body.should redirect_to '/users/sign_in'
+ end
+ end
+ context 'json形式' do
+ it 'ステータスコード401 Unauthorizedを返す' do
+ get :show, :id => @balloon.id, :format => :json
+ response.status.should eq 401
+ end
+ it '応答メッセージにUnauthorizedを返す' do
+ get :show, :id => @balloon.id, :format => :json
+ response.message.should match(/Unauthorized/)
+ end
+ end
+ end
+ end
+
end