OSDN Git Service

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