OSDN Git Service

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