OSDN Git Service

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