OSDN Git Service

t#30200:update i18n devise
[pettanr/pettanr.git] / spec / controllers / stories_controller_spec.rb
1 # -*- encoding: utf-8 -*-
2 require 'spec_helper'
3 #ストーリー
4 describe StoriesController do
5   before 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
14   end
15   
16   describe '一覧表示に於いて' do
17   end
18   
19   describe '単体表示に於いて' do
20     before do
21       sign_in @user
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)
25     end
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
30       end
31       it '@storyにアレを取得している' do
32         get :show, :id => @story.id
33         assigns(:story).should eq @story
34       end
35       context 'html形式' do
36         it 'ステータスコード200 OKを返す' do
37           get :show, :id => @story.id
38           response.should be_success
39         end
40         it 'showテンプレートを描画する' do
41           get :show, :id => @story.id
42           response.should render_template("show")
43         end
44       end
45       context 'json形式' do
46         it 'ステータスコード200 OKを返す' do
47           get :show, :id => @story.id, :format => :json
48           response.should be_success
49         end
50         it 'jsonデータを返す' do
51           get :show, :id => @story.id, :format => :json
52           lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
53         end
54         it 'ストーリーモデルにストーリーjson出力を問い合わせている' do
55           Story.any_instance.should_receive(:story_as_json).exactly(1)
56           get :show, :id => @story.id, :format => :json
57         end
58         it 'データがアレになっている' do
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
64         end
65       end
66     end
67     context '作家権限がないとき' do
68       before do
69         sign_out @user
70       end
71       context 'html形式' do
72         it 'ステータスコード302 Foundを返す' do
73           get :show, :id => @story.id
74           response.status.should eq 302
75         end
76         it 'サインインページへ遷移する' do
77           get :show, :id => @story.id
78           response.body.should redirect_to '/users/sign_in'
79         end
80       end
81       context 'json形式' do
82         it 'ステータスコード401 Unauthorizedを返す' do
83           get :show, :id => @story.id, :format => :json
84           response.status.should eq 401
85         end
86         it '応答メッセージにUnauthorizedを返す' do
87           get :show, :id => @story.id, :format => :json
88           response.message.should match(/Unauthorized/)
89         end
90       end
91     end
92   end
93   
94   describe '閲覧に於いて' do
95     before do
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])
100       sign_in @user
101     end
102     context 'パラメータチェックする' do
103       it '与えられたoffsetがセットされている' do
104         get :comic, :id => @comic.id, :offset => 5
105         assigns(:offset).should eq 5
106       end
107       it '省略されると@offsetに0値が入る' do
108         get :comic, :id => @comic.id
109         assigns(:offset).should eq 0
110       end
111       it '与えられたcountがセットされている' do
112         get :comic, :id => @comic.id, :count => 4
113         assigns(:panel_count).should eq 4
114       end
115       it '省略されると@panel_countにデフォルト値が入る' do
116         get :comic, :id => @comic.id
117         assigns(:panel_count).should eq Story.default_panel_size
118       end
119       it '最大を超えると@panel_countにデフォルト最大値が入る' do
120         get :comic, :id => @comic.id, :count => 1500
121         assigns(:panel_count).should eq Story.max_panel_size
122       end
123       it '不正な値が入ると@panel_countにデフォルト最大値が入る' do
124         get :comic, :id => @comic.id, :count => -1
125         assigns(:panel_count).should eq Story.default_panel_size
126       end
127     end
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
132       end
133       it 'ストーリーモデルにプレイリスト取得を問い合わせている' do
134         Story.should_receive(:play_list).with(@comic, @author, 0, 30).exactly(1)
135         get :comic, :id => @comic.id
136       end
137     end
138     context 'つつがなく終わるとき' do
139       it '@storiesにリストを取得している' do
140         get :comic, :id => @comic.id
141         assigns(:stories).should have_at_least(3).items
142       end
143       context 'html形式' do
144         it 'ステータスコード200 OKを返す' do
145           get :comic, :id => @comic.id
146           response.should be_success 
147         end
148         it 'comicテンプレートを描画する' do
149           get :comic, :id => @comic.id
150           response.should render_template("comic")
151         end
152       end
153       context 'json形式' do
154         it 'ステータスコード200 OKを返す' do
155           get :comic, :id => @comic.id, :format => :json
156           response.should be_success 
157         end
158         it 'jsonデータを返す' do
159           get :comic, :id => @comic.id, :format => :json
160           lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
161         end
162         it 'ストーリーモデルにストーリーリストのjson出力を問い合わせている' do
163           Story.should_receive(:list_as_json_text).exactly(1)
164           get :comic, :id => @comic.id, :format => :json
165         end
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
170         end
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
177         end
178       end
179     end
180     context '作家権限がないとき' do
181       before do
182         sign_out @user
183       end
184       context 'html形式' do
185         it 'ステータスコード302 Foundを返す' do
186           get :comic, :id => @comic.id
187           response.status.should eq 302
188         end
189         it 'サインインページへ遷移する' do
190           get :comic, :id => @comic.id
191           response.should redirect_to '/users/sign_in'
192         end
193       end
194       context 'json形式' do
195         it 'ステータスコード401 Unauthorizedを返す' do
196           get :comic, :id => @comic.id, :format => :json
197           response.status.should eq 401
198         end
199         it '応答メッセージにUnauthorizedを返す' do
200           get :comic, :id => @comic.id, :format => :json
201           response.message.should match(/Unauthorized/)
202         end
203       end
204     end
205   end
206
207   describe '新規作成フォーム表示に於いて' do
208     before do
209       sign_in @user
210     end
211     context 'つつがなく終わるとき' do
212       it '@storyに新規データを用意している' do
213         get :new
214         assigns(:story).should be_a_new(Story)
215       end
216       it 'モデルにデフォルト値補充を依頼している' do
217         Story.any_instance.should_receive(:supply_default).exactly(1)
218         get :new
219       end
220       context 'html形式' do
221         it 'ステータスコード200 OKを返す' do
222           get :new
223           response.should be_success 
224         end
225         it 'newテンプレートを描画する' do
226           get :new
227           response.should render_template("new")
228         end
229       end
230       context 'js形式' do
231         it 'ステータスコード200 OKを返す' do
232           get :new, :format => :js
233           response.should be_success 
234         end
235         it 'new.jsテンプレートを描画する' do
236           get :new, :format => :js
237           response.should render_template("new")
238         end
239       end
240       context 'json形式' do
241         it 'ステータスコード200 OKを返す' do
242           get :new, :format => :json
243           response.should be_success 
244         end
245         it 'jsonデータを返す' do
246           get :new, :format => :json
247           lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
248         end
249         it 'ストーリーモデルのストーリーのjson出力を問い合わせている' do
250           Story.any_instance.should_receive(:story_as_json).exactly(1)
251           get :new, :format => :json
252         end
253         it 'データがアレになっている' do
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
259         end
260       end
261     end
262     context '作家権限がないとき' do
263       before do
264         sign_out @user
265       end
266       context 'html形式' do
267         it 'ステータスコード302 Foundを返す' do
268           get :new
269           response.status.should eq 302
270         end
271         it 'サインインページへ遷移する' do
272           get :new
273           response.body.should redirect_to '/users/sign_in'
274         end
275       end
276       context 'json形式' do
277         it 'ステータスコード401 Unauthorizedを返す' do
278           get :new, :format => :json
279           response.status.should eq 401
280         end
281         it '応答メッセージにUnauthorizedを返す' do
282           get :new, :format => :json
283           response.message.should match(/Unauthorized/)
284         end
285       end
286     end
287   end
288   
289   describe '新規作成に於いて' do
290     before do
291       @attr = FactoryGirl.attributes_for(:story, :t => nil, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id)
292       sign_in @user
293     end
294     context 'つつがなく終わるとき' do
295       it 'デフォルト値補充を依頼する' do
296         Story.any_instance.should_receive(:supply_default).exactly(1)
297         post :create, :story => @attr
298       end
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
304       end
305       it '上書き補充を依頼する' do
306         Story.any_instance.should_receive(:overwrite).exactly(1)
307         post :create, :story => @attr
308       end
309       it 'コミックモデルに編集取得を依頼している' do
310         Comic.should_receive(:edit).with(@comic.id, @author).exactly(1)
311         post :create, :story => @attr
312       end
313       it 'コマモデルに単体取得を依頼している' do
314         Panel.should_receive(:show).with(@panel.id, @author).exactly(1)
315         post :create, :story => @attr
316       end
317       it 'モデルに保存依頼する' do
318         Story.any_instance.should_receive(:store).exactly(1)
319         post :create, :story => @attr
320       end
321       it "@storyに作成されたコマを保持していて、それがDBにある" do
322         post :create, :story => @attr
323         assigns(:story).should be_a(Story)
324         assigns(:story).should be_persisted
325       end
326       context 'html形式' do
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
331         end
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])
336         end
337       end
338       context 'json形式' do
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 
343         end
344         it '作成されたコマをjsonデータで返す' do
345           post :create, :story => @attr, :format => :json
346           lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
347         end
348         it 'ストーリーモデルのストーリーのjson出力を問い合わせている' do
349           Story.any_instance.should_receive(:story_as_json).exactly(1)
350           post :create, :story => @attr, :format => :json
351         end
352         it 'データがアレになっている' do
353           post :create, :story => @attr, :format => :json
354           json = JSON.parse response.body
355           json["t"].should eq 0
356         end
357       end
358     end
359     context '作家権限がないとき' do
360       before do
361         sign_out @user
362       end
363       context 'html形式' do
364         it 'ステータスコード302 Foundを返す' do
365           post :create, :story => @attr
366           response.status.should eq 302
367         end
368         it 'サインインページへ遷移する' do
369           post :create, :story => @attr
370           response.body.should redirect_to '/users/sign_in'
371         end
372       end
373       context 'json形式' do
374         it 'ステータスコード401 Unauthorizedを返す' do
375           post :create, :story => @attr, :format => :json
376           response.status.should eq 401
377         end
378         it '応答メッセージにUnauthorizedを返す' do
379           post :create, :story => @attr, :format => :json
380           response.message.should match(/Unauthorized/)
381         end
382       end
383     end
384     context '検証、保存に失敗した' do
385       before do
386         Story.any_instance.stub(:store).and_return(false)
387       end
388       it "未保存のコマを保持している" do
389         post :create, :story => @attr
390         assigns(:story).should be_a_new(Story)
391       end
392       context 'html形式' do
393         it 'ステータスコード200 OKを返す' do
394           post :create, :story => @attr
395           response.status.should eq 200
396         end
397         it '新規ページを描画する' do
398           post :create, :story => @attr
399           response.should render_template("new")
400         end
401       end
402       context 'json形式' do
403         it 'ステータスコード422 unprocessable_entity を返す' do
404           post :create, :story => @attr, :format => :json
405           response.status.should eq 422
406         end
407         it '応答メッセージUnprocessable Entityを返す' do
408           post :create, :story => @attr, :format => :json
409           response.message.should match(/Unprocessable/)
410         end
411       end
412     end
413   end
414
415   describe '編集フォーム表示に於いて' do
416     before do
417       @story = FactoryGirl.create :story, :author_id => @author.id
418       sign_in @user
419       Story.stub(:show).and_return(@story)
420     end
421     context 'つつがなく終わるとき' do
422       it 'ストーリーモデルに編集取得を問い合わせている' do
423         Story.should_receive(:edit).exactly(1)
424         get :edit, :id => @story.id
425       end
426       it '@storyにデータを用意している' do
427         get :edit, :id => @story.id
428         assigns(:story).should eq @story
429       end
430       context 'html形式' do
431         it 'ステータスコード200 OKを返す' do
432           get :edit, :id => @story.id
433           response.should be_success 
434         end
435         it 'editテンプレートを描画する' do
436           get :edit, :id => @story.id
437           response.should render_template("edit")
438         end
439       end
440       context 'js形式' do
441         it 'ステータスコード200 OKを返す' do
442           get :edit, :id => @story.id, :format => :js
443           response.should be_success 
444         end
445         it 'edit.jsテンプレートを描画する' do
446           get :edit, :id => @story.id, :format => :js
447           response.should render_template("edit")
448         end
449       end
450     end
451     context '作家権限がないとき' do
452       before do
453         sign_out @user
454       end
455       context 'html形式' do
456         it 'ステータスコード302 Foundを返す' do
457           get :edit, :id => @story.id
458           response.status.should eq 302
459         end
460         it 'サインインページへ遷移する' do
461           get :edit, :id => @story.id
462           response.body.should redirect_to '/users/sign_in'
463         end
464       end
465       context 'js形式' do
466         it 'ステータスコード401 Unauthorizedを返す' do
467           get :edit, :id => @story.id, :format => :js
468           response.status.should eq 401
469         end
470         it '応答メッセージにUnauthorizedを返す' do
471           get :edit, :id => @story.id, :format => :js
472           response.message.should match(/Unauthorized/)
473         end
474       end
475     end
476   end
477
478   describe '更新に於いて' do
479     before do
480       @story = FactoryGirl.create :story, :author_id => @user.author.id
481       @attr = FactoryGirl.attributes_for(:story, :author_id => @author.id)
482       sign_in @user
483     end
484     context 'つつがなく終わるとき' do
485       it 'モデルに編集取得依頼する' 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
489       end
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
494       end
495       it '上書き補充を依頼する' do
496         Story.any_instance.should_receive(:overwrite).exactly(1)
497         put :update, :id => @story.id, :story => @attr
498       end
499       it 'モデルに保存依頼する' do
500         Story.any_instance.should_receive(:store).exactly(1)
501         put :update, :id => @story.id, :story => @attr
502       end
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
507       end
508       context 'html形式' do
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
513         end
514         it 'ストーリー表示へ遷移する' do
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])
518         end
519       end
520       context 'json形式' do
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 
525         end
526       end
527     end
528     context '作家権限がないとき' do
529       before do
530         sign_out @user
531       end
532       context 'html形式' do
533         it 'ステータスコード302 Foundを返す' do
534           put :update, :id => @story.id, :story => @attr
535           response.status.should eq 302
536         end
537         it 'サインインページへ遷移する' do
538           put :update, :id => @story.id, :story => @attr
539           response.body.should redirect_to '/users/sign_in'
540         end
541       end
542       context 'json形式' do
543         it 'ステータスコード401 Unauthorizedを返す' do
544           put :update, :id => @story.id, :story => @attr, :format => :json
545           response.status.should eq 401
546         end
547         it '応答メッセージにUnauthorizedを返す' do
548           put :update, :id => @story.id, :story => @attr, :format => :json
549           response.message.should match(/Unauthorized/)
550         end
551       end
552     end
553     context '検証、保存に失敗した' do
554       before do
555         Story.any_instance.stub(:store).and_return(false)
556       end
557       it "指定のコマを保持している" do
558         put :update, :id => @story.id, :story => @attr
559         assigns(:story).should eq @story
560       end
561       context 'html形式' do
562         it 'ステータスコード200 OKを返す' do
563           put :update, :id => @story.id, :story => @attr
564           response.status.should eq 200
565         end
566         it '編集ページを描画する' do
567           put :update, :id => @story.id, :story => @attr
568           response.should render_template("edit")
569         end
570       end
571       context 'json形式' do
572         it 'ステータスコード422 unprocessable_entity を返す' do
573           put :update, :id => @story.id, :story => @attr, :format => :json
574           response.status.should eq 422
575         end
576         it '応答メッセージUnprocessable Entityを返す' do
577           put :update, :id => @story.id, :story => @attr, :format => :json
578           response.message.should match(/Unprocessable/)
579         end
580       end
581     end
582   end
583
584   describe '削除に於いて' do
585     before do
586       @story = FactoryGirl.create :story, :author_id => @author.id
587       sign_in @user
588       Story.stub(:edit).and_return(@story)
589     end
590     context 'つつがなく終わるとき' do
591       it 'ストーリーモデルに編集取得を問い合わせている' do
592         Story.should_receive(:edit).exactly(1)
593         delete :destroy, :id => @story.id
594       end
595       it '@storyにアレを取得している' do
596         delete :destroy, :id => @story.id
597         assigns(:story).id.should eq(@story.id)
598       end
599       it 'そのストーリーを一つのトランザクションで削除する' do
600         lambda {
601           delete :destroy, :id => @story.id
602         }.should change(Story, :count)
603       end
604       context 'html形式' do
605         it 'ステータスコード302 Foundを返す' do
606           delete :destroy, :id => @story.id
607           response.status.should eq 302
608         end
609         it 'ストーリー一覧ページへ遷移する' do
610           delete :destroy, :id => @story.id
611           response.should redirect_to(story_path(@story.comic_id))
612         end
613       end
614       context 'json形式' do
615         it 'ステータスコード200 OKを返す' do
616           delete :destroy, :id => @story.id, :format => :json
617           response.should be_success
618         end
619       end
620     end
621     context '作家権限がないとき' do
622       before do
623         sign_out @user
624       end
625       context 'html形式' do
626         it 'ステータスコード302 Foundを返す' do
627           delete :destroy, :id => @story.id
628           response.status.should eq 302
629         end
630         it 'サインインページへ遷移する' do
631           delete :destroy, :id => @story.id
632           response.body.should redirect_to '/users/sign_in'
633         end
634       end
635       context 'json形式' do
636         it 'ステータスコード401 Unauthorizedを返す' do
637           delete :destroy, :id => @story.id, :format => :json
638           response.status.should eq 401
639         end
640         it '応答メッセージにUnauthorizedを返す' do
641           delete :destroy, :id => @story.id, :format => :json
642           response.message.should match(/Unauthorized/)
643         end
644       end
645     end
646 =begin
647     context '対象ストーリーがないとき' do
648     end
649     context '他人のストーリーだったとき' do
650     end
651 =end
652   end
653   
654 end