OSDN Git Service

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