OSDN Git Service

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