OSDN Git Service

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