OSDN Git Service

fix test
[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         expect(assigns(:item).is_a?(Scroll)).to be true
463         expect(assigns(:item).new_record?).not_to be true
464         expect(assigns(:item)).to eq(@item)
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         expect(assigns(:item).is_a?(Scroll)).to be true
531         expect(assigns(:item).visible).to eq(0)
532         expect(assigns(:item)).to eq(@item)
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       @item =  FactoryGirl.create :scroll_hello_with_scroll_panels, author: @author_yas
605       @item_id = @item.id
606       @attr = {:id => @item_id}
607       @attr_with_json = @attr.merge(:format => :json)
608     end
609     context '正常系の共通処理' do
610       before do
611         sign_in @user_yas
612       end
613       it 'コントローラのマニフェストからScrollモデルを@my_model_classに取得している' do
614         allow_destroy Scroll
615         destroy_save @attr
616         expect(assigns(:my_model_class)).to be Scroll
617       end
618       it '@itemにリクエストしたアイテムを取得している' do
619         allow_destroy Scroll
620         destroy_save @attr
621         expect(assigns(:item).is_a?(Scroll)).to be true
622         expect(assigns(:item)).to eq(@item)
623       end
624       it '削除されている' do
625         lambda {
626           destroy_save @attr
627         }.should change Scroll, :count
628       end
629       it 'ぶら下がるリーフも削除されている' do
630         lambda {
631           destroy_save @attr
632         }.should change ScrollPanel, :count
633       end
634       context 'html形式' do
635         it 'ステータスコード302 Foundを返して, マイスクロールの一覧ページへ遷移する' do
636           destroy_save @attr
637           expect(response.status).to eq(302)
638           expect(response).to redirect_to '/home/scrolls'
639         end
640       end
641       context 'json形式' do
642         it 'ステータスコード200 OKを返して, ページ本体は特に返さない' do
643           destroy_save @attr_with_json
644           expect(response).to be_success
645           expect(response.message).to match /./
646         end
647       end
648     end
649     context '検証、保存に失敗したとき' do
650       before do
651         sign_in @user_yas
652         reject_destroy Scroll
653       end
654       context 'html形式' do
655         it 'ステータスコード302 Foundを返して, そのスクロールの詳細ページへ遷移する' do
656           destroy_save @attr
657           expect(response.status).to eq(302)
658           expect(response).to redirect_to @item
659         end
660       end
661       context 'json形式' do
662         it 'ステータスコード422 unprocessable_entityを返して, 応答メッセージUnprocessable Entityを返す' do
663           destroy_save @attr_with_json
664           expect(response.status).to eq(422)
665           expect(response.message).to match(/Unprocessable/)
666         end
667       end
668     end
669     context 'サインインの状態が例外的なとき' do
670       before do
671         allow_destroy Scroll
672       end
673       it '創作活動してないROM専の読者には作家登録ページへ遷移する' do
674         sign_in @user_rom
675         destroy_save @attr
676         announce_regist_author_for_html
677         announce_regist_author_for_json :delete, @attr_with_json
678       end
679       it 'ゲスト(サインインしていない)はサインインページに遷移する' do
680         destroy_save @attr
681         announce_sign_in_for_html
682         destroy_save @attr_with_json
683         announce_sign_in_for_json
684       end
685       it 'ユーザではなく管理者でもサインインページに遷移する' do
686         sign_in @admin
687         destroy_save id: @item_id
688         announce_sign_in_for_html
689         destroy_save @attr_with_json
690         announce_sign_in_for_json
691       end
692     end
693   end
694
695 else
696   describe '一覧表示に於いて' do
697     before do
698       @scroll = FactoryGirl.create :scroll, :author_id => @user.author.id
699       Scroll.stub(:list).and_return([@scroll, @scroll, @scroll])
700       sign_in @user
701     end
702     context '正常系の共通処理' do
703       it 'ステータスコード200 OKを返す' do
704         get :index
705         response.should be_success 
706       end
707       context 'html形式' do
708         it 'indexテンプレートを描画する' do
709           get :index
710           response.should render_template("index")
711         end
712       end
713       context 'json形式' do
714         it 'jsonデータを返す' do
715           get :index, :format => :json
716           lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
717         end
718       end
719     end
720     context 'ユーザ権限がないとき' do
721       before do
722         sign_out @user
723       end
724       it 'ステータスコード200 OKを返す' do
725         get :index
726         response.should be_success 
727       end
728       context 'html形式' do
729         it 'indexテンプレートを描画する' do
730           get :index
731           response.should render_template("index")
732         end
733       end
734       context 'json形式' do
735         it 'jsonデータを返す' do
736           get :index, :format => :json
737           lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
738         end
739       end
740     end
741   end
742   
743   describe '単体表示に於いて' do
744     before do
745       @scroll = FactoryGirl.create :scroll, :author_id => @user.author.id, :title => 'normal'
746       Scroll.stub(:show).and_return(@scroll)
747       sign_in @user
748     end
749     context '正常系の共通処理' do
750       it 'ステータスコード200 OKを返す' do
751         get :show, :id => @scroll.id
752         response.should be_success
753       end
754       context 'html形式' do
755         it 'showテンプレートを描画する' do
756           get :show, :id => @scroll.id
757           response.should render_template("show")
758         end
759       end
760       context 'json形式' do
761         it 'jsonデータを返す' do
762           get :show, :id => @scroll.id, :format => :json
763           lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
764         end
765       end
766     end
767     context 'ユーザ権限がないとき' do
768       before do
769         sign_out @user
770       end
771       it 'ステータスコード200 OKを返す' do
772         get :show, :id => @scroll.id
773         response.should be_success
774       end
775       context 'html形式' do
776         it 'showテンプレートを描画する' do
777           get :show, :id => @scroll.id
778           response.should render_template("show")
779         end
780       end
781       context 'json形式' do
782         it 'jsonデータを返す' do
783           get :show, :id => @scroll.id, :format => :json
784           lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
785         end
786       end
787     end
788   end
789   describe 'スクロール数取得に於いて' do
790     before do
791       Scroll.should_receive(:visible_count).and_return(3)
792       sign_out @user
793     end
794     context '正常系の共通処理' do
795       it 'ステータスコード200 OKを返す' do
796         get :count, :format => :json
797         response.should be_success 
798       end
799       context 'json形式' do
800         it 'jsonデータを返す' do
801           get :count, :format => :json
802           lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
803         end
804       end
805     end
806   end
807
808   describe '新規作成フォーム表示に於いて' do
809     before do
810       sign_in @user
811     end
812     context '正常系の共通処理' do
813       it 'ステータスコード200 OKを返す' do
814         get :new
815         response.should be_success 
816       end
817       context 'html形式' do
818         it 'newテンプレートを描画する' do
819           get :new
820           response.should render_template("new")
821         end
822       end
823       context 'js形式' do
824         it 'new.jsテンプレートを描画する' do
825           get :new, :format => :js
826           response.should render_template("new")
827         end
828       end
829       context 'json形式' do
830         it 'jsonデータを返す' do
831           get :new, :format => :json
832           lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
833         end
834       end
835     end
836     context 'ユーザ権限がないとき' do
837       before do
838         sign_out @user
839       end
840       context 'html形式' do
841         it 'ステータスコード302 Foundを返す' do
842           get :new
843           response.status.should eq 302
844         end
845         it 'サインインページへ遷移する' do
846           get :new
847           response.body.should redirect_to '/users/sign_in'
848         end
849       end
850       context 'js形式' do
851         it 'ステータスコード401 Unauthorizedを返す' do
852           get :new, :format => :js
853           response.status.should eq 401
854         end
855         it '応答メッセージにUnauthorizedを返す' do
856           get :new, :format => :js
857           response.message.should match(/Unauthorized/)
858         end
859       end
860       context 'json形式' do
861         it 'ステータスコード401 Unauthorizedを返す' do
862           get :new, :format => :json
863           response.status.should eq 401
864         end
865         it '応答メッセージにUnauthorizedを返す' do
866           get :new, :format => :json
867           response.message.should match(/Unauthorized/)
868         end
869       end
870     end
871   end
872
873   describe '新規作成に於いて' do
874     before do
875       sign_in @user
876       @attr = FactoryGirl.attributes_for(:scroll, :author_id => @author.id, :title => 'normal')
877     end
878     context '正常系の共通処理' do
879       context 'html形式' do
880         it 'ステータスコード302 Foundを返す' do
881           Scroll.any_instance.stub(:save).and_return(true)
882           post :create, :scroll => @attr
883           response.status.should eq 302
884         end
885         it '作成されたスクロールの表示ページへ遷移する' do
886 #          Scroll.any_instance.stub(:save).and_return(true)
887           post :create, :scroll => @attr
888           response.should redirect_to(Scroll.last)
889         end
890       end
891       context 'json形式' do
892         it 'ステータスコード200 OKを返す' do
893 #          Scroll.any_instance.stub(:save).and_return(true)
894           post :create, :scroll => @attr, :format => :json
895           response.should be_success 
896         end
897         it '作成されたスクロールをjsonデータで返す' do
898           post :create, :scroll => @attr, :format => :json
899           lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
900         end
901       end
902     end
903     context 'ユーザ権限がないとき' do
904       before do
905         sign_out @user
906       end
907       context 'html形式' do
908         it 'ステータスコード302 Foundを返す' do
909           post :create, :scroll => @attr
910           response.status.should eq 302
911         end
912         it 'サインインページへ遷移する' do
913           post :create, :scroll => @attr
914           response.body.should redirect_to '/users/sign_in'
915         end
916       end
917       context 'json形式' do
918         it 'ステータスコード401 Unauthorizedを返す' do
919           post :create, :scroll => @attr, :format => :json
920           response.status.should eq 401
921         end
922         it '応答メッセージにUnauthorizedを返す' do
923           post :create, :scroll => @attr, :format => :json
924           response.message.should match(/Unauthorized/)
925         end
926       end
927     end
928   end
929
930   describe '編集フォーム表示に於いて' do
931     before do
932       @scroll = FactoryGirl.create :scroll, :author_id => @user.author.id
933       sign_in @user
934       Scroll.stub(:edit).and_return(@scroll)
935     end
936     context '正常系の共通処理' do
937       it 'ステータスコード200 OKを返す' do
938         get :edit, :id => @scroll.id
939         response.should be_success 
940       end
941       context 'html形式' do
942         it 'editテンプレートを描画する' do
943           get :edit, :id => @scroll.id
944           response.should render_template("edit")
945         end
946       end
947       context 'js形式' do
948         it 'edit.jsテンプレートを描画する' do
949           get :edit, :id => @scroll.id, :format => :js
950           response.should render_template("edit")
951         end
952       end
953     end
954     context 'ユーザ権限がないとき' do
955       before do
956         sign_out @user
957       end
958       context 'html形式' do
959         it 'ステータスコード302 Foundを返す' do
960           get :edit, :id => @scroll.id
961           response.status.should eq 302
962         end
963         it 'サインインページへ遷移する' do
964           get :edit, :id => @scroll.id
965           response.body.should redirect_to '/users/sign_in'
966         end
967       end
968       context 'js形式' do
969         it 'ステータスコード401 Unauthorizedを返す' do
970           get :edit, :id => @scroll.id, :format => :js
971           response.status.should eq 401
972         end
973         it '応答メッセージにUnauthorizedを返す' do
974           get :edit, :id => @scroll.id, :format => :js
975           response.message.should match(/Unauthorized/)
976         end
977       end
978     end
979   end
980
981   describe '更新に於いて' do
982     before do
983       @scroll = FactoryGirl.create :scroll, :author => @author
984       @attr = FactoryGirl.attributes_for(:scroll, :author_id => @author.id, :title => 'updated title', :visible => 0)
985       sign_in @user
986     end
987     context '正常系の共通処理' do
988       context 'html形式' do
989         it 'ステータスコード302 Foundを返す' do
990           Scroll.any_instance.stub(:save).with(any_args()).and_return(true)
991           put :update, :id => @scroll.id, :scroll => @attr
992           response.status.should eq 302
993         end
994         it '更新されたスクロールの表示ページへ遷移する' do
995           put :update, :id => @scroll.id, :scroll => @attr
996           response.should redirect_to(@scroll)
997         end
998       end
999       context 'json形式' do
1000         it 'ステータスコード200 OKを返す' do
1001           Scroll.any_instance.stub(:save).with(any_args()).and_return(true)
1002           put :update, :id => @scroll.id, :scroll => @attr, :format => :json
1003           response.should be_success 
1004         end
1005         it 'ページ本体は特に返さない' do
1006           Scroll.any_instance.stub(:save).with(any_args()).and_return(true)
1007           put :update, :id => @scroll.id, :scroll => @attr, :format => :json
1008           response.body.should match /./
1009         end
1010       end
1011     end
1012     context 'ユーザ権限がないとき' do
1013       before do
1014         sign_out @user
1015       end
1016       it 'ステータスコード302 Foundを返す' do
1017         put :update, :id => @scroll.id, :scroll => @attr
1018         response.status.should eq 302
1019       end
1020       context 'html形式' do
1021         it 'サインインページへ遷移する' do
1022           put :update, :id => @scroll.id, :scroll => @attr
1023           response.body.should redirect_to '/users/sign_in'
1024         end
1025       end
1026     end
1027   end
1028
1029   describe '削除に於いて' do
1030     before do
1031       @scroll = FactoryGirl.create :scroll, :author => @author
1032       sign_in @user
1033     end
1034     context '正常系の共通処理' do
1035       context 'html形式' do
1036         before do
1037           Scroll.any_instance.stub(:destroy_with_scroll_panel).with(any_args()).and_return(true)
1038         end
1039         it 'ステータスコード302 Foundを返す' do
1040           delete :destroy, :id => @scroll.id
1041           response.status.should eq 302
1042         end
1043         it 'マイスクロールの一覧ページへ遷移する' do
1044           delete :destroy, :id => @scroll.id
1045           response.should redirect_to('/home/scroll')
1046         end
1047       end
1048       context 'json形式' do
1049         before do
1050           Scroll.any_instance.stub(:destroy_with_scroll_panel).with(any_args()).and_return(true)
1051         end
1052         it 'ステータスコード200 OKを返す' do
1053           delete :destroy, :id => @scroll.id, :format => :json
1054           response.should be_success 
1055         end
1056         it 'ページ本体は特に返さない' do
1057           delete :destroy, :id => @scroll.id, :format => :json
1058           response.body.should match /./
1059         end
1060       end
1061     end
1062     context 'ユーザ権限がないとき' do
1063       before do
1064         sign_out @user
1065       end
1066       it 'ステータスコード302 Foundを返す' do
1067         delete :destroy, :id => @scroll.id
1068         response.status.should eq 302
1069       end
1070       context 'html形式' do
1071         it 'サインインページへ遷移する' do
1072           delete :destroy, :id => @scroll.id
1073           response.body.should redirect_to '/users/sign_in'
1074         end
1075       end
1076       context 'json形式' do
1077         it '応答メッセージにUnauthorizedを返す' do
1078           delete :destroy, :id => @scroll.id, :format => :json
1079           response.message.should match(/Unauthorized/)
1080         end
1081       end
1082     end
1083   end
1084
1085 end
1086
1087 end