OSDN Git Service

t#29050:fix edit permissin
[pettanr/pettanr.git] / spec / controllers / stories_controller_spec.rb
1 # -*- encoding: utf-8 -*-
2 require 'spec_helper'
3 #ストーリー
4 describe StoriesController do
5   before do
6     Factory :admin
7     @license = Factory :license
8     @user = Factory :user_yas
9     @author = @user.author    #ユーザ作成時に連動して作成される
10     @comic = Factory :comic, :author_id => @user.author.id
11     @panel = Factory :panel, :author_id => @author.id
12   end
13   
14
15   describe '閲覧に於いて' do
16     before do
17       @story = Factory :story, :t => 0, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
18       Comic.stub(:show).and_return(@comic)
19       sign_in @user
20     end
21     context '事前チェックする' do
22     end
23     context 'つつがなく終わるとき' do
24     end
25     context '作家権限がないとき' do
26     end
27   end
28
29   describe '新規作成フォーム表示に於いて' do
30     before do
31       sign_in @user
32     end
33     context 'つつがなく終わるとき' do
34       it 'ステータスコード200 OKを返す' do
35         get :new
36         response.should be_success 
37       end
38       it '@storyに新規データを用意している' do
39         get :new
40         assigns(:story).should be_a_new(Story)
41       end
42       it 'モデルにデフォルト値補充を依頼している' do
43         Story.any_instance.should_receive(:supply_default).exactly(1)
44         get :new
45       end
46       context 'html形式' do
47         it 'newテンプレートを描画する' do
48           get :new
49           response.should render_template("new")
50         end
51       end
52       context 'js形式' do
53         it 'new.jsテンプレートを描画する' do
54           get :new, :format => :js
55           response.should render_template("new")
56         end
57       end
58     end
59     context '作家権限がないとき' do
60       before do
61         sign_out @user
62       end
63       context 'html形式' do
64         it 'ステータスコード302 Foundを返す' do
65           get :new
66           response.status.should eq 302
67         end
68         it 'サインインページへ遷移する' do
69           get :new
70           response.body.should redirect_to '/users/sign_in'
71         end
72       end
73       context 'js形式' do
74         it 'ステータスコード401 Unauthorizedを返す' do
75           get :new, :format => :js
76           response.status.should eq 401
77         end
78         it '応答メッセージにUnauthorizedを返す' do
79           get :new, :format => :js
80           response.message.should match(/Unauthorized/)
81         end
82       end
83     end
84   end
85   
86   describe '新規作成に於いて' do
87     before do
88       @attr = Factory.attributes_for(:story, :t => nil, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id)
89       sign_in @user
90     end
91     context 'つつがなく終わるとき' do
92       it 'デフォルト値補充を依頼する' do
93         Story.any_instance.should_receive(:supply_default).exactly(1)
94         post :create, :story => @attr
95       end
96       it 'POSTデータから、カラム値を復元している' do
97         Story.any_instance.stub(:store).and_return(true)
98         Story.any_instance.should_receive(:attributes=).exactly(1)
99         post :create, :story => @attr
100       end
101       it '上書き補充を依頼する' do
102         Story.any_instance.should_receive(:overwrite).exactly(1)
103         post :create, :story => @attr
104       end
105       it 'モデルに保存依頼する' do
106         Story.any_instance.should_receive(:store).exactly(1)
107         post :create, :story => @attr
108       end
109       it "@storyに作成されたコマを保持していて、それがDBにある" do
110         post :create, :story => @attr
111         assigns(:story).should be_a(Story)
112         assigns(:story).should be_persisted
113       end
114       context 'html形式' do
115         it 'ステータスコード302 Foundを返す' do
116           Story.any_instance.stub(:store).and_return(true)
117           post :create, :story => @attr
118           response.status.should eq 302
119         end
120         it 'コミックのストーリー表示へ遷移する' do
121 #          Story.any_instance.stub(:store).and_return(true)
122           post :create, :story => @attr
123           response.should redirect_to(:action => :show, :id => @attr[:comic_id])
124         end
125       end
126       context 'json形式' do
127         it 'ステータスコード200 OKを返す' do
128 #          Story.any_instance.stub(:store).and_return(true)
129           post :create, :story => @attr, :format => :json
130           response.should be_success 
131         end
132         it '作成されたコマをjsonデータで返す' do
133           post :create, :story => @attr, :format => :json
134           lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
135         end
136         it 'データがアレになっている' do
137           post :create, :story => @attr, :format => :json
138           json = JSON.parse response.body
139           json["t"].should eq 0
140         end
141       end
142     end
143     context '作家権限がないとき' do
144       before do
145         sign_out @user
146       end
147       context 'html形式' do
148         it 'ステータスコード302 Foundを返す' do
149           post :create, :story => @attr
150           response.status.should eq 302
151         end
152         it 'サインインページへ遷移する' do
153           post :create, :story => @attr
154           response.body.should redirect_to '/users/sign_in'
155         end
156       end
157       context 'json形式' do
158         it 'ステータスコード401 Unauthorizedを返す' do
159           post :create, :story => @attr, :format => :json
160           response.status.should eq 401
161         end
162         it '応答メッセージにUnauthorizedを返す' do
163           post :create, :story => @attr, :format => :json
164           response.message.should match(/Unauthorized/)
165         end
166       end
167     end
168     context '検証、保存に失敗した' do
169       before do
170         Story.any_instance.stub(:store).and_return(false)
171       end
172       it "未保存のコマを保持している" do
173         post :create, :story => @attr
174         assigns(:story).should be_a_new(Story)
175       end
176       context 'html形式' do
177         it 'ステータスコード200 OKを返す' do
178           post :create, :story => @attr
179           response.status.should eq 200
180         end
181         it '新規ページを描画する' do
182           post :create, :story => @attr
183           response.should render_template("new")
184         end
185       end
186       context 'json形式' do
187         it 'ステータスコード422 unprocessable_entity を返す' do
188           post :create, :story => @attr, :format => :json
189           response.status.should eq 422
190         end
191         it '応答メッセージUnprocessable Entityを返す' do
192           post :create, :story => @attr, :format => :json
193           response.message.should match(/Unprocessable/)
194         end
195       end
196     end
197   end
198
199   describe '編集フォーム表示に於いて' do
200     before do
201       @story = Factory :story, :author_id => @author.id
202       sign_in @user
203       Story.stub(:show).and_return(@story)
204     end
205     context 'つつがなく終わるとき' do
206       it 'ステータスコード200 OKを返す' do
207         get :edit, :id => @story.id
208         response.should be_success 
209       end
210       it 'コマモデルに単体取得を問い合わせている' do
211         Story.should_receive(:show).exactly(1)
212         get :edit, :id => @story.id
213       end
214       it '@storyにデータを用意している' do
215         get :edit, :id => @story.id
216         assigns(:story).should eq @story
217       end
218       context 'html形式' do
219         it 'editテンプレートを描画する' do
220           get :edit, :id => @story.id
221           response.should render_template("edit")
222         end
223       end
224       context 'js形式' do
225         it 'edit.jsテンプレートを描画する' do
226           get :edit, :id => @story.id, :format => :js
227           response.should render_template("edit")
228         end
229       end
230     end
231     context '作家権限がないとき' do
232       before do
233         sign_out @user
234       end
235       context 'html形式' do
236         it 'ステータスコード302 Foundを返す' do
237           get :edit, :id => @story.id
238           response.status.should eq 302
239         end
240         it 'サインインページへ遷移する' do
241           get :edit, :id => @story.id
242           response.body.should redirect_to '/users/sign_in'
243         end
244       end
245       context 'js形式' do
246         it 'ステータスコード401 Unauthorizedを返す' do
247           get :edit, :id => @story.id, :format => :js
248           response.status.should eq 401
249         end
250         it '応答メッセージにUnauthorizedを返す' do
251           get :edit, :id => @story.id, :format => :js
252           response.message.should match(/Unauthorized/)
253         end
254       end
255     end
256   end
257
258   describe '更新に於いて' do
259     before do
260       @story = Factory :story, :author_id => @user.author.id
261       @attr = Factory.attributes_for(:story, :author_id => @author.id)
262       sign_in @user
263     end
264     context 'つつがなく終わるとき' do
265       it 'モデルに編集取得依頼する' do
266         Story.stub(:edit).with(any_args).and_return(@story)
267         Story.should_receive(:edit).exactly(1)
268         put :update, :id => @story.id, :story => @attr
269       end
270       it 'POSTデータから、カラム値を復元している' do
271         Story.any_instance.stub(:store).and_return(true)
272         Story.any_instance.should_receive(:attributes=).exactly(1)
273         put :update, :id => @story.id, :story => @attr
274       end
275       it '上書き補充を依頼する' do
276         Story.any_instance.should_receive(:overwrite).exactly(1)
277         put :update, :id => @story.id, :story => @attr
278       end
279       it 'モデルに保存依頼する' do
280         Story.any_instance.should_receive(:store).exactly(1)
281         put :update, :id => @story.id, :story => @attr
282       end
283       it "@storyに作成されたストーリーを保持していて、それがDBにある" do
284         put :update, :id => @story.id, :story => @attr
285         assigns(:story).should be_a(Story)
286         assigns(:story).should be_persisted
287       end
288       context 'html形式' do
289         it 'ステータスコード302 Foundを返す' do
290           Story.any_instance.stub(:store).and_return(true)
291           put :update, :id => @story.id, :story => @attr
292           response.status.should eq 302
293         end
294         it 'コミックのストーリー表示へ遷移する' do
295 #          Story.any_instance.stub(:store).and_return(true)
296           put :update, :id => @story.id, :story => @attr
297           response.should redirect_to(:action => :show, :id => @attr[:comic_id])
298         end
299       end
300       context 'json形式' do
301         it 'ステータスコード200 OKを返す' do
302 #          Story.any_instance.stub(:store).and_return(true)
303           put :update, :id => @story.id, :story => @attr, :format => :json
304           response.should be_success 
305         end
306       end
307     end
308     context '作家権限がないとき' do
309       before do
310         sign_out @user
311       end
312       context 'html形式' do
313         it 'ステータスコード302 Foundを返す' do
314           put :update, :id => @story.id, :story => @attr
315           response.status.should eq 302
316         end
317         it 'サインインページへ遷移する' do
318           put :update, :id => @story.id, :story => @attr
319           response.body.should redirect_to '/users/sign_in'
320         end
321       end
322       context 'json形式' do
323         it 'ステータスコード401 Unauthorizedを返す' do
324           put :update, :id => @story.id, :story => @attr, :format => :json
325           response.status.should eq 401
326         end
327         it '応答メッセージにUnauthorizedを返す' do
328           put :update, :id => @story.id, :story => @attr, :format => :json
329           response.message.should match(/Unauthorized/)
330         end
331       end
332     end
333     context '検証、保存に失敗した' do
334       before do
335         Story.any_instance.stub(:store).and_return(false)
336       end
337       it "指定のコマを保持している" do
338         put :update, :id => @story.id, :story => @attr
339         assigns(:story).should eq @story
340       end
341       context 'html形式' do
342         it 'ステータスコード200 OKを返す' do
343           put :update, :id => @story.id, :story => @attr
344           response.status.should eq 200
345         end
346         it '編集ページを描画する' do
347           put :update, :id => @story.id, :story => @attr
348           response.should render_template("edit")
349         end
350       end
351       context 'json形式' do
352         it 'ステータスコード422 unprocessable_entity を返す' do
353           put :update, :id => @story.id, :story => @attr, :format => :json
354           response.status.should eq 422
355         end
356         it '応答メッセージUnprocessable Entityを返す' do
357           put :update, :id => @story.id, :story => @attr, :format => :json
358           response.message.should match(/Unprocessable/)
359         end
360       end
361     end
362   end
363
364   describe '削除に於いて' do\r
365     before do\r
366       @story = Factory :story, :author_id => @author.id
367       sign_in @user
368       Story.stub(:edit).and_return(@story)\r
369     end\r
370     context 'つつがなく終わるとき' do\r
371       it 'ストーリーモデルに編集取得を問い合わせている' do\r
372         Story.should_receive(:edit).exactly(1)\r
373         delete :destroy, :id => @story.id\r
374       end\r
375       it '@storyにアレを取得している' do\r
376         delete :destroy, :id => @story.id\r
377         assigns(:story).id.should eq(@story.id)\r
378       end\r
379       it 'そのストーリーを一つのトランザクションで削除する' do\r
380         lambda {\r
381           delete :destroy, :id => @story.id\r
382         }.should change(Story, :count)\r
383       end\r
384       context 'html形式' do\r
385         it 'ステータスコード302 Foundを返す' do\r
386           delete :destroy, :id => @story.id\r
387           response.status.should eq 302\r
388         end\r
389         it 'ストーリー一覧ページへ遷移する' do\r
390           delete :destroy, :id => @story.id\r
391           response.should redirect_to(story_path(@story.comic_id))\r
392         end\r
393       end\r
394       context 'json形式' do\r
395         it 'ステータスコード200 OKを返す' do\r
396           delete :destroy, :id => @story.id, :format => :json\r
397           response.should be_success\r
398         end\r
399       end\r
400     end\r
401     context '作家権限がないとき' do\r
402       before do\r
403         sign_out @user\r
404       end\r
405       context 'html形式' do\r
406         it 'ステータスコード302 Foundを返す' do\r
407           delete :destroy, :id => @story.id\r
408           response.status.should eq 302\r
409         end\r
410         it 'サインインページへ遷移する' do\r
411           delete :destroy, :id => @story.id\r
412           response.body.should redirect_to '/users/sign_in'\r
413         end\r
414       end\r
415       context 'json形式' do\r
416         it 'ステータスコード401 Unauthorizedを返す' do\r
417           delete :destroy, :id => @story.id, :format => :json\r
418           response.status.should eq 401\r
419         end\r
420         it '応答メッセージにUnauthorizedを返す' do\r
421           delete :destroy, :id => @story.id, :format => :json\r
422           response.message.should match(/Unauthorized/)\r
423         end\r
424       end\r
425     end\r
426 =begin\r
427     context '対象ストーリーがないとき' do\r
428     end\r
429     context '他人のストーリーだったとき' do\r
430     end\r
431 =end\r
432   end\r
433   \r
434 end