OSDN Git Service

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