OSDN Git Service

t30329#:i18n flash message
[pettanr/pettanr.git] / spec / controllers / speeches_controller_spec.rb
1 # -*- encoding: utf-8 -*-
2 #セリフ
3 require 'spec_helper'
4
5 describe SpeechesController do
6   before do
7     @admin = FactoryGirl.create :admin
8     @user = FactoryGirl.create( :user_yas)
9     @author = FactoryGirl.create :author, :user_id => @user.id
10     @artist = FactoryGirl.create :artist_yas, :author_id => @author.id
11     @sp = FactoryGirl.create :system_picture
12     @lg = FactoryGirl.create :license_group
13     @license = FactoryGirl.create :license, :license_group_id => @lg.id, :system_picture_id => @sp.id
14
15     @speech_balloon_template = FactoryGirl.create :speech_balloon_template
16     @panel = FactoryGirl.create :panel, :author_id => @author.id
17   end
18
19   describe '一覧表示に於いて' do
20     before do
21       @sb = FactoryGirl.create :speech_balloon, :panel_id => @panel.id, :speech_balloon_template_id => @speech_balloon_template.id
22       @balloon = FactoryGirl.create :balloon, :speech_balloon_id => @sb.id, :system_picture_id => @sp.id
23       @speech = FactoryGirl.create :speech, :speech_balloon_id => @sb.id
24       sign_in @user
25       Speech.stub(:list).and_return([@speech, @speech, @speech])
26     end
27     context 'パラメータpageについて' do
28       it '@pageに値が入る' do
29         get :index, :page => 5
30         assigns(:page).should eq 5
31       end
32       it '省略されると@pageに1値が入る' do
33         get :index
34         assigns(:page).should eq 1
35       end
36       it '与えられたpage_sizeがセットされている' do
37         get :index, :page_size => 15
38         assigns(:page_size).should eq 15
39       end
40       it '省略されると@page_sizeにデフォルト値が入る' do
41         get :index
42         assigns(:page_size).should eq Speech.default_page_size
43       end
44       it '最大を超えると@page_sizeにデフォルト最大値が入る' do
45         get :index, :page_size => 1500
46         assigns(:page_size).should eq Speech.max_page_size
47       end
48       it '不正な値が入ると@page_sizeにデフォルト最大値が入る' do
49         get :index, :page_size => 0
50         assigns(:page_size).should eq Speech.default_page_size
51       end
52     end
53     context 'つつがなく終わるとき' do
54       it 'ステータスコード200 OKを返す' do
55         get :index
56         response.should be_success 
57       end
58       it 'セリフモデルに一覧を問い合わせている' do
59         Speech.should_receive(:list).exactly(1)
60         get :index
61       end
62       it '@speechesにリストを取得している' do
63         get :index
64         assigns(:speeches).should have_at_least(3).items
65       end
66       context 'html形式' do
67         it 'indexテンプレートを描画する' do
68           get :index
69           response.should render_template("index")
70         end
71       end
72       context 'json形式' do
73         it 'jsonデータを返す' do
74           get :index, :format => :json
75           lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
76         end
77         it 'セリフモデルにjson一覧出力オプションを問い合わせている' do
78           Speech.should_receive(:list_json_opt).exactly(1)
79           get :index, :format => :json
80         end
81         it 'データがリスト構造になっている' do
82           get :index, :format => :json
83           json = JSON.parse response.body
84           json.should have_at_least(3).items
85         end
86         it 'リストの先頭くらいはセリフっぽいものであって欲しい' do
87           get :index, :format => :json
88           json = JSON.parse response.body
89           json.first.has_key?("speech_balloon_id").should be_true
90           json.first.has_key?("x").should be_true
91           json.first.has_key?("content").should be_true
92         end
93       end
94     end
95     context '作家権限がないとき' do
96       before do
97         sign_out @user
98       end
99       context 'html形式' do
100         it 'ステータスコード302 Foundを返す' do
101           get :index
102           response.status.should eq 302
103         end
104         it 'サインインページへ遷移する' do
105           get :index
106           response.should redirect_to '/users/sign_in'
107         end
108       end
109       context 'json形式' do
110         it 'ステータスコード401 Unauthorizedを返す' do
111           get :index, :format => :json
112           response.status.should eq 401
113         end
114         it '応答メッセージにUnauthorizedを返す' do
115           get :index, :format => :json
116           response.message.should match(/Unauthorized/)
117         end
118       end
119     end
120   end
121   
122   describe '単体表示に於いて' do
123     before do
124       sign_in @user
125       @sb = FactoryGirl.create :speech_balloon, :panel_id => @panel.id, :speech_balloon_template_id => @speech_balloon_template.id
126       @balloon = FactoryGirl.create :balloon, :speech_balloon_id => @sb.id, :system_picture_id => @sp.id
127       @speech = FactoryGirl.create :speech, :speech_balloon_id => @sb.id
128       Speech.stub(:show).and_return(@speech)
129     end
130     context 'つつがなく終わるとき' do
131       it 'ステータスコード200 OKを返す' do
132         get :show, :id => @speech.id
133         response.should be_success
134       end
135       it 'セリフモデルに単体取得を問い合わせている' do
136         Speech.should_receive(:show).exactly(1)
137         get :show
138       end
139       it '@speechにアレを取得している' do
140         get :show, :id => @speech.id
141         assigns(:speech).should eq(@speech)
142       end
143       context 'html形式' do
144         it 'showテンプレートを描画する' do
145           get :show, :id => @speech.id
146           response.should render_template("show")
147         end
148       end
149       context 'json形式' do
150         it 'jsonデータを返す' do
151           get :show, :id => @speech.id, :format => :json
152           lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
153         end
154         it 'セリフモデルにjson単体出力オプションを問い合わせている' do
155           Speech.should_receive(:show_json_opt).exactly(1)
156           get :show, :id => @speech.id, :format => :json
157         end
158         it 'データがアレになっている' do
159           get :show, :id => @speech.id, :format => :json
160           json = JSON.parse response.body
161           json["speech_balloon_id"].should_not be_nil
162           json["x"].should_not be_nil
163           json["content"].should_not be_nil
164         end
165       end
166     end
167     context '作家権限がないとき' do
168       before do
169         sign_out @user
170       end
171       context 'html形式' do
172         it 'ステータスコード302 Foundを返す' do
173           get :show, :id => @speech.id
174           response.status.should eq 302
175         end
176         it 'サインインページへ遷移する' do
177           get :show, :id => @speech.id
178           response.body.should redirect_to '/users/sign_in'
179         end
180       end
181       context 'json形式' do
182         it 'ステータスコード401 Unauthorizedを返す' do
183           get :show, :id => @speech.id, :format => :json
184           response.status.should eq 401
185         end
186         it '応答メッセージにUnauthorizedを返す' do
187           get :show, :id => @speech.id, :format => :json
188           response.message.should match(/Unauthorized/)
189         end
190       end
191     end
192   end
193   
194 end