OSDN Git Service

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