OSDN Git Service

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