OSDN Git Service

t#30143:au and ar change
[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   describe '一覧表示に於いて' do
17     before do
18       Author.stub(:list).and_return([@author, @author, @author])
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 Author.default_page_size
37       end
38       it '最大を超えると@page_sizeにデフォルト最大値が入る' do
39         get :index, :page_size => 1500
40         assigns(:page_size).should eq Author.max_page_size
41       end
42       it '不正な値が入ると@page_sizeにデフォルト最大値が入る' do
43         get :index, :page_size => 0
44         assigns(:page_size).should eq Author.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         Author.should_receive(:list).exactly(1)
54         get :index
55       end
56       it '@authorsにリストを取得している' do
57         get :index
58         assigns(:authors).should have_at_least(3).items
59       end
60       context 'html形式' do
61         it 'indexテンプレートを描画する' do
62           get :index
63           response.should render_template("index")
64         end
65       end
66       context 'json形式' do
67         it 'jsonデータを返す' do
68           get :index, :format => :json
69           lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
70         end
71         it '作家モデルにjson一覧出力オプションを問い合わせている' do
72           Author.should_receive(:list_json_opt).exactly(1)
73           get :index, :format => :json
74         end
75         it 'データがリスト構造になっている' do
76           get :index, :format => :json
77           json = JSON.parse response.body
78           json.should have_at_least(3).items
79         end
80         it 'リストの先頭くらいは作家っぽいものであって欲しい' do
81           get :index, :format => :json
82           json = JSON.parse response.body
83           json.first.has_key?("name").should be_true
84           json.first.has_key?("user_id").should be_true
85         end
86       end
87     end
88     context '作家権限がないとき' do
89       before do
90         sign_out @user
91       end
92       context 'html形式' do
93         it 'ステータスコード302 Foundを返す' do
94           get :index
95           response.status.should eq 302
96         end
97         it 'サインインページへ遷移する' do
98           get :index
99           response.should redirect_to '/users/sign_in'
100         end
101       end
102       context 'json形式' do
103         it 'ステータスコード401 Unauthorizedを返す' do
104           get :index, :format => :json
105           response.status.should eq 401
106         end
107         it '応答メッセージにUnauthorizedを返す' do
108           get :index, :format => :json
109           response.message.should match(/Unauthorized/)
110         end
111       end
112     end
113   end
114   
115   describe '閲覧に於いて' do
116     before do
117       Author.stub(:show).and_return(@author)
118       sign_in @user
119     end
120     context 'つつがなく終わるとき' do
121       it 'ステータスコード200 OKを返す' do
122         get :show, :id => @author.id
123         response.should be_success
124       end
125       it '作家モデルに単体取得を問い合わせている' do
126         Author.should_receive(:show).exactly(1)
127         get :show
128       end
129       #@authorだとログイン中のアカウントと干渉してしまう。
130       it '@auにアレを取得している' do
131         get :show, :id => @author.id
132         assigns(:au).should eq(@author)
133       end
134       context 'html形式' do
135         it 'showテンプレートを描画する' do
136           get :show, :id => @author.id
137           response.should render_template("show")
138         end
139       end
140       context 'json形式' do
141         it 'jsonデータを返す' do
142           get :show, :id => @author.id, :format => :json
143           lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
144         end
145         it '作家モデルにjson単体出力オプションを問い合わせている' do
146           Author.should_receive(:show_json_opt).exactly(1)
147           get :show, :id => @author.id, :format => :json
148         end
149         it 'データがアレになっている' do
150           get :show, :id => @author.id, :format => :json
151           json = JSON.parse response.body
152           json["name"].should match(/test/)
153           json["user_id"].should eq @author.user_id
154         end
155       end
156     end
157     context '作家権限がないとき' do
158       before do
159         sign_out @user
160       end
161       context 'html形式' do
162         it 'ステータスコード302 Foundを返す' do
163           get :show, :id => @author.id
164           response.status.should eq 302
165         end
166         it 'サインインページへ遷移する' do
167           get :show, :id => @author.id
168           response.body.should redirect_to '/users/sign_in'
169         end
170       end
171       context 'json形式' do
172         it 'ステータスコード401 Unauthorizedを返す' do
173           get :show, :id => @author.id, :format => :json
174           response.status.should eq 401
175         end
176         it '応答メッセージにUnauthorizedを返す' do
177           get :show, :id => @author.id, :format => :json
178           response.message.should match(/Unauthorized/)
179         end
180       end
181     end
182 =begin
183     context '対象作家がないとき' do
184       context 'html形式' do
185         it '例外404 not_foundを返す' do
186           lambda{
187             get :show, :id => 0
188           }.should raise_error(ActiveRecord::RecordNotFound)
189         end
190       end
191       context 'json形式' do
192         it '例外404 not_foundを返す' do
193           lambda{ 
194             get :show, :id => 0, :format => :json
195           }.should raise_error(ActiveRecord::RecordNotFound)
196         end
197       end
198     end
199 =end
200   end
201   
202   describe '作家数取得に於いて' do
203     before do
204       Author.should_receive(:visible_count).and_return(3)
205 #      sign_in @user
206     end
207     context 'つつがなく終わるとき' do
208       it 'ステータスコード200 OKを返す' do
209         get :count, :format => :json
210         response.should be_success 
211       end
212       context 'json形式' do
213         it 'jsonデータを返す' do
214           get :count, :format => :json
215           lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
216         end
217         it 'データがHash構造になっていて作家数が3である' do
218           get :count, :format => :json
219           json = JSON.parse response.body
220           json["count"].should == 3
221         end
222       end
223     end
224   end
225   
226   describe '新規作成フォーム表示に於いて' do
227     before do
228       @new_user = FactoryGirl.create( :user_yas)
229       sign_in @new_user
230     end
231     context 'つつがなく終わるとき' do
232       it 'ステータスコード200 OKを返す' do
233         get :new
234         response.should be_success 
235       end
236       it '@auに新規データを用意している' do
237         get :new
238         assigns(:au).should be_a_new(Author)
239       end
240       it '作家モデルにデフォルト値補充を依頼している' do
241         Author.any_instance.should_receive(:supply_default).exactly(1)
242         get :new
243       end
244       context 'html形式' do
245         it 'newテンプレートを描画する' do
246           get :new
247           response.should render_template("new")
248         end
249       end
250       context 'js形式' do
251         it 'new.jsテンプレートを描画する' do
252           get :new, :format => :js
253           response.should render_template("new")
254         end
255       end
256       context 'json形式' do
257         it 'jsonデータを返す' do
258           get :new, :format => :json
259           lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
260         end
261         it '作家モデルにjson単体出力オプションを問い合わせている' do
262           Author.should_receive(:show_json_opt).exactly(1)
263           get :new, :format => :json
264         end
265       end
266     end
267     context '作家権限がないとき' do
268       before do
269         sign_out @new_user
270       end
271       context 'html形式' do
272         it 'ステータスコード302 Foundを返す' do
273           get :new
274           response.status.should eq 302
275         end
276         it 'サインインページへ遷移する' do
277           get :new
278           response.body.should redirect_to '/users/sign_in'
279         end
280       end
281       context 'js形式' do
282         it 'ステータスコード401 Unauthorizedを返す' do
283           get :new, :format => :js
284           response.status.should eq 401
285         end
286         it '応答メッセージにUnauthorizedを返す' do
287           get :new, :format => :js
288           response.message.should match(/Unauthorized/)
289         end
290       end
291       context 'json形式' do
292         it 'ステータスコード401 Unauthorizedを返す' do
293           get :new, :format => :json
294           response.status.should eq 401
295         end
296         it '応答メッセージにUnauthorizedを返す' do
297           get :new, :format => :json
298           response.message.should match(/Unauthorized/)
299         end
300       end
301     end
302   end
303
304   describe '新規作成に於いて' do
305     before do
306       @new_user = FactoryGirl.create( :user_yas)
307       sign_in @new_user
308       @attr = FactoryGirl.attributes_for(:author, :user_id => @new_user.id, :name => 'ken')
309     end
310     context '事前チェックしておく' do
311       it '作家モデルにデフォルト値補充を依頼している' do
312         Author.any_instance.should_receive(:supply_default).exactly(1)
313         post :create, :author => @attr
314       end
315       it '作家モデルにカラム値復元を依頼している' do
316         Author.any_instance.should_receive(:attributes=).exactly(1)
317         post :create, :author => @attr
318       end
319       it '作家モデルに上書き補充を依頼している' do
320         Author.any_instance.should_receive(:overwrite).exactly(1)
321         post :create, :author => @attr
322       end
323       it 'モデルに保存依頼する' do
324         Author.any_instance.should_receive(:save).exactly(1)
325         post :create, :author => @attr
326       end
327     end
328     context 'つつがなく終わるとき' do
329       it "@auに作成された作家を保持していて、それがDBにある" do
330         post :create, :author => @attr
331         assigns(:au).should be_a(Author)
332         assigns(:au).should be_persisted
333       end
334       context 'html形式' do
335         it 'ステータスコード302 Foundを返す' do
336           Author.any_instance.stub(:save).and_return(true)
337           post :create, :author => @attr
338           response.status.should eq 302
339         end
340         it 'トップページへ遷移する' do
341 #          Author.any_instance.stub(:save).and_return(true)
342           post :create, :author => @attr
343           response.should redirect_to(root_path)
344         end
345       end
346       context 'json形式' do
347         it 'ステータスコード200 OKを返す' do
348 #          Author.any_instance.stub(:save).and_return(true)
349           post :create, :author => @attr, :format => :json
350           response.should be_success 
351         end
352         it '作成された作家をjsonデータで返す' do
353           post :create, :author => @attr, :format => :json
354           lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
355         end
356         it '作家モデルにjson単体出力オプションを問い合わせている' do
357           Author.should_receive(:show_json_opt).exactly(1)
358           post :create, :author => @attr, :format => :json
359         end
360         it 'データがアレになっている' do
361           post :create, :author => @attr, :format => :json
362           json = JSON.parse response.body
363           json["name"].should match(/ken/)
364         end
365       end
366     end
367     context '作家権限がないとき' do
368       before do
369         sign_out @new_user
370       end
371       context 'html形式' do
372         it 'ステータスコード302 Foundを返す' do
373           post :create, :author => @attr
374           response.status.should eq 302
375         end
376         it 'サインインページへ遷移する' do
377           post :create, :author => @attr
378           response.body.should redirect_to '/users/sign_in'
379         end
380       end
381       context 'json形式' do
382         it 'ステータスコード401 Unauthorizedを返す' do
383           post :create, :author => @attr, :format => :json
384           response.status.should eq 401
385         end
386         it '応答メッセージにUnauthorizedを返す' do
387           post :create, :author => @attr, :format => :json
388           response.message.should match(/Unauthorized/)
389         end
390       end
391     end
392     context '検証、保存に失敗した' do
393       before do
394         Author.any_instance.stub(:save).and_return(false)
395       end
396       it "未保存の作家を保持している" do
397         post :create, :author => @attr
398         assigns(:au).should be_a_new(Author)
399       end
400       context 'html形式' do
401         it 'ステータスコード200 OKを返す' do
402           post :create, :author => @attr
403           response.status.should eq 200
404         end
405         it '新規ページを描画する' do
406           post :create, :author => @attr
407           response.should render_template("new")
408         end
409       end
410       context 'json形式' do
411         it 'ステータスコード422 unprocessable_entity を返す' do
412           post :create, :author => @attr, :format => :json
413           response.status.should eq 422
414         end
415         it '応答メッセージUnprocessable Entityを返す' do
416           post :create, :author => @attr, :format => :json
417           response.message.should match(/Unprocessable/)
418         end
419       end
420     end
421   end
422
423   describe '編集フォーム表示に於いて' do
424     before do
425       sign_in @user
426       Author.stub(:edit).and_return(@author)
427     end
428     context 'つつがなく終わるとき' do
429       it 'ステータスコード200 OKを返す' do
430         get :edit, :id => @author.id
431         response.should be_success 
432       end
433       it '作家モデルに編集取得を問い合わせている' do
434         Author.should_receive(:edit).exactly(1)
435         get :edit, :id => @author.id
436       end
437       #@authorだとログイン中のアカウントと干渉してしまう。
438       it '@auにデータを用意している' do
439         get :edit, :id => @author.id
440         assigns(:au).should eq @author
441       end
442       context 'html形式' do
443         it 'editテンプレートを描画する' do
444           get :edit, :id => @author.id
445           response.should render_template("edit")
446         end
447       end
448       context 'js形式' do
449         it 'edit.jsテンプレートを描画する' do
450           get :edit, :id => @author.id, :format => :js
451           response.should render_template("edit")
452         end
453       end
454     end
455     context '作家権限がないとき' do
456       before do
457         sign_out @user
458       end
459       context 'html形式' do
460         it 'ステータスコード302 Foundを返す' do
461           get :edit, :id => @author.id
462           response.status.should eq 302
463         end
464         it 'サインインページへ遷移する' do
465           get :edit, :id => @author.id
466           response.body.should redirect_to '/users/sign_in'
467         end
468       end
469       context 'js形式' do
470         it 'ステータスコード401 Unauthorizedを返す' do
471           get :edit, :id => @author.id, :format => :js
472           response.status.should eq 401
473         end
474         it '応答メッセージにUnauthorizedを返す' do
475           get :edit, :id => @author.id, :format => :js
476           response.message.should match(/Unauthorized/)
477         end
478       end
479     end
480   end
481
482   describe '更新に於いて' do
483     before do
484       @attr = @author.attributes
485       @attr['name'] = 'ryu'
486       sign_in @user
487     end
488     context '事前チェックしておく' do
489       it '作家モデルに編集取得を問い合わせている' do
490         Author.stub(:edit).with(any_args()).and_return @author
491         Author.should_receive(:edit).exactly(1)
492         put :update, :id => @author.id, :author => @attr
493       end
494       it 'モデルにpostデータ転送を依頼する' do
495         Author.any_instance.stub(:attributes=).with(any_args)
496         Author.any_instance.should_receive(:attributes=).exactly(1)
497         put :update, :id => @author.id, :author => @attr
498       end
499       it 'モデルに上書き補充を依頼する' do
500         Author.any_instance.stub(:overwrite).with(any_args)
501         Author.any_instance.should_receive(:overwrite).exactly(1)
502         put :update, :id => @author.id, :author => @attr
503       end
504       it 'モデルに更新を依頼する' do
505         Author.any_instance.stub(:save).with(any_args).and_return true
506         Author.any_instance.should_receive(:save).exactly(1)
507         put :update, :id => @author.id, :author => @attr
508       end
509       it '@auにアレを取得している' do
510         put :update, :id => @author.id, :author => @attr
511         assigns(:au).should eq @author
512       end
513     end
514     context 'つつがなく終わるとき' do
515       it '更新される' do
516         put :update, :id => @author.id, :author => @attr
517         Author.find(@author.id).name.should eq 'ryu'
518       end
519       context 'html形式' do
520         it 'ステータスコード302 Foundを返す' do
521           Author.any_instance.stub(:save).with(any_args()).and_return(true)
522           put :update, :id => @author.id, :author => @attr
523           response.status.should eq 302
524         end
525         it '設定ページへ遷移する' do
526           put :update, :id => @author.id, :author => @attr
527           response.should redirect_to('/home/configure')
528         end
529       end
530       context 'json形式' do
531         it 'ステータスコード200 OKを返す' do
532           Author.any_instance.stub(:save).with(any_args()).and_return(true)
533           put :update, :id => @author.id, :author => @attr, :format => :json
534           response.should be_success 
535         end
536         it 'ページ本体は特に返さない' do
537           Author.any_instance.stub(:save).with(any_args()).and_return(true)
538           put :update, :id => @author.id, :author => @attr, :format => :json
539           response.body.should match /./
540         end
541       end
542     end
543     context '作家権限がないとき' do
544       before do
545         sign_out @user
546       end
547       it 'ステータスコード302 Foundを返す' do
548         put :update, :id => @author.id, :author => @attr
549         response.status.should eq 302
550       end
551       context 'html形式' do
552         it 'サインインページへ遷移する' do
553           put :update, :id => @author.id, :author => @attr
554           response.body.should redirect_to '/users/sign_in'
555         end
556       end
557       context 'json形式' do
558         it '応答メッセージにUnauthorizedを返す' do
559           put :update, :id => @author.id, :author => @attr, :format => :json
560           response.message.should match(/Unauthorized/)
561         end
562       end
563     end
564     context '検証、保存に失敗したとき' do
565       before do
566         Author.any_instance.stub(:save).and_return(false)
567       end
568       context 'html形式' do
569         it 'ステータスコード200 Okを返す' do
570           put :update, :id => @author.id, :author => @attr
571           response.status.should eq 200
572         end
573         it '編集ページを描画する' do
574           put :update, :id => @author.id, :author => @attr
575           response.should render_template("edit")
576         end
577       end
578       context 'json形式' do
579         it 'ステータスコード422 unprocessable_entity を返す' do
580           Author.any_instance.stub(:save).and_return(false)
581           put :update, :id => @author.id, :author => @attr, :format => :json
582           response.status.should eq 422
583         end
584         it '応答メッセージUnprocessable Entityを返す' do
585           put :update, :id => @author.id, :author => @attr, :format => :json
586           response.message.should match(/Unprocessable/)
587         end
588       end
589     end
590   end
591
592 end