OSDN Git Service

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