1 # -*- encoding: utf-8 -*-
5 describe AuthorsController 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
16 if MagicNumber['run_mode'] == 1
17 describe '一覧表示に於いて' do
19 Author.stub(:list).and_return([@author, @author, @author])
23 it '与えられた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 Author.default_page_size
39 it '最大を超えると@page_sizeにデフォルト最大値が入る' do
40 get :index, :page_size => 1500
41 assigns(:page_size).should eq Author.max_page_size
43 it '不正な値が入ると@page_sizeにデフォルト最大値が入る' do
44 get :index, :page_size => 0
45 assigns(:page_size).should eq Author.default_page_size
48 context 'つつがなく終わるとき' do
49 it 'ステータスコード200 OKを返す' do
51 response.should be_success
53 it '作家モデルに一覧を問い合わせている' do
54 Author.should_receive(:list).exactly(1)
57 it '@authorsにリストを取得している' do
59 assigns(:authors).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 Author.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?("name").should be_true
85 json.first.has_key?("user_id").should be_true
89 context '作家権限がないとき' do
94 it 'ステータスコード302 Foundを返す' do
96 response.status.should eq 302
100 response.should redirect_to '/users/sign_in'
104 it 'ステータスコード401 Unauthorizedを返す' do
105 get :index, :format => :json
106 response.status.should eq 401
108 it '応答メッセージにUnauthorizedを返す' do
109 get :index, :format => :json
110 response.message.should match(/Unauthorized/)
114 context '作家権限はないが管理者権限があるとき' do
119 it 'ステータスコード200 OKを返す' do
121 response.should be_success
128 Author.stub(:show).and_return(@author)
131 context 'つつがなく終わるとき' do
132 it 'ステータスコード200 OKを返す' do
133 get :show, :id => @author.id
134 response.should be_success
136 it '作家モデルに単体取得を問い合わせている' do
137 Author.should_receive(:show).exactly(1)
140 #@authorだとログイン中のアカウントと干渉してしまう。
141 it '@auにアレを取得している' do
142 get :show, :id => @author.id
143 assigns(:au).should eq(@author)
146 it 'showテンプレートを描画する' do
147 get :show, :id => @author.id
148 response.should render_template("show")
153 get :show, :id => @author.id, :format => :json
154 lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
156 it '作家モデルにjson単体出力オプションを問い合わせている' do
157 Author.should_receive(:show_json_opt).exactly(1)
158 get :show, :id => @author.id, :format => :json
161 get :show, :id => @author.id, :format => :json
162 json = JSON.parse response.body
163 json["name"].should match(/test/)
164 json["user_id"].should eq @author.user_id
168 context '作家権限がないとき' do
173 it 'ステータスコード302 Foundを返す' do
174 get :show, :id => @author.id
175 response.status.should eq 302
177 it 'サインインページへ遷移する' do
178 get :show, :id => @author.id
179 response.body.should redirect_to '/users/sign_in'
183 it 'ステータスコード401 Unauthorizedを返す' do
184 get :show, :id => @author.id, :format => :json
185 response.status.should eq 401
187 it '応答メッセージにUnauthorizedを返す' do
188 get :show, :id => @author.id, :format => :json
189 response.message.should match(/Unauthorized/)
193 context '作家権限はないが管理者権限があるとき' do
198 it 'ステータスコード200 OKを返す' do
199 get :show, :id => @author.id
200 response.should be_success
204 context '対象作家がないとき' do
206 it '例外404 not_foundを返す' do
209 }.should raise_error(ActiveRecord::RecordNotFound)
213 it '例外404 not_foundを返す' do
215 get :show, :id => 0, :format => :json
216 }.should raise_error(ActiveRecord::RecordNotFound)
223 describe '作家数取得に於いて' do
225 Author.should_receive(:visible_count).and_return(3)
228 context 'つつがなく終わるとき' do
229 it 'ステータスコード200 OKを返す' do
230 get :count, :format => :json
231 response.should be_success
235 get :count, :format => :json
236 lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
238 it 'データがHash構造になっていて作家数が3である' do
239 get :count, :format => :json
240 json = JSON.parse response.body
241 json["count"].should == 3
247 describe '新規作成フォーム表示に於いて' do
249 @new_user = FactoryGirl.create( :user_yas)
252 context 'つつがなく終わるとき' do
253 it 'ステータスコード200 OKを返す' do
255 response.should be_success
257 it '@auに新規データを用意している' do
259 assigns(:au).should be_a_new(Author)
261 it '作家モデルにデフォルト値補充を依頼している' do
262 Author.any_instance.should_receive(:supply_default).exactly(1)
266 it 'newテンプレートを描画する' do
268 response.should render_template("new")
272 it 'new.jsテンプレートを描画する' do
273 get :new, :format => :js
274 response.should render_template("new")
279 get :new, :format => :json
280 lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
282 it '作家モデルにjson単体出力オプションを問い合わせている' do
283 Author.should_receive(:show_json_opt).exactly(1)
284 get :new, :format => :json
288 context '作家権限がないとき' do
293 it 'ステータスコード302 Foundを返す' do
295 response.status.should eq 302
297 it 'サインインページへ遷移する' do
299 response.body.should redirect_to '/users/sign_in'
303 it 'ステータスコード401 Unauthorizedを返す' do
304 get :new, :format => :js
305 response.status.should eq 401
307 it '応答メッセージにUnauthorizedを返す' do
308 get :new, :format => :js
309 response.message.should match(/Unauthorized/)
313 it 'ステータスコード401 Unauthorizedを返す' do
314 get :new, :format => :json
315 response.status.should eq 401
317 it '応答メッセージにUnauthorizedを返す' do
318 get :new, :format => :json
319 response.message.should match(/Unauthorized/)
323 context '作家権限はないが管理者権限があるとき' do
329 it 'ステータスコード302 Foundを返す' do
331 response.status.should eq 302
333 it 'サインインページへ遷移する' do
335 response.body.should redirect_to '/users/sign_in'
341 describe '新規作成に於いて' do
343 @new_user = FactoryGirl.create( :user_yas)
345 @attr = FactoryGirl.attributes_for(:author, :user_id => @new_user.id, :name => 'ken')
347 context '事前チェックしておく' do
348 it '作家モデルにデフォルト値補充を依頼している' do
349 Author.any_instance.should_receive(:supply_default).exactly(1)
350 post :create, :author => @attr
352 it '作家モデルにカラム値復元を依頼している' do
353 Author.any_instance.should_receive(:attributes=).exactly(1)
354 post :create, :author => @attr
356 it '作家モデルに上書き補充を依頼している' do
357 Author.any_instance.should_receive(:overwrite).exactly(1)
358 post :create, :author => @attr
361 Author.any_instance.should_receive(:save).exactly(1)
362 post :create, :author => @attr
365 context 'つつがなく終わるとき' do
366 it "@auに作成された作家を保持していて、それがDBにある" do
367 post :create, :author => @attr
368 assigns(:au).should be_a(Author)
369 assigns(:au).should be_persisted
372 it 'ステータスコード302 Foundを返す' do
373 Author.any_instance.stub(:save).and_return(true)
374 post :create, :author => @attr
375 response.status.should eq 302
378 # Author.any_instance.stub(:save).and_return(true)
379 post :create, :author => @attr
380 response.should redirect_to(root_path)
384 it 'ステータスコード200 OKを返す' do
385 # Author.any_instance.stub(:save).and_return(true)
386 post :create, :author => @attr, :format => :json
387 response.should be_success
389 it '作成された作家をjsonデータで返す' do
390 post :create, :author => @attr, :format => :json
391 lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
393 it '作家モデルにjson単体出力オプションを問い合わせている' do
394 Author.should_receive(:show_json_opt).exactly(1)
395 post :create, :author => @attr, :format => :json
398 post :create, :author => @attr, :format => :json
399 json = JSON.parse response.body
400 json["name"].should match(/ken/)
404 context '作家権限がないとき' do
409 it 'ステータスコード302 Foundを返す' do
410 post :create, :author => @attr
411 response.status.should eq 302
413 it 'サインインページへ遷移する' do
414 post :create, :author => @attr
415 response.body.should redirect_to '/users/sign_in'
419 it 'ステータスコード401 Unauthorizedを返す' do
420 post :create, :author => @attr, :format => :json
421 response.status.should eq 401
423 it '応答メッセージにUnauthorizedを返す' do
424 post :create, :author => @attr, :format => :json
425 response.message.should match(/Unauthorized/)
429 context '作家権限はないが管理者権限があるとき' do
435 it 'ステータスコード302 Foundを返す' do
436 post :create, :author => @attr
437 response.status.should eq 302
439 it 'サインインページへ遷移する' do
440 post :create, :author => @attr
441 response.body.should redirect_to '/users/sign_in'
445 context '検証、保存に失敗した' do
447 Author.any_instance.stub(:save).and_return(false)
449 it "未保存の作家を保持している" do
450 post :create, :author => @attr
451 assigns(:au).should be_a_new(Author)
454 it 'ステータスコード200 OKを返す' do
455 post :create, :author => @attr
456 response.status.should eq 200
459 post :create, :author => @attr
460 response.should render_template("new")
464 it 'ステータスコード422 unprocessable_entity を返す' do
465 post :create, :author => @attr, :format => :json
466 response.status.should eq 422
468 it '応答メッセージUnprocessable Entityを返す' do
469 post :create, :author => @attr, :format => :json
470 response.message.should match(/Unprocessable/)
476 describe '編集フォーム表示に於いて' do
479 Author.stub(:edit).and_return(@author)
481 context 'つつがなく終わるとき' do
482 it 'ステータスコード200 OKを返す' do
483 get :edit, :id => @author.id
484 response.should be_success
486 it '作家モデルに編集取得を問い合わせている' do
487 Author.should_receive(:edit).exactly(1)
488 get :edit, :id => @author.id
490 #@authorだとログイン中のアカウントと干渉してしまう。
491 it '@auにデータを用意している' do
492 get :edit, :id => @author.id
493 assigns(:au).should eq @author
496 it 'editテンプレートを描画する' do
497 get :edit, :id => @author.id
498 response.should render_template("edit")
502 it 'edit.jsテンプレートを描画する' do
503 get :edit, :id => @author.id, :format => :js
504 response.should render_template("edit")
508 context '作家権限がないとき' do
513 it 'ステータスコード302 Foundを返す' do
514 get :edit, :id => @author.id
515 response.status.should eq 302
517 it 'サインインページへ遷移する' do
518 get :edit, :id => @author.id
519 response.body.should redirect_to '/users/sign_in'
523 it 'ステータスコード401 Unauthorizedを返す' do
524 get :edit, :id => @author.id, :format => :js
525 response.status.should eq 401
527 it '応答メッセージにUnauthorizedを返す' do
528 get :edit, :id => @author.id, :format => :js
529 response.message.should match(/Unauthorized/)
533 context '作家権限はないが管理者権限があるとき' do
539 it 'ステータスコード302 Foundを返す' do
540 get :edit, :id => @author.id
541 response.status.should eq 302
543 it 'サインインページへ遷移する' do
544 get :edit, :id => @author.id
545 response.body.should redirect_to '/users/sign_in'
553 @attr = @author.attributes
554 @attr['name'] = 'ryu'
557 context '事前チェックしておく' do
558 it '作家モデルに編集取得を問い合わせている' do
559 Author.stub(:edit).with(any_args()).and_return @author
560 Author.should_receive(:edit).exactly(1)
561 put :update, :id => @author.id, :author => @attr
563 it 'モデルにpostデータ転送を依頼する' do
564 Author.any_instance.stub(:attributes=).with(any_args)
565 Author.any_instance.should_receive(:attributes=).exactly(1)
566 put :update, :id => @author.id, :author => @attr
568 it 'モデルに上書き補充を依頼する' do
569 Author.any_instance.stub(:overwrite).with(any_args)
570 Author.any_instance.should_receive(:overwrite).exactly(1)
571 put :update, :id => @author.id, :author => @attr
574 Author.any_instance.stub(:save).with(any_args).and_return true
575 Author.any_instance.should_receive(:save).exactly(1)
576 put :update, :id => @author.id, :author => @attr
578 it '@auにアレを取得している' do
579 put :update, :id => @author.id, :author => @attr
580 assigns(:au).should eq @author
583 context 'つつがなく終わるとき' do
585 put :update, :id => @author.id, :author => @attr
586 Author.find(@author.id).name.should eq 'ryu'
589 it 'ステータスコード302 Foundを返す' do
590 Author.any_instance.stub(:save).with(any_args()).and_return(true)
591 put :update, :id => @author.id, :author => @attr
592 response.status.should eq 302
595 put :update, :id => @author.id, :author => @attr
596 response.should redirect_to('/home/configure')
600 it 'ステータスコード200 OKを返す' do
601 Author.any_instance.stub(:save).with(any_args()).and_return(true)
602 put :update, :id => @author.id, :author => @attr, :format => :json
603 response.should be_success
606 Author.any_instance.stub(:save).with(any_args()).and_return(true)
607 put :update, :id => @author.id, :author => @attr, :format => :json
608 response.body.should match /./
612 context '作家権限がないとき' do
617 it 'ステータスコード302 Foundを返す' do
618 put :update, :id => @author.id, :author => @attr
619 response.status.should eq 302
621 it 'サインインページへ遷移する' do
622 put :update, :id => @author.id, :author => @attr
623 response.body.should redirect_to '/users/sign_in'
627 it '応答メッセージにUnauthorizedを返す' do
628 put :update, :id => @author.id, :author => @attr, :format => :json
629 response.message.should match(/Unauthorized/)
633 context '作家権限はないが管理者権限があるとき' do
639 it 'ステータスコード302 Foundを返す' do
640 put :update, :id => @author.id, :author => @attr
641 response.status.should eq 302
643 it 'サインインページへ遷移する' do
644 put :update, :id => @author.id, :author => @attr
645 response.body.should redirect_to '/users/sign_in'
649 context '検証、保存に失敗したとき' do
651 Author.any_instance.stub(:save).and_return(false)
654 it 'ステータスコード200 Okを返す' do
655 put :update, :id => @author.id, :author => @attr
656 response.status.should eq 200
659 put :update, :id => @author.id, :author => @attr
660 response.should render_template("edit")
664 it 'ステータスコード422 unprocessable_entity を返す' do
665 Author.any_instance.stub(:save).and_return(false)
666 put :update, :id => @author.id, :author => @attr, :format => :json
667 response.status.should eq 422
669 it '応答メッセージUnprocessable Entityを返す' do
670 put :update, :id => @author.id, :author => @attr, :format => :json
671 response.message.should match(/Unprocessable/)
678 describe '一覧表示に於いて' do
680 Author.stub(:list).and_return([@author, @author, @author])
683 context 'つつがなく終わるとき' do
684 it 'ステータスコード200 OKを返す' do
686 response.should be_success
689 it 'indexテンプレートを描画する' do
691 response.should render_template("index")
696 get :index, :format => :json
697 lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
701 context '作家権限がないとき' do
705 it 'ステータスコード200 OKを返す' do
707 response.should be_success
710 it 'indexテンプレートを描画する' do
712 response.should render_template("index")
717 get :index, :format => :json
718 lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
726 Author.stub(:show).and_return(@author)
729 context 'つつがなく終わるとき' do
730 it 'ステータスコード200 OKを返す' do
731 get :show, :id => @author.id
732 response.should be_success
735 it 'showテンプレートを描画する' do
736 get :show, :id => @author.id
737 response.should render_template("show")
742 get :show, :id => @author.id, :format => :json
743 lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
747 context '作家権限がないとき' do
751 it 'ステータスコード200 OKを返す' do
752 get :show, :id => @author.id
753 response.should be_success
756 it 'showテンプレートを描画する' do
757 get :show, :id => @author.id
758 response.should render_template("show")
763 get :show, :id => @author.id, :format => :json
764 lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
770 describe '作家数取得に於いて' do
772 Author.should_receive(:visible_count).and_return(3)
775 context 'つつがなく終わるとき' do
776 it 'ステータスコード200 OKを返す' do
777 get :count, :format => :json
778 response.should be_success
782 get :count, :format => :json
783 lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
789 describe '新規作成フォーム表示に於いて' do
791 @new_user = FactoryGirl.create( :user_yas)
794 context 'つつがなく終わるとき' do
795 it 'ステータスコード200 OKを返す' do
797 response.should be_success
800 it 'newテンプレートを描画する' do
802 response.should render_template("new")
806 it 'new.jsテンプレートを描画する' do
807 get :new, :format => :js
808 response.should render_template("new")
813 get :new, :format => :json
814 lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
818 context '作家権限がないとき' do
823 it 'ステータスコード302 Foundを返す' do
825 response.status.should eq 302
827 it 'サインインページへ遷移する' do
829 response.body.should redirect_to '/users/sign_in'
833 it 'ステータスコード401 Unauthorizedを返す' do
834 get :new, :format => :js
835 response.status.should eq 401
837 it '応答メッセージにUnauthorizedを返す' do
838 get :new, :format => :js
839 response.message.should match(/Unauthorized/)
843 it 'ステータスコード401 Unauthorizedを返す' do
844 get :new, :format => :json
845 response.status.should eq 401
847 it '応答メッセージにUnauthorizedを返す' do
848 get :new, :format => :json
849 response.message.should match(/Unauthorized/)
855 describe '新規作成に於いて' do
857 @new_user = FactoryGirl.create( :user_yas)
859 @attr = FactoryGirl.attributes_for(:author, :user_id => @new_user.id, :name => 'ken')
861 context 'つつがなく終わるとき' do
863 it 'ステータスコード302 Foundを返す' do
864 Author.any_instance.stub(:save).and_return(true)
865 post :create, :author => @attr
866 response.status.should eq 302
869 # Author.any_instance.stub(:save).and_return(true)
870 post :create, :author => @attr
871 response.should redirect_to(root_path)
875 it 'ステータスコード200 OKを返す' do
876 # Author.any_instance.stub(:save).and_return(true)
877 post :create, :author => @attr, :format => :json
878 response.should be_success
882 context '作家権限がないとき' do
887 it 'ステータスコード302 Foundを返す' do
888 post :create, :author => @attr
889 response.status.should eq 302
891 it 'サインインページへ遷移する' do
892 post :create, :author => @attr
893 response.body.should redirect_to '/users/sign_in'
897 it 'ステータスコード401 Unauthorizedを返す' do
898 post :create, :author => @attr, :format => :json
899 response.status.should eq 401
901 it '応答メッセージにUnauthorizedを返す' do
902 post :create, :author => @attr, :format => :json
903 response.message.should match(/Unauthorized/)
909 describe '編集フォーム表示に於いて' do
912 Author.stub(:edit).and_return(@author)
914 context 'つつがなく終わるとき' do
915 it 'ステータスコード200 OKを返す' do
916 get :edit, :id => @author.id
917 response.should be_success
920 it 'editテンプレートを描画する' do
921 get :edit, :id => @author.id
922 response.should render_template("edit")
926 it 'edit.jsテンプレートを描画する' do
927 get :edit, :id => @author.id, :format => :js
928 response.should render_template("edit")
932 context '作家権限がないとき' do
937 it 'ステータスコード302 Foundを返す' do
938 get :edit, :id => @author.id
939 response.status.should eq 302
941 it 'サインインページへ遷移する' do
942 get :edit, :id => @author.id
943 response.body.should redirect_to '/users/sign_in'
947 it 'ステータスコード401 Unauthorizedを返す' do
948 get :edit, :id => @author.id, :format => :js
949 response.status.should eq 401
951 it '応答メッセージにUnauthorizedを返す' do
952 get :edit, :id => @author.id, :format => :js
953 response.message.should match(/Unauthorized/)
961 @attr = @author.attributes
962 @attr['name'] = 'ryu'
965 context 'つつがなく終わるとき' do
967 put :update, :id => @author.id, :author => @attr
968 Author.find(@author.id).name.should eq 'ryu'
971 it 'ステータスコード302 Foundを返す' do
972 Author.any_instance.stub(:save).with(any_args()).and_return(true)
973 put :update, :id => @author.id, :author => @attr
974 response.status.should eq 302
977 put :update, :id => @author.id, :author => @attr
978 response.should redirect_to('/home/configure')
982 it 'ステータスコード200 OKを返す' do
983 Author.any_instance.stub(:save).with(any_args()).and_return(true)
984 put :update, :id => @author.id, :author => @attr, :format => :json
985 response.should be_success
988 Author.any_instance.stub(:save).with(any_args()).and_return(true)
989 put :update, :id => @author.id, :author => @attr, :format => :json
990 response.body.should match /./
994 context '作家権限がないとき' do
998 it 'ステータスコード302 Foundを返す' do
999 put :update, :id => @author.id, :author => @attr
1000 response.status.should eq 302
1003 it 'サインインページへ遷移する' do
1004 put :update, :id => @author.id, :author => @attr
1005 response.body.should redirect_to '/users/sign_in'
1009 it '応答メッセージにUnauthorizedを返す' do
1010 put :update, :id => @author.id, :author => @attr, :format => :json
1011 response.message.should match(/Unauthorized/)