OSDN Git Service

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