OSDN Git Service

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