OSDN Git Service

t#29400:update artist, author:itr1
[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     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 = @user.author
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 '@artistにアレを取得している' do
129         get :show, :id => @artist.id
130         assigns(:artist).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\r
224     before do\r
225       sign_in @user\r
226     end\r
227     context 'つつがなく終わるとき' do\r
228       it 'ステータスコード200 OKを返す' do\r
229         get :new\r
230         response.should be_success \r
231       end\r
232       it '@artistに新規データを用意している' do\r
233         get :new\r
234         assigns(:artist).should be_a_new(Artist)\r
235       end\r
236       it '絵師モデルにデフォルト値補充を依頼している' do\r
237         Artist.any_instance.should_receive(:supply_default).exactly(1)\r
238         get :new\r
239       end\r
240       context 'html形式' do\r
241         it 'newテンプレートを描画する' do\r
242           get :new\r
243           response.should render_template("new")\r
244         end\r
245       end\r
246       context 'js形式' do\r
247         it 'new.jsテンプレートを描画する' do\r
248           get :new, :format => :js\r
249           response.should render_template("new")\r
250         end\r
251       end\r
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       end
258     end\r
259     context '作家権限がないとき' do\r
260       before do\r
261         sign_out @user\r
262       end\r
263       context 'html形式' do\r
264         it 'ステータスコード302 Foundを返す' do\r
265           get :new\r
266           response.status.should eq 302\r
267         end\r
268         it 'サインインページへ遷移する' do\r
269           get :new\r
270           response.body.should redirect_to '/users/sign_in'\r
271         end\r
272       end\r
273       context 'js形式' do\r
274         it 'ステータスコード401 Unauthorizedを返す' do\r
275           get :new, :format => :js\r
276           response.status.should eq 401\r
277         end\r
278         it '応答メッセージにUnauthorizedを返す' do\r
279           get :new, :format => :js\r
280           response.message.should match(/Unauthorized/)\r
281         end\r
282       end\r
283       context 'json形式' do
284         it 'ステータスコード401 Unauthorizedを返す' do
285           get :new, :format => :json
286           response.status.should eq 401
287         end
288         it '応答メッセージにUnauthorizedを返す' do
289           get :new, :format => :json
290           response.message.should match(/Unauthorized/)
291         end
292       end
293     end\r
294   end\r
295 \r
296   describe '新規作成に於いて' do\r
297     before do\r
298       sign_in @user\r
299       @attr = FactoryGirl.attributes_for(:artist, :author_id => @author.id, :name => 'ken')\r
300     end\r
301     context 'つつがなく終わるとき' do\r
302       it 'モデルに保存依頼する' do\r
303         Artist.any_instance.should_receive(:save).exactly(1)\r
304         post :create, :artist => @attr\r
305       end\r
306       it "@artistに作成された絵師を保持していて、それがDBにある" do\r
307         post :create, :artist => @attr\r
308         assigns(:artist).should be_a(Artist)\r
309         assigns(:artist).should be_persisted\r
310       end\r
311       context 'html形式' do\r
312         it 'ステータスコード302 Foundを返す' do\r
313           Artist.any_instance.stub(:save).and_return(true)\r
314           post :create, :artist => @attr\r
315           response.status.should eq 302\r
316         end\r
317         it '作成された絵師の表示ページへ遷移する' do\r
318 #          Artist.any_instance.stub(:save).and_return(true)\r
319           post :create, :artist => @attr\r
320           response.should redirect_to(Artist.last)\r
321         end\r
322       end\r
323       context 'json形式' do\r
324         it 'ステータスコード200 OKを返す' do\r
325 #          Artist.any_instance.stub(:save).and_return(true)\r
326           post :create, :artist => @attr, :format => :json\r
327           response.should be_success \r
328         end\r
329         it '作成された絵師をjsonデータで返す' do\r
330           post :create, :artist => @attr, :format => :json\r
331           lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)\r
332         end\r
333         it 'データがアレになっている' do\r
334           post :create, :artist => @attr, :format => :json\r
335           json = JSON.parse response.body\r
336           json["name"].should match(/ken/)\r
337         end\r
338       end\r
339     end\r
340     context '作家権限がないとき' do\r
341       before do\r
342         sign_out @user\r
343       end\r
344       context 'html形式' do\r
345         it 'ステータスコード302 Foundを返す' do\r
346           post :create, :artist => @attr\r
347           response.status.should eq 302\r
348         end\r
349         it 'サインインページへ遷移する' do\r
350           post :create, :artist => @attr\r
351           response.body.should redirect_to '/users/sign_in'\r
352         end\r
353       end\r
354       context 'json形式' do\r
355         it 'ステータスコード401 Unauthorizedを返す' do\r
356           post :create, :artist => @attr, :format => :json\r
357           response.status.should eq 401\r
358         end\r
359         it '応答メッセージにUnauthorizedを返す' do\r
360           post :create, :artist => @attr, :format => :json\r
361           response.message.should match(/Unauthorized/)\r
362         end\r
363       end\r
364     end\r
365     context '検証、保存に失敗した' do\r
366       before do\r
367         Artist.any_instance.stub(:save).and_return(false)\r
368       end\r
369       it "未保存の絵師を保持している" do\r
370         post :create, :artist => @attr\r
371         assigns(:artist).should be_a_new(Artist)\r
372       end\r
373       context 'html形式' do\r
374         it 'ステータスコード200 OKを返す' do\r
375           post :create, :artist => @attr\r
376           response.status.should eq 200\r
377         end\r
378         it '新規ページを描画する' do\r
379           post :create, :artist => @attr\r
380           response.should render_template("new")\r
381         end\r
382       end\r
383       context 'json形式' do\r
384         it 'ステータスコード422 unprocessable_entity を返す' do\r
385           post :create, :artist => @attr, :format => :json\r
386           response.status.should eq 422\r
387         end\r
388         it '応答メッセージUnprocessable Entityを返す' do\r
389           post :create, :artist => @attr, :format => :json\r
390           response.message.should match(/Unprocessable/)\r
391         end\r
392       end\r
393     end\r
394   end\r
395 \r
396   describe '編集フォーム表示に於いて' do\r
397     before do\r
398       sign_in @user\r
399       Artist.stub(:edit).and_return(@artist)\r
400     end\r
401     context 'つつがなく終わるとき' do\r
402       it 'ステータスコード200 OKを返す' do\r
403         get :edit, :id => @artist.id\r
404         response.should be_success \r
405       end\r
406       it '絵師モデルに編集取得を問い合わせている' do\r
407         Artist.should_receive(:edit).exactly(1)\r
408         get :edit, :id => @artist.id\r
409       end\r
410       it '@artistにデータを用意している' do\r
411         get :edit, :id => @artist.id\r
412         assigns(:artist).should eq @artist\r
413       end\r
414       context 'html形式' do\r
415         it 'editテンプレートを描画する' do\r
416           get :edit, :id => @artist.id\r
417           response.should render_template("edit")\r
418         end\r
419       end\r
420       context 'js形式' do\r
421         it 'edit.jsテンプレートを描画する' do\r
422           get :edit, :id => @artist.id, :format => :js\r
423           response.should render_template("edit")\r
424         end\r
425       end\r
426     end\r
427     context '作家権限がないとき' do\r
428       before do\r
429         sign_out @user\r
430       end\r
431       context 'html形式' do\r
432         it 'ステータスコード302 Foundを返す' do\r
433           get :edit, :id => @artist.id\r
434           response.status.should eq 302\r
435         end\r
436         it 'サインインページへ遷移する' do\r
437           get :edit, :id => @artist.id\r
438           response.body.should redirect_to '/users/sign_in'\r
439         end\r
440       end\r
441       context 'js形式' do\r
442         it 'ステータスコード401 Unauthorizedを返す' do\r
443           get :edit, :id => @artist.id, :format => :js\r
444           response.status.should eq 401\r
445         end\r
446         it '応答メッセージにUnauthorizedを返す' do\r
447           get :edit, :id => @artist.id, :format => :js\r
448           response.message.should match(/Unauthorized/)\r
449         end\r
450       end\r
451     end\r
452   end\r
453 \r
454   describe '更新に於いて' do\r
455     before do\r
456       @attr = FactoryGirl.attributes_for(:artist, :author_id => @author.id, :name => 'ryu')\r
457       sign_in @user\r
458     end\r
459     context '事前チェックしておく' do\r
460       it '絵師モデルに編集取得を問い合わせている' do\r
461         Artist.stub(:edit).with(any_args()).and_return @artist\r
462         Artist.should_receive(:edit).exactly(1)\r
463         put :update, :id => @artist.id, :artist => @attr\r
464       end\r
465       it 'モデルに更新を依頼する' do\r
466         Artist.any_instance.stub(:save).with(any_args).and_return true\r
467         Artist.any_instance.should_receive(:save).exactly(1)\r
468         put :update, :id => @artist.id, :artist => @attr\r
469       end\r
470       it '@artistにアレを取得している' do\r
471         put :update, :id => @artist.id, :artist => @attr\r
472         assigns(:artist).should eq @artist\r
473       end\r
474     end\r
475     context 'つつがなく終わるとき' do\r
476       it '更新される' do\r
477         put :update, :id => @artist.id, :artist => @attr\r
478         Artist.find(@artist.id).name.should eq 'ryu'\r
479       end\r
480       context 'html形式' do\r
481         it 'ステータスコード302 Foundを返す' do\r
482           Artist.any_instance.stub(:save).with(any_args()).and_return(true)\r
483           put :update, :id => @artist.id, :artist => @attr\r
484           response.status.should eq 302\r
485         end\r
486         it '更新された絵師の表示ページへ遷移する' do\r
487           put :update, :id => @artist.id, :artist => @attr\r
488           response.should redirect_to(@artist)\r
489         end\r
490       end\r
491       context 'json形式' do\r
492         it 'ステータスコード200 OKを返す' do\r
493           Artist.any_instance.stub(:save).with(any_args()).and_return(true)\r
494           put :update, :id => @artist.id, :artist => @attr, :format => :json\r
495           response.should be_success \r
496         end\r
497         it 'ページ本体は特に返さない' do\r
498           Artist.any_instance.stub(:save).with(any_args()).and_return(true)\r
499           put :update, :id => @artist.id, :artist => @attr, :format => :json\r
500           response.body.should match /./\r
501         end\r
502       end\r
503     end\r
504     context '作家権限がないとき' do\r
505       before do\r
506         sign_out @user\r
507       end\r
508       it 'ステータスコード302 Foundを返す' do\r
509         put :update, :id => @artist.id, :artist => @attr\r
510         response.status.should eq 302\r
511       end\r
512       context 'html形式' do\r
513         it 'サインインページへ遷移する' do\r
514           put :update, :id => @artist.id, :artist => @attr\r
515           response.body.should redirect_to '/users/sign_in'\r
516         end\r
517       end\r
518       context 'json形式' do\r
519         it '応答メッセージにUnauthorizedを返す' do\r
520           put :update, :id => @artist.id, :artist => @attr, :format => :json\r
521           response.message.should match(/Unauthorized/)\r
522         end\r
523       end\r
524     end\r
525     context '検証、保存に失敗したとき' do\r
526       before do\r
527         Artist.any_instance.stub(:save).and_return(false)\r
528       end\r
529       context 'html形式' do\r
530         it 'ステータスコード200 Okを返す' do\r
531           put :update, :id => @artist.id, :artist => @attr\r
532           response.status.should eq 200\r
533         end\r
534         it '編集ページを描画する' do\r
535           put :update, :id => @artist.id, :artist => @attr\r
536           response.should render_template("edit")\r
537         end\r
538       end\r
539       context 'json形式' do\r
540         it 'ステータスコード422 unprocessable_entity を返す' do\r
541           Artist.any_instance.stub(:save).and_return(false)\r
542           put :update, :id => @artist.id, :artist => @attr, :format => :json\r
543           response.status.should eq 422\r
544         end\r
545         it '応答メッセージUnprocessable Entityを返す' do\r
546           put :update, :id => @artist.id, :artist => @attr, :format => :json\r
547           response.message.should match(/Unprocessable/)\r
548         end\r
549       end\r
550     end\r
551   end\r
552 \r
553 end