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/)
118 Author.stub(:show).and_return(@author)
121 context 'つつがなく終わるとき' do
122 it 'ステータスコード200 OKを返す' do
123 get :show, :id => @author.id
124 response.should be_success
126 it '作家モデルに単体取得を問い合わせている' do
127 Author.should_receive(:show).exactly(1)
130 #@authorだとログイン中のアカウントと干渉してしまう。
131 it '@auにアレを取得している' do
132 get :show, :id => @author.id
133 assigns(:au).should eq(@author)
136 it 'showテンプレートを描画する' do
137 get :show, :id => @author.id
138 response.should render_template("show")
143 get :show, :id => @author.id, :format => :json
144 lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
146 it '作家モデルにjson単体出力オプションを問い合わせている' do
147 Author.should_receive(:show_json_opt).exactly(1)
148 get :show, :id => @author.id, :format => :json
151 get :show, :id => @author.id, :format => :json
152 json = JSON.parse response.body
153 json["name"].should match(/test/)
154 json["user_id"].should eq @author.user_id
158 context '作家権限がないとき' do
163 it 'ステータスコード302 Foundを返す' do
164 get :show, :id => @author.id
165 response.status.should eq 302
167 it 'サインインページへ遷移する' do
168 get :show, :id => @author.id
169 response.body.should redirect_to '/users/sign_in'
173 it 'ステータスコード401 Unauthorizedを返す' do
174 get :show, :id => @author.id, :format => :json
175 response.status.should eq 401
177 it '応答メッセージにUnauthorizedを返す' do
178 get :show, :id => @author.id, :format => :json
179 response.message.should match(/Unauthorized/)
184 context '対象作家がないとき' do
186 it '例外404 not_foundを返す' do
189 }.should raise_error(ActiveRecord::RecordNotFound)
193 it '例外404 not_foundを返す' do
195 get :show, :id => 0, :format => :json
196 }.should raise_error(ActiveRecord::RecordNotFound)
203 describe '作家数取得に於いて' do
205 Author.should_receive(:visible_count).and_return(3)
208 context 'つつがなく終わるとき' do
209 it 'ステータスコード200 OKを返す' do
210 get :count, :format => :json
211 response.should be_success
215 get :count, :format => :json
216 lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
218 it 'データがHash構造になっていて作家数が3である' do
219 get :count, :format => :json
220 json = JSON.parse response.body
221 json["count"].should == 3
227 describe '新規作成フォーム表示に於いて' do
229 @new_user = FactoryGirl.create( :user_yas)
232 context 'つつがなく終わるとき' do
233 it 'ステータスコード200 OKを返す' do
235 response.should be_success
237 it '@auに新規データを用意している' do
239 assigns(:au).should be_a_new(Author)
241 it '作家モデルにデフォルト値補充を依頼している' do
242 Author.any_instance.should_receive(:supply_default).exactly(1)
246 it 'newテンプレートを描画する' do
248 response.should render_template("new")
252 it 'new.jsテンプレートを描画する' do
253 get :new, :format => :js
254 response.should render_template("new")
259 get :new, :format => :json
260 lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
262 it '作家モデルにjson単体出力オプションを問い合わせている' do
263 Author.should_receive(:show_json_opt).exactly(1)
264 get :new, :format => :json
268 context '作家権限がないとき' do
273 it 'ステータスコード302 Foundを返す' do
275 response.status.should eq 302
277 it 'サインインページへ遷移する' do
279 response.body.should redirect_to '/users/sign_in'
283 it 'ステータスコード401 Unauthorizedを返す' do
284 get :new, :format => :js
285 response.status.should eq 401
287 it '応答メッセージにUnauthorizedを返す' do
288 get :new, :format => :js
289 response.message.should match(/Unauthorized/)
293 it 'ステータスコード401 Unauthorizedを返す' do
294 get :new, :format => :json
295 response.status.should eq 401
297 it '応答メッセージにUnauthorizedを返す' do
298 get :new, :format => :json
299 response.message.should match(/Unauthorized/)
305 describe '新規作成に於いて' do
307 @new_user = FactoryGirl.create( :user_yas)
309 @attr = FactoryGirl.attributes_for(:author, :user_id => @new_user.id, :name => 'ken')
311 context '事前チェックしておく' do
312 it '作家モデルにデフォルト値補充を依頼している' do
313 Author.any_instance.should_receive(:supply_default).exactly(1)
314 post :create, :author => @attr
316 it '作家モデルにカラム値復元を依頼している' do
317 Author.any_instance.should_receive(:attributes=).exactly(1)
318 post :create, :author => @attr
320 it '作家モデルに上書き補充を依頼している' do
321 Author.any_instance.should_receive(:overwrite).exactly(1)
322 post :create, :author => @attr
325 Author.any_instance.should_receive(:save).exactly(1)
326 post :create, :author => @attr
329 context 'つつがなく終わるとき' do
330 it "@auに作成された作家を保持していて、それがDBにある" do
331 post :create, :author => @attr
332 assigns(:au).should be_a(Author)
333 assigns(:au).should be_persisted
336 it 'ステータスコード302 Foundを返す' do
337 Author.any_instance.stub(:save).and_return(true)
338 post :create, :author => @attr
339 response.status.should eq 302
342 # Author.any_instance.stub(:save).and_return(true)
343 post :create, :author => @attr
344 response.should redirect_to(root_path)
348 it 'ステータスコード200 OKを返す' do
349 # Author.any_instance.stub(:save).and_return(true)
350 post :create, :author => @attr, :format => :json
351 response.should be_success
353 it '作成された作家をjsonデータで返す' do
354 post :create, :author => @attr, :format => :json
355 lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
357 it '作家モデルにjson単体出力オプションを問い合わせている' do
358 Author.should_receive(:show_json_opt).exactly(1)
359 post :create, :author => @attr, :format => :json
362 post :create, :author => @attr, :format => :json
363 json = JSON.parse response.body
364 json["name"].should match(/ken/)
368 context '作家権限がないとき' do
373 it 'ステータスコード302 Foundを返す' do
374 post :create, :author => @attr
375 response.status.should eq 302
377 it 'サインインページへ遷移する' do
378 post :create, :author => @attr
379 response.body.should redirect_to '/users/sign_in'
383 it 'ステータスコード401 Unauthorizedを返す' do
384 post :create, :author => @attr, :format => :json
385 response.status.should eq 401
387 it '応答メッセージにUnauthorizedを返す' do
388 post :create, :author => @attr, :format => :json
389 response.message.should match(/Unauthorized/)
393 context '検証、保存に失敗した' do
395 Author.any_instance.stub(:save).and_return(false)
397 it "未保存の作家を保持している" do
398 post :create, :author => @attr
399 assigns(:au).should be_a_new(Author)
402 it 'ステータスコード200 OKを返す' do
403 post :create, :author => @attr
404 response.status.should eq 200
407 post :create, :author => @attr
408 response.should render_template("new")
412 it 'ステータスコード422 unprocessable_entity を返す' do
413 post :create, :author => @attr, :format => :json
414 response.status.should eq 422
416 it '応答メッセージUnprocessable Entityを返す' do
417 post :create, :author => @attr, :format => :json
418 response.message.should match(/Unprocessable/)
424 describe '編集フォーム表示に於いて' do
427 Author.stub(:edit).and_return(@author)
429 context 'つつがなく終わるとき' do
430 it 'ステータスコード200 OKを返す' do
431 get :edit, :id => @author.id
432 response.should be_success
434 it '作家モデルに編集取得を問い合わせている' do
435 Author.should_receive(:edit).exactly(1)
436 get :edit, :id => @author.id
438 #@authorだとログイン中のアカウントと干渉してしまう。
439 it '@auにデータを用意している' do
440 get :edit, :id => @author.id
441 assigns(:au).should eq @author
444 it 'editテンプレートを描画する' do
445 get :edit, :id => @author.id
446 response.should render_template("edit")
450 it 'edit.jsテンプレートを描画する' do
451 get :edit, :id => @author.id, :format => :js
452 response.should render_template("edit")
456 context '作家権限がないとき' do
461 it 'ステータスコード302 Foundを返す' do
462 get :edit, :id => @author.id
463 response.status.should eq 302
465 it 'サインインページへ遷移する' do
466 get :edit, :id => @author.id
467 response.body.should redirect_to '/users/sign_in'
471 it 'ステータスコード401 Unauthorizedを返す' do
472 get :edit, :id => @author.id, :format => :js
473 response.status.should eq 401
475 it '応答メッセージにUnauthorizedを返す' do
476 get :edit, :id => @author.id, :format => :js
477 response.message.should match(/Unauthorized/)
485 @attr = @author.attributes
486 @attr['name'] = 'ryu'
489 context '事前チェックしておく' do
490 it '作家モデルに編集取得を問い合わせている' do
491 Author.stub(:edit).with(any_args()).and_return @author
492 Author.should_receive(:edit).exactly(1)
493 put :update, :id => @author.id, :author => @attr
495 it 'モデルにpostデータ転送を依頼する' do
496 Author.any_instance.stub(:attributes=).with(any_args)
497 Author.any_instance.should_receive(:attributes=).exactly(1)
498 put :update, :id => @author.id, :author => @attr
500 it 'モデルに上書き補充を依頼する' do
501 Author.any_instance.stub(:overwrite).with(any_args)
502 Author.any_instance.should_receive(:overwrite).exactly(1)
503 put :update, :id => @author.id, :author => @attr
506 Author.any_instance.stub(:save).with(any_args).and_return true
507 Author.any_instance.should_receive(:save).exactly(1)
508 put :update, :id => @author.id, :author => @attr
510 it '@auにアレを取得している' do
511 put :update, :id => @author.id, :author => @attr
512 assigns(:au).should eq @author
515 context 'つつがなく終わるとき' do
517 put :update, :id => @author.id, :author => @attr
518 Author.find(@author.id).name.should eq 'ryu'
521 it 'ステータスコード302 Foundを返す' do
522 Author.any_instance.stub(:save).with(any_args()).and_return(true)
523 put :update, :id => @author.id, :author => @attr
524 response.status.should eq 302
527 put :update, :id => @author.id, :author => @attr
528 response.should redirect_to('/home/configure')
532 it 'ステータスコード200 OKを返す' do
533 Author.any_instance.stub(:save).with(any_args()).and_return(true)
534 put :update, :id => @author.id, :author => @attr, :format => :json
535 response.should be_success
538 Author.any_instance.stub(:save).with(any_args()).and_return(true)
539 put :update, :id => @author.id, :author => @attr, :format => :json
540 response.body.should match /./
544 context '作家権限がないとき' do
548 it 'ステータスコード302 Foundを返す' do
549 put :update, :id => @author.id, :author => @attr
550 response.status.should eq 302
553 it 'サインインページへ遷移する' do
554 put :update, :id => @author.id, :author => @attr
555 response.body.should redirect_to '/users/sign_in'
559 it '応答メッセージにUnauthorizedを返す' do
560 put :update, :id => @author.id, :author => @attr, :format => :json
561 response.message.should match(/Unauthorized/)
565 context '検証、保存に失敗したとき' do
567 Author.any_instance.stub(:save).and_return(false)
570 it 'ステータスコード200 Okを返す' do
571 put :update, :id => @author.id, :author => @attr
572 response.status.should eq 200
575 put :update, :id => @author.id, :author => @attr
576 response.should render_template("edit")
580 it 'ステータスコード422 unprocessable_entity を返す' do
581 Author.any_instance.stub(:save).and_return(false)
582 put :update, :id => @author.id, :author => @attr, :format => :json
583 response.status.should eq 422
585 it '応答メッセージUnprocessable Entityを返す' do
586 put :update, :id => @author.id, :author => @attr, :format => :json
587 response.message.should match(/Unprocessable/)
594 describe '一覧表示に於いて' do
596 Author.stub(:list).and_return([@author, @author, @author])
599 context 'つつがなく終わるとき' do
600 it 'ステータスコード200 OKを返す' do
602 response.should be_success
605 it 'indexテンプレートを描画する' do
607 response.should render_template("index")
612 get :index, :format => :json
613 lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
617 context '作家権限がないとき' do
621 it 'ステータスコード200 OKを返す' do
623 response.should be_success
626 it 'indexテンプレートを描画する' do
628 response.should render_template("index")
633 get :index, :format => :json
634 lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
642 Author.stub(:show).and_return(@author)
645 context 'つつがなく終わるとき' do
646 it 'ステータスコード200 OKを返す' do
647 get :show, :id => @author.id
648 response.should be_success
651 it 'showテンプレートを描画する' do
652 get :show, :id => @author.id
653 response.should render_template("show")
658 get :show, :id => @author.id, :format => :json
659 lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
663 context '作家権限がないとき' do
667 it 'ステータスコード200 OKを返す' do
668 get :show, :id => @author.id
669 response.should be_success
672 it 'showテンプレートを描画する' do
673 get :show, :id => @author.id
674 response.should render_template("show")
679 get :show, :id => @author.id, :format => :json
680 lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
686 describe '作家数取得に於いて' do
688 Author.should_receive(:visible_count).and_return(3)
691 context 'つつがなく終わるとき' do
692 it 'ステータスコード200 OKを返す' do
693 get :count, :format => :json
694 response.should be_success
698 get :count, :format => :json
699 lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
705 describe '新規作成フォーム表示に於いて' do
707 @new_user = FactoryGirl.create( :user_yas)
710 context 'つつがなく終わるとき' do
711 it 'ステータスコード200 OKを返す' do
713 response.should be_success
716 it 'newテンプレートを描画する' do
718 response.should render_template("new")
722 it 'new.jsテンプレートを描画する' do
723 get :new, :format => :js
724 response.should render_template("new")
729 get :new, :format => :json
730 lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
734 context '作家権限がないとき' do
739 it 'ステータスコード302 Foundを返す' do
741 response.status.should eq 302
743 it 'サインインページへ遷移する' do
745 response.body.should redirect_to '/users/sign_in'
749 it 'ステータスコード401 Unauthorizedを返す' do
750 get :new, :format => :js
751 response.status.should eq 401
753 it '応答メッセージにUnauthorizedを返す' do
754 get :new, :format => :js
755 response.message.should match(/Unauthorized/)
759 it 'ステータスコード401 Unauthorizedを返す' do
760 get :new, :format => :json
761 response.status.should eq 401
763 it '応答メッセージにUnauthorizedを返す' do
764 get :new, :format => :json
765 response.message.should match(/Unauthorized/)
771 describe '新規作成に於いて' do
773 @new_user = FactoryGirl.create( :user_yas)
775 @attr = FactoryGirl.attributes_for(:author, :user_id => @new_user.id, :name => 'ken')
777 context 'つつがなく終わるとき' do
779 it 'ステータスコード302 Foundを返す' do
780 Author.any_instance.stub(:save).and_return(true)
781 post :create, :author => @attr
782 response.status.should eq 302
785 # Author.any_instance.stub(:save).and_return(true)
786 post :create, :author => @attr
787 response.should redirect_to(root_path)
791 it 'ステータスコード200 OKを返す' do
792 # Author.any_instance.stub(:save).and_return(true)
793 post :create, :author => @attr, :format => :json
794 response.should be_success
798 context '作家権限がないとき' do
803 it 'ステータスコード302 Foundを返す' do
804 post :create, :author => @attr
805 response.status.should eq 302
807 it 'サインインページへ遷移する' do
808 post :create, :author => @attr
809 response.body.should redirect_to '/users/sign_in'
813 it 'ステータスコード401 Unauthorizedを返す' do
814 post :create, :author => @attr, :format => :json
815 response.status.should eq 401
817 it '応答メッセージにUnauthorizedを返す' do
818 post :create, :author => @attr, :format => :json
819 response.message.should match(/Unauthorized/)
825 describe '編集フォーム表示に於いて' do
828 Author.stub(:edit).and_return(@author)
830 context 'つつがなく終わるとき' do
831 it 'ステータスコード200 OKを返す' do
832 get :edit, :id => @author.id
833 response.should be_success
836 it 'editテンプレートを描画する' do
837 get :edit, :id => @author.id
838 response.should render_template("edit")
842 it 'edit.jsテンプレートを描画する' do
843 get :edit, :id => @author.id, :format => :js
844 response.should render_template("edit")
848 context '作家権限がないとき' do
853 it 'ステータスコード302 Foundを返す' do
854 get :edit, :id => @author.id
855 response.status.should eq 302
857 it 'サインインページへ遷移する' do
858 get :edit, :id => @author.id
859 response.body.should redirect_to '/users/sign_in'
863 it 'ステータスコード401 Unauthorizedを返す' do
864 get :edit, :id => @author.id, :format => :js
865 response.status.should eq 401
867 it '応答メッセージにUnauthorizedを返す' do
868 get :edit, :id => @author.id, :format => :js
869 response.message.should match(/Unauthorized/)
877 @attr = @author.attributes
878 @attr['name'] = 'ryu'
881 context 'つつがなく終わるとき' do
883 put :update, :id => @author.id, :author => @attr
884 Author.find(@author.id).name.should eq 'ryu'
887 it 'ステータスコード302 Foundを返す' do
888 Author.any_instance.stub(:save).with(any_args()).and_return(true)
889 put :update, :id => @author.id, :author => @attr
890 response.status.should eq 302
893 put :update, :id => @author.id, :author => @attr
894 response.should redirect_to('/home/configure')
898 it 'ステータスコード200 OKを返す' do
899 Author.any_instance.stub(:save).with(any_args()).and_return(true)
900 put :update, :id => @author.id, :author => @attr, :format => :json
901 response.should be_success
904 Author.any_instance.stub(:save).with(any_args()).and_return(true)
905 put :update, :id => @author.id, :author => @attr, :format => :json
906 response.body.should match /./
910 context '作家権限がないとき' do
914 it 'ステータスコード302 Foundを返す' do
915 put :update, :id => @author.id, :author => @attr
916 response.status.should eq 302
919 it 'サインインページへ遷移する' do
920 put :update, :id => @author.id, :author => @attr
921 response.body.should redirect_to '/users/sign_in'
925 it '応答メッセージにUnauthorizedを返す' do
926 put :update, :id => @author.id, :author => @attr, :format => :json
927 response.message.should match(/Unauthorized/)