OSDN Git Service

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