OSDN Git Service

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