OSDN Git Service

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