OSDN Git Service

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