1 # -*- encoding: utf-8 -*-
5 describe ArtistsController 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
16 describe '一覧表示に於いて' do
18 Artist.stub(:list).and_return([@artist, @artist, @artist])
22 it '与えられたpageがセットされている' do
23 get :index, :page => 5
24 assigns(:page).should eq 5
26 it '省略されると@pageに1値が入る' do
28 assigns(:page).should eq 1
30 it '与えられたpage_sizeがセットされている' do
31 get :index, :page_size => 15
32 assigns(:page_size).should eq 15
34 it '省略されると@page_sizeにデフォルト値が入る' do
36 assigns(:page_size).should eq Artist.default_page_size
38 it '最大を超えると@page_sizeにデフォルト最大値が入る' do
39 get :index, :page_size => 1500
40 assigns(:page_size).should eq Artist.max_page_size
42 it '不正な値が入ると@page_sizeにデフォルト最大値が入る' do
43 get :index, :page_size => 0
44 assigns(:page_size).should eq Artist.default_page_size
47 context 'つつがなく終わるとき' do
48 it 'ステータスコード200 OKを返す' do
50 response.should be_success
52 it '絵師モデルに一覧を問い合わせている' do
53 Artist.should_receive(:list).exactly(1)
56 it '@artistsにリストを取得している' do
58 assigns(:artists).should have_at_least(3).items
61 it 'indexテンプレートを描画する' do
63 response.should render_template("index")
68 get :index, :format => :json
69 lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
71 it '絵師モデルにjson一覧出力オプションを問い合わせている' do
72 Artist.should_receive(:list_json_opt).exactly(1)
73 get :index, :format => :json
75 it 'データがリスト構造になっている' do
76 get :index, :format => :json
77 json = JSON.parse response.body
78 json.should have_at_least(3).items
80 it 'リストの先頭くらいは絵師っぽいものであって欲しい' do
81 get :index, :format => :json
82 json = JSON.parse response.body
83 json.first.has_key?("name").should be_true
87 context '作家権限がないとき' do
92 it 'ステータスコード302 Foundを返す' do
94 response.status.should eq 302
98 response.should redirect_to '/users/sign_in'
102 it 'ステータスコード401 Unauthorizedを返す' do
103 get :index, :format => :json
104 response.status.should eq 401
106 it '応答メッセージにUnauthorizedを返す' do
107 get :index, :format => :json
108 response.message.should match(/Unauthorized/)
116 Artist.stub(:show).and_return(@artist)
119 context 'つつがなく終わるとき' do
120 it 'ステータスコード200 OKを返す' do
121 get :show, :id => @artist.id
122 response.should be_success
124 it '絵師モデルに単体取得を問い合わせている' do
125 Artist.should_receive(:show).exactly(1)
128 it '@arにアレを取得している' do
129 get :show, :id => @artist.id
130 assigns(:ar).should eq(@artist)
133 it 'showテンプレートを描画する' do
134 get :show, :id => @artist.id
135 response.should render_template("show")
140 get :show, :id => @artist.id, :format => :json
141 lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
143 it '絵師モデルにjson単体出力オプションを問い合わせている' do
144 Artist.should_receive(:show_json_opt).exactly(1)
145 get :show, :id => @artist.id, :format => :json
148 get :show, :id => @artist.id, :format => :json
149 json = JSON.parse response.body
150 json["name"].should match(/yas/)
154 context '作家権限がないとき' do
159 it 'ステータスコード302 Foundを返す' do
160 get :show, :id => @artist.id
161 response.status.should eq 302
163 it 'サインインページへ遷移する' do
164 get :show, :id => @artist.id
165 response.body.should redirect_to '/users/sign_in'
169 it 'ステータスコード401 Unauthorizedを返す' do
170 get :show, :id => @artist.id, :format => :json
171 response.status.should eq 401
173 it '応答メッセージにUnauthorizedを返す' do
174 get :show, :id => @artist.id, :format => :json
175 response.message.should match(/Unauthorized/)
180 context '対象作家がないとき' do
182 it '例外404 not_foundを返す' do
185 }.should raise_error(ActiveRecord::RecordNotFound)
189 it '例外404 not_foundを返す' do
191 get :show, :id => 0, :format => :json
192 }.should raise_error(ActiveRecord::RecordNotFound)
199 describe '絵師数取得に於いて' do
201 Artist.should_receive(:visible_count).and_return(3)
204 context 'つつがなく終わるとき' do
205 it 'ステータスコード200 OKを返す' do
206 get :count, :format => :json
207 response.should be_success
211 get :count, :format => :json
212 lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
214 it 'データがHash構造になっていて絵師数が3である' do
215 get :count, :format => :json
216 json = JSON.parse response.body
217 json["count"].should == 3
223 describe '新規作成フォーム表示に於いて' do
227 context 'つつがなく終わるとき' do
228 it 'ステータスコード200 OKを返す' do
230 response.should be_success
232 it '@artistに新規データを用意している' do
234 assigns(:ar).should be_a_new(Artist)
236 it '絵師モデルにデフォルト値補充を依頼している' do
237 Artist.any_instance.should_receive(:supply_default).exactly(1)
241 it 'newテンプレートを描画する' do
243 response.should render_template("new")
247 it 'new.jsテンプレートを描画する' do
248 get :new, :format => :js
249 response.should render_template("new")
254 get :new, :format => :json
255 lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
257 it '絵師モデルにjson単体出力オプションを問い合わせている' do
258 Artist.should_receive(:show_json_opt).exactly(1)
259 get :new, :format => :json
263 context '作家権限がないとき' do
268 it 'ステータスコード302 Foundを返す' do
270 response.status.should eq 302
272 it 'サインインページへ遷移する' do
274 response.body.should redirect_to '/users/sign_in'
278 it 'ステータスコード401 Unauthorizedを返す' do
279 get :new, :format => :js
280 response.status.should eq 401
282 it '応答メッセージにUnauthorizedを返す' do
283 get :new, :format => :js
284 response.message.should match(/Unauthorized/)
288 it 'ステータスコード401 Unauthorizedを返す' do
289 get :new, :format => :json
290 response.status.should eq 401
292 it '応答メッセージにUnauthorizedを返す' do
293 get :new, :format => :json
294 response.message.should match(/Unauthorized/)
300 describe '新規作成に於いて' do
303 @attr = FactoryGirl.attributes_for(:artist, :author_id => @author.id, :name => 'ken')
305 context '事前チェックしておく' do
306 it '絵師モデルにデフォルト値補充を依頼している' do
307 Artist.any_instance.should_receive(:supply_default).exactly(1)
308 post :create, :artist => @attr
310 it '絵師モデルにカラム値復元を依頼している' do
311 Artist.any_instance.should_receive(:attributes=).exactly(1)
312 post :create, :artist => @attr
314 it '絵師モデルに上書き補充を依頼している' do
315 Artist.any_instance.should_receive(:overwrite).exactly(1)
316 post :create, :artist => @attr
319 Artist.any_instance.should_receive(:save).exactly(1)
320 post :create, :artist => @attr
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
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
336 # Artist.any_instance.stub(:save).and_return(true)
337 post :create, :artist => @attr
338 response.should redirect_to(root_path)
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
347 it '作成された絵師をjsonデータで返す' do
348 post :create, :artist => @attr, :format => :json
349 lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
351 it '絵師モデルにjson単体出力オプションを問い合わせている' do
352 Artist.should_receive(:show_json_opt).exactly(1)
353 post :create, :artist => @attr, :format => :json
356 post :create, :artist => @attr, :format => :json
357 json = JSON.parse response.body
358 json["name"].should match(/ken/)
362 context '作家権限がないとき' do
367 it 'ステータスコード302 Foundを返す' do
368 post :create, :artist => @attr
369 response.status.should eq 302
371 it 'サインインページへ遷移する' do
372 post :create, :artist => @attr
373 response.body.should redirect_to '/users/sign_in'
377 it 'ステータスコード401 Unauthorizedを返す' do
378 post :create, :artist => @attr, :format => :json
379 response.status.should eq 401
381 it '応答メッセージにUnauthorizedを返す' do
382 post :create, :artist => @attr, :format => :json
383 response.message.should match(/Unauthorized/)
387 context '検証、保存に失敗した' do
389 Artist.any_instance.stub(:save).and_return(false)
391 it "未保存の絵師を保持している" do
392 post :create, :artist => @attr
393 assigns(:ar).should be_a_new(Artist)
396 it 'ステータスコード200 OKを返す' do
397 post :create, :artist => @attr
398 response.status.should eq 200
401 post :create, :artist => @attr
402 response.should render_template("new")
406 it 'ステータスコード422 unprocessable_entity を返す' do
407 post :create, :artist => @attr, :format => :json
408 response.status.should eq 422
410 it '応答メッセージUnprocessable Entityを返す' do
411 post :create, :artist => @attr, :format => :json
412 response.message.should match(/Unprocessable/)
418 describe '編集フォーム表示に於いて' do
421 Artist.stub(:edit).and_return(@artist)
423 context 'つつがなく終わるとき' do
424 it 'ステータスコード200 OKを返す' do
425 get :edit, :id => @artist.id
426 response.should be_success
428 it '絵師モデルに編集取得を問い合わせている' do
429 Artist.should_receive(:edit).exactly(1)
430 get :edit, :id => @artist.id
432 it '@artistにデータを用意している' do
433 get :edit, :id => @artist.id
434 assigns(:artist).should eq @artist
437 it 'editテンプレートを描画する' do
438 get :edit, :id => @artist.id
439 response.should render_template("edit")
443 it 'edit.jsテンプレートを描画する' do
444 get :edit, :id => @artist.id, :format => :js
445 response.should render_template("edit")
449 context '作家権限がないとき' do
454 it 'ステータスコード302 Foundを返す' do
455 get :edit, :id => @artist.id
456 response.status.should eq 302
458 it 'サインインページへ遷移する' do
459 get :edit, :id => @artist.id
460 response.body.should redirect_to '/users/sign_in'
464 it 'ステータスコード401 Unauthorizedを返す' do
465 get :edit, :id => @artist.id, :format => :js
466 response.status.should eq 401
468 it '応答メッセージにUnauthorizedを返す' do
469 get :edit, :id => @artist.id, :format => :js
470 response.message.should match(/Unauthorized/)
478 @attr = FactoryGirl.attributes_for(:artist, :author_id => @author.id, :name => 'ryu')
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
487 it '絵師モデルにカラム値復元を依頼している' do
488 Artist.any_instance.should_receive(:attributes=).exactly(1)
489 put :update, :id => @artist.id, :artist => @attr
491 it '絵師モデルに上書き補充を依頼している' do
492 Artist.any_instance.should_receive(:overwrite).exactly(1)
493 put :update, :id => @artist.id, :artist => @attr
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
500 it '@arにアレを取得している' do
501 put :update, :id => @artist.id, :artist => @attr
502 assigns(:ar).should eq @artist
505 context 'つつがなく終わるとき' do
507 put :update, :id => @artist.id, :artist => @attr
508 Artist.find(@artist.id).name.should eq 'ryu'
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
517 put :update, :id => @artist.id, :artist => @attr
518 response.should redirect_to('/home/configure')
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
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 /./
534 context '作家権限がないとき' do
538 it 'ステータスコード302 Foundを返す' do
539 put :update, :id => @artist.id, :artist => @attr
540 response.status.should eq 302
543 it 'サインインページへ遷移する' do
544 put :update, :id => @artist.id, :artist => @attr
545 response.body.should redirect_to '/users/sign_in'
549 it '応答メッセージにUnauthorizedを返す' do
550 put :update, :id => @artist.id, :artist => @attr, :format => :json
551 response.message.should match(/Unauthorized/)
555 context '検証、保存に失敗したとき' do
557 Artist.any_instance.stub(:save).and_return(false)
560 it 'ステータスコード200 Okを返す' do
561 put :update, :id => @artist.id, :artist => @attr
562 response.status.should eq 200
565 put :update, :id => @artist.id, :artist => @attr
566 response.should render_template("edit")
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
575 it '応答メッセージUnprocessable Entityを返す' do
576 put :update, :id => @artist.id, :artist => @attr, :format => :json
577 response.message.should match(/Unprocessable/)