OSDN Git Service

t#30137:fix authenticate
[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, [@author, 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 =begin
141     context '対象素材がないとき' do
142       before do
143         Picture.unstub(:show)
144       end
145       context 'html形式' do
146         it '例外404 not_foundを返す' do
147           lambda{
148             get :show, :id => 0
149           }.should raise_error(ActiveRecord::RecordNotFound)
150         end
151       end
152       context 'json形式' do
153         it '例外404 not_foundを返す' do
154           lambda{ 
155             get :show, :id => 0, :format => :json
156           }.should raise_error(ActiveRecord::RecordNotFound)
157         end
158       end
159     end
160     context '他人の素材を見ようとしたとき' do
161       before do
162         Picture.stub(:show).and_return(@p)
163         Picture.any_instance.stub(:own?).with(any_args()).and_return(false)
164       end
165       context 'html形式' do
166         it '例外403 forbiddenを返す' do
167           lambda{
168             get :show, :id => @p.id
169           }.should raise_error(ActiveRecord::Forbidden)
170         end
171       end
172       context 'json形式' do
173         it '例外403 forbiddenを返す' do
174           lambda{
175             get :show, :id => @p.id, :format => :json
176           }.should raise_error(ActiveRecord::Forbidden)
177         end
178       end
179     end
180 =end
181   end
182
183   describe 'クレジット表示に於いて' do
184     before do
185       @p = FactoryGirl.create :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @artist.id
186       sign_in @user
187       Picture.stub(:show).with(@p.id.to_s, [@author, nil, nil]).and_return(@p)
188       Picture.stub(:show).with(@p.id.to_s, [nil, @admin, nil]).and_return(@p)
189       Picture.stub(:show).with(@p.id.to_s, [nil, nil, @demand_user]).and_return(@p)
190     end
191     context 'つつがなく終わるとき' do
192       it 'ステータスコード200 OKを返す' do
193         get :credit, :id => @p.id
194         response.should be_success
195       end
196       it '実素材モデルに単体取得を問い合わせている' do
197         Picture.should_receive(:show).exactly(1)
198         get :credit, :id => @p.id
199       end
200       it '@pictureにアレを取得している' do
201         get :credit, :id => @p.id
202         assigns(:picture).id.should eq(@p.id)
203       end
204       context 'html形式' do
205         it 'creditテンプレートを描画する' do
206           get :credit, :id => @p.id
207           response.should render_template("credit")
208         end
209       end
210       context 'json形式' do
211         it 'jsonデータを返す' do
212           get :credit, :id => @p.id, :format => :json
213           lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
214         end
215         it 'データがアレになっている' do
216           get :credit, :id => @p.id, :format => :json
217           json = JSON.parse response.body
218           json["revision"].should_not be_nil
219           json["ext"].should match(/png/)
220         end
221       end
222     end
223     context '作家権限がないとき' do
224       before do
225         sign_out @user
226       end
227       context 'html形式' do
228         it 'ステータスコード302 Foundを返す' do
229           get :credit, :id => @p.id
230           response.status.should eq 302
231         end
232         it 'サインインページへ遷移する' do
233           get :credit, :id => @p.id
234           response.body.should redirect_to '/users/sign_in'
235         end
236       end
237       context 'json形式' do
238         it 'ステータスコード401 Unauthorizedを返す' do
239           get :credit, :id => @p.id, :format => :json
240           response.status.should eq 401
241         end
242         it '応答メッセージにUnauthorizedを返す' do
243           get :credit, :id => @p.id, :format => :json
244           response.message.should match(/Unauthorized/)
245         end
246       end
247     end
248     context '作家権限はないが管理者権限があるとき' do
249       before do
250         sign_out @user
251         sign_in @admin
252       end
253       it 'ステータスコード200 OKを返す' do
254         get :credit, :id => @p.id
255         response.should be_success 
256       end
257     end
258     context '作家権限はないが借手権限があるとき' do
259       before do
260         sign_out @user
261         sign_in @demand_user
262       end
263       it 'ステータスコード200 OKを返す' do
264         get :credit, :id => @p.id
265         response.should be_success 
266       end
267     end
268 =begin
269     context '対象素材がないとき' do
270       before do
271         Picture.unstub(:show)
272       end
273       context 'html形式' do
274         it '例外404 not_foundを返す' do
275           lambda{
276             get :show, :id => 0
277           }.should raise_error(ActiveRecord::RecordNotFound)
278         end
279       end
280       context 'json形式' do
281         it '例外404 not_foundを返す' do
282           lambda{ 
283             get :show, :id => 0, :format => :json
284           }.should raise_error(ActiveRecord::RecordNotFound)
285         end
286       end
287     end
288 =end
289   end
290
291   describe 'md5検索の一覧に於いて' do
292     before do
293       @p = FactoryGirl.create :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @artist.id
294       sign_in @user
295       Picture.stub(:list_by_md5).with(any_args).and_return([@p, @p, @p])
296     end
297     context 'つつがなく終わるとき' do
298       it '実素材モデルにmd5検索を問い合わせている' do
299         Picture.should_receive(:list_by_md5).exactly(1)
300         get :search, :md5 => 'a'*32
301       end
302       it '@picturesにリストを取得している' do
303         get :search, :md5 => 'a'*32
304         assigns(:pictures).should have_at_least(3).items
305       end
306       context 'html形式' do
307         it 'ステータスコード200 OKを返す' do
308           get :search, :md5 => 'a'*32
309           response.should be_success 
310         end
311         it 'searchテンプレートを描画する' do
312           get :search, :md5 => 'a'*32
313           response.should render_template("search")
314         end
315       end
316       context 'json形式' do
317         it 'ステータスコード200 OKを返す' do
318           get :search, :md5 => 'a'*32, :format => :json
319           response.should be_success 
320         end
321         it 'jsonデータを返す' do
322           get :search, :md5 => 'a'*32, :format => :json
323           lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
324         end
325         it 'データがリスト構造になっている' do
326           get :search, :md5 => 'a'*32, :format => :json
327           json = JSON.parse response.body
328           json.should have_at_least(3).items
329         end
330         it 'リストの先頭くらいは実素材っぽいものであって欲しい' do
331           get :search, :md5 => 'a'*32, :format => :json
332           json = JSON.parse response.body
333           json.first.has_key?("ext").should be_true
334           json.first.has_key?("md5").should be_true
335           json.first.has_key?("artist_id").should be_true
336           json.first.has_key?("width").should be_true
337         end
338       end
339     end
340     context '作家権限がないとき' do
341       before do
342         sign_out @user
343       end
344       context 'html形式' do
345         it 'ステータスコード302 Foundを返す' do
346           get :search, :md5 => 'a'*32
347           response.status.should eq 302
348         end
349         it 'サインインページへ遷移する' do
350           get :search, :md5 => 'a'*32
351           response.should redirect_to '/users/sign_in'
352         end
353       end
354       context 'json形式' do
355         it 'ステータスコード401 Unauthorizedを返す' do
356           get :search, :md5 => 'a'*32, :format => :json
357           response.status.should eq 401
358         end
359         it '応答メッセージにUnauthorizedを返す' do
360           get :search, :md5 => 'a'*32, :format => :json
361           response.message.should match(/Unauthorized/)
362         end
363       end
364     end
365     context '作家権限はないが管理者権限があるとき' do
366       before do
367         sign_out @user
368         sign_in @admin
369       end
370       it 'ステータスコード200 OKを返す' do
371         get :search, :md5 => 'a'*32
372         response.should be_success 
373       end
374     end
375     context '作家権限はないが借手権限があるとき' do
376       before do
377         sign_out @user
378         sign_in @demand_user
379       end
380       it 'ステータスコード200 OKを返す' do
381         get :search, :md5 => 'a'*32
382         response.should be_success 
383       end
384     end
385   end
386   
387 else
388   describe '単体表示に於いて' do
389     before do
390       @p = FactoryGirl.create :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @artist.id
391       sign_in @user
392       Picture.stub(:show).with(@p.id.to_s, @author).and_return(@p)
393       Picture.stub(:show).with(@p.id.to_s, nil).and_return(@p)
394     end
395     context 'つつがなく終わるとき as json' do
396       it 'ステータスコード200 OKを返す' do
397         get :show, :id => @p.id, :format => :json
398         response.should be_success
399       end
400       context 'html形式' do
401         it 'showテンプレートを描画する' do
402           get :show, :id => @p.id
403           response.should render_template("show")
404         end
405       end
406       context 'json形式' do
407         it 'jsonデータを返す' do
408           get :show, :id => @p.id, :format => :json
409           lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
410         end
411       end
412     end
413     context '作家権限がないとき' do
414       before do
415         sign_out @user
416       end
417       it 'ステータスコード200 OKを返す' do
418         get :show, :id => @p.id, :format => :json
419         response.should be_success
420       end
421       context 'html形式' do
422         it 'showテンプレートを描画する' do
423           get :show, :id => @p.id
424           response.should render_template("show")
425         end
426       end
427       context 'json形式' do
428         it 'jsonデータを返す' do
429           get :show, :id => @p.id, :format => :json
430           lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
431         end
432       end
433     end
434   end
435
436   describe 'クレジット表示に於いて' do
437     before do
438       @p = FactoryGirl.create :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @artist.id
439       sign_in @user
440       Picture.stub(:show).with(@p.id.to_s, @author).and_return(@p)
441       Picture.stub(:show).with(@p.id.to_s, nil).and_return(@p)
442     end
443     context 'つつがなく終わるとき' do
444       it 'ステータスコード200 OKを返す' do
445         get :credit, :id => @p.id
446         response.should be_success
447       end
448       context 'html形式' do
449         it 'creditテンプレートを描画する' do
450           get :credit, :id => @p.id
451           response.should render_template("credit")
452         end
453       end
454       context 'json形式' do
455         it 'jsonデータを返す' do
456           get :credit, :id => @p.id, :format => :json
457           lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
458         end
459       end
460     end
461     context '作家権限がないとき' do
462       before do
463         sign_out @user
464       end
465       it 'ステータスコード200 OKを返す' do
466         get :credit, :id => @p.id
467         response.should be_success
468       end
469       context 'html形式' do
470         it 'creditテンプレートを描画する' do
471           get :credit, :id => @p.id
472           response.should render_template("credit")
473         end
474       end
475       context 'json形式' do
476         it 'jsonデータを返す' do
477           get :credit, :id => @p.id, :format => :json
478           lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
479         end
480       end
481     end
482   end
483
484   describe 'md5検索の一覧に於いて' do
485     before do
486       @p = FactoryGirl.create :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @artist.id
487       sign_in @user
488       Picture.stub(:list_by_md5).with(any_args).and_return([@p, @p, @p])
489     end
490     context 'つつがなく終わるとき' do
491       context 'html形式' do
492         it 'ステータスコード200 OKを返す' do
493           get :search, :md5 => 'a'*32
494           response.should be_success 
495         end
496         it 'searchテンプレートを描画する' do
497           get :search, :md5 => 'a'*32
498           response.should render_template("search")
499         end
500       end
501       context 'json形式' do
502         it 'ステータスコード200 OKを返す' do
503           get :search, :md5 => 'a'*32, :format => :json
504           response.should be_success 
505         end
506         it 'jsonデータを返す' do
507           get :search, :md5 => 'a'*32, :format => :json
508           lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
509         end
510       end
511     end
512     context '作家権限がないとき' do
513       before do
514         sign_out @user
515       end
516       context 'html形式' do
517         it 'ステータスコード200 OKを返す' do
518           get :search, :md5 => 'a'*32
519           response.should be_success 
520         end
521         it 'searchテンプレートを描画する' do
522           get :search, :md5 => 'a'*32
523           response.should render_template("search")
524         end
525       end
526       context 'json形式' do
527         it 'ステータスコード200 OKを返す' do
528           get :search, :md5 => 'a'*32, :format => :json
529           response.should be_success 
530         end
531         it 'jsonデータを返す' do
532           get :search, :md5 => 'a'*32, :format => :json
533           lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
534         end
535       end
536     end
537   end
538   
539 end
540 end