OSDN Git Service

t#32046:
[pettanr/pettanr.git] / spec / controllers / system_pictures_controller_spec.rb
1 # -*- encoding: utf-8 -*-
2 #システム画像
3 require 'spec_helper'
4
5 describe SystemPicturesController do
6   before do
7     SpeechBalloonTemplate.delete_all
8     @admin = FactoryGirl.create :admin
9     @demand_user = FactoryGirl.create :demand_user
10     @user = FactoryGirl.create( :user_yas)
11     @author = FactoryGirl.create :author, :user_id => @user.id
12     @artist = FactoryGirl.create :artist_yas, :author_id => @author.id
13     @sp = FactoryGirl.create :system_picture
14     @lg = FactoryGirl.create :license_group
15     @license = FactoryGirl.create :license, :license_group_id => @lg.id, :system_picture_id => @sp.id
16   end
17   
18 if MagicNumber['run_mode'] == 1
19   describe '一覧表示に於いて' do
20     before do
21       sign_in @user
22       SystemPicture.stub(:list).and_return([@sp, @sp, @sp])
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 SystemPicture.default_page_size
40       end
41       it '最大を超えると@page_sizeにデフォルト最大値が入る' do
42         get :index, :page_size => 1500
43         assigns(:page_size).should eq SystemPicture.max_page_size
44       end
45       it '不正な値が入ると@page_sizeにデフォルト最大値が入る' do
46         get :index, :page_size => 0
47         assigns(:page_size).should eq SystemPicture.default_page_size
48       end
49     end
50     context 'つつがなく終わるとき' do
51       it 'システム画像モデルに一覧を問い合わせている' do
52         SystemPicture.should_receive(:list).exactly(1)
53         get :index
54       end
55       it '@system_picturesにリストを取得している' do
56         get :index
57         assigns(:system_pictures).should have_at_least(3).items
58       end
59       context 'html形式' do
60         it 'ステータスコード200 OKを返す' do
61           get :index
62           response.should be_success 
63         end
64         it 'indexテンプレートを描画する' do
65           get :index
66           response.should render_template("index")
67         end
68       end
69       context 'json形式' do
70         it 'ステータスコード200 OKを返す' do
71           get :index, :format => :json
72           response.should be_success 
73         end
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           SystemPicture.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?("ext").should be_true
91           json.first.has_key?("md5").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     context 'ユーザ権限はないが管理者権限があるとき' do
121       before do
122         sign_out @user
123         sign_in @admin
124       end
125       it 'ステータスコード200 OKを返す' do
126         get :index
127         response.should be_success 
128       end
129     end
130     context 'ユーザ権限はないが借手権限があるとき' do
131       before do
132         sign_out @user
133         sign_in @demand_user
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       SystemPicture.stub(:show).and_return(@sp)
146     end
147     context 'つつがなく終わるとき' do
148       it 'ステータスコード200 OKを返す' do
149         get :show, :id => @sp.id
150         response.should be_success
151       end
152       it 'システム画像モデルに単体取得を問い合わせている' do
153         SystemPicture.should_receive(:show).exactly(1)
154         get :show
155       end
156       it '@system_pictureにアレを取得している' do
157         get :show, :id => @sp.id
158         assigns(:system_picture).should eq @sp
159       end
160       context 'html形式' do
161         it 'showテンプレートを描画する' do
162           get :show, :id => @sp.id
163           response.should render_template("show")
164         end
165       end
166       context 'json形式' do
167         it 'jsonデータを返す' do
168           get :show, :id => @sp.id, :format => :json
169           lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
170         end
171         it 'システム画像モデルにjson単体出力オプションを問い合わせている' do
172           SystemPicture.should_receive(:show_json_opt).exactly(1)
173           get :show, :id => @sp.id, :format => :json
174         end
175         it 'データがアレになっている' do
176           get :show, :id => @sp.id, :format => :json
177           json = JSON.parse response.body
178           json["ext"].should match(/png/)
179           json["md5"].should be_true
180           json["width"].should be_true
181         end
182       end
183       #画像送信では、send_dataにスタブをおいてテストしたいが、ここに噛ませると
184       #renderが働かず、エラーとなってしまう。そこで、システム画像のファイル取得部分に
185       #スタブをおいてsend_dataがデータを返す体裁でテストする。
186       context 'png形式' do
187         before do
188           SystemPicture.any_instance.stub(:mime_type).and_return('image/png')
189           SystemPicture.any_instance.stub(:restore).and_return('aaa')
190         end
191         it '画像モデルに画像データを問い合わせる' do
192           SystemPicture.any_instance.should_receive(:restore).exactly(1)
193           get :show, :id => @sp.id, :format => :png
194         end
195         it '画像モデルにMimeTypeを問い合わせる' do
196           SystemPicture.any_instance.should_receive(:mime_type).exactly(1)
197           get :show, :id => @sp.id, :format => :png
198         end
199         it '画像を送信する' do
200           get :show, :id => @sp.id, :format => :png
201           response.body.should eq 'aaa'
202         end
203       end
204       context 'gif形式' do
205         before do
206           SystemPicture.any_instance.stub(:mime_type).and_return('image/gif')
207           SystemPicture.any_instance.stub(:restore).and_return('bbb')
208         end
209         it '画像モデルに画像データを問い合わせる' do
210           SystemPicture.any_instance.should_receive(:restore).exactly(1)
211           get :show, :id => @sp.id, :format => :gif
212         end
213         it '画像モデルにMimeTypeを問い合わせる' do
214           SystemPicture.any_instance.should_receive(:mime_type).exactly(1)
215           get :show, :id => @sp.id, :format => :gif
216         end
217         it '画像を送信する' do
218           get :show, :id => @sp.id, :format => :gif
219           response.body.should eq 'bbb'
220         end
221       end
222       context 'jpeg形式' do
223         before do
224           SystemPicture.any_instance.stub(:mime_type).and_return('image/jpeg')
225           SystemPicture.any_instance.stub(:restore).and_return('ccc')
226         end
227         it '画像モデルに画像データを問い合わせる' do
228           SystemPicture.any_instance.should_receive(:restore).exactly(1)
229           get :show, :id => @sp.id, :format => :jpeg
230         end
231         it '画像モデルにMimeTypeを問い合わせる' do
232           SystemPicture.any_instance.should_receive(:mime_type).exactly(1)
233           get :show, :id => @sp.id, :format => :jpeg
234         end
235         it '画像を送信する' do
236           get :show, :id => @sp.id, :format => :jpeg
237           response.body.should eq 'ccc'
238         end
239       end
240     end
241     context 'ユーザ権限がないとき' do
242       before do
243         sign_out @user
244       end
245       context 'html形式' do
246         it 'ステータスコード302 Foundを返す' do
247           get :show, :id => @sp.id
248           response.status.should eq 302
249         end
250         it 'サインインページへ遷移する' do
251           get :show, :id => @sp.id
252           response.body.should redirect_to '/users/sign_in'
253         end
254       end
255       context 'json形式' do
256         it 'ステータスコード401 Unauthorizedを返す' do
257           get :show, :id => @sp.id, :format => :json
258           response.status.should eq 401
259         end
260         it '応答メッセージにUnauthorizedを返す' do
261           get :show, :id => @sp.id, :format => :json
262           response.message.should match(/Unauthorized/)
263         end
264       end
265     end
266     context 'ユーザ権限はないが管理者権限があるとき' do
267       before do
268         sign_out @user
269         sign_in @admin
270       end
271       it 'ステータスコード200 OKを返す' do
272         get :show, :id => @sp.id
273         response.should be_success 
274       end
275     end
276     context 'ユーザ権限はないが借手権限があるとき' do
277       before do
278         sign_out @user
279         sign_in @demand_user
280       end
281       it 'ステータスコード200 OKを返す' do
282         get :show, :id => @sp.id
283         response.should be_success 
284       end
285     end
286 =begin
287     context '対象システム画像がないとき' do
288       before do
289         SystemPicture.unstub(:show)
290       end
291       context 'html形式' do
292         it '例外404 not_foundを返す' do
293           lambda{
294             get :show, :id => 0
295           }.should raise_error(ActiveRecord::RecordNotFound)
296         end
297       end
298       context 'json形式' do
299         it '例外404 not_foundを返す' do
300           lambda{ 
301             get :show, :id => 0, :format => :json
302           }.should raise_error(ActiveRecord::RecordNotFound)
303         end
304       end
305     end
306     context '他人のシステム画像を見ようとしたとき' do
307       before do
308         SystemPicture.stub(:show).and_return(@sp)
309         SystemPicture.any_instance.stub(:own?).with(any_args()).and_return(false)
310       end
311       context 'html形式' do
312         it '例外403 forbiddenを返す' do
313           lambda{
314             get :show, :id => @sp.id
315           }.should raise_error(ActiveRecord::Forbidden)
316         end
317       end
318       context 'json形式' do
319         it '例外403 forbiddenを返す' do
320           lambda{
321             get :show, :id => @sp.id, :format => :json
322           }.should raise_error(ActiveRecord::Forbidden)
323         end
324       end
325     end
326 =end
327   end
328   
329 else
330   describe '一覧表示に於いて' do
331     before do
332       sign_in @user
333       sign_in @admin
334       SystemPicture.stub(:list).and_return([@sp, @sp, @sp])
335     end
336     context 'つつがなく終わるとき' do
337       context 'html形式' do
338         it 'ステータスコード200 OKを返す' do
339           get :index
340           response.should be_success 
341         end
342         it 'indexテンプレートを描画する' do
343           get :index
344           response.should render_template("index")
345         end
346       end
347       context 'json形式' do
348         it 'ステータスコード200 OKを返す' do
349           get :index, :format => :json
350           response.should be_success 
351         end
352         it 'jsonデータを返す' do
353           get :index, :format => :json
354           lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
355         end
356       end
357     end
358     context 'ユーザ権限がないとき' do
359       before do
360         sign_out @user
361       end
362       context 'html形式' do
363         it 'ステータスコード200 OKを返す' do
364           get :index
365           response.should be_success 
366         end
367         it 'indexテンプレートを描画する' do
368           get :index
369           response.should render_template("index")
370         end
371       end
372       context 'json形式' do
373         it 'ステータスコード200 OKを返す' do
374           get :index, :format => :json
375           response.should be_success 
376         end
377         it 'jsonデータを返す' do
378           get :index, :format => :json
379           lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
380         end
381       end
382     end
383   end
384   
385   describe '単体表示に於いて' do
386     before do
387       sign_in @user
388       SystemPicture.stub(:show).and_return(@sp)
389     end
390     context 'つつがなく終わるとき' do
391       it 'ステータスコード200 OKを返す' do
392         get :show, :id => @sp.id
393         response.should be_success
394       end
395       context 'html形式' do
396         it 'showテンプレートを描画する' do
397           get :show, :id => @sp.id
398           response.should render_template("show")
399         end
400       end
401       context 'json形式' do
402         it 'jsonデータを返す' do
403           get :show, :id => @sp.id, :format => :json
404           lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
405         end
406       end
407     end
408     context 'ユーザ権限がないとき' do
409       before do
410         sign_out @user
411       end
412       it 'ステータスコード200 OKを返す' do
413         get :show, :id => @sp.id
414         response.should be_success
415       end
416       context 'html形式' do
417         it 'showテンプレートを描画する' do
418           get :show, :id => @sp.id
419           response.should render_template("show")
420         end
421       end
422       context 'json形式' do
423         it 'jsonデータを返す' do
424           get :show, :id => @sp.id, :format => :json
425           lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
426         end
427       end
428     end
429   end
430 end
431 end