OSDN Git Service

t#30558:fix auth filter
[pettanr/pettanr.git] / spec / controllers / speech_balloons_controller_spec.rb
1 # -*- encoding: utf-8 -*-
2 #フキダシ
3 require 'spec_helper'
4 describe SpeechBalloonsController do
5   before do
6     @admin = FactoryGirl.create :admin
7     @user = FactoryGirl.create( :user_yas)
8     @author = FactoryGirl.create :author, :user_id => @user.id
9     @artist = FactoryGirl.create :artist_yas, :author_id => @author.id
10     @sp = FactoryGirl.create :system_picture
11     @lg = FactoryGirl.create :license_group
12     @license = FactoryGirl.create :license, :license_group_id => @lg.id, :system_picture_id => @sp.id
13
14     @speech_balloon_template = FactoryGirl.create :speech_balloon_template
15     @panel = FactoryGirl.create :panel, :author_id => @author.id
16   end
17
18 if MagicNumber['run_mode'] == 1
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       sign_in @user
23       SpeechBalloon.stub(:list).and_return([@sb, @sb, @sb])
24     end
25     context 'パラメータpageについて' do
26       it '@pageに値が入る' do
27         get :index, :page => 5
28         assigns(:page).should eq 5
29       end
30       it '省略されると@pageに1値が入る' do
31         get :index
32         assigns(:page).should eq 1
33       end
34       it '与えられたpage_sizeがセットされている' do
35         get :index, :page_size => 15
36         assigns(:page_size).should eq 15
37       end
38       it '省略されると@page_sizeにデフォルト値が入る' do
39         get :index
40         assigns(:page_size).should eq SpeechBalloon.default_page_size
41       end
42       it '最大を超えると@page_sizeにデフォルト最大値が入る' do
43         get :index, :page_size => 1500
44         assigns(:page_size).should eq SpeechBalloon.max_page_size
45       end
46       it '不正な値が入ると@page_sizeにデフォルト最大値が入る' do
47         get :index, :page_size => 0
48         assigns(:page_size).should eq SpeechBalloon.default_page_size
49       end
50     end
51     context 'つつがなく終わるとき' do
52       it 'ステータスコード200 OKを返す' do
53         get :index
54         response.should be_success 
55       end
56       it 'フキダシモデルに一覧を問い合わせている' do
57         SpeechBalloon.should_receive(:list).exactly(1)
58         get :index
59       end
60       it '@speech_balloonsにリストを取得している' do
61         get :index
62         assigns(:speech_balloons).should have_at_least(3).items
63       end
64       context 'html形式' do
65         it 'indexテンプレートを描画する' do
66           get :index
67           response.should render_template("index")
68         end
69       end
70       context 'json形式' do
71         it 'jsonデータを返す' do
72           get :index, :format => :json
73           lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
74         end
75         it 'フキダシモデルにjson一覧出力オプションを問い合わせている' do
76           SpeechBalloon.should_receive(:list_json_opt).exactly(1)
77           get :index, :format => :json
78         end
79         it 'データがリスト構造になっている' do
80           get :index, :format => :json
81           json = JSON.parse response.body
82           json.should have_at_least(3).items
83         end
84         it 'リストの先頭くらいはフキダシっぽいものであって欲しい' do
85           get :index, :format => :json
86           json = JSON.parse response.body
87           json.first.has_key?("panel_id").should be_true
88           json.first.has_key?("z").should be_true
89           json.first.has_key?("speech_balloon_template_id").should be_true
90         end
91       end
92     end
93     context 'ユーザ権限がないとき' do
94       before do
95         sign_out @user
96       end
97       context 'html形式' do
98         it 'ステータスコード302 Foundを返す' do
99           get :index
100           response.status.should eq 302
101         end
102         it 'サインインページへ遷移する' do
103           get :index
104           response.should redirect_to '/users/sign_in'
105         end
106       end
107       context 'json形式' do
108         it 'ステータスコード401 Unauthorizedを返す' do
109           get :index, :format => :json
110           response.status.should eq 401
111         end
112         it '応答メッセージにUnauthorizedを返す' do
113           get :index, :format => :json
114           response.message.should match(/Unauthorized/)
115         end
116       end
117     end
118     context 'ユーザ権限はないが管理者権限があるとき' do
119       before do
120         sign_out @user
121         sign_in @admin
122       end
123       it 'ステータスコード200 OKを返す' do
124         get :index
125         response.should be_success 
126       end
127     end
128     context 'ユーザだが作家登録していないとき' do
129       before do
130         @author.destroy
131       end
132       it 'ステータスコード200 OKを返す' do
133         get :index
134         response.should be_success 
135       end
136     end
137   end
138   
139   describe '単体表示に於いて' do
140     before do
141       sign_in @user
142       @sb = FactoryGirl.create :speech_balloon, :panel_id => @panel.id, :speech_balloon_template_id => @speech_balloon_template.id
143       SpeechBalloon.stub(:show).and_return(@sb)
144     end
145     context 'つつがなく終わるとき' do
146       it 'ステータスコード200 OKを返す' do
147         get :show, :id => @sb.id
148         response.should be_success
149       end
150       it 'フキダシモデルに単体取得を問い合わせている' do
151         SpeechBalloon.should_receive(:show).exactly(1)
152         get :show
153       end
154       it '@speech_balloonにアレを取得している' do
155         get :show, :id => @sb.id
156         assigns(:speech_balloon).should eq(@sb)
157       end
158       context 'html形式' do
159         it 'showテンプレートを描画する' do
160           get :show, :id => @sb.id
161           response.should render_template("show")
162         end
163       end
164       context 'json形式' do
165         it 'jsonデータを返す' do
166           get :show, :id => @sb.id, :format => :json
167           lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
168         end
169         it 'フキダシモデルにjson単体出力オプションを問い合わせている' do
170           SpeechBalloon.should_receive(:show_json_opt).exactly(1)
171           get :show, :id => @sb.id, :format => :json
172         end
173         it 'データがアレになっている' do
174           get :show, :id => @sb.id, :format => :json
175           json = JSON.parse response.body
176           json["panel_id"].should_not be_nil
177           json["z"].should_not be_nil
178           json["speech_balloon_template_id"].should_not be_nil
179         end
180       end
181     end
182     context 'ユーザ権限がないとき' do
183       before do
184         sign_out @user
185       end
186       context 'html形式' do
187         it 'ステータスコード302 Foundを返す' do
188           get :show, :id => @sb.id
189           response.status.should eq 302
190         end
191         it 'サインインページへ遷移する' do
192           get :show, :id => @sb.id
193           response.body.should redirect_to '/users/sign_in'
194         end
195       end
196       context 'json形式' do
197         it 'ステータスコード401 Unauthorizedを返す' do
198           get :show, :id => @sb.id, :format => :json
199           response.status.should eq 401
200         end
201         it '応答メッセージにUnauthorizedを返す' do
202           get :show, :id => @sb.id, :format => :json
203           response.message.should match(/Unauthorized/)
204         end
205       end
206     end
207     context 'ユーザ権限はないが管理者権限があるとき' do
208       before do
209         sign_out @user
210         sign_in @admin
211       end
212       it 'ステータスコード200 OKを返す' do
213         get :show, :id => @sb.id
214         response.should be_success 
215       end
216     end
217     context 'ユーザだが作家登録していないとき' do
218       before do
219         @author.destroy
220       end
221       it 'ステータスコード200 OKを返す' do
222         get :show, :id => @sb.id
223         response.should be_success 
224       end
225     end
226   end
227   
228 else
229   describe '一覧表示に於いて' do
230     before do
231       @sb = FactoryGirl.create :speech_balloon, :panel_id => @panel.id, :speech_balloon_template_id => @speech_balloon_template.id
232       sign_in @user
233       SpeechBalloon.stub(:list).and_return([@sb, @sb, @sb])
234     end
235     context 'つつがなく終わるとき' do
236       it 'ステータスコード200 OKを返す' do
237         get :index
238         response.should be_success 
239       end
240       context 'html形式' do
241         it 'indexテンプレートを描画する' do
242           get :index
243           response.should render_template("index")
244         end
245       end
246       context 'json形式' do
247         it 'jsonデータを返す' do
248           get :index, :format => :json
249           lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
250         end
251       end
252     end
253     context 'ユーザ権限がないとき' do
254       before do
255         sign_out @user
256       end
257       it 'ステータスコード200 OKを返す' do
258         get :index
259         response.should be_success 
260       end
261       context 'html形式' do
262         it 'indexテンプレートを描画する' do
263           get :index
264           response.should render_template("index")
265         end
266       end
267       context 'json形式' do
268         it 'jsonデータを返す' do
269           get :index, :format => :json
270           lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
271         end
272       end
273     end
274   end
275   
276   describe '単体表示に於いて' do
277     before do
278       sign_in @user
279       @sb = FactoryGirl.create :speech_balloon, :panel_id => @panel.id, :speech_balloon_template_id => @speech_balloon_template.id
280       SpeechBalloon.stub(:show).and_return(@sb)
281     end
282     context 'つつがなく終わるとき' do
283       it 'ステータスコード200 OKを返す' do
284         get :show, :id => @sb.id
285         response.should be_success
286       end
287       context 'html形式' do
288         it 'showテンプレートを描画する' do
289           get :show, :id => @sb.id
290           response.should render_template("show")
291         end
292       end
293       context 'json形式' do
294         it 'jsonデータを返す' do
295           get :show, :id => @sb.id, :format => :json
296           lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
297         end
298       end
299     end
300     context 'ユーザ権限がないとき' do
301       before do
302         sign_out @user
303       end
304       it 'ステータスコード200 OKを返す' do
305         get :show, :id => @sb.id
306         response.should be_success
307       end
308       context 'html形式' do
309         it 'showテンプレートを描画する' do
310           get :show, :id => @sb.id
311           response.should render_template("show")
312         end
313       end
314       context 'json形式' do
315         it 'jsonデータを返す' do
316           get :show, :id => @sb.id, :format => :json
317           lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
318         end
319       end
320     end
321   end
322   
323 end
324 end