1 # -*- encoding: utf-8 -*-
5 describe PicturesController 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
18 if MagicNumber['run_mode'] == 1
19 describe '単体表示に於いて' do
21 @p = FactoryGirl.create :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @artist.id
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)
27 context 'つつがなく終わるとき as json' do
28 it 'ステータスコード200 OKを返す' do
29 get :show, :id => @p.id, :format => :json
30 response.should be_success
32 it '実素材モデルに単体取得を問い合わせている' do
33 Picture.should_receive(:show).exactly(1)
34 get :show, :id => @p.id, :format => :json
36 it '@pictureにアレを取得している' do
37 get :show, :id => @p.id, :format => :json
38 assigns(:picture).id.should eq(@p.id)
41 it 'showテンプレートを描画する' do
42 get :show, :id => @p.id
43 response.should render_template("show")
48 get :show, :id => @p.id, :format => :json
49 lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
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/)
58 #画像送信では、send_dataにスタブをおいてテストしたいが、ここに噛ませると
59 #renderが働かず、エラーとなってしまう。そこで、素材のファイル取得部分に
60 #スタブをおいてsend_dataがデータを返す体裁でテストする。
62 it '画像モデルに画像データを問い合わせる' do
63 Picture.any_instance.should_receive(:restore).exactly(1)
64 get :show, :id => @p.id, :format => :png
67 Picture.any_instance.stub(:restore).and_return('aaa')
68 get :show, :id => @p.id, :format => :png
69 response.body.should eq 'aaa'
73 it '画像モデルに画像データを問い合わせる' do
74 Picture.any_instance.should_receive(:restore).exactly(1)
75 get :show, :id => @p.id, :format => :gif
78 Picture.any_instance.stub(:restore).and_return('bbb')
79 get :show, :id => @p.id, :format => :gif
80 response.body.should eq 'bbb'
84 it '画像モデルに画像データを問い合わせる' do
85 Picture.any_instance.should_receive(:restore).exactly(1)
86 get :show, :id => @p.id, :format => :jpeg
89 Picture.any_instance.stub(:restore).and_return('ccc')
90 get :show, :id => @p.id, :format => :jpeg
91 response.body.should eq 'ccc'
95 context '作家権限がないとき' do
100 it 'ステータスコード302 Foundを返す' do
101 get :show, :id => @p.id
102 response.status.should eq 302
104 it 'サインインページへ遷移する' do
105 get :show, :id => @p.id
106 response.body.should redirect_to '/users/sign_in'
110 it 'ステータスコード401 Unauthorizedを返す' do
111 get :show, :id => @p.id, :format => :json
112 response.status.should eq 401
114 it '応答メッセージにUnauthorizedを返す' do
115 get :show, :id => @p.id, :format => :json
116 response.message.should match(/Unauthorized/)
120 context '作家権限はないが管理者権限があるとき' do
125 it 'ステータスコード200 OKを返す' do
126 get :show, :id => @p.id
127 response.should be_success
130 context '作家権限はないが借手権限があるとき' do
135 it 'ステータスコード200 OKを返す' do
136 get :show, :id => @p.id
137 response.should be_success
141 context '対象素材がないとき' do
143 Picture.unstub(:show)
146 it '例外404 not_foundを返す' do
149 }.should raise_error(ActiveRecord::RecordNotFound)
153 it '例外404 not_foundを返す' do
155 get :show, :id => 0, :format => :json
156 }.should raise_error(ActiveRecord::RecordNotFound)
160 context '他人の素材を見ようとしたとき' do
162 Picture.stub(:show).and_return(@p)
163 Picture.any_instance.stub(:own?).with(any_args()).and_return(false)
166 it '例外403 forbiddenを返す' do
168 get :show, :id => @p.id
169 }.should raise_error(ActiveRecord::Forbidden)
173 it '例外403 forbiddenを返す' do
175 get :show, :id => @p.id, :format => :json
176 }.should raise_error(ActiveRecord::Forbidden)
183 describe 'クレジット表示に於いて' do
185 @p = FactoryGirl.create :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @artist.id
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)
191 context 'つつがなく終わるとき' do
192 it 'ステータスコード200 OKを返す' do
193 get :credit, :id => @p.id
194 response.should be_success
196 it '実素材モデルに単体取得を問い合わせている' do
197 Picture.should_receive(:show).exactly(1)
198 get :credit, :id => @p.id
200 it '@pictureにアレを取得している' do
201 get :credit, :id => @p.id
202 assigns(:picture).id.should eq(@p.id)
205 it 'creditテンプレートを描画する' do
206 get :credit, :id => @p.id
207 response.should render_template("credit")
212 get :credit, :id => @p.id, :format => :json
213 lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
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/)
223 context '作家権限がないとき' do
228 it 'ステータスコード302 Foundを返す' do
229 get :credit, :id => @p.id
230 response.status.should eq 302
232 it 'サインインページへ遷移する' do
233 get :credit, :id => @p.id
234 response.body.should redirect_to '/users/sign_in'
238 it 'ステータスコード401 Unauthorizedを返す' do
239 get :credit, :id => @p.id, :format => :json
240 response.status.should eq 401
242 it '応答メッセージにUnauthorizedを返す' do
243 get :credit, :id => @p.id, :format => :json
244 response.message.should match(/Unauthorized/)
248 context '作家権限はないが管理者権限があるとき' do
253 it 'ステータスコード200 OKを返す' do
254 get :credit, :id => @p.id
255 response.should be_success
258 context '作家権限はないが借手権限があるとき' do
263 it 'ステータスコード200 OKを返す' do
264 get :credit, :id => @p.id
265 response.should be_success
269 context '対象素材がないとき' do
271 Picture.unstub(:show)
274 it '例外404 not_foundを返す' do
277 }.should raise_error(ActiveRecord::RecordNotFound)
281 it '例外404 not_foundを返す' do
283 get :show, :id => 0, :format => :json
284 }.should raise_error(ActiveRecord::RecordNotFound)
291 describe 'md5検索の一覧に於いて' do
293 @p = FactoryGirl.create :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @artist.id
295 Picture.stub(:list_by_md5).with(any_args).and_return([@p, @p, @p])
297 context 'つつがなく終わるとき' do
298 it '実素材モデルにmd5検索を問い合わせている' do
299 Picture.should_receive(:list_by_md5).exactly(1)
300 get :search, :md5 => 'a'*32
302 it '@picturesにリストを取得している' do
303 get :search, :md5 => 'a'*32
304 assigns(:pictures).should have_at_least(3).items
307 it 'ステータスコード200 OKを返す' do
308 get :search, :md5 => 'a'*32
309 response.should be_success
311 it 'searchテンプレートを描画する' do
312 get :search, :md5 => 'a'*32
313 response.should render_template("search")
317 it 'ステータスコード200 OKを返す' do
318 get :search, :md5 => 'a'*32, :format => :json
319 response.should be_success
322 get :search, :md5 => 'a'*32, :format => :json
323 lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
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
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
340 context '作家権限がないとき' do
345 it 'ステータスコード302 Foundを返す' do
346 get :search, :md5 => 'a'*32
347 response.status.should eq 302
349 it 'サインインページへ遷移する' do
350 get :search, :md5 => 'a'*32
351 response.should redirect_to '/users/sign_in'
355 it 'ステータスコード401 Unauthorizedを返す' do
356 get :search, :md5 => 'a'*32, :format => :json
357 response.status.should eq 401
359 it '応答メッセージにUnauthorizedを返す' do
360 get :search, :md5 => 'a'*32, :format => :json
361 response.message.should match(/Unauthorized/)
365 context '作家権限はないが管理者権限があるとき' do
370 it 'ステータスコード200 OKを返す' do
371 get :search, :md5 => 'a'*32
372 response.should be_success
375 context '作家権限はないが借手権限があるとき' do
380 it 'ステータスコード200 OKを返す' do
381 get :search, :md5 => 'a'*32
382 response.should be_success
388 describe '単体表示に於いて' do
390 @p = FactoryGirl.create :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @artist.id
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)
395 context 'つつがなく終わるとき as json' do
396 it 'ステータスコード200 OKを返す' do
397 get :show, :id => @p.id, :format => :json
398 response.should be_success
401 it 'showテンプレートを描画する' do
402 get :show, :id => @p.id
403 response.should render_template("show")
408 get :show, :id => @p.id, :format => :json
409 lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
413 context '作家権限がないとき' do
417 it 'ステータスコード200 OKを返す' do
418 get :show, :id => @p.id, :format => :json
419 response.should be_success
422 it 'showテンプレートを描画する' do
423 get :show, :id => @p.id
424 response.should render_template("show")
429 get :show, :id => @p.id, :format => :json
430 lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
436 describe 'クレジット表示に於いて' do
438 @p = FactoryGirl.create :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @artist.id
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)
443 context 'つつがなく終わるとき' do
444 it 'ステータスコード200 OKを返す' do
445 get :credit, :id => @p.id
446 response.should be_success
449 it 'creditテンプレートを描画する' do
450 get :credit, :id => @p.id
451 response.should render_template("credit")
456 get :credit, :id => @p.id, :format => :json
457 lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
461 context '作家権限がないとき' do
465 it 'ステータスコード200 OKを返す' do
466 get :credit, :id => @p.id
467 response.should be_success
470 it 'creditテンプレートを描画する' do
471 get :credit, :id => @p.id
472 response.should render_template("credit")
477 get :credit, :id => @p.id, :format => :json
478 lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
484 describe 'md5検索の一覧に於いて' do
486 @p = FactoryGirl.create :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @artist.id
488 Picture.stub(:list_by_md5).with(any_args).and_return([@p, @p, @p])
490 context 'つつがなく終わるとき' do
492 it 'ステータスコード200 OKを返す' do
493 get :search, :md5 => 'a'*32
494 response.should be_success
496 it 'searchテンプレートを描画する' do
497 get :search, :md5 => 'a'*32
498 response.should render_template("search")
502 it 'ステータスコード200 OKを返す' do
503 get :search, :md5 => 'a'*32, :format => :json
504 response.should be_success
507 get :search, :md5 => 'a'*32, :format => :json
508 lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
512 context '作家権限がないとき' do
517 it 'ステータスコード200 OKを返す' do
518 get :search, :md5 => 'a'*32
519 response.should be_success
521 it 'searchテンプレートを描画する' do
522 get :search, :md5 => 'a'*32
523 response.should render_template("search")
527 it 'ステータスコード200 OKを返す' do
528 get :search, :md5 => 'a'*32, :format => :json
529 response.should be_success
532 get :search, :md5 => 'a'*32, :format => :json
533 lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)