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