OSDN Git Service

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