1 # -*- encoding: utf-8 -*-
5 describe PicturesController 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
17 if MagicNumber['run_mode'] == 1
18 describe '単体表示に於いて' do
20 @p = FactoryGirl.create :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @artist.id
22 Picture.stub(:show).with(@p.id.to_s, @author).and_return(@p)
24 context 'つつがなく終わるとき as json' do
25 it 'ステータスコード200 OKを返す' do
26 get :show, :id => @p.id, :format => :json
27 response.should be_success
29 it '実素材モデルに単体取得を問い合わせている' do
30 Picture.should_receive(:show).exactly(1)
31 get :show, :id => @p.id, :format => :json
33 it '@pictureにアレを取得している' do
34 get :show, :id => @p.id, :format => :json
35 assigns(:picture).id.should eq(@p.id)
38 it 'showテンプレートを描画する' do
39 get :show, :id => @p.id
40 response.should render_template("show")
45 get :show, :id => @p.id, :format => :json
46 lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
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/)
55 #画像送信では、send_dataにスタブをおいてテストしたいが、ここに噛ませると
56 #renderが働かず、エラーとなってしまう。そこで、素材のファイル取得部分に
57 #スタブをおいてsend_dataがデータを返す体裁でテストする。
59 it '画像モデルに画像データを問い合わせる' do
60 Picture.any_instance.should_receive(:restore).exactly(1)
61 get :show, :id => @p.id, :format => :png
64 Picture.any_instance.stub(:restore).and_return('aaa')
65 get :show, :id => @p.id, :format => :png
66 response.body.should eq 'aaa'
70 it '画像モデルに画像データを問い合わせる' do
71 Picture.any_instance.should_receive(:restore).exactly(1)
72 get :show, :id => @p.id, :format => :gif
75 Picture.any_instance.stub(:restore).and_return('bbb')
76 get :show, :id => @p.id, :format => :gif
77 response.body.should eq 'bbb'
81 it '画像モデルに画像データを問い合わせる' do
82 Picture.any_instance.should_receive(:restore).exactly(1)
83 get :show, :id => @p.id, :format => :jpeg
86 Picture.any_instance.stub(:restore).and_return('ccc')
87 get :show, :id => @p.id, :format => :jpeg
88 response.body.should eq 'ccc'
92 context '作家権限がないとき' do
97 it 'ステータスコード302 Foundを返す' do
98 get :show, :id => @p.id
99 response.status.should eq 302
101 it 'サインインページへ遷移する' do
102 get :show, :id => @p.id
103 response.body.should redirect_to '/users/sign_in'
107 it 'ステータスコード401 Unauthorizedを返す' do
108 get :show, :id => @p.id, :format => :json
109 response.status.should eq 401
111 it '応答メッセージにUnauthorizedを返す' do
112 get :show, :id => @p.id, :format => :json
113 response.message.should match(/Unauthorized/)
118 context '対象素材がないとき' do
120 Picture.unstub(:show)
123 it '例外404 not_foundを返す' do
126 }.should raise_error(ActiveRecord::RecordNotFound)
130 it '例外404 not_foundを返す' do
132 get :show, :id => 0, :format => :json
133 }.should raise_error(ActiveRecord::RecordNotFound)
137 context '他人の素材を見ようとしたとき' do
139 Picture.stub(:show).and_return(@p)
140 Picture.any_instance.stub(:own?).with(any_args()).and_return(false)
143 it '例外403 forbiddenを返す' do
145 get :show, :id => @p.id
146 }.should raise_error(ActiveRecord::Forbidden)
150 it '例外403 forbiddenを返す' do
152 get :show, :id => @p.id, :format => :json
153 }.should raise_error(ActiveRecord::Forbidden)
160 describe 'クレジット表示に於いて' do
162 @p = FactoryGirl.create :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @artist.id
164 Picture.stub(:show).with(@p.id.to_s, @author).and_return(@p)
166 context 'つつがなく終わるとき' do
167 it 'ステータスコード200 OKを返す' do
168 get :credit, :id => @p.id
169 response.should be_success
171 it '実素材モデルに単体取得を問い合わせている' do
172 Picture.should_receive(:show).exactly(1)
173 get :credit, :id => @p.id
175 it '@pictureにアレを取得している' do
176 get :credit, :id => @p.id
177 assigns(:picture).id.should eq(@p.id)
180 it 'creditテンプレートを描画する' do
181 get :credit, :id => @p.id
182 response.should render_template("credit")
187 get :credit, :id => @p.id, :format => :json
188 lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
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/)
198 context '作家権限がないとき' do
203 it 'ステータスコード302 Foundを返す' do
204 get :credit, :id => @p.id
205 response.status.should eq 302
207 it 'サインインページへ遷移する' do
208 get :credit, :id => @p.id
209 response.body.should redirect_to '/users/sign_in'
213 it 'ステータスコード401 Unauthorizedを返す' do
214 get :credit, :id => @p.id, :format => :json
215 response.status.should eq 401
217 it '応答メッセージにUnauthorizedを返す' do
218 get :credit, :id => @p.id, :format => :json
219 response.message.should match(/Unauthorized/)
224 context '対象素材がないとき' do
226 Picture.unstub(:show)
229 it '例外404 not_foundを返す' do
232 }.should raise_error(ActiveRecord::RecordNotFound)
236 it '例外404 not_foundを返す' do
238 get :show, :id => 0, :format => :json
239 }.should raise_error(ActiveRecord::RecordNotFound)
246 describe 'md5検索の一覧に於いて' do
248 @p = FactoryGirl.create :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @artist.id
250 Picture.stub(:list_by_md5).with(any_args).and_return([@p, @p, @p])
252 context 'つつがなく終わるとき' do
253 it '実素材モデルにmd5検索を問い合わせている' do
254 Picture.should_receive(:list_by_md5).exactly(1)
255 get :search, :md5 => 'a'*32
257 it '@picturesにリストを取得している' do
258 get :search, :md5 => 'a'*32
259 assigns(:pictures).should have_at_least(3).items
262 it 'ステータスコード200 OKを返す' do
263 get :search, :md5 => 'a'*32
264 response.should be_success
266 it 'searchテンプレートを描画する' do
267 get :search, :md5 => 'a'*32
268 response.should render_template("search")
272 it 'ステータスコード200 OKを返す' do
273 get :search, :md5 => 'a'*32, :format => :json
274 response.should be_success
277 get :search, :md5 => 'a'*32, :format => :json
278 lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
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
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
295 context '作家権限がないとき' do
300 it 'ステータスコード302 Foundを返す' do
301 get :search, :md5 => 'a'*32
302 response.status.should eq 302
304 it 'サインインページへ遷移する' do
305 get :search, :md5 => 'a'*32
306 response.should redirect_to '/users/sign_in'
310 it 'ステータスコード401 Unauthorizedを返す' do
311 get :search, :md5 => 'a'*32, :format => :json
312 response.status.should eq 401
314 it '応答メッセージにUnauthorizedを返す' do
315 get :search, :md5 => 'a'*32, :format => :json
316 response.message.should match(/Unauthorized/)
323 describe '単体表示に於いて' do
325 @p = FactoryGirl.create :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @artist.id
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)
330 context 'つつがなく終わるとき as json' do
331 it 'ステータスコード200 OKを返す' do
332 get :show, :id => @p.id, :format => :json
333 response.should be_success
336 it 'showテンプレートを描画する' do
337 get :show, :id => @p.id
338 response.should render_template("show")
343 get :show, :id => @p.id, :format => :json
344 lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
348 context '作家権限がないとき' do
352 it 'ステータスコード200 OKを返す' do
353 get :show, :id => @p.id, :format => :json
354 response.should be_success
357 it 'showテンプレートを描画する' do
358 get :show, :id => @p.id
359 response.should render_template("show")
364 get :show, :id => @p.id, :format => :json
365 lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
371 describe 'クレジット表示に於いて' do
373 @p = FactoryGirl.create :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @artist.id
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)
378 context 'つつがなく終わるとき' do
379 it 'ステータスコード200 OKを返す' do
380 get :credit, :id => @p.id
381 response.should be_success
384 it 'creditテンプレートを描画する' do
385 get :credit, :id => @p.id
386 response.should render_template("credit")
391 get :credit, :id => @p.id, :format => :json
392 lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
396 context '作家権限がないとき' do
400 it 'ステータスコード200 OKを返す' do
401 get :credit, :id => @p.id
402 response.should be_success
405 it 'creditテンプレートを描画する' do
406 get :credit, :id => @p.id
407 response.should render_template("credit")
412 get :credit, :id => @p.id, :format => :json
413 lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
419 describe 'md5検索の一覧に於いて' do
421 @p = FactoryGirl.create :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @artist.id
423 Picture.stub(:list_by_md5).with(any_args).and_return([@p, @p, @p])
425 context 'つつがなく終わるとき' do
427 it 'ステータスコード200 OKを返す' do
428 get :search, :md5 => 'a'*32
429 response.should be_success
431 it 'searchテンプレートを描画する' do
432 get :search, :md5 => 'a'*32
433 response.should render_template("search")
437 it 'ステータスコード200 OKを返す' do
438 get :search, :md5 => 'a'*32, :format => :json
439 response.should be_success
442 get :search, :md5 => 'a'*32, :format => :json
443 lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
447 context '作家権限がないとき' do
452 it 'ステータスコード200 OKを返す' do
453 get :search, :md5 => 'a'*32
454 response.should be_success
456 it 'searchテンプレートを描画する' do
457 get :search, :md5 => 'a'*32
458 response.should render_template("search")
462 it 'ステータスコード200 OKを返す' do
463 get :search, :md5 => 'a'*32, :format => :json
464 response.should be_success
467 get :search, :md5 => 'a'*32, :format => :json
468 lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)