1 # -*- encoding: utf-8 -*-
4 describe StoriesController do
6 @admin = FactoryGirl.create :admin
7 @sp = FactoryGirl.create :system_picture
8 @lg = FactoryGirl.create :license_group
9 @license = FactoryGirl.create :license, :license_group_id => @lg.id, :system_picture_id => @sp.id
10 @user = FactoryGirl.create :user_yas
11 @author = FactoryGirl.create :author, :user_id => @user.id
12 @comic = FactoryGirl.create :comic, :author_id => @user.author.id
13 @panel = FactoryGirl.create :panel, :author_id => @author.id
16 describe '一覧表示に於いて' do
19 @story = FactoryGirl.create :story, :t => 0, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
20 Story.stub(:list).and_return([@story, @story, @story])
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 Story.default_page_size
39 it '最大を超えると@page_sizeにデフォルト最大値が入る' do
40 get :index, :page_size => 1500
41 assigns(:page_size).should eq Story.max_page_size
43 it '不正な値が入ると@page_sizeにデフォルト最大値が入る' do
44 get :index, :page_size => 0
45 assigns(:page_size).should eq Story.default_page_size
48 context 'つつがなく終わるとき' do
49 it 'ステータスコード200 OKを返す' do
51 response.should be_success
53 it 'ストーリーモデルに一覧を問い合わせている' do
54 Story.should_receive(:list).exactly(1)
57 it '@storiesにリストを取得している' do
59 assigns(:stories).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 Story.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?("panel_id").should be_true
85 json.first.has_key?("comic_id").should be_true
86 json.first.has_key?("t").should be_true
90 context '作家権限がないとき' do
95 it 'ステータスコード302 Foundを返す' do
97 response.status.should eq 302
101 response.should redirect_to '/users/sign_in'
105 it 'ステータスコード401 Unauthorizedを返す' do
106 get :index, :format => :json
107 response.status.should eq 401
109 it '応答メッセージにUnauthorizedを返す' do
110 get :index, :format => :json
111 response.message.should match(/Unauthorized/)
117 describe '単体表示に於いて' do
120 @story = FactoryGirl.create :story, :t => 0, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
121 Comic.stub(:show).with(@comic.id.to_s, @author).and_return(@comic)
122 Story.stub(:show).with(@story.id.to_s, @author).and_return(@story)
124 context 'つつがなく終わるとき' do
125 it 'ストーリーモデルに単体取得を問い合わせている' do
126 Story.should_receive(:show).with(@story.id.to_s, @author).exactly(1)
127 get :show, :id => @story.id
129 it '@storyにアレを取得している' do
130 get :show, :id => @story.id
131 assigns(:story).should eq @story
134 it 'ステータスコード200 OKを返す' do
135 get :show, :id => @story.id
136 response.should be_success
138 it 'showテンプレートを描画する' do
139 get :show, :id => @story.id
140 response.should render_template("show")
144 it 'ステータスコード200 OKを返す' do
145 get :show, :id => @story.id, :format => :json
146 response.should be_success
149 get :show, :id => @story.id, :format => :json
150 lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
152 it 'ストーリーモデルにストーリーjson出力を問い合わせている' do
153 Story.any_instance.should_receive(:story_as_json).exactly(1)
154 get :show, :id => @story.id, :format => :json
157 get :show, :id => @story.id, :format => :json
158 json = JSON.parse response.body
159 json.has_key?("panel_id").should be_true
160 json.has_key?("comic_id").should be_true
161 json.has_key?("author_id").should be_true
165 context '作家権限がないとき' do
170 it 'ステータスコード302 Foundを返す' do
171 get :show, :id => @story.id
172 response.status.should eq 302
174 it 'サインインページへ遷移する' do
175 get :show, :id => @story.id
176 response.body.should redirect_to '/users/sign_in'
180 it 'ステータスコード401 Unauthorizedを返す' do
181 get :show, :id => @story.id, :format => :json
182 response.status.should eq 401
184 it '応答メッセージにUnauthorizedを返す' do
185 get :show, :id => @story.id, :format => :json
186 response.message.should match(/Unauthorized/)
194 @story = FactoryGirl.create :story, :t => 0, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
195 Comic.stub(:show).with(@comic.id.to_s, @author).and_return(@comic)
196 Story.stub(:count).and_return(10)
197 Story.stub(:play_list).with(any_args).and_return([@story, @story, @story])
200 context 'パラメータチェックする' do
201 it '与えられたoffsetがセットされている' do
202 get :comic, :id => @comic.id, :offset => 5
203 assigns(:offset).should eq 5
205 it '省略されると@offsetに0値が入る' do
206 get :comic, :id => @comic.id
207 assigns(:offset).should eq 0
209 it '与えられたcountがセットされている' do
210 get :comic, :id => @comic.id, :count => 4
211 assigns(:panel_count).should eq 4
213 it '省略されると@panel_countにデフォルト値が入る' do
214 get :comic, :id => @comic.id
215 assigns(:panel_count).should eq Story.default_panel_size
217 it '最大を超えると@panel_countにデフォルト最大値が入る' do
218 get :comic, :id => @comic.id, :count => 1500
219 assigns(:panel_count).should eq Story.max_panel_size
221 it '不正な値が入ると@panel_countにデフォルト最大値が入る' do
222 get :comic, :id => @comic.id, :count => -1
223 assigns(:panel_count).should eq Story.default_panel_size
226 context '事前チェックする' do
227 it 'コミックモデルに単体取得を問い合わせている' do
228 Comic.should_receive(:show).with(@comic.id.to_s, @author).exactly(1)
229 get :comic, :id => @comic.id
231 it 'ストーリーモデルにプレイリスト取得を問い合わせている' do
232 Story.should_receive(:play_list).with(@comic, @author, 0, 30).exactly(1)
233 get :comic, :id => @comic.id
236 context 'つつがなく終わるとき' do
237 it '@storiesにリストを取得している' do
238 get :comic, :id => @comic.id
239 assigns(:stories).should have_at_least(3).items
242 it 'ステータスコード200 OKを返す' do
243 get :comic, :id => @comic.id
244 response.should be_success
246 it 'comicテンプレートを描画する' do
247 get :comic, :id => @comic.id
248 response.should render_template("comic")
252 it 'ステータスコード200 OKを返す' do
253 get :comic, :id => @comic.id, :format => :json
254 response.should be_success
257 get :comic, :id => @comic.id, :format => :json
258 lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
260 it 'ストーリーモデルにストーリーリストのjson出力を問い合わせている' do
261 Story.should_receive(:list_as_json_text).exactly(1)
262 get :comic, :id => @comic.id, :format => :json
264 it 'データがリスト構造になっている' do
265 get :comic, :id => @comic.id, :format => :json
266 json = JSON.parse response.body
267 json.should have_at_least(3).items
269 it 'リストの先頭くらいはストーリーっぽいものであって欲しい' do
270 get :comic, :id => @comic.id, :format => :json
271 json = JSON.parse response.body
272 json.first.has_key?("panel_id").should be_true
273 json.first.has_key?("comic_id").should be_true
274 json.first.has_key?("author_id").should be_true
278 context '作家権限がないとき' do
283 it 'ステータスコード302 Foundを返す' do
284 get :comic, :id => @comic.id
285 response.status.should eq 302
287 it 'サインインページへ遷移する' do
288 get :comic, :id => @comic.id
289 response.should redirect_to '/users/sign_in'
293 it 'ステータスコード401 Unauthorizedを返す' do
294 get :comic, :id => @comic.id, :format => :json
295 response.status.should eq 401
297 it '応答メッセージにUnauthorizedを返す' do
298 get :comic, :id => @comic.id, :format => :json
299 response.message.should match(/Unauthorized/)
305 describe '新規作成フォーム表示に於いて' do
309 context 'つつがなく終わるとき' do
310 it '@storyに新規データを用意している' do
312 assigns(:story).should be_a_new(Story)
314 it 'モデルにデフォルト値補充を依頼している' do
315 Story.any_instance.should_receive(:supply_default).exactly(1)
319 it 'ステータスコード200 OKを返す' do
321 response.should be_success
323 it 'newテンプレートを描画する' do
325 response.should render_template("new")
329 it 'ステータスコード200 OKを返す' do
330 get :new, :format => :js
331 response.should be_success
333 it 'new.jsテンプレートを描画する' do
334 get :new, :format => :js
335 response.should render_template("new")
339 it 'ステータスコード200 OKを返す' do
340 get :new, :format => :json
341 response.should be_success
344 get :new, :format => :json
345 lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
347 it 'ストーリーモデルのストーリーのjson出力を問い合わせている' do
348 Story.any_instance.should_receive(:story_as_json).exactly(1)
349 get :new, :format => :json
352 get :new, :format => :json
353 json = JSON.parse response.body
354 json.has_key?("panel_id").should be_true
355 json.has_key?("comic_id").should be_true
356 json.has_key?("author_id").should be_true
360 context '作家権限がないとき' do
365 it 'ステータスコード302 Foundを返す' do
367 response.status.should eq 302
369 it 'サインインページへ遷移する' do
371 response.body.should redirect_to '/users/sign_in'
375 it 'ステータスコード401 Unauthorizedを返す' do
376 get :new, :format => :json
377 response.status.should eq 401
379 it '応答メッセージにUnauthorizedを返す' do
380 get :new, :format => :json
381 response.message.should match(/Unauthorized/)
387 describe '新規作成に於いて' do
389 @attr = FactoryGirl.attributes_for(:story, :t => nil, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id)
392 context 'つつがなく終わるとき' do
393 it 'デフォルト値補充を依頼する' do
394 Story.any_instance.should_receive(:supply_default).exactly(1)
395 post :create, :story => @attr
397 it 'POSTデータから、カラム値を復元している' do
398 Story.any_instance.stub(:store).and_return(true)
399 post :create, :story => @attr
400 assigns(:story).comic_id.should eq @comic.id
401 assigns(:story).panel_id.should eq @panel.id
404 Story.any_instance.should_receive(:overwrite).exactly(1)
405 post :create, :story => @attr
407 it 'コミックモデルに編集取得を依頼している' do
408 Comic.should_receive(:edit).with(@comic.id, @author).exactly(1)
409 post :create, :story => @attr
411 it 'コマモデルに単体取得を依頼している' do
412 Panel.should_receive(:show).with(@panel.id, @author).exactly(1)
413 post :create, :story => @attr
416 Story.any_instance.should_receive(:store).exactly(1)
417 post :create, :story => @attr
419 it "@storyに作成されたコマを保持していて、それがDBにある" do
420 post :create, :story => @attr
421 assigns(:story).should be_a(Story)
422 assigns(:story).should be_persisted
425 it 'ステータスコード302 Foundを返す' do
426 Story.any_instance.stub(:store).and_return(true)
427 post :create, :story => @attr
428 response.status.should eq 302
430 it 'コミックのストーリー表示へ遷移する' do
431 # Story.any_instance.stub(:store).and_return(true)
432 post :create, :story => @attr
433 response.should redirect_to(:action => :comic, :id => @attr[:comic_id])
437 it 'ステータスコード200 OKを返す' do
438 # Story.any_instance.stub(:store).and_return(true)
439 post :create, :story => @attr, :format => :json
440 response.should be_success
442 it '作成されたコマをjsonデータで返す' do
443 post :create, :story => @attr, :format => :json
444 lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
446 it 'ストーリーモデルのストーリーのjson出力を問い合わせている' do
447 Story.any_instance.should_receive(:story_as_json).exactly(1)
448 post :create, :story => @attr, :format => :json
451 post :create, :story => @attr, :format => :json
452 json = JSON.parse response.body
453 json["t"].should eq 0
457 context '作家権限がないとき' do
462 it 'ステータスコード302 Foundを返す' do
463 post :create, :story => @attr
464 response.status.should eq 302
466 it 'サインインページへ遷移する' do
467 post :create, :story => @attr
468 response.body.should redirect_to '/users/sign_in'
472 it 'ステータスコード401 Unauthorizedを返す' do
473 post :create, :story => @attr, :format => :json
474 response.status.should eq 401
476 it '応答メッセージにUnauthorizedを返す' do
477 post :create, :story => @attr, :format => :json
478 response.message.should match(/Unauthorized/)
482 context '検証、保存に失敗した' do
484 Story.any_instance.stub(:store).and_return(false)
486 it "未保存のコマを保持している" do
487 post :create, :story => @attr
488 assigns(:story).should be_a_new(Story)
491 it 'ステータスコード200 OKを返す' do
492 post :create, :story => @attr
493 response.status.should eq 200
496 post :create, :story => @attr
497 response.should render_template("new")
501 it 'ステータスコード422 unprocessable_entity を返す' do
502 post :create, :story => @attr, :format => :json
503 response.status.should eq 422
505 it '応答メッセージUnprocessable Entityを返す' do
506 post :create, :story => @attr, :format => :json
507 response.message.should match(/Unprocessable/)
513 describe '編集フォーム表示に於いて' do
515 @story = FactoryGirl.create :story, :author_id => @author.id
517 Story.stub(:show).and_return(@story)
519 context 'つつがなく終わるとき' do
520 it 'ストーリーモデルに編集取得を問い合わせている' do
521 Story.should_receive(:edit).exactly(1)
522 get :edit, :id => @story.id
524 it '@storyにデータを用意している' do
525 get :edit, :id => @story.id
526 assigns(:story).should eq @story
529 it 'ステータスコード200 OKを返す' do
530 get :edit, :id => @story.id
531 response.should be_success
533 it 'editテンプレートを描画する' do
534 get :edit, :id => @story.id
535 response.should render_template("edit")
539 it 'ステータスコード200 OKを返す' do
540 get :edit, :id => @story.id, :format => :js
541 response.should be_success
543 it 'edit.jsテンプレートを描画する' do
544 get :edit, :id => @story.id, :format => :js
545 response.should render_template("edit")
549 context '作家権限がないとき' do
554 it 'ステータスコード302 Foundを返す' do
555 get :edit, :id => @story.id
556 response.status.should eq 302
558 it 'サインインページへ遷移する' do
559 get :edit, :id => @story.id
560 response.body.should redirect_to '/users/sign_in'
564 it 'ステータスコード401 Unauthorizedを返す' do
565 get :edit, :id => @story.id, :format => :js
566 response.status.should eq 401
568 it '応答メッセージにUnauthorizedを返す' do
569 get :edit, :id => @story.id, :format => :js
570 response.message.should match(/Unauthorized/)
578 @story = FactoryGirl.create :story, :author_id => @user.author.id
579 @attr = FactoryGirl.attributes_for(:story, :author_id => @author.id)
582 context 'つつがなく終わるとき' do
584 Story.stub(:edit).with(any_args).and_return(@story)
585 Story.should_receive(:edit).exactly(1)
586 put :update, :id => @story.id, :story => @attr
588 it 'POSTデータから、カラム値を復元している' do
589 Story.any_instance.stub(:store).and_return(true)
590 Story.any_instance.should_receive(:attributes=).exactly(1)
591 put :update, :id => @story.id, :story => @attr
594 Story.any_instance.should_receive(:overwrite).exactly(1)
595 put :update, :id => @story.id, :story => @attr
598 Story.any_instance.should_receive(:store).exactly(1)
599 put :update, :id => @story.id, :story => @attr
601 it "@storyに作成されたストーリーを保持していて、それがDBにある" do
602 put :update, :id => @story.id, :story => @attr
603 assigns(:story).should be_a(Story)
604 assigns(:story).should be_persisted
607 it 'ステータスコード302 Foundを返す' do
608 Story.any_instance.stub(:store).and_return(true)
609 put :update, :id => @story.id, :story => @attr
610 response.status.should eq 302
613 # Story.any_instance.stub(:store).and_return(true)
614 put :update, :id => @story.id, :story => @attr
615 response.should redirect_to(:action => :comic, :id => @attr[:comic_id])
619 it 'ステータスコード200 OKを返す' do
620 # Story.any_instance.stub(:store).and_return(true)
621 put :update, :id => @story.id, :story => @attr, :format => :json
622 response.should be_success
626 context '作家権限がないとき' do
631 it 'ステータスコード302 Foundを返す' do
632 put :update, :id => @story.id, :story => @attr
633 response.status.should eq 302
635 it 'サインインページへ遷移する' do
636 put :update, :id => @story.id, :story => @attr
637 response.body.should redirect_to '/users/sign_in'
641 it 'ステータスコード401 Unauthorizedを返す' do
642 put :update, :id => @story.id, :story => @attr, :format => :json
643 response.status.should eq 401
645 it '応答メッセージにUnauthorizedを返す' do
646 put :update, :id => @story.id, :story => @attr, :format => :json
647 response.message.should match(/Unauthorized/)
651 context '検証、保存に失敗した' do
653 Story.any_instance.stub(:store).and_return(false)
656 put :update, :id => @story.id, :story => @attr
657 assigns(:story).should eq @story
660 it 'ステータスコード200 OKを返す' do
661 put :update, :id => @story.id, :story => @attr
662 response.status.should eq 200
665 put :update, :id => @story.id, :story => @attr
666 response.should render_template("edit")
670 it 'ステータスコード422 unprocessable_entity を返す' do
671 put :update, :id => @story.id, :story => @attr, :format => :json
672 response.status.should eq 422
674 it '応答メッセージUnprocessable Entityを返す' do
675 put :update, :id => @story.id, :story => @attr, :format => :json
676 response.message.should match(/Unprocessable/)
684 @story = FactoryGirl.create :story, :author_id => @author.id
686 Story.stub(:edit).and_return(@story)
688 context '事前チェックしておく' do
690 Story.stub(:edit).with(any_args()).and_return @story
691 Story.any_instance.stub(:destroy_and_shorten).with(any_args()).and_return(true)
693 it 'ストーリーモデルに編集取得を問い合わせている' do
694 Story.should_receive(:edit).exactly(1)
695 delete :destroy, :id => @story.id
698 Story.any_instance.should_receive(:destroy_and_shorten).exactly(1)
699 delete :destroy, :id => @story.id
701 it '@storyにアレを取得している' do
702 delete :destroy, :id => @story.id
703 assigns(:story).id.should eq(@story.id)
706 context 'つつがなく終わるとき' do
708 Story.any_instance.stub(:destroy_and_shorten).with(any_args()).and_return(true)
711 it 'ステータスコード302 Foundを返す' do
712 delete :destroy, :id => @story.id
713 response.status.should eq 302
716 delete :destroy, :id => @story.id
717 response.should redirect_to(:controller => 'stories', :action => :comic, :id => @story.comic_id)
721 it 'ステータスコード200 OKを返す' do
722 delete :destroy, :id => @story.id, :format => :json
723 response.should be_success
727 context '作家権限がないとき' do
732 it 'ステータスコード302 Foundを返す' do
733 delete :destroy, :id => @story.id
734 response.status.should eq 302
736 it 'サインインページへ遷移する' do
737 delete :destroy, :id => @story.id
738 response.body.should redirect_to '/users/sign_in'
742 it 'ステータスコード401 Unauthorizedを返す' do
743 delete :destroy, :id => @story.id, :format => :json
744 response.status.should eq 401
746 it '応答メッセージにUnauthorizedを返す' do
747 delete :destroy, :id => @story.id, :format => :json
748 response.message.should match(/Unauthorized/)
752 context '削除に失敗したとき' do
754 Story.any_instance.stub(:destroy_and_shorten).with(any_args()).and_return(false)
757 it 'ステータスコード302 Foundを返す' do
758 delete :destroy, :id => @story.id
759 response.status.should eq 302
761 it 'そのコミックの詳細ページへ遷移する' do
762 delete :destroy, :id => @story.id
763 response.should redirect_to(story_path(@story))
767 it 'ステータスコード422 unprocessable_entity を返す' do
768 delete :destroy, :id => @story.id, :format => :json
769 response.status.should eq 422
771 it '応答メッセージUnprocessable Entityを返す' do
772 delete :destroy, :id => @story.id, :format => :json
773 response.message.should match(/Unprocessable/)
778 context '対象ストーリーがないとき' do
780 context '他人のストーリーだったとき' do