OSDN Git Service

t#30443:add test for open mode
[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
15     @panel = FactoryGirl.create :panel, :author_id => @author.id
16   end
17
18 if MagicNumber['run_mode'] == 1
19   describe '一覧表示に於いて' do
20     before do
21       @sb = FactoryGirl.create :speech_balloon, :panel_id => @panel.id, :speech_balloon_template_id => @speech_balloon_template.id
22       sign_in @user
23       SpeechBalloon.stub(:list).and_return([@sb, @sb, @sb])
24     end
25     context 'パラメータpageについて' do
26       it '@pageに値が入る' do
27         get :index, :page => 5
28         assigns(:page).should eq 5
29       end
30       it '省略されると@pageに1値が入る' do
31         get :index
32         assigns(:page).should eq 1
33       end
34       it '与えられたpage_sizeがセットされている' do
35         get :index, :page_size => 15
36         assigns(:page_size).should eq 15
37       end
38       it '省略されると@page_sizeにデフォルト値が入る' do
39         get :index
40         assigns(:page_size).should eq SpeechBalloon.default_page_size
41       end
42       it '最大を超えると@page_sizeにデフォルト最大値が入る' do
43         get :index, :page_size => 1500
44         assigns(:page_size).should eq SpeechBalloon.max_page_size
45       end
46       it '不正な値が入ると@page_sizeにデフォルト最大値が入る' do
47         get :index, :page_size => 0
48         assigns(:page_size).should eq SpeechBalloon.default_page_size
49       end
50     end
51     context 'つつがなく終わるとき' do
52       it 'ステータスコード200 OKを返す' do
53         get :index
54         response.should be_success 
55       end
56       it 'フキダシモデルに一覧を問い合わせている' do
57         SpeechBalloon.should_receive(:list).exactly(1)
58         get :index
59       end
60       it '@speech_balloonsにリストを取得している' do
61         get :index
62         assigns(:speech_balloons).should have_at_least(3).items
63       end
64       context 'html形式' do
65         it 'indexテンプレートを描画する' do
66           get :index
67           response.should render_template("index")
68         end
69       end
70       context 'json形式' do
71         it 'jsonデータを返す' do
72           get :index, :format => :json
73           lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
74         end
75         it 'フキダシモデルにjson一覧出力オプションを問い合わせている' do
76           SpeechBalloon.should_receive(:list_json_opt).exactly(1)
77           get :index, :format => :json
78         end
79         it 'データがリスト構造になっている' do
80           get :index, :format => :json
81           json = JSON.parse response.body
82           json.should have_at_least(3).items
83         end
84         it 'リストの先頭くらいはフキダシっぽいものであって欲しい' do
85           get :index, :format => :json
86           json = JSON.parse response.body
87           json.first.has_key?("panel_id").should be_true
88           json.first.has_key?("z").should be_true
89           json.first.has_key?("speech_balloon_template_id").should be_true
90         end
91       end
92     end
93     context '作家権限がないとき' do
94       before do
95         sign_out @user
96       end
97       context 'html形式' do
98         it 'ステータスコード302 Foundを返す' do
99           get :index
100           response.status.should eq 302
101         end
102         it 'サインインページへ遷移する' do
103           get :index
104           response.should redirect_to '/users/sign_in'
105         end
106       end
107       context 'json形式' do
108         it 'ステータスコード401 Unauthorizedを返す' do
109           get :index, :format => :json
110           response.status.should eq 401
111         end
112         it '応答メッセージにUnauthorizedを返す' do
113           get :index, :format => :json
114           response.message.should match(/Unauthorized/)
115         end
116       end
117     end
118   end
119   
120   describe '単体表示に於いて' do
121     before do
122       sign_in @user
123       @sb = FactoryGirl.create :speech_balloon, :panel_id => @panel.id, :speech_balloon_template_id => @speech_balloon_template.id
124       SpeechBalloon.stub(:show).and_return(@sb)
125     end
126     context 'つつがなく終わるとき' do
127       it 'ステータスコード200 OKを返す' do
128         get :show, :id => @sb.id
129         response.should be_success
130       end
131       it 'フキダシモデルに単体取得を問い合わせている' do
132         SpeechBalloon.should_receive(:show).exactly(1)
133         get :show
134       end
135       it '@speech_balloonにアレを取得している' do
136         get :show, :id => @sb.id
137         assigns(:speech_balloon).should eq(@sb)
138       end
139       context 'html形式' do
140         it 'showテンプレートを描画する' do
141           get :show, :id => @sb.id
142           response.should render_template("show")
143         end
144       end
145       context 'json形式' do
146         it 'jsonデータを返す' do
147           get :show, :id => @sb.id, :format => :json
148           lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
149         end
150         it 'フキダシモデルにjson単体出力オプションを問い合わせている' do
151           SpeechBalloon.should_receive(:show_json_opt).exactly(1)
152           get :show, :id => @sb.id, :format => :json
153         end
154         it 'データがアレになっている' do
155           get :show, :id => @sb.id, :format => :json
156           json = JSON.parse response.body
157           json["panel_id"].should_not be_nil
158           json["z"].should_not be_nil
159           json["speech_balloon_template_id"].should_not be_nil
160         end
161       end
162     end
163     context '作家権限がないとき' do
164       before do
165         sign_out @user
166       end
167       context 'html形式' do
168         it 'ステータスコード302 Foundを返す' do
169           get :show, :id => @sb.id
170           response.status.should eq 302
171         end
172         it 'サインインページへ遷移する' do
173           get :show, :id => @sb.id
174           response.body.should redirect_to '/users/sign_in'
175         end
176       end
177       context 'json形式' do
178         it 'ステータスコード401 Unauthorizedを返す' do
179           get :show, :id => @sb.id, :format => :json
180           response.status.should eq 401
181         end
182         it '応答メッセージにUnauthorizedを返す' do
183           get :show, :id => @sb.id, :format => :json
184           response.message.should match(/Unauthorized/)
185         end
186       end
187     end
188   end
189   
190 else
191   describe '一覧表示に於いて' do
192     before do
193       @sb = FactoryGirl.create :speech_balloon, :panel_id => @panel.id, :speech_balloon_template_id => @speech_balloon_template.id
194       sign_in @user
195       SpeechBalloon.stub(:list).and_return([@sb, @sb, @sb])
196     end
197     context 'つつがなく終わるとき' do
198       it 'ステータスコード200 OKを返す' do
199         get :index
200         response.should be_success 
201       end
202       context 'html形式' do
203         it 'indexテンプレートを描画する' do
204           get :index
205           response.should render_template("index")
206         end
207       end
208       context 'json形式' do
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       it 'ステータスコード200 OKを返す' do
220         get :index
221         response.should be_success 
222       end
223       context 'html形式' do
224         it 'indexテンプレートを描画する' do
225           get :index
226           response.should render_template("index")
227         end
228       end
229       context 'json形式' do
230         it 'jsonデータを返す' do
231           get :index, :format => :json
232           lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
233         end
234       end
235     end
236   end
237   
238   describe '単体表示に於いて' do
239     before do
240       sign_in @user
241       @sb = FactoryGirl.create :speech_balloon, :panel_id => @panel.id, :speech_balloon_template_id => @speech_balloon_template.id
242       SpeechBalloon.stub(:show).and_return(@sb)
243     end
244     context 'つつがなく終わるとき' do
245       it 'ステータスコード200 OKを返す' do
246         get :show, :id => @sb.id
247         response.should be_success
248       end
249       context 'html形式' do
250         it 'showテンプレートを描画する' do
251           get :show, :id => @sb.id
252           response.should render_template("show")
253         end
254       end
255       context 'json形式' do
256         it 'jsonデータを返す' do
257           get :show, :id => @sb.id, :format => :json
258           lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
259         end
260       end
261     end
262     context '作家権限がないとき' do
263       before do
264         sign_out @user
265       end
266       it 'ステータスコード200 OKを返す' do
267         get :show, :id => @sb.id
268         response.should be_success
269       end
270       context 'html形式' do
271         it 'showテンプレートを描画する' do
272           get :show, :id => @sb.id
273           response.should render_template("show")
274         end
275       end
276       context 'json形式' do
277         it 'jsonデータを返す' do
278           get :show, :id => @sb.id, :format => :json
279           lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
280         end
281       end
282     end
283   end
284   
285 end
286 end