1 # -*- encoding: utf-8 -*-
4 describe OriginalPicturesController do
7 @user = Factory( :user_yas)
9 @artist = Factory :artist_yas, :author_id => @author.id
10 @license = Factory :license
13 describe '一覧表示に於いて' do
15 @op = Factory :original_picture, :artist_id => @artist.id , :license_id => @license.id
17 OriginalPicture.stub(:list).and_return([@op, @op, @op])
19 context 'パラメータpageについて' do
21 get :index, :page => 5
22 assigns(:page).should eq 5
24 it '省略されると@pageに1値が入る' do
26 assigns(:page).should eq 1
28 it '与えられたpage_sizeがセットされている' do
29 get :index, :page_size => 15
30 assigns(:page_size).should eq 15
32 it '省略されると@page_sizeにデフォルト値が入る' do
34 assigns(:page_size).should eq OriginalPicture.default_page_size
\r
36 it '最大を超えると@page_sizeにデフォルト最大値が入る' do
37 get :index, :page_size => 1500
38 assigns(:page_size).should eq OriginalPicture.max_page_size
40 it '不正な値が入ると@page_sizeにデフォルト最大値が入る' do
41 get :index, :page_size => 0
42 assigns(:page_size).should eq OriginalPicture.default_page_size
45 context 'つつがなく終わるとき' do
46 it 'ステータスコード200 OKを返す' do
48 response.should be_success
50 it '原画モデルに一覧を問い合わせている' do
51 OriginalPicture.should_receive(:list).exactly(1)
\r
54 it '@original_picturesにリストを取得している' do
56 assigns(:original_pictures).should have_at_least(3).items
59 it 'indexテンプレートを描画する' do
61 response.should render_template("index")
66 get :index, :format => :json
67 lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
69 it 'データがリスト構造になっている' do
70 get :index, :format => :json
71 json = JSON.parse response.body
72 json.should have_at_least(3).items
74 it 'リストの先頭くらいは原画っぽいものであって欲しい' do
75 get :index, :format => :json
76 json = JSON.parse response.body
77 json.first.has_key?("ext").should be_true
81 context '作家権限がないとき' do
86 it 'ステータスコード302 Foundを返す' do
88 response.status.should eq 302
92 response.should redirect_to '/users/sign_in'
96 it 'ステータスコード401 Unauthorizedを返す' do
97 get :index, :format => :json
98 response.status.should eq 401
100 it '応答メッセージにUnauthorizedを返す' do
101 get :index, :format => :json
102 response.message.should match(/Unauthorized/)
106 context '作家が絵師でないとき' do
108 Author.any_instance.stub(:artist?).and_return(false)
111 it 'ステータスコード302 Foundを返す' do
113 response.status.should eq 302
117 response.should redirect_to new_artist_path
121 it '例外403 forbiddenを返す' do
123 get :index, :format => :json
124 }.should raise_error(ActiveRecord::Forbidden)
130 describe '単体表示に於いて' do
132 @pic = Factory :original_picture, :artist_id => @artist.id , :license_id => @license.id
134 OriginalPicture.stub(:show).and_return(@pic)
136 context 'つつがなく終わるとき' do
137 it 'ステータスコード200 OKを返す' do
138 get :show, :id => @pic.id
139 response.should be_success
141 it '原画モデルに単体取得を問い合わせている' do
142 OriginalPicture.should_receive(:show).exactly(1)
\r
145 it '@original_pictureにアレを取得している' do
146 get :show, :id => @pic.id
147 assigns(:original_picture).id.should eq(@pic.id)
150 it 'showテンプレートを描画する' do
151 get :show, :id => @pic.id
152 response.should render_template("show")
157 get :show, :id => @pic.id, :format => :json
158 lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
161 get :show, :id => @pic.id, :format => :json
162 json = JSON.parse response.body
163 json["ext"].should match(/png/)
166 #画像送信では、send_dataにスタブをおいてテストしたいが、ここに噛ませると
167 #renderが働かず、エラーとなってしまう。そこで、原画のファイル取得部分に
168 #スタブをおいてsend_dataがデータを返す体裁でテストする。
170 it '画像モデルに画像データを問い合わせる' do
171 OriginalPicture.any_instance.should_receive(:restore).exactly(1)
\r
172 get :show, :id => @pic.id, :format => :png
175 OriginalPicture.any_instance.stub(:restore).and_return('aaa')
\r
176 get :show, :id => @pic.id, :format => :png
177 response.body.should eq 'aaa'
181 it '画像モデルに画像データを問い合わせる' do
182 OriginalPicture.any_instance.should_receive(:restore).exactly(1)
\r
183 get :show, :id => @pic.id, :format => :gif
186 OriginalPicture.any_instance.stub(:restore).and_return('bbb')
\r
187 get :show, :id => @pic.id, :format => :gif
188 response.body.should eq 'bbb'
192 it '画像モデルに画像データを問い合わせる' do
193 OriginalPicture.any_instance.should_receive(:restore).exactly(1)
\r
194 get :show, :id => @pic.id, :format => :jpeg
197 OriginalPicture.any_instance.stub(:restore).and_return('ccc')
\r
198 get :show, :id => @pic.id, :format => :jpeg
199 response.body.should eq 'ccc'
203 context '作家権限がないとき' do
208 it 'ステータスコード302 Foundを返す' do
209 get :show, :id => @pic.id
210 response.status.should eq 302
212 it 'サインインページへ遷移する' do
213 get :show, :id => @pic.id
214 response.body.should redirect_to '/users/sign_in'
218 it 'ステータスコード401 Unauthorizedを返す' do
219 get :show, :id => @pic.id, :format => :json
220 response.status.should eq 401
222 it '応答メッセージにUnauthorizedを返す' do
223 get :show, :id => @pic.id, :format => :json
224 response.message.should match(/Unauthorized/)
228 context '作家が絵師でないとき' do
230 Author.any_instance.stub(:artist?).and_return(false)
233 it 'ステータスコード302 Foundを返す' do
234 get :show, :id => @pic.id
235 response.status.should eq 302
238 get :show, :id => @pic.id
239 response.should redirect_to new_artist_path
243 it '例外403 forbiddenを返す' do
245 get :show, :id => @pic.id, :format => :json
246 }.should raise_error(ActiveRecord::Forbidden)
251 context '対象原画がないとき' do
253 OriginalPicture.unstub(:show)
256 it '例外404 not_foundを返す' do
259 }.should raise_error(ActiveRecord::RecordNotFound)
263 it '例外404 not_foundを返す' do
265 get :show, :id => 0, :format => :json
266 }.should raise_error(ActiveRecord::RecordNotFound)
270 context '他人の原画を見ようとしたとき' do
272 OriginalPicture.stub(:show).and_return(@pic)
273 OriginalPicture.any_instance.stub(:own?).with(any_args()).and_return(false)
276 it '例外403 forbiddenを返す' do
278 get :show, :id => @pic.id
279 }.should raise_error(ActiveRecord::Forbidden)
283 it '例外403 forbiddenを返す' do
285 get :show, :id => @pic.id, :format => :json
286 }.should raise_error(ActiveRecord::Forbidden)
293 describe '新規作成フォーム表示に於いて' do
297 context 'つつがなく終わるとき' do
298 it 'ステータスコード200 OKを返す' do
300 response.should be_success
302 it '@original_pictureに新規データを用意している' do
304 assigns(:original_picture).should be_a_new(OriginalPicture)
306 it '原画モデルにデフォルト値補充を依頼している' do
307 OriginalPicture.any_instance.should_receive(:supply_default).exactly(1)
\r
311 it 'ページテンプレートnewを描画する' do
313 response.should render_template("new")
317 it '部分テンプレートnew.jsを描画する' do
318 get :new, :format => :js
319 response.should render_template("new")
323 context '作家権限がないとき' do
328 it 'ステータスコード302 Foundを返す' do
330 response.status.should eq 302
332 it 'サインインページへ遷移する' do
334 response.body.should redirect_to '/users/sign_in'
338 it 'ステータスコード401 Unauthorizedを返す' do
339 get :new, :format => :js
340 response.status.should eq 401
342 it '応答メッセージにUnauthorizedを返す' do
343 get :new, :format => :js
344 response.message.should match(/Unauthorized/)
348 context '作家が絵師でないとき' do
350 Author.any_instance.stub(:artist?).and_return(false)
353 it 'ステータスコード302 Foundを返す' do
355 response.status.should eq 302
359 response.should redirect_to new_artist_path
363 it 'ステータスコード200 Okを返す' do
364 get :new, :format => :js
365 response.status.should eq 200
367 it '絵師登録部分テンプレートartists/new.jsを描画する' do
368 get :new, :format => :js
369 response.should render_template("artists/new")
375 describe '新規作成に於いて' do
379 context '事前チェックしておく' do
381 OriginalPicture.any_instance.stub(:store).with(any_args()).and_return(true)
384 OriginalPicture.any_instance.should_receive(:store).exactly(1)
385 post :create, :original_picture => Factory.attributes_for(:original_picture)
387 it "@original_pictureに作成された原画を保持している" do
388 post :create, :original_picture => Factory.attributes_for(:original_picture)
389 assigns(:original_picture).should be_a(OriginalPicture)
392 context 'つつがなく終わるとき' do
394 OriginalPicture.any_instance.stub(:store).with(any_args()).and_return {
395 assigns(:original_picture).attributes = Factory.attributes_for(:original_picture, :artist_id => @artist.id , :license_id => @license.id, :ext => 'jpeg')
396 assigns(:original_picture).save
400 it "作成された原画がDBにある" do
401 post :create, :original_picture => Factory.attributes_for(:original_picture)
402 assigns(:original_picture).should_not be_a_new OriginalPicture
405 it 'ステータスコード302 Foundを返す' do
406 post :create, :original_picture => Factory.attributes_for(:original_picture)
407 response.status.should eq 302
409 it '作成された原画の表示ページへ遷移する' do
410 post :create, :original_picture => Factory.attributes_for(:original_picture)
411 response.should redirect_to(OriginalPicture.last)
415 it 'ステータスコード200 OKを返す' do
416 post :create, :original_picture => Factory.attributes_for(:original_picture), :format => :json
417 response.should be_success
419 it '作成された原画をjsonデータで返す' do
420 post :create, :original_picture => Factory.attributes_for(:original_picture), :format => :json
421 lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
424 post :create, :original_picture => Factory.attributes_for(:original_picture, :ext => 'jpeg'), :format => :json
425 json = JSON.parse response.body
426 json["ext"].should match(/jpeg/)
430 context '作家権限がないとき' do
435 it 'ステータスコード302 Foundを返す' do
436 post :create, :original_picture => Factory.attributes_for(:original_picture)
437 response.status.should eq 302
439 it 'サインインページへ遷移する' do
440 post :create, :original_picture => Factory.attributes_for(:original_picture)
441 response.body.should redirect_to '/users/sign_in'
445 it 'ステータスコード401 Unauthorizedを返す' do
446 post :create, :original_picture => Factory.attributes_for(:original_picture), :format => :json
447 response.status.should eq 401
449 it '応答メッセージにUnauthorizedを返す' do
450 post :create, :original_picture => Factory.attributes_for(:original_picture), :format => :json
451 response.message.should match(/Unauthorized/)
455 context '作家が絵師でないとき' do
457 Author.any_instance.stub(:artist?).and_return(false)
460 it 'ステータスコード302 Foundを返す' do
461 post :create, :original_picture => Factory.attributes_for(:original_picture)
462 response.status.should eq 302
465 post :create, :original_picture => Factory.attributes_for(:original_picture)
466 response.should redirect_to new_artist_path
470 it '例外403 forbiddenを返す' do
472 post :create, :original_picture => Factory.attributes_for(:original_picture), :format => :json
473 }.should raise_error(ActiveRecord::Forbidden)
477 context '検証、保存に失敗した' do
479 OriginalPicture.any_instance.stub(:store).and_return(false)
481 it "未保存のコミックを保持している" do
482 post :create, :original_picture => Factory.attributes_for(:original_picture)
483 assigns(:original_picture).should be_a_new(OriginalPicture)
486 it 'ステータスコード200 OKを返す' do
487 post :create, :original_picture => Factory.attributes_for(:original_picture)
488 response.status.should eq 200
491 post :create, :original_picture => Factory.attributes_for(:original_picture)
492 response.should render_template("new")
496 it 'ステータスコード422 unprocessable_entity を返す' do
497 post :create, :original_picture => Factory.attributes_for(:original_picture), :format => :json
498 response.status.should eq 422
500 it '応答メッセージUnprocessable Entityを返す' do
501 post :create, :original_picture => Factory.attributes_for(:original_picture), :format => :json
502 response.message.should match(/Unprocessable/)
508 describe '編集フォーム表示に於いて' do
510 @pic = Factory :original_picture, :artist_id => @artist.id , :license_id => @license.id
512 OriginalPicture.stub(:show).and_return(@pic)
514 context 'つつがなく終わるとき' do
515 it 'ステータスコード200 OKを返す' do
516 get :edit, :id => @pic.id
517 response.should be_success
519 it '原画モデルに単体取得を問い合わせている' do
520 OriginalPicture.should_receive(:show).exactly(1)
\r
521 get :edit, :id => @pic.id
523 it '@original_pictureにデータを用意している' do
524 get :edit, :id => @pic.id
525 assigns(:original_picture).should eq @pic
528 it 'ページテンプレートeditを描画する' do
529 get :edit, :id => @pic.id
530 response.should render_template("edit")
534 it '部分テンプレートedit.jsを描画する' do
535 get :edit, :id => @pic.id, :format => :js
536 response.should render_template("edit")
540 context '作家権限がないとき' do
545 it 'ステータスコード302 Foundを返す' do
546 get :edit, :id => @pic.id
547 response.status.should eq 302
549 it 'サインインページへ遷移する' do
550 get :edit, :id => @pic.id
551 response.body.should redirect_to '/users/sign_in'
555 it 'ステータスコード401 Unauthorizedを返す' do
556 get :edit, :id => @pic.id, :format => :js
557 response.status.should eq 401
559 it '応答メッセージにUnauthorizedを返す' do
560 get :edit, :id => @pic.id, :format => :js
561 response.message.should match(/Unauthorized/)
565 context '作家が絵師でないとき' do
567 Author.any_instance.stub(:artist?).and_return(false)
570 it 'ステータスコード302 Foundを返す' do
571 get :edit, :id => @pic.id
572 response.status.should eq 302
575 get :edit, :id => @pic.id
576 response.should redirect_to new_artist_path
580 it 'ステータスコード200 Okを返す' do
581 get :edit, :id => @pic.id, :format => :js
582 response.status.should eq 200
584 it '絵師登録部分テンプレートartists/new.jsを描画する' do
585 get :edit, :id => @pic.id, :format => :js
586 response.should render_template("artists/new")
594 @pic = Factory :original_picture, :artist_id => @artist.id , :license_id => @license.id
595 OriginalPicture.stub(:edit).with(any_args()).and_return(@pic)
598 context '事前チェックしておく' do
600 OriginalPicture.any_instance.stub(:store).with(any_args()).and_return(true)
602 it '原画モデルに単体取得を問い合わせている' do
603 OriginalPicture.should_receive(:edit).exactly(1)
\r
604 put :update, :id => @pic.id, :original_picture => Factory.attributes_for(:original_picture)
607 OriginalPicture.any_instance.should_receive(:store).exactly(1)
608 put :update, :id => @pic.id, :original_picture => Factory.attributes_for(:original_picture)
610 it '@original_pictureにアレを取得している' do
611 put :update, :id => @pic.id, :original_picture => Factory.attributes_for(:original_picture)
612 assigns(:original_picture).should eq(@pic)
615 context 'つつがなく終わるとき' do
617 OriginalPicture.any_instance.stub(:store).with(any_args()).and_return {
618 assigns(:original_picture).attributes = Factory.attributes_for(:original_picture, :artist_id => @artist.id , :license_id => @license.id, :ext => 'jpeg')
619 assigns(:original_picture).save
624 put :update, :id => @pic.id, :original_picture => Factory.attributes_for(:original_picture, :ext => 'jpeg')
625 OriginalPicture.find(@pic.id).ext.should eq 'jpeg'
628 it 'ステータスコード302 Foundを返す' do
629 put :update, :id => @pic.id, :original_picture => Factory.attributes_for(:original_picture)
630 response.status.should eq 302
632 it '更新された原画の表示ページへ遷移する' do
633 put :update, :id => @pic.id, :original_picture => Factory.attributes_for(:original_picture)
634 response.should redirect_to(@pic)
638 it 'ステータスコード200 OKを返す' do
639 put :update, :id => @pic.id, :original_picture => Factory.attributes_for(:original_picture), :format => :json
640 response.should be_success
643 put :update, :id => @pic.id, :original_picture => Factory.attributes_for(:original_picture), :format => :json
644 response.body.should match /./
648 context '作家権限がないとき' do
653 it 'ステータスコード302 Foundを返す' do
654 post :create, :original_picture => Factory.attributes_for(:original_picture)
655 response.status.should eq 302
657 it 'サインインページへ遷移する' do
658 post :create, :original_picture => Factory.attributes_for(:original_picture)
659 response.body.should redirect_to '/users/sign_in'
663 it 'ステータスコード401 Unauthorizedを返す' do
664 post :create, :original_picture => Factory.attributes_for(:original_picture), :format => :json
665 response.status.should eq 401
667 it '応答メッセージにUnauthorizedを返す' do
668 post :create, :original_picture => Factory.attributes_for(:original_picture), :format => :json
669 response.message.should match(/Unauthorized/)
673 context '作家が絵師でないとき' do
675 Author.any_instance.stub(:artist?).and_return(false)
678 it 'ステータスコード302 Foundを返す' do
679 post :create, :original_picture => Factory.attributes_for(:original_picture)
680 response.status.should eq 302
683 post :create, :original_picture => Factory.attributes_for(:original_picture)
684 response.should redirect_to new_artist_path
688 it '例外403 forbiddenを返す' do
690 post :create, :original_picture => Factory.attributes_for(:original_picture), :format => :json
691 }.should raise_error(ActiveRecord::Forbidden)
695 context '検証、保存に失敗した' do
697 OriginalPicture.any_instance.stub(:store).and_return(false)
700 it 'ステータスコード200 Okを返す' do
701 put :update, :id => @pic.id, :original_picture => Factory.attributes_for(:original_picture)
702 response.status.should eq 200
705 put :update, :id => @pic.id, :original_picture => Factory.attributes_for(:original_picture)
706 response.should render_template("edit")
710 it 'ステータスコード422 unprocessable_entity を返す' do
711 put :update, :id => @pic.id, :original_picture => Factory.attributes_for(:original_picture), :format => :json
712 response.status.should eq 422
714 it '応答メッセージUnprocessable Entityを返す' do
715 put :update, :id => @pic.id, :original_picture => Factory.attributes_for(:original_picture), :format => :json
716 response.message.should match(/Unprocessable/)