OSDN Git Service

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