OSDN Git Service

fix: any
[pettanr/pettanr.git] / spec / controllers / pictures_controller_spec.rb
1 # -*- encoding: utf-8 -*-
2 #実素材
3 require 'spec_helper'
4
5 describe PicturesController do
6   before do
7     @admin = FactoryGirl.create :admin
8     @demand_user = FactoryGirl.create :demand_user
9     @sp = FactoryGirl.create :system_picture
10     @lg = FactoryGirl.create :license_group
11     @license = FactoryGirl.create :license, :license_group_id => @lg.id, :system_picture_id => @sp.id
12     @user = FactoryGirl.create( :user_yas)
13     @author = FactoryGirl.create :author, :user_id => @user.id
14     @artist = FactoryGirl.create :artist_yas, :author_id => @author.id
15     @op = FactoryGirl.create :original_picture, :artist_id => @artist.id
16   end
17
18 if MagicNumber['run_mode'] == 1
19   describe '単体表示に於いて' do
20     before do
21       @p = FactoryGirl.create :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @artist.id
22       sign_in @user
23       Picture.stub(:show).with(@p.id.to_s, [@user, nil, nil]).and_return(@p)
24       Picture.stub(:show).with(@p.id.to_s, [nil, @admin, nil]).and_return(@p)
25       Picture.stub(:show).with(@p.id.to_s, [nil, nil, @demand_user]).and_return(@p)
26     end
27     context 'つつがなく終わるとき as json' do
28       it 'ステータスコード200 OKを返す' do
29         get :show, :id => @p.id, :format => :json
30         response.should be_success
31       end
32       it '実素材モデルに単体取得を問い合わせている' do
33         Picture.should_receive(:show).exactly(1)
34         get :show, :id => @p.id, :format => :json
35       end
36       it '@pictureにアレを取得している' do
37         get :show, :id => @p.id, :format => :json
38         assigns(:picture).id.should eq(@p.id)
39       end
40       context 'html形式' do
41         it 'showテンプレートを描画する' do
42           get :show, :id => @p.id
43           response.should render_template("show")
44         end
45       end
46       context 'json形式' do
47         it 'jsonデータを返す' do
48           get :show, :id => @p.id, :format => :json
49           lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
50         end
51         it 'データがアレになっている' do
52           get :show, :id => @p.id, :format => :json
53           json = JSON.parse response.body
54           json["revision"].should_not be_nil
55           json["ext"].should match(/png/)
56         end
57       end
58       #画像送信では、send_dataにスタブをおいてテストしたいが、ここに噛ませると
59       #renderが働かず、エラーとなってしまう。そこで、素材のファイル取得部分に
60       #スタブをおいてsend_dataがデータを返す体裁でテストする。
61       context 'png形式' do
62         it '画像モデルに画像データを問い合わせる' do
63           Picture.any_instance.should_receive(:restore).exactly(1)
64           get :show, :id => @p.id, :format => :png
65         end
66         it '画像を送信する' do
67           Picture.any_instance.stub(:restore).and_return('aaa')
68           get :show, :id => @p.id, :format => :png
69           response.body.should eq 'aaa'
70         end
71       end
72       context 'gif形式' do
73         it '画像モデルに画像データを問い合わせる' do
74           Picture.any_instance.should_receive(:restore).exactly(1)
75           get :show, :id => @p.id, :format => :gif
76         end
77         it '画像を送信する' do
78           Picture.any_instance.stub(:restore).and_return('bbb')
79           get :show, :id => @p.id, :format => :gif
80           response.body.should eq 'bbb'
81         end
82       end
83       context 'jpeg形式' do
84         it '画像モデルに画像データを問い合わせる' do
85           Picture.any_instance.should_receive(:restore).exactly(1)
86           get :show, :id => @p.id, :format => :jpeg
87         end
88         it '画像を送信する' do
89           Picture.any_instance.stub(:restore).and_return('ccc')
90           get :show, :id => @p.id, :format => :jpeg
91           response.body.should eq 'ccc'
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 :show, :id => @p.id
102           response.status.should eq 302
103         end
104         it 'サインインページへ遷移する' do
105           get :show, :id => @p.id
106           response.body.should redirect_to '/users/sign_in'
107         end
108       end
109       context 'json形式' do
110         it 'ステータスコード401 Unauthorizedを返す' do
111           get :show, :id => @p.id, :format => :json
112           response.status.should eq 401
113         end
114         it '応答メッセージにUnauthorizedを返す' do
115           get :show, :id => @p.id, :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 :show, :id => @p.id
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 :show, :id => @p.id
137         response.should be_success 
138       end
139     end
140     context 'ユーザだが作家登録していないとき' do
141       before do
142         @author.destroy
143       end
144       it 'ステータスコード200 OKを返す' do
145         get :show, :id => @p.id
146         response.should be_success 
147       end
148     end
149 =begin
150     context '対象素材がないとき' do
151       before do
152         Picture.unstub(:show)
153       end
154       context 'html形式' do
155         it '例外404 not_foundを返す' do
156           lambda{
157             get :show, :id => 0
158           }.should raise_error(ActiveRecord::RecordNotFound)
159         end
160       end
161       context 'json形式' do
162         it '例外404 not_foundを返す' do
163           lambda{ 
164             get :show, :id => 0, :format => :json
165           }.should raise_error(ActiveRecord::RecordNotFound)
166         end
167       end
168     end
169     context '他人の素材を見ようとしたとき' do
170       before do
171         Picture.stub(:show).and_return(@p)
172         Picture.any_instance.stub(:own?).with(any_args()).and_return(false)
173       end
174       context 'html形式' do
175         it '例外403 forbiddenを返す' do
176           lambda{
177             get :show, :id => @p.id
178           }.should raise_error(ActiveRecord::Forbidden)
179         end
180       end
181       context 'json形式' do
182         it '例外403 forbiddenを返す' do
183           lambda{
184             get :show, :id => @p.id, :format => :json
185           }.should raise_error(ActiveRecord::Forbidden)
186         end
187       end
188     end
189 =end
190   end
191
192   describe 'クレジット表示に於いて' do
193     before do
194       @p = FactoryGirl.create :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @artist.id
195       sign_in @user
196       Picture.stub(:show).with(@p.id.to_s, [@user, nil, nil]).and_return(@p)
197       Picture.stub(:show).with(@p.id.to_s, [nil, @admin, nil]).and_return(@p)
198       Picture.stub(:show).with(@p.id.to_s, [nil, nil, @demand_user]).and_return(@p)
199     end
200     context 'つつがなく終わるとき' do
201       it 'ステータスコード200 OKを返す' do
202         get :credit, :id => @p.id
203         response.should be_success
204       end
205       it '実素材モデルに単体取得を問い合わせている' do
206         Picture.should_receive(:show).exactly(1)
207         get :credit, :id => @p.id
208       end
209       it '@pictureにアレを取得している' do
210         get :credit, :id => @p.id
211         assigns(:picture).id.should eq(@p.id)
212       end
213       context 'html形式' do
214         it 'creditテンプレートを描画する' do
215           get :credit, :id => @p.id
216           response.should render_template("credit")
217         end
218       end
219       context 'json形式' do
220         it 'jsonデータを返す' do
221           get :credit, :id => @p.id, :format => :json
222           lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
223         end
224         it 'データがアレになっている' do
225           get :credit, :id => @p.id, :format => :json
226           json = JSON.parse response.body
227           json["revision"].should_not be_nil
228           json["ext"].should match(/png/)
229         end
230       end
231     end
232     context 'ユーザ権限がないとき' do
233       before do
234         sign_out @user
235       end
236       context 'html形式' do
237         it 'ステータスコード302 Foundを返す' do
238           get :credit, :id => @p.id
239           response.status.should eq 302
240         end
241         it 'サインインページへ遷移する' do
242           get :credit, :id => @p.id
243           response.body.should redirect_to '/users/sign_in'
244         end
245       end
246       context 'json形式' do
247         it 'ステータスコード401 Unauthorizedを返す' do
248           get :credit, :id => @p.id, :format => :json
249           response.status.should eq 401
250         end
251         it '応答メッセージにUnauthorizedを返す' do
252           get :credit, :id => @p.id, :format => :json
253           response.message.should match(/Unauthorized/)
254         end
255       end
256     end
257     context 'ユーザ権限はないが管理者権限があるとき' do
258       before do
259         sign_out @user
260         sign_in @admin
261       end
262       it 'ステータスコード200 OKを返す' do
263         get :credit, :id => @p.id
264         response.should be_success 
265       end
266     end
267     context 'ユーザ権限はないが借手権限があるとき' do
268       before do
269         sign_out @user
270         sign_in @demand_user
271       end
272       it 'ステータスコード200 OKを返す' do
273         get :credit, :id => @p.id
274         response.should be_success 
275       end
276     end
277     context 'ユーザだが作家登録していないとき' do
278       before do
279         @author.destroy
280       end
281       it 'ステータスコード200 OKを返す' do
282         get :credit, :id => @p.id
283         response.should be_success 
284       end
285     end
286 =begin
287     context '対象素材がないとき' do
288       before do
289         Picture.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 =end
307   end
308
309   describe 'md5検索の一覧に於いて' do
310     before do
311       @p = FactoryGirl.create :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @artist.id
312       sign_in @user
313       Picture.stub(:list_by_md5).with(any_args).and_return([@p, @p, @p])
314     end
315     context 'つつがなく終わるとき' do
316       it '実素材モデルにmd5検索を問い合わせている' do
317         Picture.should_receive(:list_by_md5).exactly(1)
318         get :search, :md5 => 'a'*32
319       end
320       it '@picturesにリストを取得している' do
321         get :search, :md5 => 'a'*32
322         assigns(:pictures).should have_at_least(3).items
323       end
324       context 'html形式' do
325         it 'ステータスコード200 OKを返す' do
326           get :search, :md5 => 'a'*32
327           response.should be_success 
328         end
329         it 'searchテンプレートを描画する' do
330           get :search, :md5 => 'a'*32
331           response.should render_template("search")
332         end
333       end
334       context 'json形式' do
335         it 'ステータスコード200 OKを返す' do
336           get :search, :md5 => 'a'*32, :format => :json
337           response.should be_success 
338         end
339         it 'jsonデータを返す' do
340           get :search, :md5 => 'a'*32, :format => :json
341           lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
342         end
343         it 'データがリスト構造になっている' do
344           get :search, :md5 => 'a'*32, :format => :json
345           json = JSON.parse response.body
346           json.should have_at_least(3).items
347         end
348         it 'リストの先頭くらいは実素材っぽいものであって欲しい' do
349           get :search, :md5 => 'a'*32, :format => :json
350           json = JSON.parse response.body
351           json.first.has_key?("ext").should be_true
352           json.first.has_key?("md5").should be_true
353           json.first.has_key?("artist_id").should be_true
354           json.first.has_key?("width").should be_true
355         end
356       end
357     end
358     context 'ユーザ権限がないとき' do
359       before do
360         sign_out @user
361       end
362       context 'html形式' do
363         it 'ステータスコード302 Foundを返す' do
364           get :search, :md5 => 'a'*32
365           response.status.should eq 302
366         end
367         it 'サインインページへ遷移する' do
368           get :search, :md5 => 'a'*32
369           response.should redirect_to '/users/sign_in'
370         end
371       end
372       context 'json形式' do
373         it 'ステータスコード401 Unauthorizedを返す' do
374           get :search, :md5 => 'a'*32, :format => :json
375           response.status.should eq 401
376         end
377         it '応答メッセージにUnauthorizedを返す' do
378           get :search, :md5 => 'a'*32, :format => :json
379           response.message.should match(/Unauthorized/)
380         end
381       end
382     end
383     context 'ユーザ権限はないが管理者権限があるとき' do
384       before do
385         sign_out @user
386         sign_in @admin
387       end
388       it 'ステータスコード200 OKを返す' do
389         get :search, :md5 => 'a'*32
390         response.should be_success 
391       end
392     end
393     context 'ユーザ権限はないが借手権限があるとき' do
394       before do
395         sign_out @user
396         sign_in @demand_user
397       end
398       it 'ステータスコード200 OKを返す' do
399         get :search, :md5 => 'a'*32
400         response.should be_success 
401       end
402     end
403     context 'ユーザだが作家登録していないとき' do
404       before do
405         @author.destroy
406       end
407       it 'ステータスコード200 OKを返す' do
408         get :search, :md5 => 'a'*32
409         response.should be_success 
410       end
411     end
412   end
413   
414 else
415   describe '単体表示に於いて' do
416     before do
417       @p = FactoryGirl.create :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @artist.id
418       sign_in @user
419       Picture.stub(:show).with(@p.id.to_s, [nil, nil, nil]).and_return(@p)
420       Picture.stub(:show).with(@p.id.to_s, [@user, nil, nil]).and_return(@p)
421       Picture.stub(:show).with(@p.id.to_s, [nil, @admin, nil]).and_return(@p)
422       Picture.stub(:show).with(@p.id.to_s, [nil, nil, @demand_user]).and_return(@p)
423     end
424     context 'つつがなく終わるとき as json' do
425       it 'ステータスコード200 OKを返す' do
426         get :show, :id => @p.id, :format => :json
427         response.should be_success
428       end
429       context 'html形式' do
430         it 'showテンプレートを描画する' do
431           get :show, :id => @p.id
432           response.should render_template("show")
433         end
434       end
435       context 'json形式' do
436         it 'jsonデータを返す' do
437           get :show, :id => @p.id, :format => :json
438           lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
439         end
440       end
441     end
442     context 'ユーザ権限がないとき' do
443       before do
444         sign_out @user
445       end
446       it 'ステータスコード200 OKを返す' do
447         get :show, :id => @p.id, :format => :json
448         response.should be_success
449       end
450       context 'html形式' do
451         it 'showテンプレートを描画する' do
452           get :show, :id => @p.id
453           response.should render_template("show")
454         end
455       end
456       context 'json形式' do
457         it 'jsonデータを返す' do
458           get :show, :id => @p.id, :format => :json
459           lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
460         end
461       end
462     end
463   end
464
465   describe 'クレジット表示に於いて' do
466     before do
467       @p = FactoryGirl.create :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @artist.id
468       sign_in @user
469       Picture.stub(:show).with(@p.id.to_s, [nil, nil, nil]).and_return(@p)
470       Picture.stub(:show).with(@p.id.to_s, [@user, nil, nil]).and_return(@p)
471       Picture.stub(:show).with(@p.id.to_s, [nil, @admin, nil]).and_return(@p)
472       Picture.stub(:show).with(@p.id.to_s, [nil, nil, @demand_user]).and_return(@p)
473     end
474     context 'つつがなく終わるとき' do
475       it 'ステータスコード200 OKを返す' do
476         get :credit, :id => @p.id
477         response.should be_success
478       end
479       context 'html形式' do
480         it 'creditテンプレートを描画する' do
481           get :credit, :id => @p.id
482           response.should render_template("credit")
483         end
484       end
485       context 'json形式' do
486         it 'jsonデータを返す' do
487           get :credit, :id => @p.id, :format => :json
488           lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
489         end
490       end
491     end
492     context 'ユーザ権限がないとき' do
493       before do
494         sign_out @user
495       end
496       it 'ステータスコード200 OKを返す' do
497         get :credit, :id => @p.id
498         response.should be_success
499       end
500       context 'html形式' do
501         it 'creditテンプレートを描画する' do
502           get :credit, :id => @p.id
503           response.should render_template("credit")
504         end
505       end
506       context 'json形式' do
507         it 'jsonデータを返す' do
508           get :credit, :id => @p.id, :format => :json
509           lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
510         end
511       end
512     end
513   end
514
515   describe 'md5検索の一覧に於いて' do
516     before do
517       @p = FactoryGirl.create :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @artist.id
518       sign_in @user
519       Picture.stub(:list_by_md5).with(any_args).and_return([@p, @p, @p])
520     end
521     context 'つつがなく終わるとき' do
522       context 'html形式' do
523         it 'ステータスコード200 OKを返す' do
524           get :search, :md5 => 'a'*32
525           response.should be_success 
526         end
527         it 'searchテンプレートを描画する' do
528           get :search, :md5 => 'a'*32
529           response.should render_template("search")
530         end
531       end
532       context 'json形式' do
533         it 'ステータスコード200 OKを返す' do
534           get :search, :md5 => 'a'*32, :format => :json
535           response.should be_success 
536         end
537         it 'jsonデータを返す' do
538           get :search, :md5 => 'a'*32, :format => :json
539           lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
540         end
541       end
542     end
543     context 'ユーザ権限がないとき' do
544       before do
545         sign_out @user
546       end
547       context 'html形式' do
548         it 'ステータスコード200 OKを返す' do
549           get :search, :md5 => 'a'*32
550           response.should be_success 
551         end
552         it 'searchテンプレートを描画する' do
553           get :search, :md5 => 'a'*32
554           response.should render_template("search")
555         end
556       end
557       context 'json形式' do
558         it 'ステータスコード200 OKを返す' do
559           get :search, :md5 => 'a'*32, :format => :json
560           response.should be_success 
561         end
562         it 'jsonデータを返す' do
563           get :search, :md5 => 'a'*32, :format => :json
564           lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
565         end
566       end
567     end
568   end
569   
570 end
571 end