OSDN Git Service

5bc98f5d5fbeb49afafe9a68a57df5374a2bab94
[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     FactoryGirl.create :admin
8     @user = FactoryGirl.create( :user_yas)
9     @author = @user.author
10     @artist = FactoryGirl.create :artist_yas, :author_id => @author.id
11     @sp = FactoryGirl.create :system_picture
12     @lg = FactoryGirl.create :license_group
13     @license = FactoryGirl.create :license, :license_group_id => @lg.id, :system_picture_id => @sp.id
14   end
15
16   describe '一覧表示に於いて' do
17     before do
18       @op = FactoryGirl.create :original_picture, :artist_id => @artist.id
19       sign_in @user
20       OriginalPicture.stub(:mylist).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
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(:mylist).exactly(1)
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 '原画モデルにjson一覧出力オプションを問い合わせている' do
73           OriginalPicture.should_receive(:list_json_opt).exactly(1)
74           get :index, :format => :json
75         end
76         it 'データがリスト構造になっている' do
77           get :index, :format => :json
78           json = JSON.parse response.body
79           json.should have_at_least(3).items
80         end
81         it 'リストの先頭くらいは原画っぽいものであって欲しい' do
82           get :index, :format => :json
83           json = JSON.parse response.body
84           json.first.has_key?("ext").should be_true
85           json.first.has_key?("md5").should be_true
86           json.first.has_key?("artist_id").should be_true
87           json.first.has_key?("width").should be_true
88         end
89       end
90     end
91     context '作家権限がないとき' do
92       before do
93         sign_out @user
94       end
95       context 'html形式' do
96         it 'ステータスコード302 Foundを返す' do
97           get :index
98           response.status.should eq 302
99         end
100         it 'サインインページへ遷移する' do
101           get :index
102           response.should redirect_to '/users/sign_in'
103         end
104       end
105       context 'json形式' do
106         it 'ステータスコード401 Unauthorizedを返す' do
107           get :index, :format => :json
108           response.status.should eq 401
109         end
110         it '応答メッセージにUnauthorizedを返す' do
111           get :index, :format => :json
112           response.message.should match(/Unauthorized/)
113         end
114       end
115     end
116     context '作家が絵師でないとき' do
117       before do
118         Author.any_instance.stub(:artist?).and_return(false)
119       end
120       context 'html形式' do
121         it 'ステータスコード302 Foundを返す' do
122           get :index
123           response.status.should eq 302
124         end
125         it '絵師登録ページへ遷移する' do
126           get :index
127           response.should redirect_to new_artist_path
128         end
129       end
130       context 'json形式' do
131         it '例外403 forbiddenを返す' do
132           lambda{
133             get :index, :format => :json
134           }.should raise_error(ActiveRecord::Forbidden)
135         end
136       end
137     end
138   end
139   
140   describe '単体表示に於いて' do
141     before do
142       @pic = FactoryGirl.create :original_picture, :artist_id => @artist.id
143       sign_in @user
144       OriginalPicture.stub(:show).and_return(@pic)
145     end
146     context 'つつがなく終わるとき' do
147       it 'ステータスコード200 OKを返す' do
148         get :show, :id => @pic.id
149         response.should be_success
150       end
151       it '原画モデルに単体取得を問い合わせている' do
152         OriginalPicture.should_receive(:show).exactly(1)
153         get :show
154       end
155       it '@original_pictureにアレを取得している' do
156         get :show, :id => @pic.id
157         assigns(:original_picture).id.should eq(@pic.id)
158       end
159       context 'html形式' do
160         it 'showテンプレートを描画する' do
161           get :show, :id => @pic.id
162           response.should render_template("show")
163         end
164       end
165       context 'json形式' do
166         it 'jsonデータを返す' do
167           get :show, :id => @pic.id, :format => :json
168           lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
169         end
170         it 'データがアレになっている' do
171           get :show, :id => @pic.id, :format => :json
172           json = JSON.parse response.body
173           json["ext"].should match(/png/)
174         end
175       end
176       #画像送信では、send_dataにスタブをおいてテストしたいが、ここに噛ませると
177       #renderが働かず、エラーとなってしまう。そこで、原画のファイル取得部分に
178       #スタブをおいてsend_dataがデータを返す体裁でテストする。
179       context 'png形式' do
180         it '画像モデルに画像データを問い合わせる' do
181           OriginalPicture.any_instance.should_receive(:restore).exactly(1)
182           get :show, :id => @pic.id, :format => :png
183         end
184         it '画像モデルにMimeTypeを問い合わせる' do
185           OriginalPicture.any_instance.should_receive(:mime_type).exactly(1)
186           get :show, :id => @pic.id, :format => :png
187         end
188         it '画像を送信する' do
189           OriginalPicture.any_instance.stub(:restore).and_return('aaa')
190           get :show, :id => @pic.id, :format => :png
191           response.body.should eq 'aaa'
192         end
193       end
194       context 'gif形式' do
195         it '画像モデルに画像データを問い合わせる' do
196           OriginalPicture.any_instance.should_receive(:restore).exactly(1)
197           get :show, :id => @pic.id, :format => :gif
198         end
199         it '画像モデルにMimeTypeを問い合わせる' do
200           OriginalPicture.any_instance.should_receive(:mime_type).exactly(1)
201           get :show, :id => @pic.id, :format => :gif
202         end
203         it '画像を送信する' do
204           OriginalPicture.any_instance.stub(:restore).and_return('bbb')
205           get :show, :id => @pic.id, :format => :gif
206           response.body.should eq 'bbb'
207         end
208       end
209       context 'jpeg形式' do
210         it '画像モデルに画像データを問い合わせる' do
211           OriginalPicture.any_instance.should_receive(:restore).exactly(1)
212           get :show, :id => @pic.id, :format => :jpeg
213         end
214         it '画像モデルにMimeTypeを問い合わせる' do
215           OriginalPicture.any_instance.should_receive(:mime_type).exactly(1)
216           get :show, :id => @pic.id, :format => :jpeg
217         end
218         it '画像を送信する' do
219           OriginalPicture.any_instance.stub(:restore).and_return('ccc')
220           get :show, :id => @pic.id, :format => :jpeg
221           response.body.should eq 'ccc'
222         end
223       end
224     end
225     context '作家権限がないとき' do
226       before do
227         sign_out @user
228       end
229       context 'html形式' do
230         it 'ステータスコード302 Foundを返す' do
231           get :show, :id => @pic.id
232           response.status.should eq 302
233         end
234         it 'サインインページへ遷移する' do
235           get :show, :id => @pic.id
236           response.body.should redirect_to '/users/sign_in'
237         end
238       end
239       context 'json形式' do
240         it 'ステータスコード401 Unauthorizedを返す' do
241           get :show, :id => @pic.id, :format => :json
242           response.status.should eq 401
243         end
244         it '応答メッセージにUnauthorizedを返す' do
245           get :show, :id => @pic.id, :format => :json
246           response.message.should match(/Unauthorized/)
247         end
248       end
249     end
250     context '作家が絵師でないとき' do
251       before do
252         Author.any_instance.stub(:artist?).and_return(false)
253       end
254       context 'html形式' do
255         it 'ステータスコード302 Foundを返す' do
256           get :show, :id => @pic.id
257           response.status.should eq 302
258         end
259         it '絵師登録ページへ遷移する' do
260           get :show, :id => @pic.id
261           response.should redirect_to new_artist_path
262         end
263       end
264       context 'json形式' do
265         it '例外403 forbiddenを返す' do
266           lambda{
267             get :show, :id => @pic.id, :format => :json
268           }.should raise_error(ActiveRecord::Forbidden)
269         end
270       end
271     end
272 =begin
273     context '対象原画がないとき' do
274       before do
275         OriginalPicture.unstub(:show)
276       end
277       context 'html形式' do
278         it '例外404 not_foundを返す' do
279           lambda{
280             get :show, :id => 0
281           }.should raise_error(ActiveRecord::RecordNotFound)
282         end
283       end
284       context 'json形式' do
285         it '例外404 not_foundを返す' do
286           lambda{ 
287             get :show, :id => 0, :format => :json
288           }.should raise_error(ActiveRecord::RecordNotFound)
289         end
290       end
291     end
292     context '他人の原画を見ようとしたとき' do
293       before do
294         OriginalPicture.stub(:show).and_return(@pic)
295         OriginalPicture.any_instance.stub(:own?).with(any_args()).and_return(false)
296       end
297       context 'html形式' do
298         it '例外403 forbiddenを返す' do
299           lambda{
300             get :show, :id => @pic.id
301           }.should raise_error(ActiveRecord::Forbidden)
302         end
303       end
304       context 'json形式' do
305         it '例外403 forbiddenを返す' do
306           lambda{
307             get :show, :id => @pic.id, :format => :json
308           }.should raise_error(ActiveRecord::Forbidden)
309         end
310       end
311     end
312 =end
313   end
314
315   describe '履歴一覧表示に於いて' do
316     before do
317       @op = FactoryGirl.create :original_picture, :artist_id => @artist.id
318       @p = FactoryGirl.create :picture, :artist_id => @artist.id, :license_id => @license.id, :revision => 0,
319         :original_picture_id => @op.id
320       sign_in @user
321       OriginalPicture.stub(:show).and_return(@op)
322       OriginalPicture.any_instance.stub(:history).and_return([@p, @p, @p])
323     end
324     context 'つつがなく終わるとき' do
325       it 'ステータスコード200 OKを返す' do
326         get :history, :id => @op.id
327         response.should be_success 
328       end
329       it '原画モデルに単体取得を問い合わせている' do
330         OriginalPicture.should_receive(:show).exactly(1)
331         get :history, :id => @op.id
332       end
333       it '自身に履歴一覧を問い合わせている' do
334         OriginalPicture.any_instance.should_receive(:history).exactly(1)
335         get :history
336       end
337       it '@historyにリストを取得している' do
338         get :history, :id => @op.id
339         assigns(:history).should have_at_least(3).items
340       end
341       context 'html形式' do
342         it 'historyテンプレートを描画する' do
343           get :history, :id => @op.id
344           response.should render_template("history")
345         end
346       end
347       context 'json形式' do
348         it 'jsonデータを返す' do
349           get :history, :id => @op.id, :format => :json
350           lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
351         end
352         it 'データがリスト構造になっている' do
353           get :history, :id => @op.id, :format => :json
354           json = JSON.parse response.body
355           json.should have_at_least(3).items
356         end
357         it 'リストの先頭くらいは原画っぽいものであって欲しい' do
358           get :history, :id => @op.id, :format => :json
359           json = JSON.parse response.body
360           json.first.has_key?("ext").should be_true
361         end
362       end
363     end
364     context '作家権限がないとき' do
365       before do
366         sign_out @user
367       end
368       context 'html形式' do
369         it 'ステータスコード302 Foundを返す' do
370           get :history, :id => @op.id
371           response.status.should eq 302
372         end
373         it 'サインインページへ遷移する' do
374           get :history, :id => @op.id
375           response.should redirect_to '/users/sign_in'
376         end
377       end
378       context 'json形式' do
379         it 'ステータスコード401 Unauthorizedを返す' do
380           get :history, :id => @op.id, :format => :json
381           response.status.should eq 401
382         end
383         it '応答メッセージにUnauthorizedを返す' do
384           get :history, :id => @op.id, :format => :json
385           response.message.should match(/Unauthorized/)
386         end
387       end
388     end
389     context '作家が絵師でないとき' do
390       before do
391         Author.any_instance.stub(:artist?).and_return(false)
392       end
393       context 'html形式' do
394         it 'ステータスコード302 Foundを返す' do
395           get :history, :id => @op.id
396           response.status.should eq 302
397         end
398         it '絵師登録ページへ遷移する' do
399           get :history, :id => @op.id
400           response.should redirect_to new_artist_path
401         end
402       end
403       context 'json形式' do
404         it '例外403 forbiddenを返す' do
405           lambda{
406             get :history, :id => @op.id, :format => :json
407           }.should raise_error(ActiveRecord::Forbidden)
408         end
409       end
410     end
411   end
412   
413   describe '新規作成フォーム表示に於いて' do
414     before do
415       sign_in @user
416     end
417     context 'つつがなく終わるとき' do
418       it 'ステータスコード200 OKを返す' do
419         get :new
420         response.should be_success 
421       end
422       it '@original_pictureに新規データを用意している' do
423         get :new
424         assigns(:original_picture).should be_a_new(OriginalPicture)
425       end
426       it '原画モデルにデフォルト値補充を依頼している' do
427         OriginalPicture.any_instance.should_receive(:supply_default).exactly(1)
428         get :new
429       end
430       context 'html形式' do
431         it 'ページテンプレートnewを描画する' do
432           get :new
433           response.should render_template("new")
434         end
435       end
436       context 'js形式' do
437         it '部分テンプレートnew.jsを描画する' do
438           get :new, :format => :js
439           response.should render_template("new")
440         end
441       end
442     end
443       context 'json形式' do
444         it 'jsonデータを返す' do
445           get :new, :format => :json
446           lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
447         end
448         it '原画モデルにjson単体出力オプションを問い合わせている' do
449           OriginalPicture.should_receive(:show_json_opt).exactly(1)
450           get :new, :format => :json
451         end
452       end
453     context '作家権限がないとき' do
454       before do
455         sign_out @user
456       end
457       context 'html形式' do
458         it 'ステータスコード302 Foundを返す' do
459           get :new
460           response.status.should eq 302
461         end
462         it 'サインインページへ遷移する' do
463           get :new
464           response.body.should redirect_to '/users/sign_in'
465         end
466       end
467       context 'js形式' do
468         it 'ステータスコード401 Unauthorizedを返す' do
469           get :new, :format => :js
470           response.status.should eq 401
471         end
472         it '応答メッセージにUnauthorizedを返す' do
473           get :new, :format => :js
474           response.message.should match(/Unauthorized/)
475         end
476       end
477     end
478     context '作家が絵師でないとき' do
479       before do
480         Author.any_instance.stub(:artist?).and_return(false)
481       end
482       context 'html形式' do
483         it 'ステータスコード302 Foundを返す' do
484           get :new
485           response.status.should eq 302
486         end
487         it '絵師登録ページへ遷移する' do
488           get :new
489           response.should redirect_to new_artist_path
490         end
491       end
492       context 'js形式' do
493         it 'ステータスコード200 Okを返す' do
494           get :new, :format => :js
495           response.status.should eq 200
496         end
497         it '絵師登録部分テンプレートartists/new.jsを描画する' do
498           get :new, :format => :js
499           response.should render_template("artists/new")
500         end
501       end
502       context 'json形式' do
503         it '例外403 forbiddenを返す' do
504           lambda{
505           get :new, :format => :json
506           }.should raise_error(ActiveRecord::Forbidden)
507         end
508       end
509     end
510   end
511
512   describe '新規作成に於いて' do
513     before do
514       sign_in @user
515     end
516     context '事前チェックしておく' do
517       before do
518         OriginalPicture.any_instance.stub(:store).with(any_args()).and_return(true)
519       end
520       it 'モデルに保存依頼する' do
521         OriginalPicture.any_instance.should_receive(:store).exactly(1)
522         post :create, :original_picture => FactoryGirl.attributes_for(:original_picture)
523       end
524       it "@original_pictureに作成された原画を保持している" do
525         post :create, :original_picture => FactoryGirl.attributes_for(:original_picture)
526         assigns(:original_picture).should be_a(OriginalPicture)
527       end
528     end
529     context 'つつがなく終わるとき' do
530       before do
531         OriginalPicture.any_instance.stub(:store).with(any_args()).and_return {
532           assigns(:original_picture).attributes = FactoryGirl.attributes_for(:original_picture, :artist_id => @artist.id, :ext => 'jpeg')
533           assigns(:original_picture).save
534           true
535         }
536       end
537       it "作成された原画がDBにある" do
538         post :create, :original_picture => FactoryGirl.attributes_for(:original_picture)
539         assigns(:original_picture).should_not be_a_new OriginalPicture
540       end
541       context 'html形式' do
542         it 'ステータスコード302 Foundを返す' do
543           post :create, :original_picture => FactoryGirl.attributes_for(:original_picture)
544           response.status.should eq 302
545         end
546         it '作成された原画の表示ページへ遷移する' do
547           post :create, :original_picture => FactoryGirl.attributes_for(:original_picture)
548           response.should redirect_to(OriginalPicture.last)
549         end
550       end
551       context 'json形式' do
552         it 'ステータスコード200 OKを返す' do
553           post :create, :original_picture => FactoryGirl.attributes_for(:original_picture), :format => :json
554           response.should be_success 
555         end
556         it '作成された原画をjsonデータで返す' do
557           post :create, :original_picture => FactoryGirl.attributes_for(:original_picture), :format => :json
558           lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
559         end
560         it 'データがアレになっている' do
561           post :create, :original_picture => FactoryGirl.attributes_for(:original_picture, :ext => 'jpeg'), :format => :json
562           json = JSON.parse response.body
563           json["ext"].should match(/jpeg/)
564         end
565       end
566     end
567     context '作家権限がないとき' do
568       before do
569         sign_out @user
570       end
571       context 'html形式' do
572         it 'ステータスコード302 Foundを返す' do
573           post :create, :original_picture => FactoryGirl.attributes_for(:original_picture)
574           response.status.should eq 302
575         end
576         it 'サインインページへ遷移する' do
577           post :create, :original_picture => FactoryGirl.attributes_for(:original_picture)
578           response.body.should redirect_to '/users/sign_in'
579         end
580       end
581       context 'json形式' do
582         it 'ステータスコード401 Unauthorizedを返す' do
583           post :create, :original_picture => FactoryGirl.attributes_for(:original_picture), :format => :json
584           response.status.should eq 401
585         end
586         it '応答メッセージにUnauthorizedを返す' do
587           post :create, :original_picture => FactoryGirl.attributes_for(:original_picture), :format => :json
588           response.message.should match(/Unauthorized/)
589         end
590       end
591     end
592     context '作家が絵師でないとき' do
593       before do
594         Author.any_instance.stub(:artist?).and_return(false)
595       end
596       context 'html形式' do
597         it 'ステータスコード302 Foundを返す' do
598           post :create, :original_picture => FactoryGirl.attributes_for(:original_picture)
599           response.status.should eq 302
600         end
601         it '絵師登録ページへ遷移する' do
602           post :create, :original_picture => FactoryGirl.attributes_for(:original_picture)
603           response.should redirect_to new_artist_path
604         end
605       end
606       context 'json形式' do
607         it '例外403 forbiddenを返す' do
608           lambda{
609             post :create, :original_picture => FactoryGirl.attributes_for(:original_picture), :format => :json
610           }.should raise_error(ActiveRecord::Forbidden)
611         end
612       end
613     end
614     context '検証、保存に失敗した' do
615       before do
616         OriginalPicture.any_instance.stub(:store).and_return(false)
617       end
618       it "未保存のコミックを保持している" do
619         post :create, :original_picture => FactoryGirl.attributes_for(:original_picture)
620         assigns(:original_picture).should be_a_new(OriginalPicture)
621       end
622       context 'html形式' do
623         it 'ステータスコード200 OKを返す' do
624           post :create, :original_picture => FactoryGirl.attributes_for(:original_picture)
625           response.status.should eq 200
626         end
627         it '新規ページを描画する' do
628           post :create, :original_picture => FactoryGirl.attributes_for(:original_picture)
629           response.should render_template("new")
630         end
631       end
632       context 'json形式' do
633         it 'ステータスコード422 unprocessable_entity を返す' do
634           post :create, :original_picture => FactoryGirl.attributes_for(:original_picture), :format => :json
635           response.status.should eq 422
636         end
637         it '応答メッセージUnprocessable Entityを返す' do
638           post :create, :original_picture => FactoryGirl.attributes_for(:original_picture), :format => :json
639           response.message.should match(/Unprocessable/)
640         end
641       end
642     end
643   end
644
645   describe '編集フォーム表示に於いて' do
646     before do
647       @pic = FactoryGirl.create :original_picture, :artist_id => @artist.id
648       sign_in @user
649       OriginalPicture.stub(:show).and_return(@pic)
650     end
651     context 'つつがなく終わるとき' do
652       it 'ステータスコード200 OKを返す' do
653         get :edit, :id => @pic.id
654         response.should be_success 
655       end
656       it '原画モデルに編集取得を問い合わせている' do
657         OriginalPicture.should_receive(:edit).exactly(1)
658         get :edit, :id => @pic.id
659       end
660       it '@original_pictureにデータを用意している' do
661         get :edit, :id => @pic.id
662         assigns(:original_picture).should eq @pic
663       end
664       context 'html形式' do
665         it 'ページテンプレートeditを描画する' do
666           get :edit, :id => @pic.id
667           response.should render_template("edit")
668         end
669       end
670       context 'js形式' do
671         it '部分テンプレートedit.jsを描画する' do
672           get :edit, :id => @pic.id, :format => :js
673           response.should render_template("edit")
674         end
675       end
676     end
677     context '作家権限がないとき' do
678       before do
679         sign_out @user
680       end
681       context 'html形式' do
682         it 'ステータスコード302 Foundを返す' do
683           get :edit, :id => @pic.id
684           response.status.should eq 302
685         end
686         it 'サインインページへ遷移する' do
687           get :edit, :id => @pic.id
688           response.body.should redirect_to '/users/sign_in'
689         end
690       end
691       context 'js形式' do
692         it 'ステータスコード401 Unauthorizedを返す' do
693           get :edit, :id => @pic.id, :format => :js
694           response.status.should eq 401
695         end
696         it '応答メッセージにUnauthorizedを返す' do
697           get :edit, :id => @pic.id, :format => :js
698           response.message.should match(/Unauthorized/)
699         end
700       end
701     end
702     context '作家が絵師でないとき' do
703       before do
704         Author.any_instance.stub(:artist?).and_return(false)
705       end
706       context 'html形式' do
707         it 'ステータスコード302 Foundを返す' do
708           get :edit, :id => @pic.id
709           response.status.should eq 302
710         end
711         it '絵師登録ページへ遷移する' do
712           get :edit, :id => @pic.id
713           response.should redirect_to new_artist_path
714         end
715       end
716       context 'js形式' do
717         it 'ステータスコード200 Okを返す' do
718           get :edit, :id => @pic.id, :format => :js
719           response.status.should eq 200
720         end
721         it '絵師登録部分テンプレートartists/new.jsを描画する' do
722           get :edit, :id => @pic.id, :format => :js
723           response.should render_template("artists/new")
724         end
725       end
726     end
727   end
728
729   describe '更新に於いて' do
730     before do
731       @pic = FactoryGirl.create :original_picture, :artist_id => @artist.id
732       OriginalPicture.stub(:edit).with(any_args()).and_return(@pic)
733       sign_in @user
734     end
735     context '事前チェックしておく' do
736       before do
737         OriginalPicture.any_instance.stub(:store).with(any_args()).and_return(true)
738       end
739       it '原画モデルに単体取得を問い合わせている' do
740         OriginalPicture.should_receive(:edit).exactly(1)
741         put :update, :id => @pic.id, :original_picture => FactoryGirl.attributes_for(:original_picture)
742       end
743       it 'モデルに更新を依頼する' do
744         OriginalPicture.any_instance.should_receive(:store).exactly(1)
745         put :update, :id => @pic.id, :original_picture => FactoryGirl.attributes_for(:original_picture)
746       end
747       it '@original_pictureにアレを取得している' do
748         put :update, :id => @pic.id, :original_picture => FactoryGirl.attributes_for(:original_picture)
749         assigns(:original_picture).should eq(@pic)
750       end
751     end
752     context 'つつがなく終わるとき' do
753       before do
754         OriginalPicture.any_instance.stub(:store).with(any_args()).and_return {
755           assigns(:original_picture).attributes = FactoryGirl.attributes_for(:original_picture, :artist_id => @artist.id, :ext => 'jpeg')
756           assigns(:original_picture).save
757           true
758         }
759       end
760       it '更新される' do
761         put :update, :id => @pic.id, :original_picture => FactoryGirl.attributes_for(:original_picture, :ext => 'jpeg')
762         OriginalPicture.find(@pic.id).ext.should eq 'jpeg'
763       end
764       context 'html形式' do
765         it 'ステータスコード302 Foundを返す' do
766           put :update, :id => @pic.id, :original_picture => FactoryGirl.attributes_for(:original_picture)
767           response.status.should eq 302
768         end
769         it '更新された原画の表示ページへ遷移する' do
770           put :update, :id => @pic.id, :original_picture => FactoryGirl.attributes_for(:original_picture)
771           response.should redirect_to(@pic)
772         end
773       end
774       context 'json形式' do
775         it 'ステータスコード200 OKを返す' do
776           put :update, :id => @pic.id, :original_picture => FactoryGirl.attributes_for(:original_picture), :format => :json
777           response.should be_success 
778         end
779         it 'ページ本体は特に返さない' do
780           put :update, :id => @pic.id, :original_picture => FactoryGirl.attributes_for(:original_picture), :format => :json
781           response.body.should match /./
782         end
783       end
784     end
785     context '作家権限がないとき' do
786       before do
787         sign_out @user
788       end
789       context 'html形式' do
790         it 'ステータスコード302 Foundを返す' do
791           post :create, :original_picture => FactoryGirl.attributes_for(:original_picture)
792           response.status.should eq 302
793         end
794         it 'サインインページへ遷移する' do
795           post :create, :original_picture => FactoryGirl.attributes_for(:original_picture)
796           response.body.should redirect_to '/users/sign_in'
797         end
798       end
799       context 'json形式' do
800         it 'ステータスコード401 Unauthorizedを返す' do
801           post :create, :original_picture => FactoryGirl.attributes_for(:original_picture), :format => :json
802           response.status.should eq 401
803         end
804         it '応答メッセージにUnauthorizedを返す' do
805           post :create, :original_picture => FactoryGirl.attributes_for(:original_picture), :format => :json
806           response.message.should match(/Unauthorized/)
807         end
808       end
809     end
810     context '作家が絵師でないとき' do
811       before do
812         Author.any_instance.stub(:artist?).and_return(false)
813       end
814       context 'html形式' do
815         it 'ステータスコード302 Foundを返す' do
816           post :create, :original_picture => FactoryGirl.attributes_for(:original_picture)
817           response.status.should eq 302
818         end
819         it '絵師登録ページへ遷移する' do
820           post :create, :original_picture => FactoryGirl.attributes_for(:original_picture)
821           response.should redirect_to new_artist_path
822         end
823       end
824       context 'json形式' do
825         it '例外403 forbiddenを返す' do
826           lambda{
827             post :create, :original_picture => FactoryGirl.attributes_for(:original_picture), :format => :json
828           }.should raise_error(ActiveRecord::Forbidden)
829         end
830       end
831     end
832     context '検証、保存に失敗した' do
833       before do
834         OriginalPicture.any_instance.stub(:store).and_return(false)
835       end
836       context 'html形式' do
837         it 'ステータスコード200 Okを返す' do
838           put :update, :id => @pic.id, :original_picture => FactoryGirl.attributes_for(:original_picture)
839           response.status.should eq 200
840         end
841         it '編集ページを描画する' do
842           put :update, :id => @pic.id, :original_picture => FactoryGirl.attributes_for(:original_picture)
843           response.should render_template("edit")
844         end
845       end
846       context 'json形式' do
847         it 'ステータスコード422 unprocessable_entity を返す' do
848           put :update, :id => @pic.id, :original_picture => FactoryGirl.attributes_for(:original_picture), :format => :json
849           response.status.should eq 422
850         end
851         it '応答メッセージUnprocessable Entityを返す' do
852           put :update, :id => @pic.id, :original_picture => FactoryGirl.attributes_for(:original_picture), :format => :json
853           response.message.should match(/Unprocessable/)
854         end
855       end
856     end
857   end
858
859 end