OSDN Git Service

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