1 # -*- encoding: utf-8 -*-
5 describe AuthorsController 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 Author.stub(:list).and_return([@author, @author, @author])
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 Author.default_page_size
38 it '最大を超えると@page_sizeにデフォルト最大値が入る' do
39 get :index, :page_size => 1500
40 assigns(:page_size).should eq Author.max_page_size
42 it '不正な値が入ると@page_sizeにデフォルト最大値が入る' do
43 get :index, :page_size => 0
44 assigns(:page_size).should eq Author.default_page_size
47 context 'つつがなく終わるとき' do
48 it 'ステータスコード200 OKを返す' do
50 response.should be_success
52 it '作家モデルに一覧を問い合わせている' do
53 Author.should_receive(:list).exactly(1)
56 it '@authorsにリストを取得している' do
58 assigns(:authors).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 Author.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
84 json.first.has_key?("user_id").should be_true
88 context '作家権限がないとき' do
93 it 'ステータスコード302 Foundを返す' do
95 response.status.should eq 302
99 response.should redirect_to '/users/sign_in'
103 it 'ステータスコード401 Unauthorizedを返す' do
104 get :index, :format => :json
105 response.status.should eq 401
107 it '応答メッセージにUnauthorizedを返す' do
108 get :index, :format => :json
109 response.message.should match(/Unauthorized/)
117 Author.stub(:show).and_return(@author)
120 context 'つつがなく終わるとき' do
121 it 'ステータスコード200 OKを返す' do
122 get :show, :id => @author.id
123 response.should be_success
125 it '作家モデルに単体取得を問い合わせている' do
126 Author.should_receive(:show).exactly(1)
129 #@authorだとログイン中のアカウントと干渉してしまう。
130 it '@auにアレを取得している' do
131 get :show, :id => @author.id
132 assigns(:au).should eq(@author)
135 it 'showテンプレートを描画する' do
136 get :show, :id => @author.id
137 response.should render_template("show")
142 get :show, :id => @author.id, :format => :json
143 lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
145 it '作家モデルにjson単体出力オプションを問い合わせている' do
146 Author.should_receive(:show_json_opt).exactly(1)
147 get :show, :id => @author.id, :format => :json
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
157 context '作家権限がないとき' do
162 it 'ステータスコード302 Foundを返す' do
163 get :show, :id => @author.id
164 response.status.should eq 302
166 it 'サインインページへ遷移する' do
167 get :show, :id => @author.id
168 response.body.should redirect_to '/users/sign_in'
172 it 'ステータスコード401 Unauthorizedを返す' do
173 get :show, :id => @author.id, :format => :json
174 response.status.should eq 401
176 it '応答メッセージにUnauthorizedを返す' do
177 get :show, :id => @author.id, :format => :json
178 response.message.should match(/Unauthorized/)
183 context '対象作家がないとき' do
185 it '例外404 not_foundを返す' do
188 }.should raise_error(ActiveRecord::RecordNotFound)
192 it '例外404 not_foundを返す' do
194 get :show, :id => 0, :format => :json
195 }.should raise_error(ActiveRecord::RecordNotFound)
202 describe '作家数取得に於いて' do
204 Author.should_receive(:visible_count).and_return(3)
207 context 'つつがなく終わるとき' do
208 it 'ステータスコード200 OKを返す' do
209 get :count, :format => :json
210 response.should be_success
214 get :count, :format => :json
215 lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
217 it 'データがHash構造になっていて作家数が3である' do
218 get :count, :format => :json
219 json = JSON.parse response.body
220 json["count"].should == 3
226 describe '新規作成フォーム表示に於いて' do
228 @new_user = FactoryGirl.create( :user_yas)
231 context 'つつがなく終わるとき' do
232 it 'ステータスコード200 OKを返す' do
234 response.should be_success
236 it '@auに新規データを用意している' do
238 assigns(:au).should be_a_new(Author)
240 it '作家モデルにデフォルト値補充を依頼している' do
241 Author.any_instance.should_receive(:supply_default).exactly(1)
245 it 'newテンプレートを描画する' do
247 response.should render_template("new")
251 it 'new.jsテンプレートを描画する' do
252 get :new, :format => :js
253 response.should render_template("new")
258 get :new, :format => :json
259 lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
261 it '作家モデルにjson単体出力オプションを問い合わせている' do
262 Author.should_receive(:show_json_opt).exactly(1)
263 get :new, :format => :json
267 context '作家権限がないとき' do
272 it 'ステータスコード302 Foundを返す' do
274 response.status.should eq 302
276 it 'サインインページへ遷移する' do
278 response.body.should redirect_to '/users/sign_in'
282 it 'ステータスコード401 Unauthorizedを返す' do
283 get :new, :format => :js
284 response.status.should eq 401
286 it '応答メッセージにUnauthorizedを返す' do
287 get :new, :format => :js
288 response.message.should match(/Unauthorized/)
292 it 'ステータスコード401 Unauthorizedを返す' do
293 get :new, :format => :json
294 response.status.should eq 401
296 it '応答メッセージにUnauthorizedを返す' do
297 get :new, :format => :json
298 response.message.should match(/Unauthorized/)
304 describe '新規作成に於いて' do
306 @new_user = FactoryGirl.create( :user_yas)
308 @attr = FactoryGirl.attributes_for(:author, :user_id => @new_user.id, :name => 'ken')
310 context '事前チェックしておく' do
311 it '作家モデルにデフォルト値補充を依頼している' do
312 Author.any_instance.should_receive(:supply_default).exactly(1)
313 post :create, :author => @attr
315 it '作家モデルにカラム値復元を依頼している' do
316 Author.any_instance.should_receive(:attributes=).exactly(1)
317 post :create, :author => @attr
319 it '作家モデルに上書き補充を依頼している' do
320 Author.any_instance.should_receive(:overwrite).exactly(1)
321 post :create, :author => @attr
324 Author.any_instance.should_receive(:save).exactly(1)
325 post :create, :author => @attr
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
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
341 # Author.any_instance.stub(:save).and_return(true)
342 post :create, :author => @attr
343 response.should redirect_to(root_path)
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
352 it '作成された作家をjsonデータで返す' do
353 post :create, :author => @attr, :format => :json
354 lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
356 it '作家モデルにjson単体出力オプションを問い合わせている' do
357 Author.should_receive(:show_json_opt).exactly(1)
358 post :create, :author => @attr, :format => :json
361 post :create, :author => @attr, :format => :json
362 json = JSON.parse response.body
363 json["name"].should match(/ken/)
367 context '作家権限がないとき' do
372 it 'ステータスコード302 Foundを返す' do
373 post :create, :author => @attr
374 response.status.should eq 302
376 it 'サインインページへ遷移する' do
377 post :create, :author => @attr
378 response.body.should redirect_to '/users/sign_in'
382 it 'ステータスコード401 Unauthorizedを返す' do
383 post :create, :author => @attr, :format => :json
384 response.status.should eq 401
386 it '応答メッセージにUnauthorizedを返す' do
387 post :create, :author => @attr, :format => :json
388 response.message.should match(/Unauthorized/)
392 context '検証、保存に失敗した' do
394 Author.any_instance.stub(:save).and_return(false)
396 it "未保存の作家を保持している" do
397 post :create, :author => @attr
398 assigns(:au).should be_a_new(Author)
401 it 'ステータスコード200 OKを返す' do
402 post :create, :author => @attr
403 response.status.should eq 200
406 post :create, :author => @attr
407 response.should render_template("new")
411 it 'ステータスコード422 unprocessable_entity を返す' do
412 post :create, :author => @attr, :format => :json
413 response.status.should eq 422
415 it '応答メッセージUnprocessable Entityを返す' do
416 post :create, :author => @attr, :format => :json
417 response.message.should match(/Unprocessable/)
423 describe '編集フォーム表示に於いて' do
426 Author.stub(:edit).and_return(@author)
428 context 'つつがなく終わるとき' do
429 it 'ステータスコード200 OKを返す' do
430 get :edit, :id => @author.id
431 response.should be_success
433 it '作家モデルに編集取得を問い合わせている' do
434 Author.should_receive(:edit).exactly(1)
435 get :edit, :id => @author.id
437 #@authorだとログイン中のアカウントと干渉してしまう。
438 it '@auにデータを用意している' do
439 get :edit, :id => @author.id
440 assigns(:au).should eq @author
443 it 'editテンプレートを描画する' do
444 get :edit, :id => @author.id
445 response.should render_template("edit")
449 it 'edit.jsテンプレートを描画する' do
450 get :edit, :id => @author.id, :format => :js
451 response.should render_template("edit")
455 context '作家権限がないとき' do
460 it 'ステータスコード302 Foundを返す' do
461 get :edit, :id => @author.id
462 response.status.should eq 302
464 it 'サインインページへ遷移する' do
465 get :edit, :id => @author.id
466 response.body.should redirect_to '/users/sign_in'
470 it 'ステータスコード401 Unauthorizedを返す' do
471 get :edit, :id => @author.id, :format => :js
472 response.status.should eq 401
474 it '応答メッセージにUnauthorizedを返す' do
475 get :edit, :id => @author.id, :format => :js
476 response.message.should match(/Unauthorized/)
484 @attr = @author.attributes
485 @attr['name'] = 'ryu'
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
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
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
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
509 it '@auにアレを取得している' do
510 put :update, :id => @author.id, :author => @attr
511 assigns(:au).should eq @author
514 context 'つつがなく終わるとき' do
516 put :update, :id => @author.id, :author => @attr
517 Author.find(@author.id).name.should eq 'ryu'
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
526 put :update, :id => @author.id, :author => @attr
527 response.should redirect_to('/home/configure')
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
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 /./
543 context '作家権限がないとき' do
547 it 'ステータスコード302 Foundを返す' do
548 put :update, :id => @author.id, :author => @attr
549 response.status.should eq 302
552 it 'サインインページへ遷移する' do
553 put :update, :id => @author.id, :author => @attr
554 response.body.should redirect_to '/users/sign_in'
558 it '応答メッセージにUnauthorizedを返す' do
559 put :update, :id => @author.id, :author => @attr, :format => :json
560 response.message.should match(/Unauthorized/)
564 context '検証、保存に失敗したとき' do
566 Author.any_instance.stub(:save).and_return(false)
569 it 'ステータスコード200 Okを返す' do
570 put :update, :id => @author.id, :author => @attr
571 response.status.should eq 200
574 put :update, :id => @author.id, :author => @attr
575 response.should render_template("edit")
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
584 it '応答メッセージUnprocessable Entityを返す' do
585 put :update, :id => @author.id, :author => @attr, :format => :json
586 response.message.should match(/Unprocessable/)