OSDN Git Service

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