OSDN Git Service

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