OSDN Git Service

top page moved
[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     context 'ユーザだが作家登録していないとき' do
132       before do
133         @author.destroy
134       end
135       it 'ステータスコード200 OKを返す' do
136         get :index
137         response.should be_success 
138       end
139     end
140   end
141   
142   describe '単体表示に於いて' do
143     before do
144       sign_in @user
145       @sb = FactoryGirl.create :speech_balloon, :panel_id => @panel.id, :speech_balloon_template_id => @speech_balloon_template.id
146       @balloon = FactoryGirl.create :balloon, :speech_balloon_id => @sb.id, :system_picture_id => @sp.id
147       @speech = FactoryGirl.create :speech, :speech_balloon_id => @sb.id
148       Balloon.stub(:show).and_return(@balloon)
149     end
150     context 'つつがなく終わるとき' do
151       it 'ステータスコード200 OKを返す' do
152         get :show, :id => @balloon.id
153         response.should be_success
154       end
155       it 'フキダシ枠モデルに単体取得を問い合わせている' do
156         Balloon.should_receive(:show).exactly(1)
157         get :show
158       end
159       it '@balloonにアレを取得している' do
160         get :show, :id => @balloon.id
161         assigns(:balloon).should eq(@balloon)
162       end
163       context 'html形式' do
164         it 'showテンプレートを描画する' do
165           get :show, :id => @balloon.id
166           response.should render_template("show")
167         end
168       end
169       context 'json形式' do
170         it 'jsonデータを返す' do
171           get :show, :id => @balloon.id, :format => :json
172           lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
173         end
174         it 'フキダシ枠モデルにjson単体出力オプションを問い合わせている' do
175           Balloon.should_receive(:show_json_opt).exactly(1)
176           get :show, :id => @balloon.id, :format => :json
177         end
178         it 'データがアレになっている' do
179           get :show, :id => @balloon.id, :format => :json
180           json = JSON.parse response.body
181           json["speech_balloon_id"].should_not be_nil
182           json["x"].should_not be_nil
183           json["system_picture_id"].should_not be_nil
184         end
185       end
186     end
187     context 'ユーザ権限がないとき' do
188       before do
189         sign_out @user
190       end
191       context 'html形式' do
192         it 'ステータスコード302 Foundを返す' do
193           get :show, :id => @balloon.id
194           response.status.should eq 302
195         end
196         it 'サインインページへ遷移する' do
197           get :show, :id => @balloon.id
198           response.body.should redirect_to '/users/sign_in'
199         end
200       end
201       context 'json形式' do
202         it 'ステータスコード401 Unauthorizedを返す' do
203           get :show, :id => @balloon.id, :format => :json
204           response.status.should eq 401
205         end
206         it '応答メッセージにUnauthorizedを返す' do
207           get :show, :id => @balloon.id, :format => :json
208           response.message.should match(/Unauthorized/)
209         end
210       end
211     end
212     context 'ユーザ権限はないが管理者権限があるとき' do
213       before do
214         sign_out @user
215         sign_in @admin
216       end
217       it 'ステータスコード200 OKを返す' do
218         get :show, :id => @balloon.id
219         response.should be_success 
220       end
221     end
222     context 'ユーザだが作家登録していないとき' do
223       before do
224         @author.destroy
225       end
226       it 'ステータスコード200 OKを返す' do
227         get :show, :id => @balloon.id
228         response.should be_success 
229       end
230     end
231   end
232   
233 else
234   describe '一覧表示に於いて' do
235     before do
236       @sb = FactoryGirl.create :speech_balloon, :panel_id => @panel.id, :speech_balloon_template_id => @speech_balloon_template.id
237       @balloon = FactoryGirl.create :balloon, :speech_balloon_id => @sb.id, :system_picture_id => @sp.id
238       @speech = FactoryGirl.create :speech, :speech_balloon_id => @sb.id
239       sign_in @user
240       Balloon.stub(:list).and_return([@balloon, @balloon, @balloon])
241     end
242     context 'つつがなく終わるとき' do
243       it 'ステータスコード200 OKを返す' do
244         get :index
245         response.should be_success 
246       end
247       context 'html形式' do
248         it 'indexテンプレートを描画する' do
249           get :index
250           response.should render_template("index")
251         end
252       end
253       context 'json形式' do
254         it 'jsonデータを返す' do
255           get :index, :format => :json
256           lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
257         end
258       end
259     end
260     context 'ユーザ権限がないとき' do
261       before do
262         sign_out @user
263       end
264       it 'ステータスコード200 OKを返す' do
265         get :index
266         response.should be_success 
267       end
268       context 'html形式' do
269         it 'indexテンプレートを描画する' do
270           get :index
271           response.should render_template("index")
272         end
273       end
274       context 'json形式' do
275         it 'jsonデータを返す' do
276           get :index, :format => :json
277           lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
278         end
279       end
280     end
281   end
282   
283   describe '単体表示に於いて' do
284     before do
285       sign_in @user
286       @sb = FactoryGirl.create :speech_balloon, :panel_id => @panel.id, :speech_balloon_template_id => @speech_balloon_template.id
287       @balloon = FactoryGirl.create :balloon, :speech_balloon_id => @sb.id, :system_picture_id => @sp.id
288       @speech = FactoryGirl.create :speech, :speech_balloon_id => @sb.id
289       Balloon.stub(:show).and_return(@balloon)
290     end
291     context 'つつがなく終わるとき' do
292       it 'ステータスコード200 OKを返す' do
293         get :show, :id => @balloon.id
294         response.should be_success
295       end
296       context 'html形式' do
297         it 'showテンプレートを描画する' do
298           get :show, :id => @balloon.id
299           response.should render_template("show")
300         end
301       end
302       context 'json形式' do
303         it 'jsonデータを返す' do
304           get :show, :id => @balloon.id, :format => :json
305           lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
306         end
307       end
308     end
309     context 'ユーザ権限がないとき' do
310       before do
311         sign_out @user
312       end
313       it 'ステータスコード200 OKを返す' do
314         get :show, :id => @balloon.id
315         response.should be_success
316       end
317       context 'html形式' do
318         it 'showテンプレートを描画する' do
319           get :show, :id => @balloon.id
320           response.should render_template("show")
321         end
322       end
323       context 'json形式' do
324         it 'jsonデータを返す' do
325           get :show, :id => @balloon.id, :format => :json
326           lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
327         end
328       end
329     end
330   end
331   
332 end
333 end