1 # -*- encoding: utf-8 -*-
\r
2 require 'spec_helper'
\r
4 describe PanelsController do
\r
7 @user = Factory :user_yas
\r
8 @author = @user.author #ユーザ作成時に連動して作成される
\r
11 describe '一覧表示に於いて' do
\r
13 @panel = Factory :panel, :author_id => @user.author.id
\r
14 Panel.stub(:list).and_return([@panel, @panel, @panel])
\r
17 context '事前チェックする' do
\r
18 it '与えられたpageがセットされている' do
\r
19 get :index, :page => 5
\r
20 assigns(:page).should eq 5
\r
22 it '省略されると@pageに1値が入る' do
\r
24 assigns(:page).should eq 1
\r
26 it '与えられたpage_sizeがセットされている' do
\r
27 get :index, :page_size => 15
\r
28 assigns(:page_size).should eq 15
\r
30 it '省略されると@page_sizeにデフォルト値が入る' do
\r
32 assigns(:page_size).should eq Panel.default_page_size
\r
34 it '最大を超えると@page_sizeにデフォルト最大値が入る' do
\r
35 get :index, :page_size => 1500
\r
36 assigns(:page_size).should eq Panel.max_page_size
\r
38 it '不正な値が入ると@page_sizeにデフォルト最大値が入る' do
\r
39 get :index, :page_size => 0
\r
40 assigns(:page_size).should eq Panel.default_page_size
\r
43 context 'つつがなく終わるとき' do
\r
44 it 'ステータスコード200 OKを返す' do
\r
46 response.should be_success
\r
48 it 'コマモデルに一覧を問い合わせている' do
\r
49 Panel.should_receive(:list).exactly(1)
\r
52 it '@panelsにリストを取得している' do
\r
54 assigns(:panels).should have_at_least(3).items
\r
57 it 'indexテンプレートを描画する' do
\r
59 response.should render_template("index")
\r
64 get :index, :format => :json
\r
65 lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
\r
67 it 'データがリスト構造になっている' do
\r
68 get :index, :format => :json
\r
69 json = JSON.parse response.body
\r
70 json.should have_at_least(3).items
\r
72 it 'リストの先頭くらいはコマっぽいものであって欲しい' do
\r
73 get :index, :format => :json
\r
74 json = JSON.parse response.body
\r
75 json.first.has_key?("comic_id").should be_true
\r
79 context '作家権限がないとき' do
\r
84 it 'ステータスコード302 Foundを返す' do
\r
86 response.status.should eq 302
\r
88 it 'サインインページへ遷移する' do
\r
90 response.should redirect_to '/users/sign_in'
\r
94 it 'ステータスコード401 Unauthorizedを返す' do
\r
95 get :index, :format => :json
\r
96 response.status.should eq 401
\r
98 it '応答メッセージにUnauthorizedを返す' do
\r
99 get :index, :format => :json
\r
100 response.message.should match(/Unauthorized/)
\r
106 describe '単体表示に於いて' do
\r
108 @panel = Factory :panel, :author_id => @user.author.id
\r
109 Panel.stub(:show).and_return(@panel)
\r
112 context 'つつがなく終わるとき' do
\r
113 it 'ステータスコード200 OKを返す' do
\r
114 get :show, :id => @panel.id
\r
115 response.should be_success
\r
117 it 'コマモデルに単体取得を問い合わせている' do
\r
118 Panel.should_receive(:show).exactly(1)
\r
121 it '@panelにアレを取得している' do
\r
122 get :show, :id => @panel.id
\r
123 assigns(:panel).id.should eq(@panel.id)
\r
125 context 'html形式' do
\r
126 it 'showテンプレートを描画する' do
\r
127 get :show, :id => @panel.id
\r
128 response.should render_template("show")
\r
131 context 'json形式' do
\r
133 get :show, :id => @panel.id, :format => :json
\r
134 lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
\r
136 it 'データがアレになっている' do
\r
137 get :show, :id => @panel.id, :format => :json
\r
138 json = JSON.parse response.body
\r
139 json["width"].should match(/438/)
\r
143 context '作家権限がないとき' do
\r
147 context 'html形式' do
\r
148 it 'ステータスコード302 Foundを返す' do
\r
149 get :show, :id => @panel.id
\r
150 response.status.should eq 302
\r
152 it 'サインインページへ遷移する' do
\r
153 get :show, :id => @panel.id
\r
154 response.body.should redirect_to '/users/sign_in'
\r
157 context 'json形式' do
\r
158 it 'ステータスコード401 Unauthorizedを返す' do
\r
159 get :show, :id => @panel.id, :format => :json
\r
160 response.status.should eq 401
\r
162 it '応答メッセージにUnauthorizedを返す' do
\r
163 get :show, :id => @panel.id, :format => :json
\r
164 response.message.should match(/Unauthorized/)
\r
169 context '対象コマがないとき' do
\r
170 context 'html形式' do
\r
171 it '例外404 not_foundを返す' do
\r
173 get :show, :id => 0
\r
174 }.should raise_error(ActiveRecord::RecordNotFound)
\r
177 context 'json形式' do
\r
178 it '例外404 not_foundを返す' do
\r
180 get :show, :id => 0, :format => :json
\r
181 }.should raise_error(ActiveRecord::RecordNotFound)
\r
185 context '非公開コマを見ようとしたとき' do
\r
186 context 'html形式' do
\r
187 it '例外403 forbiddenを返す' do
\r
188 Panel.any_instance.stub(:visible?).with(any_args()).and_return(false)
\r
189 hidden = Factory :hidden_panel, :author_id => @author.id
\r
191 get :show, :id => hidden
\r
192 }.should raise_error(ActiveRecord::Forbidden)
\r
195 context 'json形式' do
\r
196 it '例外403 forbiddenを返す' do
\r
197 Panel.any_instance.stub(:visible?).with(any_args()).and_return(false)
\r
198 hidden = Factory :hidden_panel, :author_id => @author.id
\r
200 get :show, :id => hidden, :format => :json
\r
201 }.should raise_error(ActiveRecord::Forbidden)
\r
208 describe 'コマ数取得に於いて' do
\r
210 Panel.should_receive(:visible_count).and_return(3)
\r
213 context 'つつがなく終わるとき' do
\r
214 it 'ステータスコード200 OKを返す' do
\r
215 get :count, :format => :json
\r
216 response.should be_success
\r
218 context 'json形式' do
\r
220 get :count, :format => :json
\r
221 lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
\r
223 it 'データがHash構造になっていてコマ数が1である' do
\r
224 get :count, :format => :json
\r
225 json = JSON.parse response.body
\r
226 json["count"].should == 3
\r
232 describe '新規作成フォーム表示に於いて' do
\r
236 context 'つつがなく終わるとき' do
\r
237 it 'ステータスコード200 OKを返す' do
\r
239 response.should be_success
\r
241 it '@panelに新規データを用意している' do
\r
243 assigns(:panel).should be_a_new(Panel)
\r
245 it 'コマモデルにデフォルト値補充を依頼している' do
\r
246 Panel.any_instance.should_receive(:supply_default).exactly(1)
\r
249 context 'html形式' do
\r
250 it 'newテンプレートを描画する' do
\r
252 response.should render_template("new")
\r
256 it 'new.jsテンプレートを描画する' do
\r
257 get :new, :format => :js
\r
258 response.should render_template("new")
\r
262 context '作家権限がないとき' do
\r
266 context 'html形式' do
\r
267 it 'ステータスコード302 Foundを返す' do
\r
269 response.status.should eq 302
\r
271 it 'サインインページへ遷移する' do
\r
273 response.body.should redirect_to '/users/sign_in'
\r
277 it 'ステータスコード401 Unauthorizedを返す' do
\r
278 get :new, :format => :js
\r
279 response.status.should eq 401
\r
281 it '応答メッセージにUnauthorizedを返す' do
\r
282 get :new, :format => :js
\r
283 response.message.should match(/Unauthorized/)
\r
289 describe '新規作成に於いて' do
\r
291 @panel = Factory :normal_panel, :author_id => @user.author.id
\r
294 context '事前チェックする' do
\r
297 Panel.stub(:count).and_return(10)
\r
299 it 'panelがパラメータにあれば、展開する' do
\r
300 post :create, :panel => Factory.attributes_for(:panel, :panel_id => @panel.id)
\r
301 assigns(:prm)['border'].to_i.should eq 1
\r
303 it 'jsonがパラメータにあれば、展開する' do
\r
304 post :create, :json => Factory.attributes_for(:panel, :panel_id => @panel.id, :border => 4).to_s
\r
305 assigns(:prm)['border'].to_i.should eq 4
\r
307 it 'panel・json両パラメータがあれば、panelを優先して展開する' do
\r
309 :panel => Factory.attributes_for(:panel, :panel_id => @panel.id),
\r
310 :json => Factory.attributes_for(:panel, :panel_id => @panel.id, :border => 4).to_s
\r
312 assigns(:prm)['border'].to_i.should eq 1
\r
315 context 'つつがなく終わるとき' do
\r
317 Panel.any_instance.should_receive(:save).exactly(1)
\r
318 post :create, :panel => Factory.attributes_for(:panel, :author => @author.id)
\r
320 it "@panelに作成されたコマを保持していて、それがDBにある" do
\r
321 post :create, :panel => Factory.attributes_for(:panel, :author => @author.id)
\r
322 assigns(:panel).should be_a(Panel)
\r
323 assigns(:panel).should be_persisted
\r
325 context 'html形式' do
\r
326 it 'ステータスコード302 Foundを返す' do
\r
327 Panel.any_instance.stub(:save).and_return(true)
\r
328 post :create, :panel => Factory.attributes_for(:panel, :author => @author.id)
\r
329 response.status.should eq 302
\r
331 it '作成されたコマの表示ページへ遷移する' do
\r
332 # Panel.any_instance.stub(:save).and_return(true)
\r
333 post :create, :panel => Factory.attributes_for(:panel, :author => @author.id)
\r
334 response.should redirect_to(Panel.last)
\r
337 context 'json形式' do
\r
338 it 'ステータスコード200 OKを返す' do
\r
339 # Panel.any_instance.stub(:save).and_return(true)
\r
340 post :create, :panel => Factory.attributes_for(:panel, :author => @author.id), :format => :json
\r
341 response.should be_success
\r
343 it '作成されたコマをjsonデータで返す' do
\r
344 post :create, :panel => Factory.attributes_for(:panel, :author => @author.id), :format => :json
\r
345 lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
\r
347 it 'データがアレになっている' do
\r
348 post :create, :panel => Factory.attributes_for(:panel, :author => @author.id), :format => :json
\r
349 json = JSON.parse response.body
\r
350 json["title"].should match(/normal/)
\r
354 context '作家権限がないとき' do
\r
358 context 'html形式' do
\r
359 it 'ステータスコード302 Foundを返す' do
\r
360 post :create, :panel => Factory.attributes_for(:panel, :author => @author.id)
\r
361 response.status.should eq 302
\r
363 it 'サインインページへ遷移する' do
\r
364 post :create, :panel => Factory.attributes_for(:panel, :author => @author.id)
\r
365 response.body.should redirect_to '/users/sign_in'
\r
368 context 'json形式' do
\r
369 it 'ステータスコード401 Unauthorizedを返す' do
\r
370 post :create, :panel => Factory.attributes_for(:panel, :author => @author.id), :format => :json
\r
371 response.status.should eq 401
\r
373 it '応答メッセージにUnauthorizedを返す' do
\r
374 post :create, :panel => Factory.attributes_for(:panel, :author => @author.id), :format => :json
\r
375 response.message.should match(/Unauthorized/)
\r
379 context '検証、保存に失敗した' do
\r
381 Panel.any_instance.stub(:save).and_return(false)
\r
383 it "未保存のコマを保持している" do
\r
384 post :create, :panel => Factory.attributes_for(:panel, :author => @author.id)
\r
385 assigns(:panel).should be_a_new(Panel)
\r
387 context 'html形式' do
\r
388 it 'ステータスコード200 OKを返す' do
\r
389 post :create, :panel => Factory.attributes_for(:panel, :author => @author.id)
\r
390 response.status.should eq 200
\r
393 post :create, :panel => Factory.attributes_for(:panel, :author => @author.id)
\r
394 response.should render_template("new")
\r
397 context 'json形式' do
\r
398 it 'ステータスコード422 unprocessable_entity を返す' do
\r
399 post :create, :panel => Factory.attributes_for(:panel, :author => @author.id), :format => :json
\r
400 response.status.should eq 422
\r
402 it '応答メッセージUnprocessable Entityを返す' do
\r
403 post :create, :panel => Factory.attributes_for(:panel, :author => @author.id), :format => :json
\r
404 response.message.should match(/Unprocessable/)
\r
410 describe '編集フォーム表示に於いて' do
\r
412 @panel = Factory :panel, :author_id => @author.id
\r
414 Panel.stub(:show).and_return(@panel)
\r
416 context 'つつがなく終わるとき' do
\r
417 it 'ステータスコード200 OKを返す' do
\r
418 get :edit, :id => @panel.id
\r
419 response.should be_success
\r
421 it 'コマモデルに単体取得を問い合わせている' do
\r
422 Panel.should_receive(:show).exactly(1)
\r
423 get :edit, :id => @panel.id
\r
425 it '@panelにデータを用意している' do
\r
426 get :edit, :id => @panel.id
\r
427 assigns(:panel).should eq @panel
\r
429 context 'html形式' do
\r
430 it 'editテンプレートを描画する' do
\r
431 get :edit, :id => @panel.id
\r
432 response.should render_template("edit")
\r
436 it 'edit.jsテンプレートを描画する' do
\r
437 get :edit, :id => @panel.id, :format => :js
\r
438 response.should render_template("edit")
\r
442 context '作家権限がないとき' do
\r
446 context 'html形式' do
\r
447 it 'ステータスコード302 Foundを返す' do
\r
448 get :edit, :id => @panel.id
\r
449 response.status.should eq 302
\r
451 it 'サインインページへ遷移する' do
\r
452 get :edit, :id => @panel.id
\r
453 response.body.should redirect_to '/users/sign_in'
\r
457 it 'ステータスコード401 Unauthorizedを返す' do
\r
458 get :edit, :id => @panel.id, :format => :js
\r
459 response.status.should eq 401
\r
461 it '応答メッセージにUnauthorizedを返す' do
\r
462 get :edit, :id => @panel.id, :format => :js
\r
463 response.message.should match(/Unauthorized/)
\r
469 describe '更新に於いて' do
\r
471 @panel = Factory :panel, :author => @author.id
\r
474 context '事前チェックしておく' do
\r
475 it 'コマモデルに単体取得を問い合わせている' do
\r
476 Panel.stub(:show).with(any_args()).and_return @panel
\r
477 Panel.should_receive(:show).exactly(1)
\r
478 put :update, :id => @panel.id, :panel => Factory.attributes_for(:panel, :author => @author.id)
\r
480 it 'モデルに更新を依頼する' do
\r
481 Panel.any_instance.should_receive(:update_attributes).with(any_args)
\r
482 put :update, :id => @panel.id, :panel => Factory.attributes_for(:panel, :author => @author.id)
\r
484 it '@panelにアレを取得している' do
\r
485 put :update, :id => @panel.id, :panel => Factory.attributes_for(:panel, :author => @author.id)
\r
486 assigns(:panel).id.should eq(@panel.id)
\r
489 context 'つつがなく終わるとき' do
\r
491 put :update, :id => @panel.id, :panel => Factory.attributes_for(:panel, :author => @author.id)
\r
492 Panel.find(@panel.id).visible.should eq 0
\r
494 context 'html形式' do
\r
495 it 'ステータスコード302 Foundを返す' do
\r
496 Panel.any_instance.stub(:update_attributes).with(any_args()).and_return(true)
\r
497 put :update, :id => @panel.id, :panel => Factory.attributes_for(:panel, :author => @author.id)
\r
498 response.status.should eq 302
\r
500 it '更新されたコマの表示ページへ遷移する' do
\r
501 put :update, :id => @panel.id, :panel => Factory.attributes_for(:panel, :author => @author.id)
\r
502 response.should redirect_to(@panel)
\r
505 context 'json形式' do
\r
506 it 'ステータスコード200 OKを返す' do
\r
507 Panel.any_instance.stub(:update_attributes).with(any_args()).and_return(true)
\r
508 put :update, :id => @panel.id, :panel => Factory.attributes_for(:panel, :author => @author.id), :format => :json
\r
509 response.should be_success
\r
511 it 'ページ本体は特に返さない' do
\r
512 Panel.any_instance.stub(:update_attributes).with(any_args()).and_return(true)
\r
513 put :update, :id => @panel.id, :panel => Factory.attributes_for(:panel, :author => @author.id), :format => :json
\r
514 response.body.should match /./
\r
518 context '作家権限がないとき' do
\r
522 it 'ステータスコード302 Foundを返す' do
\r
523 put :update, :id => @panel.id, :panel => Factory.attributes_for(:panel, :author => @author.id)
\r
524 response.status.should eq 302
\r
526 context 'html形式' do
\r
527 it 'サインインページへ遷移する' do
\r
528 put :update, :id => @panel.id, :panel => Factory.attributes_for(:panel, :author => @author.id)
\r
529 response.body.should redirect_to '/users/sign_in'
\r
532 context 'json形式' do
\r
533 it '応答メッセージにUnauthorizedを返す' do
\r
534 put :update, :id => @panel.id, :panel => Factory.attributes_for(:panel, :author => @author.id), :format => :json
\r
535 response.message.should match(/Unauthorized/)
\r
539 context '検証、保存に失敗したとき' do
\r
541 Panel.any_instance.stub(:update_attributes).and_return(false)
\r
543 context 'html形式' do
\r
544 it 'ステータスコード200 Okを返す' do
\r
545 put :update, :id => @panel.id, :panel => Factory.attributes_for(:panel, :author => @author.id)
\r
546 response.status.should eq 200
\r
549 put :update, :id => @panel.id, :panel => Factory.attributes_for(:panel, :author => @author.id)
\r
550 response.should render_template("edit")
\r
553 context 'json形式' do
\r
554 it 'ステータスコード422 unprocessable_entity を返す' do
\r
555 Panel.any_instance.stub(:update_attributes).and_return(false)
\r
556 put :update, :id => @panel.id, :panel => Factory.attributes_for(:panel, :author => @author.id), :format => :json
\r
557 response.status.should eq 422
\r
559 it '応答メッセージUnprocessable Entityを返す' do
\r
560 put :update, :id => @panel.id, :panel => Factory.attributes_for(:panel, :author => @author.id), :format => :json
\r
561 response.message.should match(/Unprocessable/)
\r