OSDN Git Service

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