OSDN Git Service

t#29050:fix edit permissin
[pettanr/pettanr.git] / spec / controllers / original_pictures_controller_spec.rb
1 # -*- encoding: utf-8 -*-
2 require 'spec_helper'
3
4 describe OriginalPicturesController do
5   before do
6     Factory :admin
7     @user = Factory( :user_yas)
8     @author = @user.author
9     @artist = Factory :artist_yas, :author_id => @author.id
10     @license = Factory :license
11   end
12
13   describe '一覧表示に於いて' do
14     before do
15       @op = Factory :original_picture, :artist_id => @artist.id , :license_id => @license.id
16       sign_in @user
17       OriginalPicture.stub(:list).and_return([@op, @op, @op])
18     end
19     context 'パラメータpageについて' do
20       it '@pageに値が入る' do
21         get :index, :page => 5
22         assigns(:page).should eq 5
23       end
24       it '省略されると@pageに1値が入る' do
25         get :index
26         assigns(:page).should eq 1
27       end
28       it '与えられたpage_sizeがセットされている' do
29         get :index, :page_size => 15
30         assigns(:page_size).should eq 15
31       end
32       it '省略されると@page_sizeにデフォルト値が入る' do
33         get :index
34         assigns(:page_size).should eq OriginalPicture.default_page_size\r
35       end
36       it '最大を超えると@page_sizeにデフォルト最大値が入る' do
37         get :index, :page_size => 1500
38         assigns(:page_size).should eq OriginalPicture.max_page_size
39       end
40       it '不正な値が入ると@page_sizeにデフォルト最大値が入る' do
41         get :index, :page_size => 0
42         assigns(:page_size).should eq OriginalPicture.default_page_size
43       end
44     end
45     context 'つつがなく終わるとき' do
46       it 'ステータスコード200 OKを返す' do
47         get :index
48         response.should be_success 
49       end
50       it '原画モデルに一覧を問い合わせている' do
51         OriginalPicture.should_receive(:list).exactly(1)\r
52         get :index
53       end
54       it '@original_picturesにリストを取得している' do
55         get :index
56         assigns(:original_pictures).should have_at_least(3).items
57       end
58       context 'html形式' do
59         it 'indexテンプレートを描画する' do
60           get :index
61           response.should render_template("index")
62         end
63       end
64       context 'json形式' do
65         it 'jsonデータを返す' do
66           get :index, :format => :json
67           lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
68         end
69         it 'データがリスト構造になっている' do
70           get :index, :format => :json
71           json = JSON.parse response.body
72           json.should have_at_least(3).items
73         end
74         it 'リストの先頭くらいは原画っぽいものであって欲しい' do
75           get :index, :format => :json
76           json = JSON.parse response.body
77           json.first.has_key?("ext").should be_true
78         end
79       end
80     end
81     context '作家権限がないとき' do
82       before do
83         sign_out @user
84       end
85       context 'html形式' do
86         it 'ステータスコード302 Foundを返す' do
87           get :index
88           response.status.should eq 302
89         end
90         it 'サインインページへ遷移する' do
91           get :index
92           response.should redirect_to '/users/sign_in'
93         end
94       end
95       context 'json形式' do
96         it 'ステータスコード401 Unauthorizedを返す' do
97           get :index, :format => :json
98           response.status.should eq 401
99         end
100         it '応答メッセージにUnauthorizedを返す' do
101           get :index, :format => :json
102           response.message.should match(/Unauthorized/)
103         end
104       end
105     end
106     context '作家が絵師でないとき' do
107       before do
108         Author.any_instance.stub(:artist?).and_return(false)
109       end
110       context 'html形式' do
111         it 'ステータスコード302 Foundを返す' do
112           get :index
113           response.status.should eq 302
114         end
115         it '絵師登録ページへ遷移する' do
116           get :index
117           response.should redirect_to new_artist_path
118         end
119       end
120       context 'json形式' do
121         it '例外403 forbiddenを返す' do
122           lambda{
123             get :index, :format => :json
124           }.should raise_error(ActiveRecord::Forbidden)
125         end
126       end
127     end
128   end
129   
130   describe '単体表示に於いて' do
131     before do
132       @pic = Factory :original_picture, :artist_id => @artist.id , :license_id => @license.id
133       sign_in @user
134       OriginalPicture.stub(:show).and_return(@pic)
135     end
136     context 'つつがなく終わるとき' do
137       it 'ステータスコード200 OKを返す' do
138         get :show, :id => @pic.id
139         response.should be_success
140       end
141       it '原画モデルに単体取得を問い合わせている' do
142         OriginalPicture.should_receive(:show).exactly(1)\r
143         get :show
144       end
145       it '@original_pictureにアレを取得している' do
146         get :show, :id => @pic.id
147         assigns(:original_picture).id.should eq(@pic.id)
148       end
149       context 'html形式' do
150         it 'showテンプレートを描画する' do
151           get :show, :id => @pic.id
152           response.should render_template("show")
153         end
154       end
155       context 'json形式' do
156         it 'jsonデータを返す' do
157           get :show, :id => @pic.id, :format => :json
158           lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
159         end
160         it 'データがアレになっている' do
161           get :show, :id => @pic.id, :format => :json
162           json = JSON.parse response.body
163           json["ext"].should match(/png/)
164         end
165       end
166       #画像送信では、send_dataにスタブをおいてテストしたいが、ここに噛ませると
167       #renderが働かず、エラーとなってしまう。そこで、原画のファイル取得部分に
168       #スタブをおいてsend_dataがデータを返す体裁でテストする。
169       context 'png形式' do
170         it '画像モデルに画像データを問い合わせる' do
171           OriginalPicture.any_instance.should_receive(:restore).exactly(1)\r
172           get :show, :id => @pic.id, :format => :png
173         end
174         it '画像を送信する' do
175           OriginalPicture.any_instance.stub(:restore).and_return('aaa')\r
176           get :show, :id => @pic.id, :format => :png
177           response.body.should eq 'aaa'
178         end
179       end
180       context 'gif形式' do
181         it '画像モデルに画像データを問い合わせる' do
182           OriginalPicture.any_instance.should_receive(:restore).exactly(1)\r
183           get :show, :id => @pic.id, :format => :gif
184         end
185         it '画像を送信する' do
186           OriginalPicture.any_instance.stub(:restore).and_return('bbb')\r
187           get :show, :id => @pic.id, :format => :gif
188           response.body.should eq 'bbb'
189         end
190       end
191       context 'jpeg形式' do
192         it '画像モデルに画像データを問い合わせる' do
193           OriginalPicture.any_instance.should_receive(:restore).exactly(1)\r
194           get :show, :id => @pic.id, :format => :jpeg
195         end
196         it '画像を送信する' do
197           OriginalPicture.any_instance.stub(:restore).and_return('ccc')\r
198           get :show, :id => @pic.id, :format => :jpeg
199           response.body.should eq 'ccc'
200         end
201       end
202     end
203     context '作家権限がないとき' do
204       before do
205         sign_out @user
206       end
207       context 'html形式' do
208         it 'ステータスコード302 Foundを返す' do
209           get :show, :id => @pic.id
210           response.status.should eq 302
211         end
212         it 'サインインページへ遷移する' do
213           get :show, :id => @pic.id
214           response.body.should redirect_to '/users/sign_in'
215         end
216       end
217       context 'json形式' do
218         it 'ステータスコード401 Unauthorizedを返す' do
219           get :show, :id => @pic.id, :format => :json
220           response.status.should eq 401
221         end
222         it '応答メッセージにUnauthorizedを返す' do
223           get :show, :id => @pic.id, :format => :json
224           response.message.should match(/Unauthorized/)
225         end
226       end
227     end
228     context '作家が絵師でないとき' do
229       before do
230         Author.any_instance.stub(:artist?).and_return(false)
231       end
232       context 'html形式' do
233         it 'ステータスコード302 Foundを返す' do
234           get :show, :id => @pic.id
235           response.status.should eq 302
236         end
237         it '絵師登録ページへ遷移する' do
238           get :show, :id => @pic.id
239           response.should redirect_to new_artist_path
240         end
241       end
242       context 'json形式' do
243         it '例外403 forbiddenを返す' do
244           lambda{
245             get :show, :id => @pic.id, :format => :json
246           }.should raise_error(ActiveRecord::Forbidden)
247         end
248       end
249     end
250 =begin
251     context '対象原画がないとき' do
252       before do
253         OriginalPicture.unstub(:show)
254       end
255       context 'html形式' do
256         it '例外404 not_foundを返す' do
257           lambda{
258             get :show, :id => 0
259           }.should raise_error(ActiveRecord::RecordNotFound)
260         end
261       end
262       context 'json形式' do
263         it '例外404 not_foundを返す' do
264           lambda{ 
265             get :show, :id => 0, :format => :json
266           }.should raise_error(ActiveRecord::RecordNotFound)
267         end
268       end
269     end
270     context '他人の原画を見ようとしたとき' do
271       before do
272         OriginalPicture.stub(:show).and_return(@pic)
273         OriginalPicture.any_instance.stub(:own?).with(any_args()).and_return(false)
274       end
275       context 'html形式' do
276         it '例外403 forbiddenを返す' do
277           lambda{
278             get :show, :id => @pic.id
279           }.should raise_error(ActiveRecord::Forbidden)
280         end
281       end
282       context 'json形式' do
283         it '例外403 forbiddenを返す' do
284           lambda{
285             get :show, :id => @pic.id, :format => :json
286           }.should raise_error(ActiveRecord::Forbidden)
287         end
288       end
289     end
290 =end
291   end
292
293   describe '新規作成フォーム表示に於いて' do
294     before do
295       sign_in @user
296     end
297     context 'つつがなく終わるとき' do
298       it 'ステータスコード200 OKを返す' do
299         get :new
300         response.should be_success 
301       end
302       it '@original_pictureに新規データを用意している' do
303         get :new
304         assigns(:original_picture).should be_a_new(OriginalPicture)
305       end
306       it '原画モデルにデフォルト値補充を依頼している' do
307         OriginalPicture.any_instance.should_receive(:supply_default).exactly(1)\r
308         get :new
309       end
310       context 'html形式' do
311         it 'ページテンプレートnewを描画する' do
312           get :new
313           response.should render_template("new")
314         end
315       end
316       context 'js形式' do
317         it '部分テンプレートnew.jsを描画する' do
318           get :new, :format => :js
319           response.should render_template("new")
320         end
321       end
322     end
323     context '作家権限がないとき' do
324       before do
325         sign_out @user
326       end
327       context 'html形式' do
328         it 'ステータスコード302 Foundを返す' do
329           get :new
330           response.status.should eq 302
331         end
332         it 'サインインページへ遷移する' do
333           get :new
334           response.body.should redirect_to '/users/sign_in'
335         end
336       end
337       context 'js形式' do
338         it 'ステータスコード401 Unauthorizedを返す' do
339           get :new, :format => :js
340           response.status.should eq 401
341         end
342         it '応答メッセージにUnauthorizedを返す' do
343           get :new, :format => :js
344           response.message.should match(/Unauthorized/)
345         end
346       end
347     end
348     context '作家が絵師でないとき' do
349       before do
350         Author.any_instance.stub(:artist?).and_return(false)
351       end
352       context 'html形式' do
353         it 'ステータスコード302 Foundを返す' do
354           get :new
355           response.status.should eq 302
356         end
357         it '絵師登録ページへ遷移する' do
358           get :new
359           response.should redirect_to new_artist_path
360         end
361       end
362       context 'js形式' do
363         it 'ステータスコード200 Okを返す' do
364           get :new, :format => :js
365           response.status.should eq 200
366         end
367         it '絵師登録部分テンプレートartists/new.jsを描画する' do
368           get :new, :format => :js
369           response.should render_template("artists/new")
370         end
371       end
372     end
373   end
374
375   describe '新規作成に於いて' do
376     before do
377       sign_in @user
378     end
379     context '事前チェックしておく' do
380       before do
381         OriginalPicture.any_instance.stub(:store).with(any_args()).and_return(true)
382       end
383       it 'モデルに保存依頼する' do
384         OriginalPicture.any_instance.should_receive(:store).exactly(1)
385         post :create, :original_picture => Factory.attributes_for(:original_picture)
386       end
387       it "@original_pictureに作成された原画を保持している" do
388         post :create, :original_picture => Factory.attributes_for(:original_picture)
389         assigns(:original_picture).should be_a(OriginalPicture)
390       end
391     end
392     context 'つつがなく終わるとき' do
393       before do
394         OriginalPicture.any_instance.stub(:store).with(any_args()).and_return {
395           assigns(:original_picture).attributes = Factory.attributes_for(:original_picture, :artist_id => @artist.id , :license_id => @license.id, :ext => 'jpeg')
396           assigns(:original_picture).save
397           true
398         }
399       end
400       it "作成された原画がDBにある" do
401         post :create, :original_picture => Factory.attributes_for(:original_picture)
402         assigns(:original_picture).should_not be_a_new OriginalPicture
403       end
404       context 'html形式' do
405         it 'ステータスコード302 Foundを返す' do
406           post :create, :original_picture => Factory.attributes_for(:original_picture)
407           response.status.should eq 302
408         end
409         it '作成された原画の表示ページへ遷移する' do
410           post :create, :original_picture => Factory.attributes_for(:original_picture)
411           response.should redirect_to(OriginalPicture.last)
412         end
413       end
414       context 'json形式' do
415         it 'ステータスコード200 OKを返す' do
416           post :create, :original_picture => Factory.attributes_for(:original_picture), :format => :json
417           response.should be_success 
418         end
419         it '作成された原画をjsonデータで返す' do
420           post :create, :original_picture => Factory.attributes_for(:original_picture), :format => :json
421           lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
422         end
423         it 'データがアレになっている' do
424           post :create, :original_picture => Factory.attributes_for(:original_picture, :ext => 'jpeg'), :format => :json
425           json = JSON.parse response.body
426           json["ext"].should match(/jpeg/)
427         end
428       end
429     end
430     context '作家権限がないとき' do
431       before do
432         sign_out @user
433       end
434       context 'html形式' do
435         it 'ステータスコード302 Foundを返す' do
436           post :create, :original_picture => Factory.attributes_for(:original_picture)
437           response.status.should eq 302
438         end
439         it 'サインインページへ遷移する' do
440           post :create, :original_picture => Factory.attributes_for(:original_picture)
441           response.body.should redirect_to '/users/sign_in'
442         end
443       end
444       context 'json形式' do
445         it 'ステータスコード401 Unauthorizedを返す' do
446           post :create, :original_picture => Factory.attributes_for(:original_picture), :format => :json
447           response.status.should eq 401
448         end
449         it '応答メッセージにUnauthorizedを返す' do
450           post :create, :original_picture => Factory.attributes_for(:original_picture), :format => :json
451           response.message.should match(/Unauthorized/)
452         end
453       end
454     end
455     context '作家が絵師でないとき' do
456       before do
457         Author.any_instance.stub(:artist?).and_return(false)
458       end
459       context 'html形式' do
460         it 'ステータスコード302 Foundを返す' do
461           post :create, :original_picture => Factory.attributes_for(:original_picture)
462           response.status.should eq 302
463         end
464         it '絵師登録ページへ遷移する' do
465           post :create, :original_picture => Factory.attributes_for(:original_picture)
466           response.should redirect_to new_artist_path
467         end
468       end
469       context 'json形式' do
470         it '例外403 forbiddenを返す' do
471           lambda{
472             post :create, :original_picture => Factory.attributes_for(:original_picture), :format => :json
473           }.should raise_error(ActiveRecord::Forbidden)
474         end
475       end
476     end
477     context '検証、保存に失敗した' do
478       before do
479         OriginalPicture.any_instance.stub(:store).and_return(false)
480       end
481       it "未保存のコミックを保持している" do
482         post :create, :original_picture => Factory.attributes_for(:original_picture)
483         assigns(:original_picture).should be_a_new(OriginalPicture)
484       end
485       context 'html形式' do
486         it 'ステータスコード200 OKを返す' do
487           post :create, :original_picture => Factory.attributes_for(:original_picture)
488           response.status.should eq 200
489         end
490         it '新規ページを描画する' do
491           post :create, :original_picture => Factory.attributes_for(:original_picture)
492           response.should render_template("new")
493         end
494       end
495       context 'json形式' do
496         it 'ステータスコード422 unprocessable_entity を返す' do
497           post :create, :original_picture => Factory.attributes_for(:original_picture), :format => :json
498           response.status.should eq 422
499         end
500         it '応答メッセージUnprocessable Entityを返す' do
501           post :create, :original_picture => Factory.attributes_for(:original_picture), :format => :json
502           response.message.should match(/Unprocessable/)
503         end
504       end
505     end
506   end
507
508   describe '編集フォーム表示に於いて' do
509     before do
510       @pic = Factory :original_picture, :artist_id => @artist.id , :license_id => @license.id
511       sign_in @user
512       OriginalPicture.stub(:show).and_return(@pic)
513     end
514     context 'つつがなく終わるとき' do
515       it 'ステータスコード200 OKを返す' do
516         get :edit, :id => @pic.id
517         response.should be_success 
518       end
519       it '原画モデルに単体取得を問い合わせている' do
520         OriginalPicture.should_receive(:show).exactly(1)\r
521         get :edit, :id => @pic.id
522       end
523       it '@original_pictureにデータを用意している' do
524         get :edit, :id => @pic.id
525         assigns(:original_picture).should eq @pic
526       end
527       context 'html形式' do
528         it 'ページテンプレートeditを描画する' do
529           get :edit, :id => @pic.id
530           response.should render_template("edit")
531         end
532       end
533       context 'js形式' do
534         it '部分テンプレートedit.jsを描画する' do
535           get :edit, :id => @pic.id, :format => :js
536           response.should render_template("edit")
537         end
538       end
539     end
540     context '作家権限がないとき' do
541       before do
542         sign_out @user
543       end
544       context 'html形式' do
545         it 'ステータスコード302 Foundを返す' do
546           get :edit, :id => @pic.id
547           response.status.should eq 302
548         end
549         it 'サインインページへ遷移する' do
550           get :edit, :id => @pic.id
551           response.body.should redirect_to '/users/sign_in'
552         end
553       end
554       context 'js形式' do
555         it 'ステータスコード401 Unauthorizedを返す' do
556           get :edit, :id => @pic.id, :format => :js
557           response.status.should eq 401
558         end
559         it '応答メッセージにUnauthorizedを返す' do
560           get :edit, :id => @pic.id, :format => :js
561           response.message.should match(/Unauthorized/)
562         end
563       end
564     end
565     context '作家が絵師でないとき' do
566       before do
567         Author.any_instance.stub(:artist?).and_return(false)
568       end
569       context 'html形式' do
570         it 'ステータスコード302 Foundを返す' do
571           get :edit, :id => @pic.id
572           response.status.should eq 302
573         end
574         it '絵師登録ページへ遷移する' do
575           get :edit, :id => @pic.id
576           response.should redirect_to new_artist_path
577         end
578       end
579       context 'js形式' do
580         it 'ステータスコード200 Okを返す' do
581           get :edit, :id => @pic.id, :format => :js
582           response.status.should eq 200
583         end
584         it '絵師登録部分テンプレートartists/new.jsを描画する' do
585           get :edit, :id => @pic.id, :format => :js
586           response.should render_template("artists/new")
587         end
588       end
589     end
590   end
591
592   describe '更新に於いて' do
593     before do
594       @pic = Factory :original_picture, :artist_id => @artist.id , :license_id => @license.id
595       OriginalPicture.stub(:edit).with(any_args()).and_return(@pic)
596       sign_in @user
597     end
598     context '事前チェックしておく' do
599       before do
600         OriginalPicture.any_instance.stub(:store).with(any_args()).and_return(true)
601       end
602       it '原画モデルに単体取得を問い合わせている' do
603         OriginalPicture.should_receive(:edit).exactly(1)\r
604         put :update, :id => @pic.id, :original_picture => Factory.attributes_for(:original_picture)
605       end
606       it 'モデルに更新を依頼する' do
607         OriginalPicture.any_instance.should_receive(:store).exactly(1)
608         put :update, :id => @pic.id, :original_picture => Factory.attributes_for(:original_picture)
609       end
610       it '@original_pictureにアレを取得している' do
611         put :update, :id => @pic.id, :original_picture => Factory.attributes_for(:original_picture)
612         assigns(:original_picture).should eq(@pic)
613       end
614     end
615     context 'つつがなく終わるとき' do
616       before do
617         OriginalPicture.any_instance.stub(:store).with(any_args()).and_return {
618           assigns(:original_picture).attributes = Factory.attributes_for(:original_picture, :artist_id => @artist.id , :license_id => @license.id, :ext => 'jpeg')
619           assigns(:original_picture).save
620           true
621         }
622       end
623       it '更新される' do
624         put :update, :id => @pic.id, :original_picture => Factory.attributes_for(:original_picture, :ext => 'jpeg')
625         OriginalPicture.find(@pic.id).ext.should eq 'jpeg'
626       end
627       context 'html形式' do
628         it 'ステータスコード302 Foundを返す' do
629           put :update, :id => @pic.id, :original_picture => Factory.attributes_for(:original_picture)
630           response.status.should eq 302
631         end
632         it '更新された原画の表示ページへ遷移する' do
633           put :update, :id => @pic.id, :original_picture => Factory.attributes_for(:original_picture)
634           response.should redirect_to(@pic)
635         end
636       end
637       context 'json形式' do
638         it 'ステータスコード200 OKを返す' do
639           put :update, :id => @pic.id, :original_picture => Factory.attributes_for(:original_picture), :format => :json
640           response.should be_success 
641         end
642         it 'ページ本体は特に返さない' do
643           put :update, :id => @pic.id, :original_picture => Factory.attributes_for(:original_picture), :format => :json
644           response.body.should match /./
645         end
646       end
647     end
648     context '作家権限がないとき' do
649       before do
650         sign_out @user
651       end
652       context 'html形式' do
653         it 'ステータスコード302 Foundを返す' do
654           post :create, :original_picture => Factory.attributes_for(:original_picture)
655           response.status.should eq 302
656         end
657         it 'サインインページへ遷移する' do
658           post :create, :original_picture => Factory.attributes_for(:original_picture)
659           response.body.should redirect_to '/users/sign_in'
660         end
661       end
662       context 'json形式' do
663         it 'ステータスコード401 Unauthorizedを返す' do
664           post :create, :original_picture => Factory.attributes_for(:original_picture), :format => :json
665           response.status.should eq 401
666         end
667         it '応答メッセージにUnauthorizedを返す' do
668           post :create, :original_picture => Factory.attributes_for(:original_picture), :format => :json
669           response.message.should match(/Unauthorized/)
670         end
671       end
672     end
673     context '作家が絵師でないとき' do
674       before do
675         Author.any_instance.stub(:artist?).and_return(false)
676       end
677       context 'html形式' do
678         it 'ステータスコード302 Foundを返す' do
679           post :create, :original_picture => Factory.attributes_for(:original_picture)
680           response.status.should eq 302
681         end
682         it '絵師登録ページへ遷移する' do
683           post :create, :original_picture => Factory.attributes_for(:original_picture)
684           response.should redirect_to new_artist_path
685         end
686       end
687       context 'json形式' do
688         it '例外403 forbiddenを返す' do
689           lambda{
690             post :create, :original_picture => Factory.attributes_for(:original_picture), :format => :json
691           }.should raise_error(ActiveRecord::Forbidden)
692         end
693       end
694     end
695     context '検証、保存に失敗した' do
696       before do
697         OriginalPicture.any_instance.stub(:store).and_return(false)
698       end
699       context 'html形式' do
700         it 'ステータスコード200 Okを返す' do
701           put :update, :id => @pic.id, :original_picture => Factory.attributes_for(:original_picture)
702           response.status.should eq 200
703         end
704         it '編集ページを描画する' do
705           put :update, :id => @pic.id, :original_picture => Factory.attributes_for(:original_picture)
706           response.should render_template("edit")
707         end
708       end
709       context 'json形式' do
710         it 'ステータスコード422 unprocessable_entity を返す' do
711           put :update, :id => @pic.id, :original_picture => Factory.attributes_for(:original_picture), :format => :json
712           response.status.should eq 422
713         end
714         it '応答メッセージUnprocessable Entityを返す' do
715           put :update, :id => @pic.id, :original_picture => Factory.attributes_for(:original_picture), :format => :json
716           response.message.should match(/Unprocessable/)
717         end
718       end
719     end
720   end
721
722 end