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 describe '単体表示に於いて' do
22 @story = FactoryGirl.create :story, :t => 0, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
23 Comic.stub(:show).with(@comic.id.to_s, @author).and_return(@comic)
24 Story.stub(:show).with(@story.id.to_s, @author).and_return(@story)
26 context 'つつがなく終わるとき' do
27 it 'ストーリーモデルに単体取得を問い合わせている' do
28 Story.should_receive(:show).with(@story.id.to_s, @author).exactly(1)
29 get :show, :id => @story.id
31 it '@storyにアレを取得している' do
32 get :show, :id => @story.id
33 assigns(:story).should eq @story
36 it 'ステータスコード200 OKを返す' do
37 get :show, :id => @story.id
38 response.should be_success
40 it 'showテンプレートを描画する' do
41 get :show, :id => @story.id
42 response.should render_template("show")
46 it 'ステータスコード200 OKを返す' do
47 get :show, :id => @story.id, :format => :json
48 response.should be_success
51 get :show, :id => @story.id, :format => :json
52 lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
54 it 'ストーリーモデルにストーリーjson出力を問い合わせている' do
55 Story.any_instance.should_receive(:story_as_json).exactly(1)
56 get :show, :id => @story.id, :format => :json
59 get :show, :id => @story.id, :format => :json
60 json = JSON.parse response.body
61 json.has_key?("panel_id").should be_true
62 json.has_key?("comic_id").should be_true
63 json.has_key?("author_id").should be_true
67 context '作家権限がないとき' do
72 it 'ステータスコード302 Foundを返す' do
73 get :show, :id => @story.id
74 response.status.should eq 302
77 get :show, :id => @story.id
78 response.body.should redirect_to '/users/sign_in'
82 it 'ステータスコード401 Unauthorizedを返す' do
83 get :show, :id => @story.id, :format => :json
84 response.status.should eq 401
86 it '応答メッセージにUnauthorizedを返す' do
87 get :show, :id => @story.id, :format => :json
88 response.message.should match(/Unauthorized/)
96 @story = FactoryGirl.create :story, :t => 0, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
97 Comic.stub(:show).with(@comic.id.to_s, @author).and_return(@comic)
98 Story.stub(:count).and_return(10)
99 Story.stub(:play_list).with(any_args).and_return([@story, @story, @story])
102 context 'パラメータチェックする' do
103 it '与えられたoffsetがセットされている' do
104 get :comic, :id => @comic.id, :offset => 5
105 assigns(:offset).should eq 5
107 it '省略されると@offsetに0値が入る' do
108 get :comic, :id => @comic.id
109 assigns(:offset).should eq 0
111 it '与えられたcountがセットされている' do
112 get :comic, :id => @comic.id, :count => 4
113 assigns(:panel_count).should eq 4
115 it '省略されると@panel_countにデフォルト値が入る' do
116 get :comic, :id => @comic.id
117 assigns(:panel_count).should eq Story.default_panel_size
119 it '最大を超えると@panel_countにデフォルト最大値が入る' do
120 get :comic, :id => @comic.id, :count => 1500
121 assigns(:panel_count).should eq Story.max_panel_size
123 it '不正な値が入ると@panel_countにデフォルト最大値が入る' do
124 get :comic, :id => @comic.id, :count => -1
125 assigns(:panel_count).should eq Story.default_panel_size
128 context '事前チェックする' do
129 it 'コミックモデルに単体取得を問い合わせている' do
130 Comic.should_receive(:show).with(@comic.id.to_s, @author).exactly(1)
131 get :comic, :id => @comic.id
133 it 'ストーリーモデルにプレイリスト取得を問い合わせている' do
134 Story.should_receive(:play_list).with(@comic, @author, 0, 30).exactly(1)
135 get :comic, :id => @comic.id
138 context 'つつがなく終わるとき' do
139 it '@storiesにリストを取得している' do
140 get :comic, :id => @comic.id
141 assigns(:stories).should have_at_least(3).items
144 it 'ステータスコード200 OKを返す' do
145 get :comic, :id => @comic.id
146 response.should be_success
148 it 'comicテンプレートを描画する' do
149 get :comic, :id => @comic.id
150 response.should render_template("comic")
154 it 'ステータスコード200 OKを返す' do
155 get :comic, :id => @comic.id, :format => :json
156 response.should be_success
159 get :comic, :id => @comic.id, :format => :json
160 lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
162 it 'ストーリーモデルにストーリーリストのjson出力を問い合わせている' do
163 Story.should_receive(:list_as_json_text).exactly(1)
164 get :comic, :id => @comic.id, :format => :json
166 it 'データがリスト構造になっている' do
167 get :comic, :id => @comic.id, :format => :json
168 json = JSON.parse response.body
169 json.should have_at_least(3).items
171 it 'リストの先頭くらいはストーリーっぽいものであって欲しい' do
172 get :comic, :id => @comic.id, :format => :json
173 json = JSON.parse response.body
174 json.first.has_key?("panel_id").should be_true
175 json.first.has_key?("comic_id").should be_true
176 json.first.has_key?("author_id").should be_true
180 context '作家権限がないとき' do
185 it 'ステータスコード302 Foundを返す' do
186 get :comic, :id => @comic.id
187 response.status.should eq 302
189 it 'サインインページへ遷移する' do
190 get :comic, :id => @comic.id
191 response.should redirect_to '/users/sign_in'
195 it 'ステータスコード401 Unauthorizedを返す' do
196 get :comic, :id => @comic.id, :format => :json
197 response.status.should eq 401
199 it '応答メッセージにUnauthorizedを返す' do
200 get :comic, :id => @comic.id, :format => :json
201 response.message.should match(/Unauthorized/)
207 describe '新規作成フォーム表示に於いて' do
211 context 'つつがなく終わるとき' do
212 it '@storyに新規データを用意している' do
214 assigns(:story).should be_a_new(Story)
216 it 'モデルにデフォルト値補充を依頼している' do
217 Story.any_instance.should_receive(:supply_default).exactly(1)
221 it 'ステータスコード200 OKを返す' do
223 response.should be_success
225 it 'newテンプレートを描画する' do
227 response.should render_template("new")
231 it 'ステータスコード200 OKを返す' do
232 get :new, :format => :js
233 response.should be_success
235 it 'new.jsテンプレートを描画する' do
236 get :new, :format => :js
237 response.should render_template("new")
241 it 'ステータスコード200 OKを返す' do
242 get :new, :format => :json
243 response.should be_success
246 get :new, :format => :json
247 lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
249 it 'ストーリーモデルのストーリーのjson出力を問い合わせている' do
250 Story.any_instance.should_receive(:story_as_json).exactly(1)
251 get :new, :format => :json
254 get :new, :format => :json
255 json = JSON.parse response.body
256 json.has_key?("panel_id").should be_true
257 json.has_key?("comic_id").should be_true
258 json.has_key?("author_id").should be_true
262 context '作家権限がないとき' do
267 it 'ステータスコード302 Foundを返す' do
269 response.status.should eq 302
271 it 'サインインページへ遷移する' do
273 response.body.should redirect_to '/users/sign_in'
277 it 'ステータスコード401 Unauthorizedを返す' do
278 get :new, :format => :json
279 response.status.should eq 401
281 it '応答メッセージにUnauthorizedを返す' do
282 get :new, :format => :json
283 response.message.should match(/Unauthorized/)
289 describe '新規作成に於いて' do
291 @attr = FactoryGirl.attributes_for(:story, :t => nil, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id)
294 context 'つつがなく終わるとき' do
295 it 'デフォルト値補充を依頼する' do
296 Story.any_instance.should_receive(:supply_default).exactly(1)
297 post :create, :story => @attr
299 it 'POSTデータから、カラム値を復元している' do
300 Story.any_instance.stub(:store).and_return(true)
301 post :create, :story => @attr
302 assigns(:story).comic_id.should eq @comic.id
303 assigns(:story).panel_id.should eq @panel.id
306 Story.any_instance.should_receive(:overwrite).exactly(1)
307 post :create, :story => @attr
309 it 'コミックモデルに編集取得を依頼している' do
310 Comic.should_receive(:edit).with(@comic.id, @author).exactly(1)
311 post :create, :story => @attr
313 it 'コマモデルに単体取得を依頼している' do
314 Panel.should_receive(:show).with(@panel.id, @author).exactly(1)
315 post :create, :story => @attr
318 Story.any_instance.should_receive(:store).exactly(1)
319 post :create, :story => @attr
321 it "@storyに作成されたコマを保持していて、それがDBにある" do
322 post :create, :story => @attr
323 assigns(:story).should be_a(Story)
324 assigns(:story).should be_persisted
327 it 'ステータスコード302 Foundを返す' do
328 Story.any_instance.stub(:store).and_return(true)
329 post :create, :story => @attr
330 response.status.should eq 302
332 it 'コミックのストーリー表示へ遷移する' do
333 # Story.any_instance.stub(:store).and_return(true)
334 post :create, :story => @attr
335 response.should redirect_to(:action => :comic, :id => @attr[:comic_id])
339 it 'ステータスコード200 OKを返す' do
340 # Story.any_instance.stub(:store).and_return(true)
341 post :create, :story => @attr, :format => :json
342 response.should be_success
344 it '作成されたコマをjsonデータで返す' do
345 post :create, :story => @attr, :format => :json
346 lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
348 it 'ストーリーモデルのストーリーのjson出力を問い合わせている' do
349 Story.any_instance.should_receive(:story_as_json).exactly(1)
350 post :create, :story => @attr, :format => :json
353 post :create, :story => @attr, :format => :json
354 json = JSON.parse response.body
355 json["t"].should eq 0
359 context '作家権限がないとき' do
364 it 'ステータスコード302 Foundを返す' do
365 post :create, :story => @attr
366 response.status.should eq 302
368 it 'サインインページへ遷移する' do
369 post :create, :story => @attr
370 response.body.should redirect_to '/users/sign_in'
374 it 'ステータスコード401 Unauthorizedを返す' do
375 post :create, :story => @attr, :format => :json
376 response.status.should eq 401
378 it '応答メッセージにUnauthorizedを返す' do
379 post :create, :story => @attr, :format => :json
380 response.message.should match(/Unauthorized/)
384 context '検証、保存に失敗した' do
386 Story.any_instance.stub(:store).and_return(false)
388 it "未保存のコマを保持している" do
389 post :create, :story => @attr
390 assigns(:story).should be_a_new(Story)
393 it 'ステータスコード200 OKを返す' do
394 post :create, :story => @attr
395 response.status.should eq 200
398 post :create, :story => @attr
399 response.should render_template("new")
403 it 'ステータスコード422 unprocessable_entity を返す' do
404 post :create, :story => @attr, :format => :json
405 response.status.should eq 422
407 it '応答メッセージUnprocessable Entityを返す' do
408 post :create, :story => @attr, :format => :json
409 response.message.should match(/Unprocessable/)
415 describe '編集フォーム表示に於いて' do
417 @story = FactoryGirl.create :story, :author_id => @author.id
419 Story.stub(:show).and_return(@story)
421 context 'つつがなく終わるとき' do
422 it 'ストーリーモデルに編集取得を問い合わせている' do
423 Story.should_receive(:edit).exactly(1)
424 get :edit, :id => @story.id
426 it '@storyにデータを用意している' do
427 get :edit, :id => @story.id
428 assigns(:story).should eq @story
431 it 'ステータスコード200 OKを返す' do
432 get :edit, :id => @story.id
433 response.should be_success
435 it 'editテンプレートを描画する' do
436 get :edit, :id => @story.id
437 response.should render_template("edit")
441 it 'ステータスコード200 OKを返す' do
442 get :edit, :id => @story.id, :format => :js
443 response.should be_success
445 it 'edit.jsテンプレートを描画する' do
446 get :edit, :id => @story.id, :format => :js
447 response.should render_template("edit")
451 context '作家権限がないとき' do
456 it 'ステータスコード302 Foundを返す' do
457 get :edit, :id => @story.id
458 response.status.should eq 302
460 it 'サインインページへ遷移する' do
461 get :edit, :id => @story.id
462 response.body.should redirect_to '/users/sign_in'
466 it 'ステータスコード401 Unauthorizedを返す' do
467 get :edit, :id => @story.id, :format => :js
468 response.status.should eq 401
470 it '応答メッセージにUnauthorizedを返す' do
471 get :edit, :id => @story.id, :format => :js
472 response.message.should match(/Unauthorized/)
480 @story = FactoryGirl.create :story, :author_id => @user.author.id
481 @attr = FactoryGirl.attributes_for(:story, :author_id => @author.id)
484 context 'つつがなく終わるとき' do
486 Story.stub(:edit).with(any_args).and_return(@story)
487 Story.should_receive(:edit).exactly(1)
488 put :update, :id => @story.id, :story => @attr
490 it 'POSTデータから、カラム値を復元している' do
491 Story.any_instance.stub(:store).and_return(true)
492 Story.any_instance.should_receive(:attributes=).exactly(1)
493 put :update, :id => @story.id, :story => @attr
496 Story.any_instance.should_receive(:overwrite).exactly(1)
497 put :update, :id => @story.id, :story => @attr
500 Story.any_instance.should_receive(:store).exactly(1)
501 put :update, :id => @story.id, :story => @attr
503 it "@storyに作成されたストーリーを保持していて、それがDBにある" do
504 put :update, :id => @story.id, :story => @attr
505 assigns(:story).should be_a(Story)
506 assigns(:story).should be_persisted
509 it 'ステータスコード302 Foundを返す' do
510 Story.any_instance.stub(:store).and_return(true)
511 put :update, :id => @story.id, :story => @attr
512 response.status.should eq 302
515 # Story.any_instance.stub(:store).and_return(true)
516 put :update, :id => @story.id, :story => @attr
517 response.should redirect_to(:action => :comic, :id => @attr[:comic_id])
521 it 'ステータスコード200 OKを返す' do
522 # Story.any_instance.stub(:store).and_return(true)
523 put :update, :id => @story.id, :story => @attr, :format => :json
524 response.should be_success
528 context '作家権限がないとき' do
533 it 'ステータスコード302 Foundを返す' do
534 put :update, :id => @story.id, :story => @attr
535 response.status.should eq 302
537 it 'サインインページへ遷移する' do
538 put :update, :id => @story.id, :story => @attr
539 response.body.should redirect_to '/users/sign_in'
543 it 'ステータスコード401 Unauthorizedを返す' do
544 put :update, :id => @story.id, :story => @attr, :format => :json
545 response.status.should eq 401
547 it '応答メッセージにUnauthorizedを返す' do
548 put :update, :id => @story.id, :story => @attr, :format => :json
549 response.message.should match(/Unauthorized/)
553 context '検証、保存に失敗した' do
555 Story.any_instance.stub(:store).and_return(false)
558 put :update, :id => @story.id, :story => @attr
559 assigns(:story).should eq @story
562 it 'ステータスコード200 OKを返す' do
563 put :update, :id => @story.id, :story => @attr
564 response.status.should eq 200
567 put :update, :id => @story.id, :story => @attr
568 response.should render_template("edit")
572 it 'ステータスコード422 unprocessable_entity を返す' do
573 put :update, :id => @story.id, :story => @attr, :format => :json
574 response.status.should eq 422
576 it '応答メッセージUnprocessable Entityを返す' do
577 put :update, :id => @story.id, :story => @attr, :format => :json
578 response.message.should match(/Unprocessable/)
586 @story = FactoryGirl.create :story, :author_id => @author.id
588 Story.stub(:edit).and_return(@story)
590 context 'つつがなく終わるとき' do
591 it 'ストーリーモデルに編集取得を問い合わせている' do
592 Story.should_receive(:edit).exactly(1)
593 delete :destroy, :id => @story.id
595 it '@storyにアレを取得している' do
596 delete :destroy, :id => @story.id
597 assigns(:story).id.should eq(@story.id)
599 it 'そのストーリーを一つのトランザクションで削除する' do
601 delete :destroy, :id => @story.id
602 }.should change(Story, :count)
605 it 'ステータスコード302 Foundを返す' do
606 delete :destroy, :id => @story.id
607 response.status.should eq 302
609 it 'ストーリー一覧ページへ遷移する' do
610 delete :destroy, :id => @story.id
611 response.should redirect_to(story_path(@story.comic_id))
615 it 'ステータスコード200 OKを返す' do
616 delete :destroy, :id => @story.id, :format => :json
617 response.should be_success
621 context '作家権限がないとき' do
626 it 'ステータスコード302 Foundを返す' do
627 delete :destroy, :id => @story.id
628 response.status.should eq 302
630 it 'サインインページへ遷移する' do
631 delete :destroy, :id => @story.id
632 response.body.should redirect_to '/users/sign_in'
636 it 'ステータスコード401 Unauthorizedを返す' do
637 delete :destroy, :id => @story.id, :format => :json
638 response.status.should eq 401
640 it '応答メッセージにUnauthorizedを返す' do
641 delete :destroy, :id => @story.id, :format => :json
642 response.message.should match(/Unauthorized/)
647 context '対象ストーリーがないとき' do
649 context '他人のストーリーだったとき' do