1 # -*- encoding: utf-8 -*-
5 describe OriginalPicturesController do
7 FactoryGirl.create :admin
8 @user = FactoryGirl.create( :user_yas)
10 @artist = FactoryGirl.create :artist_yas, :author_id => @author.id
11 @sp = FactoryGirl.create :system_picture
12 @lg = FactoryGirl.create :license_group
13 @license = FactoryGirl.create :license, :license_group_id => @lg.id, :system_picture_id => @sp.id
16 describe '一覧表示に於いて' do
18 @op = FactoryGirl.create :original_picture, :artist_id => @artist.id
20 OriginalPicture.stub(:mylist).and_return([@op, @op, @op])
22 context 'パラメータpageについて' do
24 get :index, :page => 5
25 assigns(:page).should eq 5
27 it '省略されると@pageに1値が入る' do
29 assigns(:page).should eq 1
31 it '与えられたpage_sizeがセットされている' do
32 get :index, :page_size => 15
33 assigns(:page_size).should eq 15
35 it '省略されると@page_sizeにデフォルト値が入る' do
37 assigns(:page_size).should eq OriginalPicture.default_page_size
39 it '最大を超えると@page_sizeにデフォルト最大値が入る' do
40 get :index, :page_size => 1500
41 assigns(:page_size).should eq OriginalPicture.max_page_size
43 it '不正な値が入ると@page_sizeにデフォルト最大値が入る' do
44 get :index, :page_size => 0
45 assigns(:page_size).should eq OriginalPicture.default_page_size
48 context 'つつがなく終わるとき' do
49 it 'ステータスコード200 OKを返す' do
51 response.should be_success
53 it '原画モデルにマイリストを問い合わせている' do
54 OriginalPicture.should_receive(:mylist).exactly(1)
57 it '@original_picturesにリストを取得している' do
59 assigns(:original_pictures).should have_at_least(3).items
62 it 'indexテンプレートを描画する' do
64 response.should render_template("index")
69 get :index, :format => :json
70 lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
72 it '原画モデルにjson一覧出力オプションを問い合わせている' do
73 OriginalPicture.should_receive(:list_json_opt).exactly(1)
74 get :index, :format => :json
76 it 'データがリスト構造になっている' do
77 get :index, :format => :json
78 json = JSON.parse response.body
79 json.should have_at_least(3).items
81 it 'リストの先頭くらいは原画っぽいものであって欲しい' do
82 get :index, :format => :json
83 json = JSON.parse response.body
84 json.first.has_key?("ext").should be_true
85 json.first.has_key?("md5").should be_true
86 json.first.has_key?("artist_id").should be_true
87 json.first.has_key?("width").should be_true
91 context '作家権限がないとき' do
96 it 'ステータスコード302 Foundを返す' do
98 response.status.should eq 302
100 it 'サインインページへ遷移する' do
102 response.should redirect_to '/users/sign_in'
106 it 'ステータスコード401 Unauthorizedを返す' do
107 get :index, :format => :json
108 response.status.should eq 401
110 it '応答メッセージにUnauthorizedを返す' do
111 get :index, :format => :json
112 response.message.should match(/Unauthorized/)
116 context '作家が絵師でないとき' do
118 Author.any_instance.stub(:artist?).and_return(false)
121 it 'ステータスコード302 Foundを返す' do
123 response.status.should eq 302
127 response.should redirect_to new_artist_path
131 it '例外403 forbiddenを返す' do
133 get :index, :format => :json
134 }.should raise_error(ActiveRecord::Forbidden)
140 describe '単体表示に於いて' do
142 @pic = FactoryGirl.create :original_picture, :artist_id => @artist.id
144 OriginalPicture.stub(:show).and_return(@pic)
146 context 'つつがなく終わるとき' do
147 it 'ステータスコード200 OKを返す' do
148 get :show, :id => @pic.id
149 response.should be_success
151 it '原画モデルに単体取得を問い合わせている' do
152 OriginalPicture.should_receive(:show).exactly(1)
155 it '@original_pictureにアレを取得している' do
156 get :show, :id => @pic.id
157 assigns(:original_picture).id.should eq(@pic.id)
160 it 'showテンプレートを描画する' do
161 get :show, :id => @pic.id
162 response.should render_template("show")
167 get :show, :id => @pic.id, :format => :json
168 lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
170 it '原画モデルにjson単体出力オプションを問い合わせている' do
171 OriginalPicture.should_receive(:show_json_opt).exactly(1)
172 get :show, :id => @pic.id, :format => :json
175 get :show, :id => @pic.id, :format => :json
176 json = JSON.parse response.body
177 json["ext"].should match(/png/)
178 json["md5"].should be_true
179 json["artist_id"].should be_true
180 json["width"].should be_true
183 #画像送信では、send_dataにスタブをおいてテストしたいが、ここに噛ませると
184 #renderが働かず、エラーとなってしまう。そこで、原画のファイル取得部分に
185 #スタブをおいてsend_dataがデータを返す体裁でテストする。
188 OriginalPicture.any_instance.stub(:mime_type).and_return('image/png')
189 OriginalPicture.any_instance.stub(:restore).and_return('aaa')
191 it '画像モデルに画像データを問い合わせる' do
192 OriginalPicture.any_instance.should_receive(:restore).exactly(1)
193 get :show, :id => @pic.id, :format => :png
195 it '画像モデルにMimeTypeを問い合わせる' do
196 OriginalPicture.any_instance.should_receive(:mime_type).exactly(1)
197 get :show, :id => @pic.id, :format => :png
200 get :show, :id => @pic.id, :format => :png
201 response.body.should eq 'aaa'
206 OriginalPicture.any_instance.stub(:mime_type).and_return('image/gif')
207 OriginalPicture.any_instance.stub(:restore).and_return('bbb')
209 it '画像モデルに画像データを問い合わせる' do
210 OriginalPicture.any_instance.should_receive(:restore).exactly(1)
211 get :show, :id => @pic.id, :format => :gif
213 it '画像モデルにMimeTypeを問い合わせる' do
214 OriginalPicture.any_instance.should_receive(:mime_type).exactly(1)
215 get :show, :id => @pic.id, :format => :gif
218 get :show, :id => @pic.id, :format => :gif
219 response.body.should eq 'bbb'
224 OriginalPicture.any_instance.stub(:mime_type).and_return('image/jpeg')
225 OriginalPicture.any_instance.stub(:restore).and_return('ccc')
227 it '画像モデルに画像データを問い合わせる' do
228 OriginalPicture.any_instance.should_receive(:restore).exactly(1)
229 get :show, :id => @pic.id, :format => :jpeg
231 it '画像モデルにMimeTypeを問い合わせる' do
232 OriginalPicture.any_instance.should_receive(:mime_type).exactly(1)
233 get :show, :id => @pic.id, :format => :jpeg
236 get :show, :id => @pic.id, :format => :jpeg
237 response.body.should eq 'ccc'
241 context '作家権限がないとき' do
246 it 'ステータスコード302 Foundを返す' do
247 get :show, :id => @pic.id
248 response.status.should eq 302
250 it 'サインインページへ遷移する' do
251 get :show, :id => @pic.id
252 response.body.should redirect_to '/users/sign_in'
256 it 'ステータスコード401 Unauthorizedを返す' do
257 get :show, :id => @pic.id, :format => :json
258 response.status.should eq 401
260 it '応答メッセージにUnauthorizedを返す' do
261 get :show, :id => @pic.id, :format => :json
262 response.message.should match(/Unauthorized/)
266 context '作家が絵師でないとき' do
268 Author.any_instance.stub(:artist?).and_return(false)
271 it 'ステータスコード302 Foundを返す' do
272 get :show, :id => @pic.id
273 response.status.should eq 302
276 get :show, :id => @pic.id
277 response.should redirect_to new_artist_path
281 it '例外403 forbiddenを返す' do
283 get :show, :id => @pic.id, :format => :json
284 }.should raise_error(ActiveRecord::Forbidden)
289 context '対象原画がないとき' do
291 OriginalPicture.unstub(:show)
294 it '例外404 not_foundを返す' do
297 }.should raise_error(ActiveRecord::RecordNotFound)
301 it '例外404 not_foundを返す' do
303 get :show, :id => 0, :format => :json
304 }.should raise_error(ActiveRecord::RecordNotFound)
308 context '他人の原画を見ようとしたとき' do
310 OriginalPicture.stub(:show).and_return(@pic)
311 OriginalPicture.any_instance.stub(:own?).with(any_args()).and_return(false)
314 it '例外403 forbiddenを返す' do
316 get :show, :id => @pic.id
317 }.should raise_error(ActiveRecord::Forbidden)
321 it '例外403 forbiddenを返す' do
323 get :show, :id => @pic.id, :format => :json
324 }.should raise_error(ActiveRecord::Forbidden)
331 describe '履歴一覧表示に於いて' do
333 @op = FactoryGirl.create :original_picture, :artist_id => @artist.id
334 @p = FactoryGirl.create :picture, :artist_id => @artist.id, :license_id => @license.id, :revision => 0,
335 :original_picture_id => @op.id
337 OriginalPicture.stub(:show).and_return(@op)
338 OriginalPicture.any_instance.stub(:history).and_return([@p, @p, @p])
340 context 'つつがなく終わるとき' do
341 it 'ステータスコード200 OKを返す' do
342 get :history, :id => @op.id
343 response.should be_success
345 it '原画モデルに単体取得を問い合わせている' do
346 OriginalPicture.should_receive(:show).exactly(1)
347 get :history, :id => @op.id
349 it '自身に履歴一覧を問い合わせている' do
350 OriginalPicture.any_instance.should_receive(:history).exactly(1)
353 it '@historyにリストを取得している' do
354 get :history, :id => @op.id
355 assigns(:history).should have_at_least(3).items
358 it 'historyテンプレートを描画する' do
359 get :history, :id => @op.id
360 response.should render_template("history")
365 get :history, :id => @op.id, :format => :json
366 lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
368 it 'データがリスト構造になっている' do
369 get :history, :id => @op.id, :format => :json
370 json = JSON.parse response.body
371 json.should have_at_least(3).items
373 it 'リストの先頭くらいは原画っぽいものであって欲しい' do
374 get :history, :id => @op.id, :format => :json
375 json = JSON.parse response.body
376 json.first.has_key?("ext").should be_true
380 context '作家権限がないとき' do
385 it 'ステータスコード302 Foundを返す' do
386 get :history, :id => @op.id
387 response.status.should eq 302
389 it 'サインインページへ遷移する' do
390 get :history, :id => @op.id
391 response.should redirect_to '/users/sign_in'
395 it 'ステータスコード401 Unauthorizedを返す' do
396 get :history, :id => @op.id, :format => :json
397 response.status.should eq 401
399 it '応答メッセージにUnauthorizedを返す' do
400 get :history, :id => @op.id, :format => :json
401 response.message.should match(/Unauthorized/)
405 context '作家が絵師でないとき' do
407 Author.any_instance.stub(:artist?).and_return(false)
410 it 'ステータスコード302 Foundを返す' do
411 get :history, :id => @op.id
412 response.status.should eq 302
415 get :history, :id => @op.id
416 response.should redirect_to new_artist_path
420 it '例外403 forbiddenを返す' do
422 get :history, :id => @op.id, :format => :json
423 }.should raise_error(ActiveRecord::Forbidden)
429 describe '新規作成フォーム表示に於いて' do
433 context 'つつがなく終わるとき' do
434 it '@original_pictureに新規データを用意している' do
436 assigns(:original_picture).should be_a_new(OriginalPicture)
438 it '原画モデルにデフォルト値補充を依頼している' do
439 OriginalPicture.any_instance.should_receive(:supply_default).exactly(1)
443 it 'ステータスコード200 OKを返す' do
445 response.should be_success
447 it 'ページテンプレートnewを描画する' do
449 response.should render_template("new")
453 it 'ステータスコード200 OKを返す' do
454 get :new, :format => :js
455 response.should be_success
457 it '部分テンプレートnew.jsを描画する' do
458 get :new, :format => :js
459 response.should render_template("new")
464 get :new, :format => :json
465 lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
467 it '原画モデルにjson単体出力オプションを問い合わせている' do
468 OriginalPicture.should_receive(:show_json_opt).exactly(1)
469 get :new, :format => :json
473 context '作家権限がないとき' do
478 it 'ステータスコード302 Foundを返す' do
480 response.status.should eq 302
482 it 'サインインページへ遷移する' do
484 response.body.should redirect_to '/users/sign_in'
488 it 'ステータスコード401 Unauthorizedを返す' do
489 get :new, :format => :js
490 response.status.should eq 401
492 it '応答メッセージにUnauthorizedを返す' do
493 get :new, :format => :js
494 response.message.should match(/Unauthorized/)
498 context '作家が絵師でないとき' do
500 Author.any_instance.stub(:artist?).and_return(false)
503 it 'ステータスコード302 Foundを返す' do
505 response.status.should eq 302
509 response.should redirect_to new_artist_path
513 it 'ステータスコード200 Okを返す' do
514 get :new, :format => :js
515 response.status.should eq 200
517 it '絵師登録部分テンプレートartists/new.jsを描画する' do
518 get :new, :format => :js
519 response.should render_template("artists/new")
523 it '例外403 forbiddenを返す' do
525 get :new, :format => :json
526 }.should raise_error(ActiveRecord::Forbidden)
532 describe '新規作成に於いて' do
536 context '事前チェックしておく' do
538 OriginalPicture.any_instance.stub(:store).with(any_args()).and_return(true)
541 OriginalPicture.any_instance.should_receive(:store).exactly(1)
542 post :create, :original_picture => FactoryGirl.attributes_for(:original_picture)
544 it "@original_pictureに作成された原画を保持している" do
545 post :create, :original_picture => FactoryGirl.attributes_for(:original_picture)
546 assigns(:original_picture).should be_a(OriginalPicture)
549 context 'つつがなく終わるとき' do
551 OriginalPicture.any_instance.stub(:store).with(any_args()).and_return {
552 assigns(:original_picture).attributes = FactoryGirl.attributes_for(:original_picture, :artist_id => @artist.id, :ext => 'jpeg')
553 assigns(:original_picture).save
557 it "作成された原画がDBにある" do
558 post :create, :original_picture => FactoryGirl.attributes_for(:original_picture)
559 assigns(:original_picture).should_not be_a_new OriginalPicture
562 it 'ステータスコード302 Foundを返す' do
563 post :create, :original_picture => FactoryGirl.attributes_for(:original_picture)
564 response.status.should eq 302
566 it '作成された原画の表示ページへ遷移する' do
567 post :create, :original_picture => FactoryGirl.attributes_for(:original_picture)
568 response.should redirect_to(OriginalPicture.last)
572 it 'ステータスコード200 OKを返す' do
573 post :create, :original_picture => FactoryGirl.attributes_for(:original_picture), :format => :json
574 response.should be_success
576 it '作成された原画をjsonデータで返す' do
577 post :create, :original_picture => FactoryGirl.attributes_for(:original_picture), :format => :json
578 lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
580 it '原画モデルにjson単体出力オプションを問い合わせている' do
581 OriginalPicture.should_receive(:show_json_opt).exactly(1)
582 post :create, :original_picture => FactoryGirl.attributes_for(:original_picture), :format => :json
585 post :create, :original_picture => FactoryGirl.attributes_for(:original_picture, :ext => 'jpeg'), :format => :json
586 json = JSON.parse response.body
587 json["ext"].should match(/jpeg/)
588 json["md5"].should be_true
589 json["artist_id"].should be_true
590 json["width"].should be_true
594 context '作家権限がないとき' do
599 it 'ステータスコード302 Foundを返す' do
600 post :create, :original_picture => FactoryGirl.attributes_for(:original_picture)
601 response.status.should eq 302
603 it 'サインインページへ遷移する' do
604 post :create, :original_picture => FactoryGirl.attributes_for(:original_picture)
605 response.body.should redirect_to '/users/sign_in'
609 it 'ステータスコード401 Unauthorizedを返す' do
610 post :create, :original_picture => FactoryGirl.attributes_for(:original_picture), :format => :json
611 response.status.should eq 401
613 it '応答メッセージにUnauthorizedを返す' do
614 post :create, :original_picture => FactoryGirl.attributes_for(:original_picture), :format => :json
615 response.message.should match(/Unauthorized/)
619 context '作家が絵師でないとき' do
621 Author.any_instance.stub(:artist?).and_return(false)
624 it 'ステータスコード302 Foundを返す' do
625 post :create, :original_picture => FactoryGirl.attributes_for(:original_picture)
626 response.status.should eq 302
629 post :create, :original_picture => FactoryGirl.attributes_for(:original_picture)
630 response.should redirect_to new_artist_path
634 it '例外403 forbiddenを返す' do
636 post :create, :original_picture => FactoryGirl.attributes_for(:original_picture), :format => :json
637 }.should raise_error(ActiveRecord::Forbidden)
641 context '検証、保存に失敗した' do
643 OriginalPicture.any_instance.stub(:store).and_return(false)
645 it "未保存の原画を保持している" do
646 post :create, :original_picture => FactoryGirl.attributes_for(:original_picture)
647 assigns(:original_picture).should be_a_new(OriginalPicture)
650 it 'ステータスコード200 OKを返す' do
651 post :create, :original_picture => FactoryGirl.attributes_for(:original_picture)
652 response.status.should eq 200
655 post :create, :original_picture => FactoryGirl.attributes_for(:original_picture)
656 response.should render_template("new")
660 it 'ステータスコード422 unprocessable_entity を返す' do
661 post :create, :original_picture => FactoryGirl.attributes_for(:original_picture), :format => :json
662 response.status.should eq 422
664 it '応答メッセージUnprocessable Entityを返す' do
665 post :create, :original_picture => FactoryGirl.attributes_for(:original_picture), :format => :json
666 response.message.should match(/Unprocessable/)
672 describe '編集フォーム表示に於いて' do
674 @pic = FactoryGirl.create :original_picture, :artist_id => @artist.id
676 OriginalPicture.stub(:show).and_return(@pic)
678 context 'つつがなく終わるとき' do
679 it 'ステータスコード200 OKを返す' do
680 get :edit, :id => @pic.id
681 response.should be_success
683 it '原画モデルに編集取得を問い合わせている' do
684 OriginalPicture.should_receive(:edit).exactly(1)
685 get :edit, :id => @pic.id
687 it '@original_pictureにデータを用意している' do
688 get :edit, :id => @pic.id
689 assigns(:original_picture).should eq @pic
692 it 'ページテンプレートeditを描画する' do
693 get :edit, :id => @pic.id
694 response.should render_template("edit")
698 it '部分テンプレートedit.jsを描画する' do
699 get :edit, :id => @pic.id, :format => :js
700 response.should render_template("edit")
704 context '作家権限がないとき' do
709 it 'ステータスコード302 Foundを返す' do
710 get :edit, :id => @pic.id
711 response.status.should eq 302
713 it 'サインインページへ遷移する' do
714 get :edit, :id => @pic.id
715 response.body.should redirect_to '/users/sign_in'
719 it 'ステータスコード401 Unauthorizedを返す' do
720 get :edit, :id => @pic.id, :format => :js
721 response.status.should eq 401
723 it '応答メッセージにUnauthorizedを返す' do
724 get :edit, :id => @pic.id, :format => :js
725 response.message.should match(/Unauthorized/)
729 context '作家が絵師でないとき' do
731 Author.any_instance.stub(:artist?).and_return(false)
734 it 'ステータスコード302 Foundを返す' do
735 get :edit, :id => @pic.id
736 response.status.should eq 302
739 get :edit, :id => @pic.id
740 response.should redirect_to new_artist_path
744 it 'ステータスコード200 Okを返す' do
745 get :edit, :id => @pic.id, :format => :js
746 response.status.should eq 200
748 it '絵師登録部分テンプレートartists/new.jsを描画する' do
749 get :edit, :id => @pic.id, :format => :js
750 response.should render_template("artists/new")
758 @pic = FactoryGirl.create :original_picture, :artist_id => @artist.id
759 OriginalPicture.stub(:edit).with(any_args()).and_return(@pic)
762 context '事前チェックしておく' do
764 OriginalPicture.any_instance.stub(:store).with(any_args()).and_return(true)
766 it '原画モデルに単体取得を問い合わせている' do
767 OriginalPicture.should_receive(:edit).exactly(1)
768 put :update, :id => @pic.id, :original_picture => FactoryGirl.attributes_for(:original_picture)
771 OriginalPicture.any_instance.should_receive(:store).exactly(1)
772 put :update, :id => @pic.id, :original_picture => FactoryGirl.attributes_for(:original_picture)
774 it '@original_pictureにアレを取得している' do
775 put :update, :id => @pic.id, :original_picture => FactoryGirl.attributes_for(:original_picture)
776 assigns(:original_picture).should eq(@pic)
779 context 'つつがなく終わるとき' do
781 OriginalPicture.any_instance.stub(:store).with(any_args()).and_return {
782 assigns(:original_picture).attributes = FactoryGirl.attributes_for(:original_picture, :artist_id => @artist.id, :ext => 'jpeg')
783 assigns(:original_picture).save
788 put :update, :id => @pic.id, :original_picture => FactoryGirl.attributes_for(:original_picture, :ext => 'jpeg')
789 OriginalPicture.find(@pic.id).ext.should eq 'jpeg'
792 it 'ステータスコード302 Foundを返す' do
793 put :update, :id => @pic.id, :original_picture => FactoryGirl.attributes_for(:original_picture)
794 response.status.should eq 302
796 it '更新された原画の表示ページへ遷移する' do
797 put :update, :id => @pic.id, :original_picture => FactoryGirl.attributes_for(:original_picture)
798 response.should redirect_to(@pic)
802 it 'ステータスコード200 OKを返す' do
803 put :update, :id => @pic.id, :original_picture => FactoryGirl.attributes_for(:original_picture), :format => :json
804 response.should be_success
807 put :update, :id => @pic.id, :original_picture => FactoryGirl.attributes_for(:original_picture), :format => :json
808 response.body.should match /./
812 context '作家権限がないとき' do
817 it 'ステータスコード302 Foundを返す' do
818 post :create, :original_picture => FactoryGirl.attributes_for(:original_picture)
819 response.status.should eq 302
821 it 'サインインページへ遷移する' do
822 post :create, :original_picture => FactoryGirl.attributes_for(:original_picture)
823 response.body.should redirect_to '/users/sign_in'
827 it 'ステータスコード401 Unauthorizedを返す' do
828 post :create, :original_picture => FactoryGirl.attributes_for(:original_picture), :format => :json
829 response.status.should eq 401
831 it '応答メッセージにUnauthorizedを返す' do
832 post :create, :original_picture => FactoryGirl.attributes_for(:original_picture), :format => :json
833 response.message.should match(/Unauthorized/)
837 context '作家が絵師でないとき' do
839 Author.any_instance.stub(:artist?).and_return(false)
842 it 'ステータスコード302 Foundを返す' do
843 post :create, :original_picture => FactoryGirl.attributes_for(:original_picture)
844 response.status.should eq 302
847 post :create, :original_picture => FactoryGirl.attributes_for(:original_picture)
848 response.should redirect_to new_artist_path
852 it '例外403 forbiddenを返す' do
854 post :create, :original_picture => FactoryGirl.attributes_for(:original_picture), :format => :json
855 }.should raise_error(ActiveRecord::Forbidden)
859 context '検証、保存に失敗した' do
861 OriginalPicture.any_instance.stub(:store).and_return(false)
864 it 'ステータスコード200 Okを返す' do
865 put :update, :id => @pic.id, :original_picture => FactoryGirl.attributes_for(:original_picture)
866 response.status.should eq 200
869 put :update, :id => @pic.id, :original_picture => FactoryGirl.attributes_for(:original_picture)
870 response.should render_template("edit")
874 it 'ステータスコード422 unprocessable_entity を返す' do
875 put :update, :id => @pic.id, :original_picture => FactoryGirl.attributes_for(:original_picture), :format => :json
876 response.status.should eq 422
878 it '応答メッセージUnprocessable Entityを返す' do
879 put :update, :id => @pic.id, :original_picture => FactoryGirl.attributes_for(:original_picture), :format => :json
880 response.message.should match(/Unprocessable/)