OSDN Git Service

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