OSDN Git Service

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