OSDN Git Service

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