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, [@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)
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
140 context 'ユーザだが作家登録していないとき' do
144 it 'ステータスコード200 OKを返す' do
145 get :show, :id => @p.id
146 response.should be_success
150 context '対象素材がないとき' do
152 Picture.unstub(:show)
155 it '例外404 not_foundを返す' do
158 }.should raise_error(ActiveRecord::RecordNotFound)
162 it '例外404 not_foundを返す' do
164 get :show, :id => 0, :format => :json
165 }.should raise_error(ActiveRecord::RecordNotFound)
169 context '他人の素材を見ようとしたとき' do
171 Picture.stub(:show).and_return(@p)
172 Picture.any_instance.stub(:own?).with(any_args()).and_return(false)
175 it '例外403 forbiddenを返す' do
177 get :show, :id => @p.id
178 }.should raise_error(ActiveRecord::Forbidden)
182 it '例外403 forbiddenを返す' do
184 get :show, :id => @p.id, :format => :json
185 }.should raise_error(ActiveRecord::Forbidden)
192 describe 'クレジット表示に於いて' do
194 @p = FactoryGirl.create :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @artist.id
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)
200 context 'つつがなく終わるとき' do
201 it 'ステータスコード200 OKを返す' do
202 get :credit, :id => @p.id
203 response.should be_success
205 it '実素材モデルに単体取得を問い合わせている' do
206 Picture.should_receive(:show).exactly(1)
207 get :credit, :id => @p.id
209 it '@pictureにアレを取得している' do
210 get :credit, :id => @p.id
211 assigns(:picture).id.should eq(@p.id)
214 it 'creditテンプレートを描画する' do
215 get :credit, :id => @p.id
216 response.should render_template("credit")
221 get :credit, :id => @p.id, :format => :json
222 lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
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/)
232 context 'ユーザ権限がないとき' do
237 it 'ステータスコード302 Foundを返す' do
238 get :credit, :id => @p.id
239 response.status.should eq 302
241 it 'サインインページへ遷移する' do
242 get :credit, :id => @p.id
243 response.body.should redirect_to '/users/sign_in'
247 it 'ステータスコード401 Unauthorizedを返す' do
248 get :credit, :id => @p.id, :format => :json
249 response.status.should eq 401
251 it '応答メッセージにUnauthorizedを返す' do
252 get :credit, :id => @p.id, :format => :json
253 response.message.should match(/Unauthorized/)
257 context 'ユーザ権限はないが管理者権限があるとき' do
262 it 'ステータスコード200 OKを返す' do
263 get :credit, :id => @p.id
264 response.should be_success
267 context 'ユーザ権限はないが借手権限があるとき' do
272 it 'ステータスコード200 OKを返す' do
273 get :credit, :id => @p.id
274 response.should be_success
277 context 'ユーザだが作家登録していないとき' do
281 it 'ステータスコード200 OKを返す' do
282 get :credit, :id => @p.id
283 response.should be_success
287 context '対象素材がないとき' do
289 Picture.unstub(:show)
292 it '例外404 not_foundを返す' do
295 }.should raise_error(ActiveRecord::RecordNotFound)
299 it '例外404 not_foundを返す' do
301 get :show, :id => 0, :format => :json
302 }.should raise_error(ActiveRecord::RecordNotFound)
309 describe 'md5検索の一覧に於いて' do
311 @p = FactoryGirl.create :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @artist.id
313 Picture.stub(:list_by_md5).with(any_args).and_return([@p, @p, @p])
315 context 'つつがなく終わるとき' do
316 it '実素材モデルにmd5検索を問い合わせている' do
317 Picture.should_receive(:list_by_md5).exactly(1)
318 get :search, :md5 => 'a'*32
320 it '@picturesにリストを取得している' do
321 get :search, :md5 => 'a'*32
322 assigns(:pictures).should have_at_least(3).items
325 it 'ステータスコード200 OKを返す' do
326 get :search, :md5 => 'a'*32
327 response.should be_success
329 it 'searchテンプレートを描画する' do
330 get :search, :md5 => 'a'*32
331 response.should render_template("search")
335 it 'ステータスコード200 OKを返す' do
336 get :search, :md5 => 'a'*32, :format => :json
337 response.should be_success
340 get :search, :md5 => 'a'*32, :format => :json
341 lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
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
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
358 context 'ユーザ権限がないとき' do
363 it 'ステータスコード302 Foundを返す' do
364 get :search, :md5 => 'a'*32
365 response.status.should eq 302
367 it 'サインインページへ遷移する' do
368 get :search, :md5 => 'a'*32
369 response.should redirect_to '/users/sign_in'
373 it 'ステータスコード401 Unauthorizedを返す' do
374 get :search, :md5 => 'a'*32, :format => :json
375 response.status.should eq 401
377 it '応答メッセージにUnauthorizedを返す' do
378 get :search, :md5 => 'a'*32, :format => :json
379 response.message.should match(/Unauthorized/)
383 context 'ユーザ権限はないが管理者権限があるとき' do
388 it 'ステータスコード200 OKを返す' do
389 get :search, :md5 => 'a'*32
390 response.should be_success
393 context 'ユーザ権限はないが借手権限があるとき' do
398 it 'ステータスコード200 OKを返す' do
399 get :search, :md5 => 'a'*32
400 response.should be_success
403 context 'ユーザだが作家登録していないとき' do
407 it 'ステータスコード200 OKを返す' do
408 get :search, :md5 => 'a'*32
409 response.should be_success
415 describe '単体表示に於いて' do
417 @p = FactoryGirl.create :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @artist.id
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)
424 context 'つつがなく終わるとき as json' do
425 it 'ステータスコード200 OKを返す' do
426 get :show, :id => @p.id, :format => :json
427 response.should be_success
430 it 'showテンプレートを描画する' do
431 get :show, :id => @p.id
432 response.should render_template("show")
437 get :show, :id => @p.id, :format => :json
438 lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
442 context 'ユーザ権限がないとき' do
446 it 'ステータスコード200 OKを返す' do
447 get :show, :id => @p.id, :format => :json
448 response.should be_success
451 it 'showテンプレートを描画する' do
452 get :show, :id => @p.id
453 response.should render_template("show")
458 get :show, :id => @p.id, :format => :json
459 lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
465 describe 'クレジット表示に於いて' do
467 @p = FactoryGirl.create :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @artist.id
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)
474 context 'つつがなく終わるとき' do
475 it 'ステータスコード200 OKを返す' do
476 get :credit, :id => @p.id
477 response.should be_success
480 it 'creditテンプレートを描画する' do
481 get :credit, :id => @p.id
482 response.should render_template("credit")
487 get :credit, :id => @p.id, :format => :json
488 lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
492 context 'ユーザ権限がないとき' do
496 it 'ステータスコード200 OKを返す' do
497 get :credit, :id => @p.id
498 response.should be_success
501 it 'creditテンプレートを描画する' do
502 get :credit, :id => @p.id
503 response.should render_template("credit")
508 get :credit, :id => @p.id, :format => :json
509 lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
515 describe 'md5検索の一覧に於いて' do
517 @p = FactoryGirl.create :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @artist.id
519 Picture.stub(:list_by_md5).with(any_args).and_return([@p, @p, @p])
521 context 'つつがなく終わるとき' do
523 it 'ステータスコード200 OKを返す' do
524 get :search, :md5 => 'a'*32
525 response.should be_success
527 it 'searchテンプレートを描画する' do
528 get :search, :md5 => 'a'*32
529 response.should render_template("search")
533 it 'ステータスコード200 OKを返す' do
534 get :search, :md5 => 'a'*32, :format => :json
535 response.should be_success
538 get :search, :md5 => 'a'*32, :format => :json
539 lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
543 context 'ユーザ権限がないとき' do
548 it 'ステータスコード200 OKを返す' do
549 get :search, :md5 => 'a'*32
550 response.should be_success
552 it 'searchテンプレートを描画する' do
553 get :search, :md5 => 'a'*32
554 response.should render_template("search")
558 it 'ステータスコード200 OKを返す' do
559 get :search, :md5 => 'a'*32, :format => :json
560 response.should be_success
563 get :search, :md5 => 'a'*32, :format => :json
564 lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)