OSDN Git Service

Merge branch 'v05' of git.sourceforge.jp:/gitroot/pettanr/pettanr into v05client
[pettanr/pettanr.git] / spec / controllers / panel_pictures_controller_spec.rb
1 # -*- encoding: utf-8 -*-
2 #コマ絵
3 require 'spec_helper'
4
5 describe PanelPicturesController do
6   before do
7     @admin = FactoryGirl.create :admin
8     @sp = FactoryGirl.create :system_picture
9     @lg = FactoryGirl.create :license_group
10     @license = FactoryGirl.create :license, :license_group_id => @lg.id, :system_picture_id => @sp.id
11     @user = FactoryGirl.create( :user_yas)
12     @author = FactoryGirl.create :author, :user_id => @user.id
13     @artist = FactoryGirl.create :artist_yas, :author_id => @author.id
14     @op = FactoryGirl.create :original_picture, :artist_id => @artist.id
15     @p = FactoryGirl.create :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @artist.id
16     @rp = FactoryGirl.create :resource_picture, :artist_id => @artist.id, :license_id => @license.id, :original_picture_id => @op.id, :picture_id => @p.id
17     @panel = FactoryGirl.create :panel, :author_id => @author.id
18     @attr = {:picture_id => @p.id, :panel_id => @panel.id, :width => @p.width, :height => @p.height}
19   end
20
21 if MagicNumber['run_mode'] == 1
22   describe '一覧表示に於いて' do
23     before do
24       @pp = FactoryGirl.create :panel_picture, @attr
25       sign_in @user
26       PanelPicture.stub(:list).and_return([@pp, @pp, @pp])
27     end
28     context 'パラメータpageについて' do
29       it '@pageに値が入る' do
30         get :index, :page => 5
31         assigns(:page).should eq 5
32       end
33       it '省略されると@pageに1値が入る' do
34         get :index
35         assigns(:page).should eq 1
36       end
37       it '与えられたpage_sizeがセットされている' do
38         get :index, :page_size => 15
39         assigns(:page_size).should eq 15
40       end
41       it '省略されると@page_sizeにデフォルト値が入る' do
42         get :index
43         assigns(:page_size).should eq PanelPicture.default_page_size
44       end
45       it '最大を超えると@page_sizeにデフォルト最大値が入る' do
46         get :index, :page_size => 1500
47         assigns(:page_size).should eq PanelPicture.max_page_size
48       end
49       it '不正な値が入ると@page_sizeにデフォルト最大値が入る' do
50         get :index, :page_size => 0
51         assigns(:page_size).should eq PanelPicture.default_page_size
52       end
53     end
54     context 'つつがなく終わるとき' do
55       it 'ステータスコード200 OKを返す' do
56         get :index
57         response.should be_success 
58       end
59       it 'コマ絵モデルに一覧を問い合わせている' do
60         PanelPicture.should_receive(:list).exactly(1)
61         get :index
62       end
63       it '@panel_picturesにリストを取得している' do
64         get :index
65         assigns(:panel_pictures).should have_at_least(3).items
66       end
67       context 'html形式' do
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           PanelPicture.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?("link").should be_true
91           json.first.has_key?("x").should be_true
92           json.first.has_key?("y").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       @panel_picture = FactoryGirl.create :panel_picture, @attr
146       PanelPicture.stub(:show).and_return(@panel_picture)
147     end
148     context 'つつがなく終わるとき' do
149       it 'ステータスコード200 OKを返す' do
150         get :show, :id => @panel_picture.id
151         response.should be_success
152       end
153       it 'コマ絵モデルに単体取得を問い合わせている' do
154         PanelPicture.should_receive(:show).exactly(1)
155         get :show
156       end
157       it '@panel_pictureにアレを取得している' do
158         get :show, :id => @panel_picture.id
159         assigns(:panel_picture).should eq(@panel_picture)
160       end
161       context 'html形式' do
162         it 'showテンプレートを描画する' do
163           get :show, :id => @panel_picture.id
164           response.should render_template("show")
165         end
166       end
167       context 'json形式' do
168         it 'jsonデータを返す' do
169           get :show, :id => @panel_picture.id, :format => :json
170           lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
171         end
172         it 'コマ絵モデルにjson単体出力オプションを問い合わせている' do
173           PanelPicture.should_receive(:show_json_opt).exactly(1)
174           get :show, :id => @panel_picture.id, :format => :json
175         end
176         it 'データがアレになっている' do
177           get :show, :id => @panel_picture.id, :format => :json
178           json = JSON.parse response.body
179           json["link"].should_not be_nil
180           json["x"].should_not be_nil
181           json["y"].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 => @panel_picture.id
192           response.status.should eq 302
193         end
194         it 'サインインページへ遷移する' do
195           get :show, :id => @panel_picture.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 => @panel_picture.id, :format => :json
202           response.status.should eq 401
203         end
204         it '応答メッセージにUnauthorizedを返す' do
205           get :show, :id => @panel_picture.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 => @panel_picture.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 => @panel_picture.id
226         response.should be_success 
227       end
228     end
229   end
230   
231 else
232   describe '一覧表示に於いて' do
233     before do
234       @pp = FactoryGirl.create :panel_picture, @attr
235       sign_in @user
236       PanelPicture.stub(:list).and_return([@pp, @pp, @pp])
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       @panel_picture = FactoryGirl.create :panel_picture, @attr
283       PanelPicture.stub(:show).and_return(@panel_picture)
284     end
285     context 'つつがなく終わるとき' do
286       it 'ステータスコード200 OKを返す' do
287         get :show, :id => @panel_picture.id
288         response.should be_success
289       end
290       context 'html形式' do
291         it 'showテンプレートを描画する' do
292           get :show, :id => @panel_picture.id
293           response.should render_template("show")
294         end
295       end
296       context 'json形式' do
297         it 'jsonデータを返す' do
298           get :show, :id => @panel_picture.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 => @panel_picture.id
309         response.should be_success
310       end
311       context 'html形式' do
312         it 'showテンプレートを描画する' do
313           get :show, :id => @panel_picture.id
314           response.should render_template("show")
315         end
316       end
317       context 'json形式' do
318         it 'jsonデータを返す' do
319           get :show, :id => @panel_picture.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