OSDN Git Service

78ad23adf27b5f58f1286e92c9a3379330d8c63d
[pettanr/pettanr.git] / spec / controllers / scrolls_controller_spec.rb
1 # -*- encoding: utf-8 -*-
2 require 'spec_helper'
3 #スクロール
4 describe ScrollsController do
5   before do
6     @admin =FactoryGirl.create :admin
7     @author_yas =  FactoryGirl.create :yas_with_scrolls
8     @user_yas = @author_yas.user
9     @user_rom = FactoryGirl.create :user_rom
10   end
11
12 if Manifest.manifest.magic_numbers['run_mode'] == 1
13   describe '一覧表示に於いて' do
14     before do
15     end
16     
17     context '正常系の共通処理' do
18       before do
19         sign_in @user_yas
20       end
21       it 'ステータス OKを返して、ファイラーまたはjsonデータを返す。' do
22         list_should_return_filer_or_json :index
23       end
24       it 'コントローラのマニフェストからScrollモデルを@my_model_classに取得している' do
25         get :index
26         expect(assigns(:my_model_class)).to be Scroll
27       end
28       it 'リストグループのマニフェストから新着リストの取得処理を@listに取得している' do
29         get :index
30         expect(assigns(:list).item_name).to eq('scroll')
31         expect(assigns(:list).list_name).to eq('public')
32       end
33       it '@itemsにリストを取得している' do
34         get :index
35         expect(assigns(:items).size).to be >= 3
36       end
37       context 'html形式' do
38         it '@filerにファイラーを取得している' do
39           get :index
40           expect(assigns(:filer).class).to be Locmare::Filer
41         end
42       end
43       context 'json形式' do
44         it '@filerにファイラーをセットしていない' do
45           get :index, format: :json
46           expect(assigns(:filer)).to be nil
47         end
48       end
49     end
50     context '件数取得' do
51       before do
52         sign_in @user_yas
53       end
54       it 'ステータス OKを返す' do
55         return_ok_at_get :count, format: :json
56       end
57       context 'json形式' do
58         it 'jsonデータを返す' do
59           get :count, format: :json
60           expect {JSON.parse(response.body)}.not_to raise_error
61         end
62       end
63     end
64     context 'サインインの状態が例外的なとき' do
65       it 'ユーザではなく管理者でも通常通り応答する' do
66         sign_in @admin
67         return_ok_at_get :index
68         return_ok_at_get :index, format: :json
69       end
70       it '創作活動してないROM専の読者にも通常通り応答する' do
71         sign_in @user_rom
72         return_ok_at_get :index
73         return_ok_at_get :index, format: :json
74       end
75       it 'ゲスト(サインインしていない)はサインインページに遷移する' do
76         announce_sign_in_at_get :index
77       end
78     end
79   end
80   describe '作家フィルタ一覧及びカウント' do
81     before do
82     end
83     context '正常系の共通処理' do
84       before do
85         sign_in @user_yas
86       end
87       it '@itemsにリストを取得している' do
88         get :by_author
89         expect(assigns(:items).size).to be >= 3
90       end
91       it 'ステータスコード200 OKを返す' do
92         list_should_return_filer_or_json :by_author, id: @author_yas.id
93       end
94     end
95     context '権限' do
96       it 'ユーザ権限はないが管理者権限があるとき 200 OKを返す' do
97         sign_in @admin
98         return_ok_at_get :by_author, id: @author_yas.id
99         return_ok_at_get :by_author, id: @author_yas.id, format: :json
100       end
101       it 'ユーザだが作家登録していないとき 200 OKを返す' do
102         sign_in @user_rom
103         return_ok_at_get :by_author, id: @author_yas.id
104         return_ok_at_get :by_author, id: @author_yas.id, format: :json
105       end
106       it 'sign_inを返す' do
107         announce_sign_in_at_get :by_author, id: @author_yas.id
108       end
109     end
110   end
111   
112   describe '単体表示に於いて' do
113     before do
114       @item = @author_yas.scrolls.first
115       @item_id = @item.id
116     end
117     context '正常系の共通処理' do
118       before do
119         sign_in @user_yas
120       end
121       it '[html, json, prof]でステータスコード200 OKを返す' do
122         return_ok_at_get :show, id: @item_id
123         return_ok_at_get :show, id: @item_id, format: :json
124         return_ok_at_get :show, id: @item_id, format: :prof
125       end
126       it 'コントローラのマニフェストからScrollモデルを@my_model_classに取得している' do
127         get :show, id: @item_id
128         expect(assigns(:my_model_class)).to be Scroll
129       end
130       it '@itemにリクエストしたアイテムを取得している' do
131         get :show, id: @item_id
132         expect(assigns(:item)).to eq(@item)
133       end
134       context 'html形式' do
135         it 'showテンプレートを描画する' do
136           get :show, id: @item_id
137           expect(response).to render_template("show")
138         end
139       end
140       context 'json形式' do
141         it 'jsonデータを返す' do
142           get :show, id: @item_id, format: :json
143           expect {JSON.parse(response.body)}.not_to raise_error
144         end
145         it 'プロファイラーは必要ないので@profilerに用意していない' do
146           get :show, id: @item_id, format: :json
147           expect(assigns(:profiler)).to be nil
148         end
149       end
150       context 'prof形式' do
151         it 'プロファイラーテンプレートを描画する' do
152           get :show, id: @item_id, format: :prof
153           expect(response).to render_template("templates/r/profiler/profiler")
154         end
155       end
156     end
157     context 'サインインの状態が例外的なとき' do
158       it 'ユーザではなく管理者でも通常通り応答する' do
159         sign_in @admin
160         return_ok_at_get :show, id: @item_id
161         return_ok_at_get :show, id: @item_id, format: :json
162       end
163       it '創作活動してないROM専の読者にも通常通り応答する' do
164         sign_in @user_rom
165         return_ok_at_get :show, id: @item_id
166         return_ok_at_get :show, id: @item_id, format: :json
167       end
168       it 'ゲスト(サインインしていない)はサインインページに遷移する' do
169         announce_sign_in_at_get :show, id: @item_id
170       end
171     end
172   end
173   
174   describe 'プレイリストに於いて' do
175     before do
176       @item = @author_yas.scrolls.first
177       @item_id = @item.id
178     end
179     context '正常系の共通処理' do
180       before do
181         sign_in @user_yas
182       end
183       it '[html, json]でステータス OKを返す' do
184         return_ok_at_get :play, id: @item_id
185         return_ok_at_get :play, id: @item_id, format: :json
186       end
187       it 'コントローラのマニフェストからScrollPanelモデルを@my_model_classに取得している' do
188         # リクエストの対象はスクロールであるが、返されるのはスクコマのリストである。
189         # くれぐれも間違えないように。 
190         get :play, :id => @item_id
191         expect(assigns(:my_model_class)).to be ScrollPanel
192       end
193       it '@itemにリクエストしたスクロールを取得している' do
194         get :play, :id => @item_id
195         expect(assigns(:item)).to eq(@item)
196       end
197       it 'リストグループのマニフェストからプレイリストの取得処理を@listに取得している' do
198         get :play, :id => @item_id
199         expect(assigns(:list).item_name).to eq('scroll_panel')
200         expect(assigns(:list).list_name).to eq('play')
201       end
202       it '@itemsにプレイリストを取得している' do
203         get :play, :id => @item_id
204         expect(assigns(:items).size).to be >= 3
205         expect(assigns(:count)).to be >= 3
206         expect(assigns(:pager)).to be nil
207       end
208       context 'html形式' do
209         it 'playテンプレートを描画する' do
210           get :play, :id => @item_id
211           expect(response).to render_template("play")
212         end
213       end
214       context 'json形式' do
215         it 'jsonデータを返す' do
216           get :play, :id => @item_id, :format => :json
217           expect {JSON.parse(response.body)}.not_to raise_error
218         end
219       end
220     end
221     context 'サインインの状態が例外的なとき' do
222       it 'ユーザではなく管理者でも通常通り応答する' do
223         sign_in @admin
224         return_ok_at_get :play, id: @item_id
225         return_ok_at_get :play, id: @item_id, format: :json
226       end
227       it '創作活動してないROM専の読者にも通常通り応答する' do
228         sign_in @user_rom
229         return_ok_at_get :play, id: @item_id
230         return_ok_at_get :play, id: @item_id, format: :json
231       end
232       it 'ゲスト(サインインしていない)はサインインページに遷移する' do
233         announce_sign_in_at_get :play, id: @item_id
234       end
235     end
236   end
237   
238   describe '新規作成フォーム表示に於いて' do
239     before do
240       @item = @author_yas.scrolls.first
241       @item_id = @item.id
242     end
243     context '正常系の共通処理' do
244       before do
245         sign_in @user_yas
246       end
247       it '[html, json]でステータス OKを返す' do
248         return_ok_at_get :new
249         return_ok_at_get :new, format: :json
250       end
251       it 'コントローラのマニフェストからScrollモデルを@my_model_classに取得している' do
252         get :new
253         expect(assigns(:my_model_class)).to be Scroll
254       end
255       it '@itemに新規データを用意している' do
256         get :new
257         expect(assigns(:item)).to eq(@item)
258       end
259       it '@formに入力フォーム(Bucket)を取得している' do
260         get :new
261         expect(assigns(:form).item).to eq(@item)
262         expect(assigns(:form).mounted).to eq(true)
263         expect(assigns(:form).submit).to eq(true)
264       end
265       it 'スクロールモデルにデフォルト値補充とブーストを依頼している' do
266         expect_any_instance_of(Scroll).to receive(:supply_default)
267         expect_any_instance_of(Scroll).to receive(:boosts)
268         get :new
269       end
270       context 'html形式' do
271         it 'formテンプレートを描画する' do
272           get :new
273           expect(response).to render_template("templates/r/form/form")
274         end
275       end
276       context 'json形式' do
277         it 'jsonデータを返す' do
278           get :new, format: :json
279           expect {JSON.parse(response.body)}.not_to raise_error
280         end
281       end
282     end
283     context 'サインインの状態が例外的なとき' do
284       before do
285         sign_in @user_rom
286       end
287       it '創作活動してないROM専の読者には作家登録ページへ遷移する' do
288         get :new
289         expect(response.status).to eq(302)
290         expect(response).to redirect_to new_author_path
291         return_ok_at_get :play, id: @item_id, format: :json
292       end
293       it 'ゲスト(サインインしていない)はサインインページに遷移する' do
294         announce_sign_in_at_get :new, id: @item_id
295       end
296       it 'ユーザではなく管理者でもサインインページに遷移する' do
297         sign_in @admin
298         announce_sign_in_at_get :new, id: @item_id
299       end
300     end
301     context 'ユーザだが作家登録していないとき' do
302       before do
303         @author.destroy
304       end
305       context 'html形式' do
306         it 'ステータスコード302 Foundを返す' do
307           get :new, @attr
308           response.status.should eq 302
309         end
310         it '' do
311           get :new, @attr
312           response.body.should redirect_to new_author_path
313         end
314       end
315     end
316   end
317
318   describe '新規作成に於いて' do
319     before do
320       sign_in @user
321       @attr = FactoryGirl.attributes_for(:scroll, :author_id => @author.id, :title => 'normal')
322     end
323     context '事前チェックしておく' do
324       it 'スクロールモデルにデフォルト値補充を依頼している' do
325         Scroll.any_instance.should_receive(:supply_default).exactly(1)
326         post :create, :scroll => @attr
327       end
328       it 'スクロールモデルにカラム値復元を依頼している' do
329         Scroll.any_instance.should_receive(:attributes=).exactly(1)
330         post :create, :scroll => @attr
331       end
332       it 'スクロールモデルに上書き補充を依頼している' do
333         Scroll.any_instance.should_receive(:overwrite).exactly(1)
334         post :create, :scroll => @attr
335       end
336       it 'モデルに保存依頼する' do
337         Scroll.any_instance.should_receive(:save).exactly(1)
338         post :create, :scroll => @attr
339       end
340     end
341     context '正常系の共通処理' do
342       it "@scrollに作成されたスクロールを保持していて、それがDBにある" do
343         post :create, :scroll => @attr
344         assigns(:scroll).should be_a(Scroll)
345         assigns(:scroll).should be_persisted
346       end
347       context 'html形式' do
348         it 'ステータスコード302 Foundを返す' do
349           Scroll.any_instance.stub(:save).and_return(true)
350           post :create, :scroll => @attr
351           response.status.should eq 302
352         end
353         it '作成されたスクロールの表示ページへ遷移する' do
354 #          Scroll.any_instance.stub(:save).and_return(true)
355           post :create, :scroll => @attr
356           response.should redirect_to(Scroll.last)
357         end
358       end
359       context 'json形式' do
360         it 'ステータスコード200 OKを返す' do
361 #          Scroll.any_instance.stub(:save).and_return(true)
362           post :create, :scroll => @attr, :format => :json
363           response.should be_success 
364         end
365         it '作成されたスクロールをjsonデータで返す' do
366           post :create, :scroll => @attr, :format => :json
367           lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
368         end
369         it 'データがアレになっている' do
370           post :create, :scroll => @attr, :format => :json
371           json = JSON.parse response.body
372           json["title"].should match(/normal/)
373           json["visible"].should_not be_nil
374         end
375       end
376     end
377     context 'ユーザ権限がないとき' do
378       before do
379         sign_out @user
380       end
381       context 'html形式' do
382         it 'ステータスコード302 Foundを返す' do
383           post :create, :scroll => @attr
384           response.status.should eq 302
385         end
386         it 'サインインページへ遷移する' do
387           post :create, :scroll => @attr
388           response.body.should redirect_to '/users/sign_in'
389         end
390       end
391       context 'json形式' do
392         it 'ステータスコード401 Unauthorizedを返す' do
393           post :create, :scroll => @attr, :format => :json
394           response.status.should eq 401
395         end
396         it '応答メッセージにUnauthorizedを返す' do
397           post :create, :scroll => @attr, :format => :json
398           response.message.should match(/Unauthorized/)
399         end
400       end
401     end
402     context 'ユーザ権限はないが管理者権限があるとき' do
403       before do
404         sign_out @user
405         sign_in @admin
406       end
407       context 'html形式' do
408         it 'ステータスコード302 Foundを返す' do
409           post :create, :scroll => @attr
410           response.status.should eq 302
411         end
412         it 'サインインページへ遷移する' do
413           post :create, :scroll => @attr
414           response.body.should redirect_to '/users/sign_in'
415         end
416       end
417     end
418     context 'ユーザだが作家登録していないとき' do
419       before do
420         @author.destroy
421       end
422       context 'html形式' do
423         it 'ステータスコード302 Foundを返す' do
424           post :create, :scroll => @attr
425           response.status.should eq 302
426         end
427         it '作家登録ページへ遷移する' do
428           post :create, :scroll => @attr
429           response.body.should redirect_to new_author_path
430         end
431       end
432     end
433     context '検証、保存に失敗した' do
434       before do
435         Scroll.any_instance.stub(:save).and_return(false)
436       end
437       it "未保存のスクロールを保持している" do
438         post :create, :scroll => @attr
439         assigns(:scroll).should be_a_new(Scroll)
440       end
441       context 'html形式' do
442         it 'ステータスコード200 OKを返す' do
443           post :create, :scroll => @attr
444           response.status.should eq 200
445         end
446         it '新規ページを描画する' do
447           post :create, :scroll => @attr
448           response.should render_template("new")
449         end
450       end
451       context 'json形式' do
452         it 'ステータスコード422 unprocessable_entity を返す' do
453           post :create, :scroll => @attr, :format => :json
454           response.status.should eq 422
455         end
456         it '応答メッセージUnprocessable Entityを返す' do
457           post :create, :scroll => @attr, :format => :json
458           response.message.should match(/Unprocessable/)
459         end
460       end
461     end
462   end
463
464   describe '編集フォーム表示に於いて' do
465     before do
466       @scroll = FactoryGirl.create :scroll, :author_id => @user.author.id
467       sign_in @user
468       Scroll.stub(:edit).and_return(@scroll)
469     end
470     context '正常系の共通処理' do
471       it 'ステータスコード200 OKを返す' do
472         get :edit, :id => @scroll.id
473         response.should be_success 
474       end
475       it 'スクロールモデルに編集取得を問い合わせている' do
476         Scroll.should_receive(:edit).exactly(1)
477         get :edit, :id => @scroll.id
478       end
479       it '@scrollにデータを用意している' do
480         get :edit, :id => @scroll.id
481         assigns(:scroll).should eq @scroll
482       end
483       context 'html形式' do
484         it 'editテンプレートを描画する' do
485           get :edit, :id => @scroll.id
486           response.should render_template("edit")
487         end
488       end
489       context 'js形式' do
490         it 'edit.jsテンプレートを描画する' do
491           get :edit, :id => @scroll.id, :format => :js
492           response.should render_template("edit")
493         end
494       end
495     end
496     context 'ユーザ権限がないとき' do
497       before do
498         sign_out @user
499       end
500       context 'html形式' do
501         it 'ステータスコード302 Foundを返す' do
502           get :edit, :id => @scroll.id
503           response.status.should eq 302
504         end
505         it 'サインインページへ遷移する' do
506           get :edit, :id => @scroll.id
507           response.body.should redirect_to '/users/sign_in'
508         end
509       end
510       context 'js形式' do
511         it 'ステータスコード401 Unauthorizedを返す' do
512           get :edit, :id => @scroll.id, :format => :js
513           response.status.should eq 401
514         end
515         it '応答メッセージにUnauthorizedを返す' do
516           get :edit, :id => @scroll.id, :format => :js
517           response.message.should match(/Unauthorized/)
518         end
519       end
520     end
521     context 'ユーザ権限はないが管理者権限があるとき' do
522       before do
523         sign_out @user
524         sign_in @admin
525       end
526       context 'html形式' do
527         it 'ステータスコード302 Foundを返す' do
528           get :edit, :id => @scroll.id
529           response.status.should eq 302
530         end
531         it 'サインインページへ遷移する' do
532           get :edit, :id => @scroll.id
533           response.body.should redirect_to '/users/sign_in'
534         end
535       end
536     end
537     context 'ユーザだが作家登録していないとき' do
538       before do
539         @author.destroy
540       end
541       context 'html形式' do
542         it 'ステータスコード302 Foundを返す' do
543           get :edit, :id => @scroll.id
544           response.status.should eq 302
545         end
546         it '作家登録ページへ遷移する' do
547           get :edit, :id => @scroll.id
548           response.body.should redirect_to new_author_path
549         end
550       end
551     end
552   end
553
554   describe '更新に於いて' do
555     before do
556       @scroll = FactoryGirl.create :scroll, :author => @author
557       @attr = FactoryGirl.attributes_for(:scroll, :author_id => @author.id, :title => 'updated title', :visible => 0)
558       sign_in @user
559     end
560     context '事前チェックしておく' do
561       it 'スクロールモデルに編集取得を問い合わせている' do
562         Scroll.stub(:edit).with(any_args()).and_return @scroll
563         Scroll.should_receive(:edit).exactly(1)
564         put :update, :id => @scroll.id, :scroll => @attr
565       end
566       it 'スクロールモデルにカラム値復元を依頼している' do
567         Scroll.any_instance.should_receive(:attributes=).exactly(1)
568         put :update, :id => @scroll.id, :scroll => @attr
569       end
570       it 'スクロールモデルに上書き補充を依頼している' do
571         Scroll.any_instance.should_receive(:overwrite).exactly(1)
572         put :update, :id => @scroll.id, :scroll => @attr
573       end
574       it 'モデルに更新を依頼する' do
575         Scroll.any_instance.stub(:save).with(any_args).and_return true
576         Scroll.any_instance.should_receive(:save).exactly(1)
577         put :update, :id => @scroll.id, :scroll => @attr
578       end
579       it '@scrollにアレを取得している' do
580         put :update, :id => @scroll.id, :scroll => @attr
581         assigns(:scroll).id.should eq(@scroll.id)
582       end
583     end
584     context '正常系の共通処理' do
585       it '更新される' do
586         put :update, :id => @scroll.id, :scroll => @attr
587         Scroll.find(@scroll.id).visible.should eq 0
588       end
589       context 'html形式' do
590         it 'ステータスコード302 Foundを返す' do
591           Scroll.any_instance.stub(:save).with(any_args()).and_return(true)
592           put :update, :id => @scroll.id, :scroll => @attr
593           response.status.should eq 302
594         end
595         it '更新されたスクロールの表示ページへ遷移する' do
596           put :update, :id => @scroll.id, :scroll => @attr
597           response.should redirect_to(@scroll)
598         end
599       end
600       context 'json形式' do
601         it 'ステータスコード200 OKを返す' do
602           Scroll.any_instance.stub(:save).with(any_args()).and_return(true)
603           put :update, :id => @scroll.id, :scroll => @attr, :format => :json
604           response.should be_success 
605         end
606         it 'ページ本体は特に返さない' do
607           Scroll.any_instance.stub(:save).with(any_args()).and_return(true)
608           put :update, :id => @scroll.id, :scroll => @attr, :format => :json
609           response.body.should match /./
610         end
611       end
612     end
613     context 'ユーザ権限がないとき' do
614       before do
615         sign_out @user
616       end
617       context 'html形式' do
618         it 'ステータスコード302 Foundを返す' do
619           put :update, :id => @scroll.id, :scroll => @attr
620           response.status.should eq 302
621         end
622         it 'サインインページへ遷移する' do
623           put :update, :id => @scroll.id, :scroll => @attr
624           response.body.should redirect_to '/users/sign_in'
625         end
626       end
627       context 'json形式' do
628         it '応答メッセージにUnauthorizedを返す' do
629           put :update, :id => @scroll.id, :scroll => @attr, :format => :json
630           response.message.should match(/Unauthorized/)
631         end
632       end
633     end
634     context 'ユーザ権限はないが管理者権限があるとき' do
635       before do
636         sign_out @user
637         sign_in @admin
638       end
639       context 'html形式' do
640         it 'ステータスコード302 Foundを返す' do
641           put :update, :id => @scroll.id, :scroll => @attr
642           response.status.should eq 302
643         end
644         it 'サインインページへ遷移する' do
645           put :update, :id => @scroll.id, :scroll => @attr
646           response.body.should redirect_to '/users/sign_in'
647         end
648       end
649     end
650     context 'ユーザだが作家登録していないとき' do
651       before do
652         @author.destroy
653       end
654       context 'html形式' do
655         it 'ステータスコード302 Foundを返す' do
656           put :update, :id => @scroll.id, :scroll => @attr
657           response.status.should eq 302
658         end
659         it '作家登録ページへ遷移する' do
660           put :update, :id => @scroll.id, :scroll => @attr
661           response.body.should redirect_to new_author_path
662         end
663       end
664     end
665     context '検証、保存に失敗したとき' do
666       before do
667         Scroll.any_instance.stub(:save).and_return(false)
668       end
669       context 'html形式' do
670         it 'ステータスコード200 Okを返す' do
671           put :update, :id => @scroll.id, :scroll => @attr
672           response.status.should eq 200
673         end
674         it '編集ページを描画する' do
675           put :update, :id => @scroll.id, :scroll => @attr
676           response.should render_template("edit")
677         end
678       end
679       context 'json形式' do
680         it 'ステータスコード422 unprocessable_entity を返す' do
681           Scroll.any_instance.stub(:save).and_return(false)
682           put :update, :id => @scroll.id, :scroll => @attr, :format => :json
683           response.status.should eq 422
684         end
685         it '応答メッセージUnprocessable Entityを返す' do
686           put :update, :id => @scroll.id, :scroll => @attr, :format => :json
687           response.message.should match(/Unprocessable/)
688         end
689       end
690     end
691   end
692
693   describe '削除に於いて' do
694     before do
695       @scroll = FactoryGirl.create :scroll, :author => @author
696       sign_in @user
697     end
698     context '事前チェックしておく' do
699       before do
700         Scroll.stub(:edit).with(any_args()).and_return @scroll
701         Scroll.any_instance.stub(:destroy_with_scroll_panel).with(any_args()).and_return(true)
702       end
703       it 'スクロールモデルに編集取得を問い合わせている' do
704         Scroll.should_receive(:edit).exactly(1)
705         delete :destroy, :id => @scroll.id
706       end
707       it 'モデルに削除を依頼する' do
708         Scroll.any_instance.should_receive(:destroy_with_scroll_panel).exactly(1)
709         delete :destroy, :id => @scroll.id
710       end
711       it '@scrollにアレを取得している' do
712         delete :destroy, :id => @scroll.id
713         assigns(:scroll).id.should eq(@scroll.id)
714       end
715     end
716     context '正常系の共通処理' do
717       it '削除される' do
718         lambda {
719           delete :destroy, :id => @scroll.id
720         }.should change Scroll, :count
721       end
722       context 'html形式' do
723         before do
724           Scroll.any_instance.stub(:destroy_with_scroll_panel).with(any_args()).and_return(true)
725         end
726         it 'ステータスコード302 Foundを返す' do
727           delete :destroy, :id => @scroll.id
728           response.status.should eq 302
729         end
730         it 'マイスクロールの一覧ページへ遷移する' do
731           delete :destroy, :id => @scroll.id
732           response.should redirect_to('/home/scrolls')
733         end
734       end
735       context 'json形式' do
736         before do
737           Scroll.any_instance.stub(:destroy_with_scroll_panel).with(any_args()).and_return(true)
738         end
739         it 'ステータスコード200 OKを返す' do
740           delete :destroy, :id => @scroll.id, :format => :json
741           response.should be_success 
742         end
743         it 'ページ本体は特に返さない' do
744           delete :destroy, :id => @scroll.id, :format => :json
745           response.body.should match /./
746         end
747       end
748     end
749     context 'ユーザ権限がないとき' do
750       before do
751         sign_out @user
752       end
753       context 'html形式' do
754         it 'ステータスコード302 Foundを返す' do
755           delete :destroy, :id => @scroll.id
756           response.status.should eq 302
757         end
758         it 'サインインページへ遷移する' do
759           delete :destroy, :id => @scroll.id
760           response.body.should redirect_to '/users/sign_in'
761         end
762       end
763       context 'json形式' do
764         it '応答メッセージにUnauthorizedを返す' do
765           delete :destroy, :id => @scroll.id, :format => :json
766           response.message.should match(/Unauthorized/)
767         end
768       end
769     end
770     context 'ユーザ権限はないが管理者権限があるとき' do
771       before do
772         sign_out @user
773         sign_in @admin
774       end
775       context 'html形式' do
776         it 'ステータスコード302 Foundを返す' do
777           delete :destroy, :id => @scroll.id
778           response.status.should eq 302
779         end
780         it 'サインインページへ遷移する' do
781           delete :destroy, :id => @scroll.id
782           response.body.should redirect_to '/users/sign_in'
783         end
784       end
785     end
786     context 'ユーザだが作家登録していないとき' do
787       before do
788         @author.destroy
789       end
790       context 'html形式' do
791         it 'ステータスコード302 Foundを返す' do
792           delete :destroy, :id => @scroll.id
793           response.status.should eq 302
794         end
795         it '作家登録ページへ遷移する' do
796           delete :destroy, :id => @scroll.id
797           response.body.should redirect_to new_author_path
798         end
799       end
800     end
801     context '削除に失敗したとき' do
802       before do
803         Scroll.any_instance.stub(:destroy_with_scroll_panel).and_return(false)
804       end
805       context 'html形式' do
806         it 'ステータスコード302 Foundを返す' do
807           delete :destroy, :id => @scroll.id
808           response.status.should eq 302
809         end
810         it 'そのスクロールの詳細ページへ遷移する' do
811           delete :destroy, :id => @scroll.id
812           response.should redirect_to(scroll_path(@scroll))
813         end
814       end
815       context 'json形式' do
816         it 'ステータスコード422 unprocessable_entity を返す' do
817           delete :destroy, :id => @scroll.id, :format => :json
818           response.status.should eq 422
819         end
820         it '応答メッセージUnprocessable Entityを返す' do
821           delete :destroy, :id => @scroll.id, :format => :json
822           response.message.should match(/Unprocessable/)
823         end
824       end
825     end
826   end
827
828 else
829   describe '一覧表示に於いて' do
830     before do
831       @scroll = FactoryGirl.create :scroll, :author_id => @user.author.id
832       Scroll.stub(:list).and_return([@scroll, @scroll, @scroll])
833       sign_in @user
834     end
835     context '正常系の共通処理' do
836       it 'ステータスコード200 OKを返す' do
837         get :index
838         response.should be_success 
839       end
840       context 'html形式' do
841         it 'indexテンプレートを描画する' do
842           get :index
843           response.should render_template("index")
844         end
845       end
846       context 'json形式' do
847         it 'jsonデータを返す' do
848           get :index, :format => :json
849           lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
850         end
851       end
852     end
853     context 'ユーザ権限がないとき' do
854       before do
855         sign_out @user
856       end
857       it 'ステータスコード200 OKを返す' do
858         get :index
859         response.should be_success 
860       end
861       context 'html形式' do
862         it 'indexテンプレートを描画する' do
863           get :index
864           response.should render_template("index")
865         end
866       end
867       context 'json形式' do
868         it 'jsonデータを返す' do
869           get :index, :format => :json
870           lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
871         end
872       end
873     end
874   end
875   
876   describe '単体表示に於いて' do
877     before do
878       @scroll = FactoryGirl.create :scroll, :author_id => @user.author.id, :title => 'normal'
879       Scroll.stub(:show).and_return(@scroll)
880       sign_in @user
881     end
882     context '正常系の共通処理' do
883       it 'ステータスコード200 OKを返す' do
884         get :show, :id => @scroll.id
885         response.should be_success
886       end
887       context 'html形式' do
888         it 'showテンプレートを描画する' do
889           get :show, :id => @scroll.id
890           response.should render_template("show")
891         end
892       end
893       context 'json形式' do
894         it 'jsonデータを返す' do
895           get :show, :id => @scroll.id, :format => :json
896           lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
897         end
898       end
899     end
900     context 'ユーザ権限がないとき' do
901       before do
902         sign_out @user
903       end
904       it 'ステータスコード200 OKを返す' do
905         get :show, :id => @scroll.id
906         response.should be_success
907       end
908       context 'html形式' do
909         it 'showテンプレートを描画する' do
910           get :show, :id => @scroll.id
911           response.should render_template("show")
912         end
913       end
914       context 'json形式' do
915         it 'jsonデータを返す' do
916           get :show, :id => @scroll.id, :format => :json
917           lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
918         end
919       end
920     end
921   end
922   describe 'スクロール数取得に於いて' do
923     before do
924       Scroll.should_receive(:visible_count).and_return(3)
925       sign_out @user
926     end
927     context '正常系の共通処理' do
928       it 'ステータスコード200 OKを返す' do
929         get :count, :format => :json
930         response.should be_success 
931       end
932       context 'json形式' do
933         it 'jsonデータを返す' do
934           get :count, :format => :json
935           lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
936         end
937       end
938     end
939   end
940
941   describe '新規作成フォーム表示に於いて' do
942     before do
943       sign_in @user
944     end
945     context '正常系の共通処理' do
946       it 'ステータスコード200 OKを返す' do
947         get :new
948         response.should be_success 
949       end
950       context 'html形式' do
951         it 'newテンプレートを描画する' do
952           get :new
953           response.should render_template("new")
954         end
955       end
956       context 'js形式' do
957         it 'new.jsテンプレートを描画する' do
958           get :new, :format => :js
959           response.should render_template("new")
960         end
961       end
962       context 'json形式' do
963         it 'jsonデータを返す' do
964           get :new, :format => :json
965           lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
966         end
967       end
968     end
969     context 'ユーザ権限がないとき' do
970       before do
971         sign_out @user
972       end
973       context 'html形式' do
974         it 'ステータスコード302 Foundを返す' do
975           get :new
976           response.status.should eq 302
977         end
978         it 'サインインページへ遷移する' do
979           get :new
980           response.body.should redirect_to '/users/sign_in'
981         end
982       end
983       context 'js形式' do
984         it 'ステータスコード401 Unauthorizedを返す' do
985           get :new, :format => :js
986           response.status.should eq 401
987         end
988         it '応答メッセージにUnauthorizedを返す' do
989           get :new, :format => :js
990           response.message.should match(/Unauthorized/)
991         end
992       end
993       context 'json形式' do
994         it 'ステータスコード401 Unauthorizedを返す' do
995           get :new, :format => :json
996           response.status.should eq 401
997         end
998         it '応答メッセージにUnauthorizedを返す' do
999           get :new, :format => :json
1000           response.message.should match(/Unauthorized/)
1001         end
1002       end
1003     end
1004   end
1005
1006   describe '新規作成に於いて' do
1007     before do
1008       sign_in @user
1009       @attr = FactoryGirl.attributes_for(:scroll, :author_id => @author.id, :title => 'normal')
1010     end
1011     context '正常系の共通処理' do
1012       context 'html形式' do
1013         it 'ステータスコード302 Foundを返す' do
1014           Scroll.any_instance.stub(:save).and_return(true)
1015           post :create, :scroll => @attr
1016           response.status.should eq 302
1017         end
1018         it '作成されたスクロールの表示ページへ遷移する' do
1019 #          Scroll.any_instance.stub(:save).and_return(true)
1020           post :create, :scroll => @attr
1021           response.should redirect_to(Scroll.last)
1022         end
1023       end
1024       context 'json形式' do
1025         it 'ステータスコード200 OKを返す' do
1026 #          Scroll.any_instance.stub(:save).and_return(true)
1027           post :create, :scroll => @attr, :format => :json
1028           response.should be_success 
1029         end
1030         it '作成されたスクロールをjsonデータで返す' do
1031           post :create, :scroll => @attr, :format => :json
1032           lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
1033         end
1034       end
1035     end
1036     context 'ユーザ権限がないとき' do
1037       before do
1038         sign_out @user
1039       end
1040       context 'html形式' do
1041         it 'ステータスコード302 Foundを返す' do
1042           post :create, :scroll => @attr
1043           response.status.should eq 302
1044         end
1045         it 'サインインページへ遷移する' do
1046           post :create, :scroll => @attr
1047           response.body.should redirect_to '/users/sign_in'
1048         end
1049       end
1050       context 'json形式' do
1051         it 'ステータスコード401 Unauthorizedを返す' do
1052           post :create, :scroll => @attr, :format => :json
1053           response.status.should eq 401
1054         end
1055         it '応答メッセージにUnauthorizedを返す' do
1056           post :create, :scroll => @attr, :format => :json
1057           response.message.should match(/Unauthorized/)
1058         end
1059       end
1060     end
1061   end
1062
1063   describe '編集フォーム表示に於いて' do
1064     before do
1065       @scroll = FactoryGirl.create :scroll, :author_id => @user.author.id
1066       sign_in @user
1067       Scroll.stub(:edit).and_return(@scroll)
1068     end
1069     context '正常系の共通処理' do
1070       it 'ステータスコード200 OKを返す' do
1071         get :edit, :id => @scroll.id
1072         response.should be_success 
1073       end
1074       context 'html形式' do
1075         it 'editテンプレートを描画する' do
1076           get :edit, :id => @scroll.id
1077           response.should render_template("edit")
1078         end
1079       end
1080       context 'js形式' do
1081         it 'edit.jsテンプレートを描画する' do
1082           get :edit, :id => @scroll.id, :format => :js
1083           response.should render_template("edit")
1084         end
1085       end
1086     end
1087     context 'ユーザ権限がないとき' do
1088       before do
1089         sign_out @user
1090       end
1091       context 'html形式' do
1092         it 'ステータスコード302 Foundを返す' do
1093           get :edit, :id => @scroll.id
1094           response.status.should eq 302
1095         end
1096         it 'サインインページへ遷移する' do
1097           get :edit, :id => @scroll.id
1098           response.body.should redirect_to '/users/sign_in'
1099         end
1100       end
1101       context 'js形式' do
1102         it 'ステータスコード401 Unauthorizedを返す' do
1103           get :edit, :id => @scroll.id, :format => :js
1104           response.status.should eq 401
1105         end
1106         it '応答メッセージにUnauthorizedを返す' do
1107           get :edit, :id => @scroll.id, :format => :js
1108           response.message.should match(/Unauthorized/)
1109         end
1110       end
1111     end
1112   end
1113
1114   describe '更新に於いて' do
1115     before do
1116       @scroll = FactoryGirl.create :scroll, :author => @author
1117       @attr = FactoryGirl.attributes_for(:scroll, :author_id => @author.id, :title => 'updated title', :visible => 0)
1118       sign_in @user
1119     end
1120     context '正常系の共通処理' do
1121       context 'html形式' do
1122         it 'ステータスコード302 Foundを返す' do
1123           Scroll.any_instance.stub(:save).with(any_args()).and_return(true)
1124           put :update, :id => @scroll.id, :scroll => @attr
1125           response.status.should eq 302
1126         end
1127         it '更新されたスクロールの表示ページへ遷移する' do
1128           put :update, :id => @scroll.id, :scroll => @attr
1129           response.should redirect_to(@scroll)
1130         end
1131       end
1132       context 'json形式' do
1133         it 'ステータスコード200 OKを返す' do
1134           Scroll.any_instance.stub(:save).with(any_args()).and_return(true)
1135           put :update, :id => @scroll.id, :scroll => @attr, :format => :json
1136           response.should be_success 
1137         end
1138         it 'ページ本体は特に返さない' do
1139           Scroll.any_instance.stub(:save).with(any_args()).and_return(true)
1140           put :update, :id => @scroll.id, :scroll => @attr, :format => :json
1141           response.body.should match /./
1142         end
1143       end
1144     end
1145     context 'ユーザ権限がないとき' do
1146       before do
1147         sign_out @user
1148       end
1149       it 'ステータスコード302 Foundを返す' do
1150         put :update, :id => @scroll.id, :scroll => @attr
1151         response.status.should eq 302
1152       end
1153       context 'html形式' do
1154         it 'サインインページへ遷移する' do
1155           put :update, :id => @scroll.id, :scroll => @attr
1156           response.body.should redirect_to '/users/sign_in'
1157         end
1158       end
1159     end
1160   end
1161
1162   describe '削除に於いて' do
1163     before do
1164       @scroll = FactoryGirl.create :scroll, :author => @author
1165       sign_in @user
1166     end
1167     context '正常系の共通処理' do
1168       context 'html形式' do
1169         before do
1170           Scroll.any_instance.stub(:destroy_with_scroll_panel).with(any_args()).and_return(true)
1171         end
1172         it 'ステータスコード302 Foundを返す' do
1173           delete :destroy, :id => @scroll.id
1174           response.status.should eq 302
1175         end
1176         it 'マイスクロールの一覧ページへ遷移する' do
1177           delete :destroy, :id => @scroll.id
1178           response.should redirect_to('/home/scroll')
1179         end
1180       end
1181       context 'json形式' do
1182         before do
1183           Scroll.any_instance.stub(:destroy_with_scroll_panel).with(any_args()).and_return(true)
1184         end
1185         it 'ステータスコード200 OKを返す' do
1186           delete :destroy, :id => @scroll.id, :format => :json
1187           response.should be_success 
1188         end
1189         it 'ページ本体は特に返さない' do
1190           delete :destroy, :id => @scroll.id, :format => :json
1191           response.body.should match /./
1192         end
1193       end
1194     end
1195     context 'ユーザ権限がないとき' do
1196       before do
1197         sign_out @user
1198       end
1199       it 'ステータスコード302 Foundを返す' do
1200         delete :destroy, :id => @scroll.id
1201         response.status.should eq 302
1202       end
1203       context 'html形式' do
1204         it 'サインインページへ遷移する' do
1205           delete :destroy, :id => @scroll.id
1206           response.body.should redirect_to '/users/sign_in'
1207         end
1208       end
1209       context 'json形式' do
1210         it '応答メッセージにUnauthorizedを返す' do
1211           delete :destroy, :id => @scroll.id, :format => :json
1212           response.message.should match(/Unauthorized/)
1213         end
1214       end
1215     end
1216   end
1217
1218 end
1219
1220 end