OSDN Git Service

Merge branch 'v04' of git.sourceforge.jp:/gitroot/pettanr/pettanr into v04
[pettanr/pettanr.git] / spec / controllers / authors_controller_spec.rb
1 # -*- encoding: utf-8 -*-
2 require 'spec_helper'
3 #作家
4
5 describe AuthorsController 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       Author.stub(:list).and_return([@author, @author, @author])
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 Author.default_page_size
37       end
38       it '最大を超えると@page_sizeにデフォルト最大値が入る' do
39         get :index, :page_size => 1500
40         assigns(:page_size).should eq Author.max_page_size
41       end
42       it '不正な値が入ると@page_sizeにデフォルト最大値が入る' do
43         get :index, :page_size => 0
44         assigns(:page_size).should eq Author.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         Author.should_receive(:list).exactly(1)
54         get :index
55       end
56       it '@authorsにリストを取得している' do
57         get :index
58         assigns(:authors).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           Author.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           json.first.has_key?("user_id").should be_true
85         end
86       end
87     end
88     context '作家権限がないとき' do
89       before do
90         sign_out @user
91       end
92       context 'html形式' do
93         it 'ステータスコード302 Foundを返す' do
94           get :index
95           response.status.should eq 302
96         end
97         it 'サインインページへ遷移する' do
98           get :index
99           response.should redirect_to '/users/sign_in'
100         end
101       end
102       context 'json形式' do
103         it 'ステータスコード401 Unauthorizedを返す' do
104           get :index, :format => :json
105           response.status.should eq 401
106         end
107         it '応答メッセージにUnauthorizedを返す' do
108           get :index, :format => :json
109           response.message.should match(/Unauthorized/)
110         end
111       end
112     end
113   end
114   
115   describe '閲覧に於いて' do
116     before do
117       Author.stub(:show).and_return(@author)
118       sign_in @user
119     end
120     context 'つつがなく終わるとき' do
121       it 'ステータスコード200 OKを返す' do
122         get :show, :id => @author.id
123         response.should be_success
124       end
125       it '作家モデルに単体取得を問い合わせている' do
126         Author.should_receive(:show).exactly(1)
127         get :show
128       end
129       #@authorだとログイン中のアカウントと干渉してしまう。
130       it '@auにアレを取得している' do
131         get :show, :id => @author.id
132         assigns(:au).should eq(@author)
133       end
134       context 'html形式' do
135         it 'showテンプレートを描画する' do
136           get :show, :id => @author.id
137           response.should render_template("show")
138         end
139       end
140       context 'json形式' do
141         it 'jsonデータを返す' do
142           get :show, :id => @author.id, :format => :json
143           lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
144         end
145         it '作家モデルにjson単体出力オプションを問い合わせている' do
146           Author.should_receive(:show_json_opt).exactly(1)
147           get :show, :id => @author.id, :format => :json
148         end
149         it 'データがアレになっている' do
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
154         end
155       end
156     end
157     context '作家権限がないとき' do
158       before do
159         sign_out @user
160       end
161       context 'html形式' do
162         it 'ステータスコード302 Foundを返す' do
163           get :show, :id => @author.id
164           response.status.should eq 302
165         end
166         it 'サインインページへ遷移する' do
167           get :show, :id => @author.id
168           response.body.should redirect_to '/users/sign_in'
169         end
170       end
171       context 'json形式' do
172         it 'ステータスコード401 Unauthorizedを返す' do
173           get :show, :id => @author.id, :format => :json
174           response.status.should eq 401
175         end
176         it '応答メッセージにUnauthorizedを返す' do
177           get :show, :id => @author.id, :format => :json
178           response.message.should match(/Unauthorized/)
179         end
180       end
181     end
182 =begin
183     context '対象作家がないとき' do
184       context 'html形式' do
185         it '例外404 not_foundを返す' do
186           lambda{
187             get :show, :id => 0
188           }.should raise_error(ActiveRecord::RecordNotFound)
189         end
190       end
191       context 'json形式' do
192         it '例外404 not_foundを返す' do
193           lambda{ 
194             get :show, :id => 0, :format => :json
195           }.should raise_error(ActiveRecord::RecordNotFound)
196         end
197       end
198     end
199 =end
200   end
201   
202   describe '作家数取得に於いて' do
203     before do
204       Author.should_receive(:visible_count).and_return(3)
205 #      sign_in @user
206     end
207     context 'つつがなく終わるとき' do
208       it 'ステータスコード200 OKを返す' do
209         get :count, :format => :json
210         response.should be_success 
211       end
212       context 'json形式' do
213         it 'jsonデータを返す' do
214           get :count, :format => :json
215           lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
216         end
217         it 'データがHash構造になっていて作家数が3である' do
218           get :count, :format => :json
219           json = JSON.parse response.body
220           json["count"].should == 3
221         end
222       end
223     end
224   end
225   
226   describe '編集フォーム表示に於いて' do\r
227     before do\r
228       sign_in @user\r
229       Author.stub(:edit).and_return(@author)\r
230     end\r
231     context 'つつがなく終わるとき' do\r
232       it 'ステータスコード200 OKを返す' do\r
233         get :edit, :id => @author.id\r
234         response.should be_success \r
235       end\r
236       it '作家モデルに編集取得を問い合わせている' do\r
237         Author.should_receive(:edit).exactly(1)\r
238         get :edit, :id => @author.id\r
239       end\r
240       #@authorだとログイン中のアカウントと干渉してしまう。
241       it '@auにデータを用意している' do\r
242         get :edit, :id => @author.id\r
243         assigns(:au).should eq @author\r
244       end\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
249         end\r
250       end\r
251       context 'js形式' do\r
252         it 'edit.jsテンプレートを描画する' do\r
253           get :edit, :id => @author.id, :format => :js\r
254           response.should render_template("edit")\r
255         end\r
256       end\r
257     end\r
258     context '作家権限がないとき' do\r
259       before do\r
260         sign_out @user\r
261       end\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
266         end\r
267         it 'サインインページへ遷移する' do\r
268           get :edit, :id => @author.id\r
269           response.body.should redirect_to '/users/sign_in'\r
270         end\r
271       end\r
272       context 'js形式' do\r
273         it 'ステータスコード401 Unauthorizedを返す' do\r
274           get :edit, :id => @author.id, :format => :js\r
275           response.status.should eq 401\r
276         end\r
277         it '応答メッセージにUnauthorizedを返す' do\r
278           get :edit, :id => @author.id, :format => :js\r
279           response.message.should match(/Unauthorized/)\r
280         end\r
281       end\r
282     end\r
283   end\r
284 \r
285   describe '更新に於いて' do\r
286     before do\r
287       @attr = @author.attributes
288       @attr['name'] = 'ryu'\r
289       sign_in @user\r
290     end\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
296       end\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
301       end\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
306       end\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
311       end\r
312       it '@auにアレを取得している' do\r
313         put :update, :id => @author.id, :author => @attr\r
314         assigns(:au).should eq @author\r
315       end\r
316     end\r
317     context 'つつがなく終わるとき' do\r
318       it '更新される' do\r
319         put :update, :id => @author.id, :author => @attr\r
320         Author.find(@author.id).name.should eq 'ryu'\r
321       end\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
327         end\r
328         it '更新された作家の表示ページへ遷移する' do\r
329           put :update, :id => @author.id, :author => @attr\r
330           response.should redirect_to(@author)\r
331         end\r
332       end\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
338         end\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
343         end\r
344       end\r
345     end\r
346     context '作家権限がないとき' do\r
347       before do\r
348         sign_out @user\r
349       end\r
350       it 'ステータスコード302 Foundを返す' do\r
351         put :update, :id => @author.id, :author => @attr\r
352         response.status.should eq 302\r
353       end\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
358         end\r
359       end\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
364         end\r
365       end\r
366     end\r
367     context '検証、保存に失敗したとき' do\r
368       before do\r
369         Author.any_instance.stub(:save).and_return(false)\r
370       end\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
375         end\r
376         it '編集ページを描画する' do\r
377           put :update, :id => @author.id, :author => @attr\r
378           response.should render_template("edit")\r
379         end\r
380       end\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
386         end\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
390         end\r
391       end\r
392     end\r
393   end\r
394
395 end