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
124 context 'ユーザだが作家登録していないとき' do
128 it 'ステータスコード200 OKを返す' do
130 response.should be_success
137 Author.stub(:show).and_return(@author)
140 context 'つつがなく終わるとき' do
141 it 'ステータスコード200 OKを返す' do
142 get :show, :id => @author.id
143 response.should be_success
145 it '作家モデルに単体取得を問い合わせている' do
146 Author.should_receive(:show).exactly(1)
149 #@authorだとログイン中のアカウントと干渉してしまう。
150 it '@auにアレを取得している' do
151 get :show, :id => @author.id
152 assigns(:au).should eq(@author)
155 it 'showテンプレートを描画する' do
156 get :show, :id => @author.id
157 response.should render_template("show")
162 get :show, :id => @author.id, :format => :json
163 lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
165 it '作家モデルにjson単体出力オプションを問い合わせている' do
166 Author.should_receive(:show_json_opt).exactly(1)
167 get :show, :id => @author.id, :format => :json
170 get :show, :id => @author.id, :format => :json
171 json = JSON.parse response.body
172 json["name"].should match(/test/)
173 json["user_id"].should eq @author.user_id
177 context 'ユーザ権限がないとき' do
182 it 'ステータスコード302 Foundを返す' do
183 get :show, :id => @author.id
184 response.status.should eq 302
186 it 'サインインページへ遷移する' do
187 get :show, :id => @author.id
188 response.body.should redirect_to '/users/sign_in'
192 it 'ステータスコード401 Unauthorizedを返す' do
193 get :show, :id => @author.id, :format => :json
194 response.status.should eq 401
196 it '応答メッセージにUnauthorizedを返す' do
197 get :show, :id => @author.id, :format => :json
198 response.message.should match(/Unauthorized/)
202 context 'ユーザ権限はないが管理者権限があるとき' do
207 it 'ステータスコード200 OKを返す' do
208 get :show, :id => @author.id
209 response.should be_success
212 context 'ユーザだが作家登録していないとき' do
216 it 'ステータスコード200 OKを返す' do
217 get :show, :id => @author.id
218 response.should be_success
222 context '対象作家がないとき' do
224 it '例外404 not_foundを返す' do
227 }.should raise_error(ActiveRecord::RecordNotFound)
231 it '例外404 not_foundを返す' do
233 get :show, :id => 0, :format => :json
234 }.should raise_error(ActiveRecord::RecordNotFound)
241 describe '作家数取得に於いて' do
243 Author.should_receive(:visible_count).and_return(3)
246 context 'つつがなく終わるとき' do
247 it 'ステータスコード200 OKを返す' do
248 get :count, :format => :json
249 response.should be_success
253 get :count, :format => :json
254 lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
256 it 'データがHash構造になっていて作家数が3である' do
257 get :count, :format => :json
258 json = JSON.parse response.body
259 json["count"].should == 3
265 describe '新規作成フォーム表示に於いて' do
267 @new_user = FactoryGirl.create( :user_yas)
270 context 'つつがなく終わるとき' do
271 it 'ステータスコード200 OKを返す' do
273 response.should be_success
275 it '@auに新規データを用意している' do
277 assigns(:au).should be_a_new(Author)
279 it '作家モデルにデフォルト値補充を依頼している' do
280 Author.any_instance.should_receive(:supply_default).exactly(1)
284 it 'newテンプレートを描画する' do
286 response.should render_template("new")
290 it 'new.jsテンプレートを描画する' do
291 get :new, :format => :js
292 response.should render_template("new")
297 get :new, :format => :json
298 lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
300 it '作家モデルにjson単体出力オプションを問い合わせている' do
301 Author.should_receive(:show_json_opt).exactly(1)
302 get :new, :format => :json
306 context 'ユーザ権限がないとき' do
311 it 'ステータスコード302 Foundを返す' do
313 response.status.should eq 302
315 it 'サインインページへ遷移する' do
317 response.body.should redirect_to '/users/sign_in'
321 it 'ステータスコード401 Unauthorizedを返す' do
322 get :new, :format => :js
323 response.status.should eq 401
325 it '応答メッセージにUnauthorizedを返す' do
326 get :new, :format => :js
327 response.message.should match(/Unauthorized/)
331 it 'ステータスコード401 Unauthorizedを返す' do
332 get :new, :format => :json
333 response.status.should eq 401
335 it '応答メッセージにUnauthorizedを返す' do
336 get :new, :format => :json
337 response.message.should match(/Unauthorized/)
341 context 'ユーザ権限はないが管理者権限があるとき' do
347 it 'ステータスコード302 Foundを返す' do
349 response.status.should eq 302
351 it 'サインインページへ遷移する' do
353 response.body.should redirect_to '/users/sign_in'
357 context 'ユーザだが作家登録していないとき' do
361 it 'ステータスコード200 OKを返す' do
363 response.should be_success
368 describe '新規作成に於いて' do
370 @new_user = FactoryGirl.create( :user_yas)
372 @attr = FactoryGirl.attributes_for(:author, :user_id => @new_user.id, :name => 'ken')
374 context '事前チェックしておく' do
375 it '作家モデルにデフォルト値補充を依頼している' do
376 Author.any_instance.should_receive(:supply_default).exactly(1)
377 post :create, :author => @attr
379 it '作家モデルにカラム値復元を依頼している' do
380 Author.any_instance.should_receive(:attributes=).exactly(1)
381 post :create, :author => @attr
383 it '作家モデルに上書き補充を依頼している' do
384 Author.any_instance.should_receive(:overwrite).exactly(1)
385 post :create, :author => @attr
388 Author.any_instance.should_receive(:save).exactly(1)
389 post :create, :author => @attr
392 context 'つつがなく終わるとき' do
393 it "@auに作成された作家を保持していて、それがDBにある" do
394 post :create, :author => @attr
395 assigns(:au).should be_a(Author)
396 assigns(:au).should be_persisted
399 it 'ステータスコード302 Foundを返す' do
400 Author.any_instance.stub(:save).and_return(true)
401 post :create, :author => @attr
402 response.status.should eq 302
405 # Author.any_instance.stub(:save).and_return(true)
406 post :create, :author => @attr
407 response.should redirect_to(root_path)
411 it 'ステータスコード200 OKを返す' do
412 # Author.any_instance.stub(:save).and_return(true)
413 post :create, :author => @attr, :format => :json
414 response.should be_success
416 it '作成された作家をjsonデータで返す' do
417 post :create, :author => @attr, :format => :json
418 lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
420 it '作家モデルにjson単体出力オプションを問い合わせている' do
421 Author.should_receive(:show_json_opt).exactly(1)
422 post :create, :author => @attr, :format => :json
425 post :create, :author => @attr, :format => :json
426 json = JSON.parse response.body
427 json["name"].should match(/ken/)
431 context 'ユーザ権限がないとき' do
436 it 'ステータスコード302 Foundを返す' do
437 post :create, :author => @attr
438 response.status.should eq 302
440 it 'サインインページへ遷移する' do
441 post :create, :author => @attr
442 response.body.should redirect_to '/users/sign_in'
446 it 'ステータスコード401 Unauthorizedを返す' do
447 post :create, :author => @attr, :format => :json
448 response.status.should eq 401
450 it '応答メッセージにUnauthorizedを返す' do
451 post :create, :author => @attr, :format => :json
452 response.message.should match(/Unauthorized/)
456 context 'ユーザ権限はないが管理者権限があるとき' do
462 it 'ステータスコード302 Foundを返す' do
463 post :create, :author => @attr
464 response.status.should eq 302
466 it 'サインインページへ遷移する' do
467 post :create, :author => @attr
468 response.body.should redirect_to '/users/sign_in'
472 context '検証、保存に失敗した' do
474 Author.any_instance.stub(:save).and_return(false)
476 it "未保存の作家を保持している" do
477 post :create, :author => @attr
478 assigns(:au).should be_a_new(Author)
481 it 'ステータスコード200 OKを返す' do
482 post :create, :author => @attr
483 response.status.should eq 200
486 post :create, :author => @attr
487 response.should render_template("new")
491 it 'ステータスコード422 unprocessable_entity を返す' do
492 post :create, :author => @attr, :format => :json
493 response.status.should eq 422
495 it '応答メッセージUnprocessable Entityを返す' do
496 post :create, :author => @attr, :format => :json
497 response.message.should match(/Unprocessable/)
503 describe '編集フォーム表示に於いて' do
506 Author.stub(:edit).and_return(@author)
508 context 'つつがなく終わるとき' do
509 it 'ステータスコード200 OKを返す' do
510 get :edit, :id => @author.id
511 response.should be_success
513 it '作家モデルに編集取得を問い合わせている' do
514 Author.should_receive(:edit).exactly(1)
515 get :edit, :id => @author.id
517 #@authorだとログイン中のアカウントと干渉してしまう。
518 it '@auにデータを用意している' do
519 get :edit, :id => @author.id
520 assigns(:au).should eq @author
523 it 'editテンプレートを描画する' do
524 get :edit, :id => @author.id
525 response.should render_template("edit")
529 it 'edit.jsテンプレートを描画する' do
530 get :edit, :id => @author.id, :format => :js
531 response.should render_template("edit")
535 context 'ユーザ権限がないとき' do
540 it 'ステータスコード302 Foundを返す' do
541 get :edit, :id => @author.id
542 response.status.should eq 302
544 it 'サインインページへ遷移する' do
545 get :edit, :id => @author.id
546 response.body.should redirect_to '/users/sign_in'
550 it 'ステータスコード401 Unauthorizedを返す' do
551 get :edit, :id => @author.id, :format => :js
552 response.status.should eq 401
554 it '応答メッセージにUnauthorizedを返す' do
555 get :edit, :id => @author.id, :format => :js
556 response.message.should match(/Unauthorized/)
560 context 'ユーザ権限はないが管理者権限があるとき' do
566 it 'ステータスコード302 Foundを返す' do
567 get :edit, :id => @author.id
568 response.status.should eq 302
570 it 'サインインページへ遷移する' do
571 get :edit, :id => @author.id
572 response.body.should redirect_to '/users/sign_in'
576 context 'ユーザだが作家登録していないとき' do
578 @other_user = FactoryGirl.create( :user_yas)
579 @other_author = FactoryGirl.create :author, :user_id => @other_user.id
583 it 'ステータスコード302 Foundを返す' do
584 get :edit, :id => @other_author.id
585 response.status.should eq 302
588 get :edit, :id => @other_author.id
589 response.body.should redirect_to new_author_path
597 @attr = @author.attributes
598 @attr['name'] = 'ryu'
601 context '事前チェックしておく' do
602 it '作家モデルに編集取得を問い合わせている' do
603 Author.stub(:edit).with(any_args()).and_return @author
604 Author.should_receive(:edit).exactly(1)
605 put :update, :id => @author.id, :author => @attr
607 it 'モデルにpostデータ転送を依頼する' do
608 Author.any_instance.stub(:attributes=).with(any_args)
609 Author.any_instance.should_receive(:attributes=).exactly(1)
610 put :update, :id => @author.id, :author => @attr
612 it 'モデルに上書き補充を依頼する' do
613 Author.any_instance.stub(:overwrite).with(any_args)
614 Author.any_instance.should_receive(:overwrite).exactly(1)
615 put :update, :id => @author.id, :author => @attr
618 Author.any_instance.stub(:save).with(any_args).and_return true
619 Author.any_instance.should_receive(:save).exactly(1)
620 put :update, :id => @author.id, :author => @attr
622 it '@auにアレを取得している' do
623 put :update, :id => @author.id, :author => @attr
624 assigns(:au).should eq @author
627 context 'つつがなく終わるとき' do
629 put :update, :id => @author.id, :author => @attr
630 Author.find(@author.id).name.should eq 'ryu'
633 it 'ステータスコード302 Foundを返す' do
634 Author.any_instance.stub(:save).with(any_args()).and_return(true)
635 put :update, :id => @author.id, :author => @attr
636 response.status.should eq 302
639 put :update, :id => @author.id, :author => @attr
640 response.should redirect_to('/home/configure')
644 it 'ステータスコード200 OKを返す' do
645 Author.any_instance.stub(:save).with(any_args()).and_return(true)
646 put :update, :id => @author.id, :author => @attr, :format => :json
647 response.should be_success
650 Author.any_instance.stub(:save).with(any_args()).and_return(true)
651 put :update, :id => @author.id, :author => @attr, :format => :json
652 response.body.should match /./
656 context 'ユーザ権限がないとき' do
661 it 'ステータスコード302 Foundを返す' do
662 put :update, :id => @author.id, :author => @attr
663 response.status.should eq 302
665 it 'サインインページへ遷移する' do
666 put :update, :id => @author.id, :author => @attr
667 response.body.should redirect_to '/users/sign_in'
671 it '応答メッセージにUnauthorizedを返す' do
672 put :update, :id => @author.id, :author => @attr, :format => :json
673 response.message.should match(/Unauthorized/)
677 context 'ユーザ権限はないが管理者権限があるとき' do
683 it 'ステータスコード302 Foundを返す' do
684 put :update, :id => @author.id, :author => @attr
685 response.status.should eq 302
687 it 'サインインページへ遷移する' do
688 put :update, :id => @author.id, :author => @attr
689 response.body.should redirect_to '/users/sign_in'
693 context 'ユーザだが作家登録していないとき' do
695 @other_user = FactoryGirl.create( :user_yas)
696 @other_author = FactoryGirl.create :author, :user_id => @other_user.id
700 it 'ステータスコード302 Foundを返す' do
701 get :update, :id => @other_author.id
702 response.status.should eq 302
705 get :update, :id => @other_author.id
706 response.body.should redirect_to new_author_path
710 context '検証、保存に失敗したとき' do
712 Author.any_instance.stub(:save).and_return(false)
715 it 'ステータスコード200 Okを返す' do
716 put :update, :id => @author.id, :author => @attr
717 response.status.should eq 200
720 put :update, :id => @author.id, :author => @attr
721 response.should render_template("edit")
725 it 'ステータスコード422 unprocessable_entity を返す' do
726 Author.any_instance.stub(:save).and_return(false)
727 put :update, :id => @author.id, :author => @attr, :format => :json
728 response.status.should eq 422
730 it '応答メッセージUnprocessable Entityを返す' do
731 put :update, :id => @author.id, :author => @attr, :format => :json
732 response.message.should match(/Unprocessable/)
739 describe '一覧表示に於いて' do
741 Author.stub(:list).and_return([@author, @author, @author])
744 context 'つつがなく終わるとき' do
745 it 'ステータスコード200 OKを返す' do
747 response.should be_success
750 it 'indexテンプレートを描画する' do
752 response.should render_template("index")
757 get :index, :format => :json
758 lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
762 context 'ユーザ権限がないとき' do
766 it 'ステータスコード200 OKを返す' do
768 response.should be_success
771 it 'indexテンプレートを描画する' do
773 response.should render_template("index")
778 get :index, :format => :json
779 lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
787 Author.stub(:show).and_return(@author)
790 context 'つつがなく終わるとき' do
791 it 'ステータスコード200 OKを返す' do
792 get :show, :id => @author.id
793 response.should be_success
796 it 'showテンプレートを描画する' do
797 get :show, :id => @author.id
798 response.should render_template("show")
803 get :show, :id => @author.id, :format => :json
804 lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
808 context 'ユーザ権限がないとき' do
812 it 'ステータスコード200 OKを返す' do
813 get :show, :id => @author.id
814 response.should be_success
817 it 'showテンプレートを描画する' do
818 get :show, :id => @author.id
819 response.should render_template("show")
824 get :show, :id => @author.id, :format => :json
825 lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
831 describe '作家数取得に於いて' do
833 Author.should_receive(:visible_count).and_return(3)
836 context 'つつがなく終わるとき' do
837 it 'ステータスコード200 OKを返す' do
838 get :count, :format => :json
839 response.should be_success
843 get :count, :format => :json
844 lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
850 describe '新規作成フォーム表示に於いて' do
852 @new_user = FactoryGirl.create( :user_yas)
855 context 'つつがなく終わるとき' do
856 it 'ステータスコード200 OKを返す' do
858 response.should be_success
861 it 'newテンプレートを描画する' do
863 response.should render_template("new")
867 it 'new.jsテンプレートを描画する' do
868 get :new, :format => :js
869 response.should render_template("new")
874 get :new, :format => :json
875 lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
879 context 'ユーザ権限がないとき' do
884 it 'ステータスコード302 Foundを返す' do
886 response.status.should eq 302
888 it 'サインインページへ遷移する' do
890 response.body.should redirect_to '/users/sign_in'
894 it 'ステータスコード401 Unauthorizedを返す' do
895 get :new, :format => :js
896 response.status.should eq 401
898 it '応答メッセージにUnauthorizedを返す' do
899 get :new, :format => :js
900 response.message.should match(/Unauthorized/)
904 it 'ステータスコード401 Unauthorizedを返す' do
905 get :new, :format => :json
906 response.status.should eq 401
908 it '応答メッセージにUnauthorizedを返す' do
909 get :new, :format => :json
910 response.message.should match(/Unauthorized/)
916 describe '新規作成に於いて' do
918 @new_user = FactoryGirl.create( :user_yas)
920 @attr = FactoryGirl.attributes_for(:author, :user_id => @new_user.id, :name => 'ken')
922 context 'つつがなく終わるとき' do
924 it 'ステータスコード302 Foundを返す' do
925 Author.any_instance.stub(:save).and_return(true)
926 post :create, :author => @attr
927 response.status.should eq 302
930 # Author.any_instance.stub(:save).and_return(true)
931 post :create, :author => @attr
932 response.should redirect_to(root_path)
936 it 'ステータスコード200 OKを返す' do
937 # Author.any_instance.stub(:save).and_return(true)
938 post :create, :author => @attr, :format => :json
939 response.should be_success
943 context 'ユーザ権限がないとき' do
948 it 'ステータスコード302 Foundを返す' do
949 post :create, :author => @attr
950 response.status.should eq 302
952 it 'サインインページへ遷移する' do
953 post :create, :author => @attr
954 response.body.should redirect_to '/users/sign_in'
958 it 'ステータスコード401 Unauthorizedを返す' do
959 post :create, :author => @attr, :format => :json
960 response.status.should eq 401
962 it '応答メッセージにUnauthorizedを返す' do
963 post :create, :author => @attr, :format => :json
964 response.message.should match(/Unauthorized/)
970 describe '編集フォーム表示に於いて' do
973 Author.stub(:edit).and_return(@author)
975 context 'つつがなく終わるとき' do
976 it 'ステータスコード200 OKを返す' do
977 get :edit, :id => @author.id
978 response.should be_success
981 it 'editテンプレートを描画する' do
982 get :edit, :id => @author.id
983 response.should render_template("edit")
987 it 'edit.jsテンプレートを描画する' do
988 get :edit, :id => @author.id, :format => :js
989 response.should render_template("edit")
993 context 'ユーザ権限がないとき' do
998 it 'ステータスコード302 Foundを返す' do
999 get :edit, :id => @author.id
1000 response.status.should eq 302
1002 it 'サインインページへ遷移する' do
1003 get :edit, :id => @author.id
1004 response.body.should redirect_to '/users/sign_in'
1008 it 'ステータスコード401 Unauthorizedを返す' do
1009 get :edit, :id => @author.id, :format => :js
1010 response.status.should eq 401
1012 it '応答メッセージにUnauthorizedを返す' do
1013 get :edit, :id => @author.id, :format => :js
1014 response.message.should match(/Unauthorized/)
1020 describe '更新に於いて' do
1022 @attr = @author.attributes
1023 @attr['name'] = 'ryu'
1026 context 'つつがなく終わるとき' do
1028 put :update, :id => @author.id, :author => @attr
1029 Author.find(@author.id).name.should eq 'ryu'
1032 it 'ステータスコード302 Foundを返す' do
1033 Author.any_instance.stub(:save).with(any_args()).and_return(true)
1034 put :update, :id => @author.id, :author => @attr
1035 response.status.should eq 302
1038 put :update, :id => @author.id, :author => @attr
1039 response.should redirect_to('/home/configure')
1043 it 'ステータスコード200 OKを返す' do
1044 Author.any_instance.stub(:save).with(any_args()).and_return(true)
1045 put :update, :id => @author.id, :author => @attr, :format => :json
1046 response.should be_success
1048 it 'ページ本体は特に返さない' do
1049 Author.any_instance.stub(:save).with(any_args()).and_return(true)
1050 put :update, :id => @author.id, :author => @attr, :format => :json
1051 response.body.should match /./
1055 context 'ユーザ権限がないとき' do
1059 it 'ステータスコード302 Foundを返す' do
1060 put :update, :id => @author.id, :author => @attr
1061 response.status.should eq 302
1064 it 'サインインページへ遷移する' do
1065 put :update, :id => @author.id, :author => @attr
1066 response.body.should redirect_to '/users/sign_in'
1070 it '応答メッセージにUnauthorizedを返す' do
1071 put :update, :id => @author.id, :author => @attr, :format => :json
1072 response.message.should match(/Unauthorized/)