1 # -*- encoding: utf-8 -*-
5 describe AuthorsController 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
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(/name/)
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
\r
229 Author.stub(:edit).and_return(@author)
\r
231 context 'つつがなく終わるとき' do
\r
232 it 'ステータスコード200 OKを返す' do
\r
233 get :edit, :id => @author.id
\r
234 response.should be_success
\r
236 it '作家モデルに編集取得を問い合わせている' do
\r
237 Author.should_receive(:edit).exactly(1)
\r
238 get :edit, :id => @author.id
\r
240 #@authorだとログイン中のアカウントと干渉してしまう。
241 it '@auにデータを用意している' do
\r
242 get :edit, :id => @author.id
\r
243 assigns(:au).should eq @author
\r
245 context 'html形式' do
\r
246 it 'editテンプレートを描画する' do
\r
247 get :edit, :id => @author.id
\r
248 response.should render_template("edit")
\r
252 it 'edit.jsテンプレートを描画する' do
\r
253 get :edit, :id => @author.id, :format => :js
\r
254 response.should render_template("edit")
\r
258 context '作家権限がないとき' do
\r
262 context 'html形式' do
\r
263 it 'ステータスコード302 Foundを返す' do
\r
264 get :edit, :id => @author.id
\r
265 response.status.should eq 302
\r
267 it 'サインインページへ遷移する' do
\r
268 get :edit, :id => @author.id
\r
269 response.body.should redirect_to '/users/sign_in'
\r
273 it 'ステータスコード401 Unauthorizedを返す' do
\r
274 get :edit, :id => @author.id, :format => :js
\r
275 response.status.should eq 401
\r
277 it '応答メッセージにUnauthorizedを返す' do
\r
278 get :edit, :id => @author.id, :format => :js
\r
279 response.message.should match(/Unauthorized/)
\r
285 describe '更新に於いて' do
\r
287 @attr = @author.attributes
288 @attr['name'] = 'ryu'
\r
291 context '事前チェックしておく' do
\r
292 it '作家モデルに編集取得を問い合わせている' do
\r
293 Author.stub(:edit).with(any_args()).and_return @author
\r
294 Author.should_receive(:edit).exactly(1)
\r
295 put :update, :id => @author.id, :author => @attr
\r
297 it 'モデルにpostデータ転送を依頼する' do
\r
298 Author.any_instance.stub(:attributes=).with(any_args)
\r
299 Author.any_instance.should_receive(:attributes=).exactly(1)
\r
300 put :update, :id => @author.id, :author => @attr
\r
302 it 'モデルに上書き補充を依頼する' do
\r
303 Author.any_instance.stub(:overwrite).with(any_args)
\r
304 Author.any_instance.should_receive(:overwrite).exactly(1)
\r
305 put :update, :id => @author.id, :author => @attr
\r
307 it 'モデルに更新を依頼する' do
\r
308 Author.any_instance.stub(:save).with(any_args).and_return true
\r
309 Author.any_instance.should_receive(:save).exactly(1)
\r
310 put :update, :id => @author.id, :author => @attr
\r
312 it '@auにアレを取得している' do
\r
313 put :update, :id => @author.id, :author => @attr
\r
314 assigns(:au).should eq @author
\r
317 context 'つつがなく終わるとき' do
\r
319 put :update, :id => @author.id, :author => @attr
\r
320 Author.find(@author.id).name.should eq 'ryu'
\r
322 context 'html形式' do
\r
323 it 'ステータスコード302 Foundを返す' do
\r
324 Author.any_instance.stub(:save).with(any_args()).and_return(true)
\r
325 put :update, :id => @author.id, :author => @attr
\r
326 response.status.should eq 302
\r
328 it '更新された作家の表示ページへ遷移する' do
\r
329 put :update, :id => @author.id, :author => @attr
\r
330 response.should redirect_to(@author)
\r
333 context 'json形式' do
\r
334 it 'ステータスコード200 OKを返す' do
\r
335 Author.any_instance.stub(:save).with(any_args()).and_return(true)
\r
336 put :update, :id => @author.id, :author => @attr, :format => :json
\r
337 response.should be_success
\r
339 it 'ページ本体は特に返さない' do
\r
340 Author.any_instance.stub(:save).with(any_args()).and_return(true)
\r
341 put :update, :id => @author.id, :author => @attr, :format => :json
\r
342 response.body.should match /./
\r
346 context '作家権限がないとき' do
\r
350 it 'ステータスコード302 Foundを返す' do
\r
351 put :update, :id => @author.id, :author => @attr
\r
352 response.status.should eq 302
\r
354 context 'html形式' do
\r
355 it 'サインインページへ遷移する' do
\r
356 put :update, :id => @author.id, :author => @attr
\r
357 response.body.should redirect_to '/users/sign_in'
\r
360 context 'json形式' do
\r
361 it '応答メッセージにUnauthorizedを返す' do
\r
362 put :update, :id => @author.id, :author => @attr, :format => :json
\r
363 response.message.should match(/Unauthorized/)
\r
367 context '検証、保存に失敗したとき' do
\r
369 Author.any_instance.stub(:save).and_return(false)
\r
371 context 'html形式' do
\r
372 it 'ステータスコード200 Okを返す' do
\r
373 put :update, :id => @author.id, :author => @attr
\r
374 response.status.should eq 200
\r
377 put :update, :id => @author.id, :author => @attr
\r
378 response.should render_template("edit")
\r
381 context 'json形式' do
\r
382 it 'ステータスコード422 unprocessable_entity を返す' do
\r
383 Author.any_instance.stub(:save).and_return(false)
\r
384 put :update, :id => @author.id, :author => @attr, :format => :json
\r
385 response.status.should eq 422
\r
387 it '応答メッセージUnprocessable Entityを返す' do
\r
388 put :update, :id => @author.id, :author => @attr, :format => :json
\r
389 response.message.should match(/Unprocessable/)
\r