OSDN Git Service

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