OSDN Git Service

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