OSDN Git Service

add test on comic.play
[pettanr/pettanr.git] / spec / controllers / comics_controller_spec.rb
1 # -*- encoding: utf-8 -*-\r
2 require 'spec_helper'
3
4 describe ComicsController do
5   before do
6     Factory :admin
7     @user = Factory :user_yas\r
8     @author = @user.author    #ユーザ作成時に連動して作成される
9   end
10   
11   describe '一覧表示に於いて' do
12     before do
13       @comic = Factory :normal_comic, :author_id => @user.author.id
14       Comic.stub(:list).and_return([@comic, @comic, @comic])
15       sign_in @user
16     end
17     context '事前チェックする' do
18       it '与えられたpageがセットされている' do
19         get :index, :page => 5
20         assigns(:page).should eq 5
21       end
22       it '省略されると@pageに1値が入る' do
23         get :index
24         assigns(:page).should eq 1
25       end
26       it '与えられたpage_sizeがセットされている' do
27         get :index, :page_size => 15
28         assigns(:page_size).should eq 15
29       end
30       it '省略されると@page_sizeにデフォルト値が入る' do
31         get :index
32         assigns(:page_size).should eq Comic.default_page_size\r
33       end
34       it '最大を超えると@page_sizeにデフォルト最大値が入る' do
35         get :index, :page_size => 1500
36         assigns(:page_size).should eq Comic.max_page_size
37       end
38       it '不正な値が入ると@page_sizeにデフォルト最大値が入る' do
39         get :index, :page_size => 0
40         assigns(:page_size).should eq Comic.default_page_size
41       end
42     end
43     context 'つつがなく終わるとき' do
44       it 'ステータスコード200 OKを返す' do
45         get :index
46         response.should be_success 
47       end
48       it 'コミックモデルに一覧を問い合わせている' do
49         Comic.should_receive(:list).exactly(1)\r
50         get :index
51       end
52       it '@comicsにリストを取得している' do
53         get :index
54         assigns(:comics).should have_at_least(3).items
55       end
56       context 'html形式' do
57         it 'indexテンプレートを描画する' do
58           get :index
59           response.should render_template("index")
60         end
61       end
62       context 'json形式' do
63         it 'jsonデータを返す' do
64           get :index, :format => :json
65           lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
66         end
67         it 'データがリスト構造になっている' do
68           get :index, :format => :json
69           json = JSON.parse response.body\r
70           json.should have_at_least(3).items
71         end
72         it 'リストの先頭くらいはコミックっぽいものであって欲しい' do
73           get :index, :format => :json
74           json = JSON.parse response.body
75           json.first.has_key?("title").should be_true
76         end
77       end
78     end
79     context '作家権限がないとき' do
80       before do
81         sign_out @user
82       end
83       context 'html形式' do
84         it 'ステータスコード302 Foundを返す' do
85           get :index
86           response.status.should eq 302
87         end
88         it 'サインインページへ遷移する' do
89           get :index
90           response.should redirect_to '/users/sign_in'
91         end
92       end
93       context 'json形式' do
94         it 'ステータスコード401 Unauthorizedを返す' do
95           get :index, :format => :json
96           response.status.should eq 401
97         end
98         it '応答メッセージにUnauthorizedを返す' do
99           get :index, :format => :json\r
100           response.message.should match(/Unauthorized/)
101         end
102       end
103     end
104   end
105   
106   describe '単体表示に於いて' do
107     before do
108       @comic = Factory :normal_comic, :author_id => @user.author.id
109       Comic.stub(:show).and_return(@comic)
110       sign_in @user
111     end
112     context 'つつがなく終わるとき' do
113       it 'ステータスコード200 OKを返す' do
114         get :show, :id => @comic.id
115         response.should be_success
116       end
117       it 'コミックモデルに単体取得を問い合わせている' do
118         Comic.should_receive(:show).exactly(1)\r
119         get :show
120       end
121       it '@comicにアレを取得している' do
122         get :show, :id => @comic.id
123         assigns(:comic).id.should eq(@comic.id)
124       end
125       context 'html形式' do
126         it 'showテンプレートを描画する' do
127           get :show, :id => @comic.id
128           response.should render_template("show")
129         end
130       end
131       context 'json形式' do
132         it 'jsonデータを返す' do
133           get :show, :id => @comic.id, :format => :json
134           lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
135         end
136         it 'データがアレになっている' do
137           get :show, :id => @comic.id, :format => :json
138           json = JSON.parse response.body
139           json["title"].should match(/normal/)
140         end
141       end
142     end
143     context '作家権限がないとき' do
144       before do
145         sign_out @user
146       end
147       context 'html形式' do
148         it 'ステータスコード302 Foundを返す' do
149           get :show, :id => @comic.id
150           response.status.should eq 302
151         end
152         it 'サインインページへ遷移する' do
153           get :show, :id => @comic.id
154           response.body.should redirect_to '/users/sign_in'
155         end
156       end
157       context 'json形式' do
158         it 'ステータスコード401 Unauthorizedを返す' do
159           get :show, :id => @comic.id, :format => :json
160           response.status.should eq 401
161         end
162         it '応答メッセージにUnauthorizedを返す' do
163           get :show, :id => @comic.id, :format => :json
164           response.message.should match(/Unauthorized/)
165         end
166       end
167     end
168 =begin
169     context '対象コミックがないとき' do
170       context 'html形式' do
171         it '例外404 not_foundを返す' do
172           lambda{\r
173             get :show, :id => 0
174           }.should raise_error(ActiveRecord::RecordNotFound)
175         end
176       end
177       context 'json形式' do
178         it '例外404 not_foundを返す' do
179           lambda{ \r
180             get :show, :id => 0, :format => :json
181           }.should raise_error(ActiveRecord::RecordNotFound)
182         end
183       end
184     end
185     context '非公開コミックを見ようとしたとき' do
186       context 'html形式' do
187         it '例外403 forbiddenを返す' do
188           Comic.any_instance.stub(:visible?).with(any_args()).and_return(false)
189           hidden = Factory :hidden_comic, :author_id => @author.id
190           lambda{\r
191             get :show, :id => hidden
192           }.should raise_error(ActiveRecord::Forbidden)
193         end
194       end
195       context 'json形式' do
196         it '例外403 forbiddenを返す' do
197           Comic.any_instance.stub(:visible?).with(any_args()).and_return(false)
198           hidden = Factory :hidden_comic, :author_id => @author.id
199           lambda{\r
200             get :show, :id => hidden, :format => :json
201           }.should raise_error(ActiveRecord::Forbidden)
202         end
203       end
204     end
205 =end
206   end
207   describe '閲覧に於いて' do
208     before do
209       @comic = Factory :normal_comic, :author_id => @user.author.id
210       Comic.stub(:show).and_return(@comic)
211       sign_in @user
212     end
213     context '事前チェックする' do
214       before do
215         Panel.stub(:count).and_return(10)
216       end
217       it '与えられたoffsetがセットされている' do
218         get :play, :id => @comic.id, :offset => 7
219         assigns(:offset).should eq 7
220       end
221       it '省略されると@offsetに0値が入る' do
222         get :play, :id => @comic.id
223         assigns(:offset).should eq 0
224       end
225       it 'コマ数以上が与えられるとコマ数-1が入る' do
226         get :play, :id => @comic.id, :offset => 10
227         assigns(:offset).should eq 9
228       end
229       it '負の値が与えられると末尾(コマ数)からさかのぼった値が入る' do
230         get :play, :id => @comic.id, :offset => -3
231         assigns(:offset).should eq 7
232       end
233       it 'コマ数以上の負の値が与えられると計算できないので0値が入る' do
234         get :play, :id => @comic.id, :offset => -13
235         assigns(:offset).should eq 0
236       end
237       it '与えられたcountがセットされている' do
238         get :play, :id => @comic.id, :count => 18
239         assigns(:count).should eq 18
240       end
241       it '省略されると@countにデフォルト値が入る' do
242         get :play, :id => @comic.id
243         assigns(:count).should eq Comic.default_panel_size\r
244       end
245       it '最大を超えると@countにデフォルト最大値が入る' do
246         get :play, :id => @comic.id, :count => 1500
247         assigns(:count).should eq Comic.max_panel_size
248       end
249       it '不正な値が入ると@countにデフォルト最大値が入る' do
250         get :play, :id => @comic.id, :page_size => 0
251         assigns(:count).should eq Comic.default_panel_size
252       end
253     end
254     context 'つつがなく終わるとき' do
255       it 'ステータスコード200 OKを返す' do
256         get :play, :id => @comic.id
257         response.should be_success
258       end
259       it 'コミックモデルに単体取得を問い合わせている' do
260         Comic.should_receive(:show).exactly(1)\r
261         get :play, :id => @comic.id
262       end
263       it '@comicにアレを取得している' do
264         get :play, :id => @comic.id
265         assigns(:comic).id.should eq(@comic.id)
266       end
267       context 'html形式' do
268         it 'playテンプレートを描画する' do
269           get :play, :id => @comic.id
270           response.should render_template("play")
271         end
272       end
273       context 'json形式' do
274         it 'jsonデータを返す' do
275           get :play, :id => @comic.id, :format => :json
276           lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
277         end
278         it 'データがアレになっている' do
279           get :play, :id => @comic.id, :format => :json
280           json = JSON.parse response.body
281           json["title"].should match(/normal/)
282         end
283       end
284     end
285     context '作家権限がないとき' do
286       before do
287         sign_out @user
288       end
289       context 'html形式' do
290         it 'ステータスコード302 Foundを返す' do
291           get :play, :id => @comic.id
292           response.status.should eq 302
293         end
294         it 'サインインページへ遷移する' do
295           get :play, :id => @comic.id
296           response.body.should redirect_to '/users/sign_in'
297         end
298       end
299       context 'json形式' do
300         it 'ステータスコード401 Unauthorizedを返す' do
301           get :play, :id => @comic.id, :format => :json
302           response.status.should eq 401
303         end
304         it '応答メッセージにUnauthorizedを返す' do
305           get :play, :id => @comic.id, :format => :json
306           response.message.should match(/Unauthorized/)
307         end
308       end
309     end
310   end
311
312   describe 'コミック数取得に於いて' do
313     before do
314       Comic.should_receive(:visible_count).and_return(3)
315 #      sign_in @user
316     end
317     context 'つつがなく終わるとき' do
318       it 'ステータスコード200 OKを返す' do
319         get :count, :format => :json
320         response.should be_success 
321       end
322       context 'json形式' do
323         it 'jsonデータを返す' do
324           get :count, :format => :json
325           lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
326         end
327         it 'データがHash構造になっていてコミック数が1である' do
328           get :count, :format => :json
329           json = JSON.parse response.body
330           json["count"].should == 3
331         end
332       end
333     end
334   end
335
336   describe '新規作成フォーム表示に於いて' do
337     before do
338       sign_in @user
339     end
340     context 'つつがなく終わるとき' do
341       it 'ステータスコード200 OKを返す' do
342         get :new
343         response.should be_success 
344       end
345       it '@comicに新規データを用意している' do
346         get :new
347         assigns(:comic).should be_a_new(Comic)
348       end
349       it 'コミックモデルにデフォルト値補充を依頼している' do
350         Comic.any_instance.should_receive(:supply_default).exactly(1)\r
351         get :new
352       end
353       context 'html形式' do
354         it 'newテンプレートを描画する' do
355           get :new
356           response.should render_template("new")
357         end
358       end
359       context 'js形式' do
360         it 'new.jsテンプレートを描画する' do
361           get :new, :format => :js
362           response.should render_template("new")
363         end
364       end
365     end
366     context '作家権限がないとき' do
367       before do
368         sign_out @user
369       end
370       context 'html形式' do
371         it 'ステータスコード302 Foundを返す' do
372           get :new
373           response.status.should eq 302
374         end
375         it 'サインインページへ遷移する' do
376           get :new
377           response.body.should redirect_to '/users/sign_in'
378         end
379       end
380       context 'js形式' do
381         it 'ステータスコード401 Unauthorizedを返す' do
382           get :new, :format => :js
383           response.status.should eq 401
384         end
385         it '応答メッセージにUnauthorizedを返す' do
386           get :new, :format => :js
387           response.message.should match(/Unauthorized/)
388         end
389       end
390     end
391   end
392
393   describe '新規作成に於いて' do
394     before do
395       sign_in @user
396     end
397     context 'つつがなく終わるとき' do
398       it 'モデルに保存依頼する' do
399         Comic.any_instance.should_receive(:save).exactly(1)
400         post :create, :comic => Factory.attributes_for(:normal_comic)
401       end
402       it "@comicに作成されたコミックを保持していて、それがDBにある" do
403         post :create, :comic => Factory.attributes_for(:normal_comic)
404         assigns(:comic).should be_a(Comic)
405         assigns(:comic).should be_persisted
406       end
407       context 'html形式' do
408         it 'ステータスコード302 Foundを返す' do
409           Comic.any_instance.stub(:save).and_return(true)
410           post :create, :comic => Factory.attributes_for(:normal_comic)
411           response.status.should eq 302
412         end
413         it '作成されたコミックの表示ページへ遷移する' do
414 #          Comic.any_instance.stub(:save).and_return(true)
415           post :create, :comic => Factory.attributes_for(:normal_comic)
416           response.should redirect_to(Comic.last)
417         end
418       end
419       context 'json形式' do
420         it 'ステータスコード200 OKを返す' do
421 #          Comic.any_instance.stub(:save).and_return(true)
422           post :create, :comic => Factory.attributes_for(:normal_comic), :format => :json
423           response.should be_success 
424         end
425         it '作成されたコミックをjsonデータで返す' do
426           post :create, :comic => Factory.attributes_for(:normal_comic), :format => :json
427           lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
428         end
429         it 'データがアレになっている' do
430           post :create, :comic => Factory.attributes_for(:normal_comic), :format => :json
431           json = JSON.parse response.body
432           json["title"].should match(/normal/)
433         end
434       end
435     end
436     context '作家権限がないとき' do
437       before do
438         sign_out @user
439       end
440       context 'html形式' do
441         it 'ステータスコード302 Foundを返す' do
442           post :create, :comic => Factory.attributes_for(:normal_comic)
443           response.status.should eq 302
444         end
445         it 'サインインページへ遷移する' do
446           post :create, :comic => Factory.attributes_for(:normal_comic)
447           response.body.should redirect_to '/users/sign_in'
448         end
449       end
450       context 'json形式' do
451         it 'ステータスコード401 Unauthorizedを返す' do
452           post :create, :comic => Factory.attributes_for(:normal_comic), :format => :json
453           response.status.should eq 401
454         end
455         it '応答メッセージにUnauthorizedを返す' do
456           post :create, :comic => Factory.attributes_for(:normal_comic), :format => :json
457           response.message.should match(/Unauthorized/)
458         end
459       end
460     end
461     context '検証、保存に失敗した' do
462       before do
463         Comic.any_instance.stub(:save).and_return(false)
464       end
465       it "未保存のコミックを保持している" do
466         post :create, :comic => {}
467         assigns(:comic).should be_a_new(Comic)
468       end
469       context 'html形式' do
470         it 'ステータスコード200 OKを返す' do
471           post :create, :comic => {}
472           response.status.should eq 200
473         end
474         it '新規ページを描画する' do
475           post :create, :comic => {}
476           response.should render_template("new")
477         end
478       end
479       context 'json形式' do
480         it 'ステータスコード422 unprocessable_entity を返す' do
481           post :create, :comic => {}, :format => :json
482           response.status.should eq 422
483         end
484         it '応答メッセージUnprocessable Entityを返す' do
485           post :create, :comic => {}, :format => :json
486           response.message.should match(/Unprocessable/)
487         end
488       end
489     end
490   end
491
492   describe '編集フォーム表示に於いて' do
493     before do
494       @comic = Factory :normal_comic, :author_id => @user.author.id
495       sign_in @user
496       Comic.stub(:show).and_return(@comic)
497     end
498     context 'つつがなく終わるとき' do
499       it 'ステータスコード200 OKを返す' do
500         get :edit, :id => @comic.id
501         response.should be_success 
502       end
503       it 'コミックモデルに単体取得を問い合わせている' do
504         Comic.should_receive(:show).exactly(1)\r
505         get :edit, :id => @comic.id
506       end
507       it '@comicにデータを用意している' do
508         get :edit, :id => @comic.id
509         assigns(:comic).should eq @comic
510       end
511       context 'html形式' do
512         it 'editテンプレートを描画する' do
513           get :edit, :id => @comic.id
514           response.should render_template("edit")
515         end
516       end
517       context 'js形式' do
518         it 'edit.jsテンプレートを描画する' do
519           get :edit, :id => @comic.id, :format => :js
520           response.should render_template("edit")
521         end
522       end
523     end
524     context '作家権限がないとき' do
525       before do
526         sign_out @user
527       end
528       context 'html形式' do
529         it 'ステータスコード302 Foundを返す' do
530           get :edit, :id => @comic.id
531           response.status.should eq 302
532         end
533         it 'サインインページへ遷移する' do
534           get :edit, :id => @comic.id
535           response.body.should redirect_to '/users/sign_in'
536         end
537       end
538       context 'js形式' do
539         it 'ステータスコード401 Unauthorizedを返す' do
540           get :edit, :id => @comic.id, :format => :js
541           response.status.should eq 401
542         end
543         it '応答メッセージにUnauthorizedを返す' do
544           get :edit, :id => @comic.id, :format => :js
545           response.message.should match(/Unauthorized/)
546         end
547       end
548     end
549   end
550
551   describe '更新に於いて' do
552     before do\r
553       @comic = Factory :normal_comic, :author => @author
554       sign_in @user
555     end
556     context '事前チェックしておく' do
557       it 'コミックモデルに単体取得を問い合わせている' do
558         Comic.stub(:show).with(any_args()).and_return @comic\r
559         Comic.should_receive(:show).exactly(1)\r
560         put :update, :id => @comic.id, :comic => Factory.attributes_for(:normal_comic)
561       end
562       it 'モデルに更新を依頼する' do
563         Comic.any_instance.should_receive(:update_attributes).with(any_args)
564         put :update, :id => @comic.id, :comic => Factory.attributes_for(:normal_comic)
565       end
566       it '@comicにアレを取得している' do
567         put :update, :id => @comic.id, :comic => Factory.attributes_for(:normal_comic)
568         assigns(:comic).id.should eq(@comic.id)
569       end
570     end
571     context 'つつがなく終わるとき' do
572       it '更新される' do\r
573         put :update, :id => @comic.id, :comic => Factory.attributes_for(:hidden_comic)\r
574         Comic.find(@comic.id).visible.should eq 0
575       end
576       context 'html形式' do
577         it 'ステータスコード302 Foundを返す' do
578           Comic.any_instance.stub(:update_attributes).with(any_args()).and_return(true)
579           put :update, :id => @comic.id, :comic => Factory.attributes_for(:hidden_comic)
580           response.status.should eq 302
581         end
582         it '更新されたコミックの表示ページへ遷移する' do
583           put :update, :id => @comic.id, :comic => Factory.attributes_for(:hidden_comic)
584           response.should redirect_to(@comic)
585         end
586       end
587       context 'json形式' do
588         it 'ステータスコード200 OKを返す' do
589           Comic.any_instance.stub(:update_attributes).with(any_args()).and_return(true)
590           put :update, :id => @comic.id, :comic => Factory.attributes_for(:hidden_comic), :format => :json
591           response.should be_success 
592         end
593         it 'ページ本体は特に返さない' do
594           Comic.any_instance.stub(:update_attributes).with(any_args()).and_return(true)
595           put :update, :id => @comic.id, :comic => Factory.attributes_for(:hidden_comic), :format => :json
596           response.body.should match /./
597         end
598       end
599     end
600     context '作家権限がないとき' do
601       before do
602         sign_out @user
603       end
604       it 'ステータスコード302 Foundを返す' do
605         put :update, :id => @comic.id, :comic => Factory.attributes_for(:hidden_comic)
606         response.status.should eq 302
607       end
608       context 'html形式' do
609         it 'サインインページへ遷移する' do
610           put :update, :id => @comic.id, :comic => Factory.attributes_for(:hidden_comic)
611           response.body.should redirect_to '/users/sign_in'
612         end
613       end
614       context 'json形式' do
615         it '応答メッセージにUnauthorizedを返す' do
616           put :update, :id => @comic.id, :comic => Factory.attributes_for(:hidden_comic), :format => :json
617           response.message.should match(/Unauthorized/)
618         end
619       end
620     end
621     context '検証、保存に失敗したとき' do
622       before do
623         Comic.any_instance.stub(:update_attributes).and_return(false)
624       end
625       context 'html形式' do
626         it 'ステータスコード200 Okを返す' do
627           put :update, :id => @comic.id, :comic => Factory.attributes_for(:hidden_comic)
628           response.status.should eq 200
629         end
630         it '編集ページを描画する' do
631           put :update, :id => @comic.id, :comic => Factory.attributes_for(:hidden_comic)
632           response.should render_template("edit")
633         end
634       end
635       context 'json形式' do
636         it 'ステータスコード422 unprocessable_entity を返す' do
637           Comic.any_instance.stub(:update_attributes).and_return(false)
638           put :update, :id => @comic.id, :comic => Factory.attributes_for(:hidden_comic), :format => :json
639           response.status.should eq 422
640         end
641         it '応答メッセージUnprocessable Entityを返す' do
642           put :update, :id => @comic.id, :comic => Factory.attributes_for(:hidden_comic), :format => :json
643           response.message.should match(/Unprocessable/)
644         end
645       end
646     end
647   end
648
649
650 end