OSDN Git Service

t#30469:fix artist auth error
[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     @admin = FactoryGirl.create :admin
8     @user = FactoryGirl.create( :user_yas)
9     @author = FactoryGirl.create :author, :user_id => @user.id
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 if MagicNumber['run_mode'] == 1
17   describe '一覧表示に於いて' do
18     before do
19       @op = FactoryGirl.create :original_picture, :artist_id => @artist.id
20       sign_in @user
21       OriginalPicture.stub(:mylist).and_return([@op, @op, @op])
22     end
23     context 'パラメータpageについて' do
24       it '@pageに値が入る' do
25         get :index, :page => 5
26         assigns(:page).should eq 5
27       end
28       it '省略されると@pageに1値が入る' do
29         get :index
30         assigns(:page).should eq 1
31       end
32       it '与えられたpage_sizeがセットされている' do
33         get :index, :page_size => 15
34         assigns(:page_size).should eq 15
35       end
36       it '省略されると@page_sizeにデフォルト値が入る' do
37         get :index
38         assigns(:page_size).should eq OriginalPicture.default_page_size
39       end
40       it '最大を超えると@page_sizeにデフォルト最大値が入る' do
41         get :index, :page_size => 1500
42         assigns(:page_size).should eq OriginalPicture.max_page_size
43       end
44       it '不正な値が入ると@page_sizeにデフォルト最大値が入る' do
45         get :index, :page_size => 0
46         assigns(:page_size).should eq OriginalPicture.default_page_size
47       end
48     end
49     context 'つつがなく終わるとき' do
50       it 'ステータスコード200 OKを返す' do
51         get :index
52         response.should be_success 
53       end
54       it '原画モデルにマイリストを問い合わせている' do
55         OriginalPicture.should_receive(:mylist).exactly(1)
56         get :index
57       end
58       it '@original_picturesにリストを取得している' do
59         get :index
60         assigns(:original_pictures).should have_at_least(3).items
61       end
62       context 'html形式' do
63         it 'indexテンプレートを描画する' do
64           get :index
65           response.should render_template("index")
66         end
67       end
68       context 'json形式' do
69         it 'jsonデータを返す' do
70           get :index, :format => :json
71           lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
72         end
73         it '原画モデルにjson一覧出力オプションを問い合わせている' do
74           OriginalPicture.should_receive(:list_json_opt).exactly(1)
75           get :index, :format => :json
76         end
77         it 'データがリスト構造になっている' do
78           get :index, :format => :json
79           json = JSON.parse response.body
80           json.should have_at_least(3).items
81         end
82         it 'リストの先頭くらいは原画っぽいものであって欲しい' do
83           get :index, :format => :json
84           json = JSON.parse response.body
85           json.first.has_key?("ext").should be_true
86           json.first.has_key?("md5").should be_true
87           json.first.has_key?("artist_id").should be_true
88           json.first.has_key?("width").should be_true
89         end
90       end
91     end
92     context '作家権限がないとき' do
93       before do
94         sign_out @user
95       end
96       context 'html形式' do
97         it 'ステータスコード302 Foundを返す' do
98           get :index
99           response.status.should eq 302
100         end
101         it 'サインインページへ遷移する' do
102           get :index
103           response.should redirect_to '/users/sign_in'
104         end
105       end
106       context 'json形式' do
107         it 'ステータスコード401 Unauthorizedを返す' do
108           get :index, :format => :json
109           response.status.should eq 401
110         end
111         it '応答メッセージにUnauthorizedを返す' do
112           get :index, :format => :json
113           response.message.should match(/Unauthorized/)
114         end
115       end
116     end
117     context '作家が絵師でないとき' do
118       before do
119         Author.any_instance.stub(:artist?).and_return(false)
120       end
121       context 'html形式' do
122         it 'ステータスコード302 Foundを返す' do
123           get :index
124           response.status.should eq 302
125         end
126         it '絵師登録ページへ遷移する' do
127           get :index
128           response.should redirect_to new_artist_path
129         end
130       end
131       context 'json形式' do
132         it '応答メッセージにUnauthorizedを返す' do
133           get :index, :format => :json
134           response.message.should match(/Unauthorized/)
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 '原画モデルにjson単体出力オプションを問い合わせている' do
171           OriginalPicture.should_receive(:show_json_opt).exactly(1)
172           get :show, :id => @pic.id, :format => :json
173         end
174         it 'データがアレになっている' do
175           get :show, :id => @pic.id, :format => :json
176           json = JSON.parse response.body
177           json["ext"].should match(/png/)
178           json["md5"].should be_true
179           json["artist_id"].should be_true
180           json["width"].should be_true
181         end
182       end
183       #画像送信では、send_dataにスタブをおいてテストしたいが、ここに噛ませると
184       #renderが働かず、エラーとなってしまう。そこで、原画のファイル取得部分に
185       #スタブをおいてsend_dataがデータを返す体裁でテストする。
186       context 'png形式' do
187         before do
188           OriginalPicture.any_instance.stub(:mime_type).and_return('image/png')
189           OriginalPicture.any_instance.stub(:restore).and_return('aaa')
190         end
191         it '画像モデルに画像データを問い合わせる' do
192           OriginalPicture.any_instance.should_receive(:restore).exactly(1)
193           get :show, :id => @pic.id, :format => :png
194         end
195         it '画像モデルにMimeTypeを問い合わせる' do
196           OriginalPicture.any_instance.should_receive(:mime_type).exactly(1)
197           get :show, :id => @pic.id, :format => :png
198         end
199         it '画像を送信する' do
200           get :show, :id => @pic.id, :format => :png
201           response.body.should eq 'aaa'
202         end
203       end
204       context 'gif形式' do
205         before do
206           OriginalPicture.any_instance.stub(:mime_type).and_return('image/gif')
207           OriginalPicture.any_instance.stub(:restore).and_return('bbb')
208         end
209         it '画像モデルに画像データを問い合わせる' do
210           OriginalPicture.any_instance.should_receive(:restore).exactly(1)
211           get :show, :id => @pic.id, :format => :gif
212         end
213         it '画像モデルにMimeTypeを問い合わせる' do
214           OriginalPicture.any_instance.should_receive(:mime_type).exactly(1)
215           get :show, :id => @pic.id, :format => :gif
216         end
217         it '画像を送信する' do
218           get :show, :id => @pic.id, :format => :gif
219           response.body.should eq 'bbb'
220         end
221       end
222       context 'jpeg形式' do
223         before do
224           OriginalPicture.any_instance.stub(:mime_type).and_return('image/jpeg')
225           OriginalPicture.any_instance.stub(:restore).and_return('ccc')
226         end
227         it '画像モデルに画像データを問い合わせる' do
228           OriginalPicture.any_instance.should_receive(:restore).exactly(1)
229           get :show, :id => @pic.id, :format => :jpeg
230         end
231         it '画像モデルにMimeTypeを問い合わせる' do
232           OriginalPicture.any_instance.should_receive(:mime_type).exactly(1)
233           get :show, :id => @pic.id, :format => :jpeg
234         end
235         it '画像を送信する' do
236           get :show, :id => @pic.id, :format => :jpeg
237           response.body.should eq 'ccc'
238         end
239       end
240     end
241     context '作家権限がないとき' do
242       before do
243         sign_out @user
244       end
245       context 'html形式' do
246         it 'ステータスコード302 Foundを返す' do
247           get :show, :id => @pic.id
248           response.status.should eq 302
249         end
250         it 'サインインページへ遷移する' do
251           get :show, :id => @pic.id
252           response.body.should redirect_to '/users/sign_in'
253         end
254       end
255       context 'json形式' do
256         it 'ステータスコード401 Unauthorizedを返す' do
257           get :show, :id => @pic.id, :format => :json
258           response.status.should eq 401
259         end
260         it '応答メッセージにUnauthorizedを返す' do
261           get :show, :id => @pic.id, :format => :json
262           response.message.should match(/Unauthorized/)
263         end
264       end
265     end
266     context '作家が絵師でないとき' do
267       before do
268         Author.any_instance.stub(:artist?).and_return(false)
269       end
270       context 'html形式' do
271         it 'ステータスコード302 Foundを返す' do
272           get :show, :id => @pic.id
273           response.status.should eq 302
274         end
275         it '絵師登録ページへ遷移する' do
276           get :show, :id => @pic.id
277           response.should redirect_to new_artist_path
278         end
279       end
280       context 'json形式' do
281         it '応答メッセージにUnauthorizedを返す' do
282           get :show, :id => @pic.id, :format => :json
283           response.message.should match(/Unauthorized/)
284         end
285       end
286     end
287 =begin
288     context '対象原画がないとき' do
289       before do
290         OriginalPicture.unstub(:show)
291       end
292       context 'html形式' do
293         it '例外404 not_foundを返す' do
294           lambda{
295             get :show, :id => 0
296           }.should raise_error(ActiveRecord::RecordNotFound)
297         end
298       end
299       context 'json形式' do
300         it '例外404 not_foundを返す' do
301           lambda{ 
302             get :show, :id => 0, :format => :json
303           }.should raise_error(ActiveRecord::RecordNotFound)
304         end
305       end
306     end
307     context '他人の原画を見ようとしたとき' do
308       before do
309         OriginalPicture.stub(:show).and_return(@pic)
310         OriginalPicture.any_instance.stub(:own?).with(any_args()).and_return(false)
311       end
312       context 'html形式' do
313         it '例外403 forbiddenを返す' do
314           lambda{
315             get :show, :id => @pic.id
316           }.should raise_error(ActiveRecord::Forbidden)
317         end
318       end
319       context 'json形式' do
320         it '例外403 forbiddenを返す' do
321           lambda{
322             get :show, :id => @pic.id, :format => :json
323           }.should raise_error(ActiveRecord::Forbidden)
324         end
325       end
326     end
327 =end
328   end
329
330   describe '履歴一覧表示に於いて' do
331     before do
332       @op = FactoryGirl.create :original_picture, :artist_id => @artist.id
333       @p = FactoryGirl.create :picture, :artist_id => @artist.id, :license_id => @license.id, :revision => 0,
334         :original_picture_id => @op.id
335       sign_in @user
336       OriginalPicture.stub(:show).and_return(@op)
337       OriginalPicture.any_instance.stub(:history).and_return([@p, @p, @p])
338     end
339     context 'つつがなく終わるとき' do
340       it 'ステータスコード200 OKを返す' do
341         get :history, :id => @op.id
342         response.should be_success 
343       end
344       it '原画モデルに単体取得を問い合わせている' do
345         OriginalPicture.should_receive(:show).exactly(1)
346         get :history, :id => @op.id
347       end
348       it '自身に履歴一覧を問い合わせている' do
349         OriginalPicture.any_instance.should_receive(:history).exactly(1)
350         get :history, :id => @op.id
351       end
352       it '@historyにリストを取得している' do
353         get :history, :id => @op.id
354         assigns(:history).should have_at_least(3).items
355       end
356       context 'html形式' do
357         it 'historyテンプレートを描画する' do
358           get :history, :id => @op.id
359           response.should render_template("history")
360         end
361       end
362       context 'json形式' do
363         it 'jsonデータを返す' do
364           get :history, :id => @op.id, :format => :json
365           lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
366         end
367         it 'データがリスト構造になっている' do
368           get :history, :id => @op.id, :format => :json
369           json = JSON.parse response.body
370           json.should have_at_least(3).items
371         end
372         it 'リストの先頭くらいは原画っぽいものであって欲しい' do
373           get :history, :id => @op.id, :format => :json
374           json = JSON.parse response.body
375           json.first.has_key?("ext").should be_true
376         end
377       end
378     end
379     context '作家権限がないとき' do
380       before do
381         sign_out @user
382       end
383       context 'html形式' do
384         it 'ステータスコード302 Foundを返す' do
385           get :history, :id => @op.id
386           response.status.should eq 302
387         end
388         it 'サインインページへ遷移する' do
389           get :history, :id => @op.id
390           response.should redirect_to '/users/sign_in'
391         end
392       end
393       context 'json形式' do
394         it 'ステータスコード401 Unauthorizedを返す' do
395           get :history, :id => @op.id, :format => :json
396           response.status.should eq 401
397         end
398         it '応答メッセージにUnauthorizedを返す' do
399           get :history, :id => @op.id, :format => :json
400           response.message.should match(/Unauthorized/)
401         end
402       end
403     end
404     context '作家が絵師でないとき' do
405       before do
406         Author.any_instance.stub(:artist?).and_return(false)
407       end
408       context 'html形式' do
409         it 'ステータスコード302 Foundを返す' do
410           get :history, :id => @op.id
411           response.status.should eq 302
412         end
413         it '絵師登録ページへ遷移する' do
414           get :history, :id => @op.id
415           response.should redirect_to new_artist_path
416         end
417       end
418       context 'json形式' do
419         it '応答メッセージにUnauthorizedを返す' do
420           get :history, :id => @op.id, :format => :json
421           response.message.should match(/Unauthorized/)
422         end
423       end
424     end
425   end
426   
427   describe '新規作成フォーム表示に於いて' do
428     before do
429       sign_in @user
430     end
431     context 'つつがなく終わるとき' do
432       it '@original_pictureに新規データを用意している' do
433         get :new
434         assigns(:original_picture).should be_a_new(OriginalPicture)
435       end
436       it '原画モデルにデフォルト値補充を依頼している' do
437         OriginalPicture.any_instance.should_receive(:supply_default).exactly(1)
438         get :new
439       end
440       context 'html形式' do
441         it 'ステータスコード200 OKを返す' do
442           get :new
443           response.should be_success 
444         end
445         it 'ページテンプレートnewを描画する' do
446           get :new
447           response.should render_template("new")
448         end
449       end
450       context 'js形式' do
451         it 'ステータスコード200 OKを返す' do
452           get :new, :format => :js
453           response.should be_success 
454         end
455         it '部分テンプレートnew.jsを描画する' do
456           get :new, :format => :js
457           response.should render_template("new")
458         end
459       end
460       context 'json形式' do
461         it 'jsonデータを返す' do
462           get :new, :format => :json
463           lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
464         end
465         it '原画モデルにjson単体出力オプションを問い合わせている' do
466           OriginalPicture.should_receive(:show_json_opt).exactly(1)
467           get :new, :format => :json
468         end
469       end
470     end
471     context '作家権限がないとき' do
472       before do
473         sign_out @user
474       end
475       context 'html形式' do
476         it 'ステータスコード302 Foundを返す' do
477           get :new
478           response.status.should eq 302
479         end
480         it 'サインインページへ遷移する' do
481           get :new
482           response.body.should redirect_to '/users/sign_in'
483         end
484       end
485       context 'js形式' do
486         it 'ステータスコード401 Unauthorizedを返す' do
487           get :new, :format => :js
488           response.status.should eq 401
489         end
490         it '応答メッセージにUnauthorizedを返す' do
491           get :new, :format => :js
492           response.message.should match(/Unauthorized/)
493         end
494       end
495     end
496     context '作家が絵師でないとき' do
497       before do
498         Author.any_instance.stub(:artist?).and_return(false)
499       end
500       context 'html形式' do
501         it 'ステータスコード302 Foundを返す' do
502           get :new
503           response.status.should eq 302
504         end
505         it '絵師登録ページへ遷移する' do
506           get :new
507           response.should redirect_to new_artist_path
508         end
509       end
510       context 'js形式' do
511         it 'ステータスコード200 Okを返す' do
512           get :new, :format => :js
513           response.status.should eq 200
514         end
515         it '絵師登録部分テンプレートartists/new.jsを描画する' do
516           get :new, :format => :js
517           response.should render_template("artists/new")
518         end
519       end
520       context 'json形式' do
521         it '応答メッセージにUnauthorizedを返す' do
522           get :new, :format => :json
523           response.message.should match(/Unauthorized/)
524         end
525       end
526     end
527   end
528
529   describe '新規作成に於いて' do
530     before do
531       sign_in @user
532       @attr = {:original_picture => {:file => "abc\ndef\nghi"}}
533       @imager = ImagerTest.load("abc\ndef\nghi")
534       OriginalPicturesController.any_instance.stub(:set_image).with(any_args()).and_return("abc\ndef\nghi")
535     end
536     context '事前チェックしておく' do
537       before do
538         PettanImager.stub(:load).with("abc\ndef\nghi").and_return(@imager)
539         OriginalPicture.any_instance.stub(:store).with(@imager).and_return(true)
540       end
541       it '画像ライブラリをロードする' do
542         PettanImager.should_receive(:load).with(any_args()).exactly(1)
543         post :create, @attr
544       end
545       it '原画モデルにデフォルト値補充を依頼している' do
546         OriginalPicture.any_instance.should_receive(:supply_default).with(any_args()).exactly(1)
547         post :create, @attr
548       end
549       it '原画モデルに上書き補充を依頼している' do
550         OriginalPicture.any_instance.should_receive(:overwrite).with(@artist).exactly(1)
551         post :create, @attr
552       end
553       it 'モデルに保存依頼する' do
554         OriginalPicture.any_instance.should_receive(:store).with(@imager).exactly(1)
555         post :create, @attr
556       end
557     end
558     context 'つつがなく終わるとき' do
559       before do
560         PettanImager.stub(:load).with("abc\ndef\nghi").and_return(@imager)
561       end
562       it "@original_pictureに作成された原画を保持している" do
563         post :create, @attr
564         assigns(:original_picture).should be_a(OriginalPicture)
565       end
566       context 'html形式' do
567         it 'ステータスコード302 Foundを返す' do
568           post :create, @attr
569           response.status.should eq 302
570         end
571         it '作成された原画の表示ページへ遷移する' do
572           post :create, @attr
573           response.should redirect_to(OriginalPicture.last)
574         end
575       end
576       context 'json形式' do
577         before do
578           @attr.merge!({:format => :json})
579         end
580         it 'ステータスコード200 OKを返す' do
581           post :create, @attr
582           response.should be_success 
583         end
584         it '作成された原画をjsonデータで返す' do
585           post :create, @attr
586           lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
587         end
588         it '原画モデルにjson単体出力オプションを問い合わせている' do
589           OriginalPicture.should_receive(:show_json_opt).exactly(1)
590           post :create, @attr
591         end
592         it 'データがアレになっている' do
593           post :create, @attr
594           json = JSON.parse response.body
595           json["ext"].should match(/png/)
596           json["md5"].should be_true
597           json["artist_id"].should be_true
598           json["width"].should be_true
599         end
600       end
601     end
602     context '作家権限がないとき' do
603       before do
604         sign_out @user
605       end
606       context 'html形式' do
607         it 'ステータスコード302 Foundを返す' do
608           post :create, @attr
609           response.status.should eq 302
610         end
611         it 'サインインページへ遷移する' do
612           post :create, @attr
613           response.body.should redirect_to '/users/sign_in'
614         end
615       end
616       context 'json形式' do
617         before do
618           @attr.merge!({:format => :json})
619         end
620         it 'ステータスコード401 Unauthorizedを返す' do
621           post :create, @attr
622           response.status.should eq 401
623         end
624         it '応答メッセージにUnauthorizedを返す' do
625           post :create, @attr
626           response.message.should match(/Unauthorized/)
627         end
628       end
629     end
630     context '作家が絵師でないとき' do
631       before do
632         Author.any_instance.stub(:artist?).and_return(false)
633       end
634       context 'html形式' do
635         it 'ステータスコード302 Foundを返す' do
636           post :create, @attr
637           response.status.should eq 302
638         end
639         it '絵師登録ページへ遷移する' do
640           post :create, @attr
641           response.should redirect_to new_artist_path
642         end
643       end
644       context 'json形式' do
645         before do
646           @attr.merge!({:format => :json})
647         end
648         it '応答メッセージにUnauthorizedを返す' do
649           post :create, @attr
650           response.message.should match(/Unauthorized/)
651         end
652       end
653     end
654     context '検証、保存に失敗した' do
655       before do
656         PettanImager.stub(:load).with("abc\ndef\nghi").and_return(@imager)
657         OriginalPicture.any_instance.stub(:store).with(@imager).and_return(false)
658       end
659       it "未保存の原画を保持している" do
660         post :create, @attr
661         assigns(:original_picture).should be_a_new(OriginalPicture)
662       end
663       context 'html形式' do
664         it 'ステータスコード200 OKを返す' do
665           post :create, @attr
666           response.status.should eq 200
667         end
668         it '新規ページを描画する' do
669           post :create, @attr
670           response.should render_template("new")
671         end
672       end
673       context 'json形式' do
674         before do
675           @attr.merge!({:format => :json})
676         end
677         it 'ステータスコード422 unprocessable_entity を返す' do
678           post :create, @attr
679           response.status.should eq 422
680         end
681         it '応答メッセージUnprocessable Entityを返す' do
682           post :create, @attr
683           response.message.should match(/Unprocessable/)
684         end
685       end
686     end
687     context 'ファイルが指定されていないとき' do
688       before do
689         @attr = {}
690         PettanImager.stub(:load).and_return(nil)
691       end
692       it "未保存の原画を保持している" do
693         post :create, @attr
694         assigns(:original_picture).should be_a_new(OriginalPicture)
695       end
696       context 'html形式' do
697         it 'ステータスコード200 OKを返す' do
698           post :create, @attr
699           response.status.should eq 200
700         end
701         it '新規ページを描画する' do
702           post :create, @attr
703           response.should render_template("new")
704         end
705       end
706       context 'json形式' do
707         before do
708           @attr.merge!({:format => :json})
709         end
710         it 'ステータスコード422 unprocessable_entity を返す' do
711           post :create, @attr
712           response.status.should eq 422
713         end
714         it '応答メッセージUnprocessable Entityを返す' do
715           post :create, @attr
716           response.message.should match(/Unprocessable/)
717         end
718       end
719     end
720   end
721
722   describe '編集フォーム表示に於いて' do
723     before do
724       @pic = FactoryGirl.create :original_picture, :artist_id => @artist.id
725       sign_in @user
726       OriginalPicture.stub(:show).and_return(@pic)
727     end
728     context 'つつがなく終わるとき' do
729       it 'ステータスコード200 OKを返す' do
730         get :edit, :id => @pic.id
731         response.should be_success 
732       end
733       it '原画モデルに編集取得を問い合わせている' do
734         OriginalPicture.should_receive(:edit).exactly(1)
735         get :edit, :id => @pic.id
736       end
737       it '@original_pictureにデータを用意している' do
738         get :edit, :id => @pic.id
739         assigns(:original_picture).should eq @pic
740       end
741       context 'html形式' do
742         it 'ページテンプレートeditを描画する' do
743           get :edit, :id => @pic.id
744           response.should render_template("edit")
745         end
746       end
747       context 'js形式' do
748         it '部分テンプレートedit.jsを描画する' do
749           get :edit, :id => @pic.id, :format => :js
750           response.should render_template("edit")
751         end
752       end
753     end
754     context '作家権限がないとき' do
755       before do
756         sign_out @user
757       end
758       context 'html形式' do
759         it 'ステータスコード302 Foundを返す' do
760           get :edit, :id => @pic.id
761           response.status.should eq 302
762         end
763         it 'サインインページへ遷移する' do
764           get :edit, :id => @pic.id
765           response.body.should redirect_to '/users/sign_in'
766         end
767       end
768       context 'js形式' do
769         it 'ステータスコード401 Unauthorizedを返す' do
770           get :edit, :id => @pic.id, :format => :js
771           response.status.should eq 401
772         end
773         it '応答メッセージにUnauthorizedを返す' do
774           get :edit, :id => @pic.id, :format => :js
775           response.message.should match(/Unauthorized/)
776         end
777       end
778     end
779     context '作家が絵師でないとき' do
780       before do
781         Author.any_instance.stub(:artist?).and_return(false)
782       end
783       context 'html形式' do
784         it 'ステータスコード302 Foundを返す' do
785           get :edit, :id => @pic.id
786           response.status.should eq 302
787         end
788         it '絵師登録ページへ遷移する' do
789           get :edit, :id => @pic.id
790           response.should redirect_to new_artist_path
791         end
792       end
793       context 'js形式' do
794         it 'ステータスコード200 Okを返す' do
795           get :edit, :id => @pic.id, :format => :js
796           response.status.should eq 200
797         end
798         it '絵師登録部分テンプレートartists/new.jsを描画する' do
799           get :edit, :id => @pic.id, :format => :js
800           response.should render_template("artists/new")
801         end
802       end
803     end
804   end
805
806   describe '更新に於いて' do
807     before do
808       @op = FactoryGirl.create :original_picture, :artist_id => @artist.id
809       OriginalPicture.stub(:edit).with(@op.id.to_s, @artist).and_return(@op)
810       OriginalPicturesController.any_instance.stub(:set_image).with(any_args()).and_return("abc\ndef\nghi")
811       sign_in @user
812       @attr = {:file => "abc\ndef\nghi"}
813       @imager = ImagerTest.load("abc\ndef\nghi")
814     end
815     context '事前チェックしておく' do
816       before do
817         PettanImager.stub(:load).with("abc\ndef\nghi").and_return(@imager)
818         OriginalPicture.any_instance.stub(:store).with(@imager).and_return(true)
819       end
820       it '画像ライブラリをロードする' do
821         PettanImager.should_receive(:load).with(any_args()).exactly(1)
822         put :update, :id => @op.id, :original_picture => @attr
823       end
824       it '原画モデルに編集取得を問い合わせている' do
825         OriginalPicture.should_receive(:edit).with(@op.id.to_s, @artist).exactly(1)
826         put :update, :id => @op.id, :original_picture => @attr
827       end
828       it '原画モデルに上書き補充を依頼している' do
829         OriginalPicture.any_instance.should_receive(:overwrite).with(@artist).exactly(1)
830         put :update, :id => @op.id, :original_picture => @attr
831       end
832       it 'モデルに更新を依頼する' do
833         OriginalPicture.any_instance.should_receive(:store).with(@imager).exactly(1)
834         put :update, :id => @op.id, :original_picture => @attr
835       end
836     end
837     context 'つつがなく終わるとき' do
838       before do
839         PettanImager.stub(:load).with("abc\ndef\nghi").and_return(@imager)
840       end
841       it '@original_pictureにアレを取得している' do
842         put :update, :id => @op.id, :original_picture => @attr
843         assigns(:original_picture).should eq(@op)
844       end
845       context 'html形式' do
846         it 'ステータスコード302 Foundを返す' do
847           put :update, :id => @op.id, :original_picture => @attr
848           response.status.should eq 302
849         end
850         it '更新された原画の表示ページへ遷移する' do
851           put :update, :id => @op.id, :original_picture => @attr
852           response.should redirect_to(@pic)
853         end
854       end
855       context 'json形式' do
856         it 'ステータスコード200 OKを返す' do
857           put :update, :id => @op.id, :original_picture => @attr, :format => :json
858           response.should be_success 
859         end
860         it 'ページ本体は特に返さない' do
861           put :update, :id => @op.id, :original_picture => @attr, :format => :json
862           response.body.should match /./
863         end
864       end
865     end
866     context '作家権限がないとき' do
867       before do
868         sign_out @user
869       end
870       context 'html形式' do
871         it 'ステータスコード302 Foundを返す' do
872           put :update, :id => @op.id, :original_picture => @attr
873           response.status.should eq 302
874         end
875         it 'サインインページへ遷移する' do
876           put :update, :id => @op.id, :original_picture => @attr
877           response.body.should redirect_to '/users/sign_in'
878         end
879       end
880       context 'json形式' do
881         it 'ステータスコード401 Unauthorizedを返す' do
882           put :update, :id => @op.id, :original_picture => @attr, :format => :json
883           response.status.should eq 401
884         end
885         it '応答メッセージにUnauthorizedを返す' do
886           put :update, :id => @op.id, :original_picture => @attr, :format => :json
887           response.message.should match(/Unauthorized/)
888         end
889       end
890     end
891     context '作家が絵師でないとき' do
892       before do
893         Author.any_instance.stub(:artist?).and_return(false)
894       end
895       context 'html形式' do
896         it 'ステータスコード302 Foundを返す' do
897           put :update, :id => @op.id, :original_picture => @attr
898           response.status.should eq 302
899         end
900         it '絵師登録ページへ遷移する' do
901           put :update, :id => @op.id, :original_picture => @attr
902           response.should redirect_to new_artist_path
903         end
904       end
905       context 'json形式' do
906         it '応答メッセージにUnauthorizedを返す' do
907           put :update, :id => @op.id, :original_picture => @attr, :format => :json
908           response.message.should match(/Unauthorized/)
909         end
910       end
911     end
912     context '検証、保存に失敗した' do
913       before do
914         PettanImager.stub(:load).with("abc\ndef\nghi").and_return(@imager)
915         OriginalPicture.any_instance.stub(:store).with(@imager).and_return(false)
916       end
917       context 'html形式' do
918         it 'ステータスコード200 Okを返す' do
919           put :update, :id => @op.id, :original_picture => @attr
920           response.status.should eq 200
921         end
922         it '編集ページを描画する' do
923           put :update, :id => @op.id, :original_picture => @attr
924           response.should render_template("edit")
925         end
926       end
927       context 'json形式' do
928         it 'ステータスコード422 unprocessable_entity を返す' do
929           put :update, :id => @op.id, :original_picture => @attr, :format => :json
930           response.status.should eq 422
931         end
932         it '応答メッセージUnprocessable Entityを返す' do
933           put :update, :id => @op.id, :original_picture => @attr, :format => :json
934           response.message.should match(/Unprocessable/)
935         end
936       end
937     end
938     context 'ファイルが指定されていないとき' do
939       before do
940         @attr = {}
941         PettanImager.stub(:load).and_return(nil)
942       end
943       context 'html形式' do
944         it 'ステータスコード200 Okを返す' do
945           put :update, :id => @op.id, :original_picture => @attr
946           response.status.should eq 200
947         end
948         it '編集ページを描画する' do
949           put :update, :id => @op.id, :original_picture => @attr
950           response.should render_template("edit")
951         end
952       end
953       context 'json形式' do
954         it 'ステータスコード422 unprocessable_entity を返す' do
955           put :update, :id => @op.id, :original_picture => @attr, :format => :json
956           response.status.should eq 422
957         end
958         it '応答メッセージUnprocessable Entityを返す' do
959           put :update, :id => @op.id, :original_picture => @attr, :format => :json
960           response.message.should match(/Unprocessable/)
961         end
962       end
963     end
964   end
965
966   describe '削除に於いて' do
967     before do
968       @op = FactoryGirl.create :original_picture, :artist_id => @artist.id
969       sign_in @user
970     end
971     context '事前チェックしておく' do
972       before do
973         OriginalPicture.stub(:edit).with(any_args()).and_return @op
974         OriginalPicture.any_instance.stub(:destroy_with_resource_picture).with(any_args()).and_return(true)
975       end
976       it '原画モデルに編集取得を問い合わせている' do
977         OriginalPicture.should_receive(:edit).exactly(1)
978         delete :destroy, :id => @op.id
979       end
980       it 'モデルに削除を依頼する' do
981         OriginalPicture.any_instance.should_receive(:destroy_with_resource_picture).exactly(1)
982         delete :destroy, :id => @op.id
983       end
984       it '@original_pictureにアレを取得している' do
985         delete :destroy, :id => @op.id
986         assigns(:original_picture).id.should eq(@op.id)
987       end
988     end
989     context 'つつがなく終わるとき' do
990       before do
991         OriginalPicture.any_instance.stub(:destroy_with_resource_picture).with(any_args()).and_return(true)
992       end
993       context 'html形式' do
994         it 'ステータスコード302 Foundを返す' do
995           delete :destroy, :id => @op.id
996           response.status.should eq 302
997         end
998         it '原画の一覧ページへ遷移する' do
999           delete :destroy, :id => @op.id
1000           response.should redirect_to(original_pictures_path)
1001         end
1002       end
1003       context 'json形式' do
1004         it 'ステータスコード200 OKを返す' do
1005           delete :destroy, :id => @op.id, :format => :json
1006           response.should be_success 
1007         end
1008         it 'ページ本体は特に返さない' do
1009           delete :destroy, :id => @op.id, :format => :json
1010           response.body.should match /./
1011         end
1012       end
1013     end
1014     context '作家権限がないとき' do
1015       before do
1016         sign_out @user
1017       end
1018       it 'ステータスコード302 Foundを返す' do
1019         delete :destroy, :id => @op.id
1020         response.status.should eq 302
1021       end
1022       context 'html形式' do
1023         it 'サインインページへ遷移する' do
1024           delete :destroy, :id => @op.id
1025           response.body.should redirect_to '/users/sign_in'
1026         end
1027       end
1028       context 'json形式' do
1029         it '応答メッセージにUnauthorizedを返す' do
1030           delete :destroy, :id => @op.id, :format => :json
1031           response.message.should match(/Unauthorized/)
1032         end
1033       end
1034     end
1035     context '作家が絵師でないとき' do
1036       before do
1037         Author.any_instance.stub(:artist?).and_return(false)
1038       end
1039       context 'html形式' do
1040         it 'ステータスコード302 Foundを返す' do
1041           delete :destroy, :id => @op.id
1042           response.status.should eq 302
1043         end
1044         it '絵師登録ページへ遷移する' do
1045           delete :destroy, :id => @op.id
1046           response.should redirect_to new_artist_path
1047         end
1048       end
1049       context 'json形式' do
1050         it '応答メッセージにUnauthorizedを返す' do
1051           delete :destroy, :id => @op.id, :format => :json
1052           response.message.should match(/Unauthorized/)
1053         end
1054       end
1055     end
1056     context '削除に失敗したとき' do
1057       before do
1058         OriginalPicture.any_instance.stub(:destroy_with_resource_picture).and_return(false)
1059       end
1060       context 'html形式' do
1061         it 'ステータスコード302 Foundを返す' do
1062           delete :destroy, :id => @op.id
1063           response.status.should eq 302
1064         end
1065         it 'その原画の詳細ページへ遷移する' do
1066           delete :destroy, :id => @op.id
1067           response.should redirect_to(original_picture_path(@op))
1068         end
1069       end
1070       context 'json形式' do
1071         it 'ステータスコード422 unprocessable_entity を返す' do
1072           delete :destroy, :id => @op.id, :format => :json
1073           response.status.should eq 422
1074         end
1075         it '応答メッセージUnprocessable Entityを返す' do
1076           delete :destroy, :id => @op.id, :format => :json
1077           response.message.should match(/Unprocessable/)
1078         end
1079       end
1080     end
1081   end
1082
1083 else
1084   describe '一覧表示に於いて' do
1085     before do
1086       @op = FactoryGirl.create :original_picture, :artist_id => @artist.id
1087       sign_in @user
1088       OriginalPicture.stub(:mylist).and_return([@op, @op, @op])
1089     end
1090     context 'つつがなく終わるとき' do
1091       it 'ステータスコード200 OKを返す' do
1092         get :index
1093         response.should be_success 
1094       end
1095       context 'html形式' do
1096         it 'indexテンプレートを描画する' do
1097           get :index
1098           response.should render_template("index")
1099         end
1100       end
1101       context 'json形式' do
1102         it 'jsonデータを返す' do
1103           get :index, :format => :json
1104           lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
1105         end
1106       end
1107     end
1108     context '作家権限がないとき' do
1109       before do
1110         sign_out @user
1111       end
1112       context 'html形式' do
1113         it 'ステータスコード302 Foundを返す' do
1114           get :index
1115           response.status.should eq 302
1116         end
1117         it 'サインインページへ遷移する' do
1118           get :index
1119           response.should redirect_to '/users/sign_in'
1120         end
1121       end
1122       context 'json形式' do
1123         it 'ステータスコード401 Unauthorizedを返す' do
1124           get :index, :format => :json
1125           response.status.should eq 401
1126         end
1127         it '応答メッセージにUnauthorizedを返す' do
1128           get :index, :format => :json
1129           response.message.should match(/Unauthorized/)
1130         end
1131       end
1132     end
1133   end
1134   
1135   describe '単体表示に於いて' do
1136     before do
1137       @pic = FactoryGirl.create :original_picture, :artist_id => @artist.id
1138       sign_in @user
1139       OriginalPicture.stub(:show).and_return(@pic)
1140     end
1141     context 'つつがなく終わるとき' do
1142       it 'ステータスコード200 OKを返す' do
1143         get :show, :id => @pic.id
1144         response.should be_success
1145       end
1146       context 'html形式' do
1147         it 'showテンプレートを描画する' do
1148           get :show, :id => @pic.id
1149           response.should render_template("show")
1150         end
1151       end
1152       context 'json形式' do
1153         it 'jsonデータを返す' do
1154           get :show, :id => @pic.id, :format => :json
1155           lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
1156         end
1157       end
1158     end
1159     context '作家権限がないとき' do
1160       before do
1161         sign_out @user
1162       end
1163       context 'html形式' do
1164         it 'ステータスコード302 Foundを返す' do
1165           get :show, :id => @pic.id
1166           response.status.should eq 302
1167         end
1168         it 'サインインページへ遷移する' do
1169           get :show, :id => @pic.id
1170           response.body.should redirect_to '/users/sign_in'
1171         end
1172       end
1173       context 'json形式' do
1174         it 'ステータスコード401 Unauthorizedを返す' do
1175           get :show, :id => @pic.id, :format => :json
1176           response.status.should eq 401
1177         end
1178         it '応答メッセージにUnauthorizedを返す' do
1179           get :show, :id => @pic.id, :format => :json
1180           response.message.should match(/Unauthorized/)
1181         end
1182       end
1183     end
1184   end
1185
1186   describe '履歴一覧表示に於いて' do
1187     before do
1188       @op = FactoryGirl.create :original_picture, :artist_id => @artist.id
1189       @p = FactoryGirl.create :picture, :artist_id => @artist.id, :license_id => @license.id, :revision => 0,
1190         :original_picture_id => @op.id
1191       sign_in @user
1192       OriginalPicture.stub(:show).and_return(@op)
1193       OriginalPicture.any_instance.stub(:history).and_return([@p, @p, @p])
1194     end
1195     context 'つつがなく終わるとき' do
1196       it 'ステータスコード200 OKを返す' do
1197         get :history, :id => @op.id
1198         response.should be_success 
1199       end
1200       context 'html形式' do
1201         it 'historyテンプレートを描画する' do
1202           get :history, :id => @op.id
1203           response.should render_template("history")
1204         end
1205       end
1206       context 'json形式' do
1207         it 'jsonデータを返す' do
1208           get :history, :id => @op.id, :format => :json
1209           lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
1210         end
1211       end
1212     end
1213     context '作家権限がないとき' do
1214       before do
1215         sign_out @user
1216       end
1217       context 'html形式' do
1218         it 'ステータスコード302 Foundを返す' do
1219           get :history, :id => @op.id
1220           response.status.should eq 302
1221         end
1222         it 'サインインページへ遷移する' do
1223           get :history, :id => @op.id
1224           response.should redirect_to '/users/sign_in'
1225         end
1226       end
1227       context 'json形式' do
1228         it 'ステータスコード401 Unauthorizedを返す' do
1229           get :history, :id => @op.id, :format => :json
1230           response.status.should eq 401
1231         end
1232         it '応答メッセージにUnauthorizedを返す' do
1233           get :history, :id => @op.id, :format => :json
1234           response.message.should match(/Unauthorized/)
1235         end
1236       end
1237     end
1238   end
1239   
1240   describe '新規作成フォーム表示に於いて' do
1241     before do
1242       sign_in @user
1243     end
1244     context 'つつがなく終わるとき' do
1245       context 'html形式' do
1246         it 'ステータスコード200 OKを返す' do
1247           get :new
1248           response.should be_success 
1249         end
1250         it 'ページテンプレートnewを描画する' do
1251           get :new
1252           response.should render_template("new")
1253         end
1254       end
1255       context 'js形式' do
1256         it 'ステータスコード200 OKを返す' do
1257           get :new, :format => :js
1258           response.should be_success 
1259         end
1260         it '部分テンプレートnew.jsを描画する' do
1261           get :new, :format => :js
1262           response.should render_template("new")
1263         end
1264       end
1265       context 'json形式' do
1266         it 'jsonデータを返す' do
1267           get :new, :format => :json
1268           lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
1269         end
1270       end
1271     end
1272     context '作家権限がないとき' do
1273       before do
1274         sign_out @user
1275       end
1276       context 'html形式' do
1277         it 'ステータスコード302 Foundを返す' do
1278           get :new
1279           response.status.should eq 302
1280         end
1281         it 'サインインページへ遷移する' do
1282           get :new
1283           response.body.should redirect_to '/users/sign_in'
1284         end
1285       end
1286       context 'js形式' do
1287         it 'ステータスコード401 Unauthorizedを返す' do
1288           get :new, :format => :js
1289           response.status.should eq 401
1290         end
1291         it '応答メッセージにUnauthorizedを返す' do
1292           get :new, :format => :js
1293           response.message.should match(/Unauthorized/)
1294         end
1295       end
1296     end
1297   end
1298
1299   describe '新規作成に於いて' do
1300     before do
1301       sign_in @user
1302       @attr = {:original_picture => {:file => "abc\ndef\nghi"}}
1303       @imager = ImagerTest.load("abc\ndef\nghi")
1304       OriginalPicturesController.any_instance.stub(:set_image).with(any_args()).and_return("abc\ndef\nghi")
1305     end
1306     context 'つつがなく終わるとき' do
1307       before do
1308         PettanImager.stub(:load).with("abc\ndef\nghi").and_return(@imager)
1309       end
1310       context 'html形式' do
1311         it 'ステータスコード302 Foundを返す' do
1312           post :create, @attr
1313           response.status.should eq 302
1314         end
1315         it '作成された原画の表示ページへ遷移する' do
1316           post :create, @attr
1317           response.should redirect_to(OriginalPicture.last)
1318         end
1319       end
1320       context 'json形式' do
1321         before do
1322           @attr.merge!({:format => :json})
1323         end
1324         it 'ステータスコード200 OKを返す' do
1325           post :create, @attr
1326           response.should be_success 
1327         end
1328         it '作成された原画をjsonデータで返す' do
1329           post :create, @attr
1330           lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
1331         end
1332       end
1333     end
1334     context '作家権限がないとき' do
1335       before do
1336         sign_out @user
1337       end
1338       context 'html形式' do
1339         it 'ステータスコード302 Foundを返す' do
1340           post :create, @attr
1341           response.status.should eq 302
1342         end
1343         it 'サインインページへ遷移する' do
1344           post :create, @attr
1345           response.body.should redirect_to '/users/sign_in'
1346         end
1347       end
1348       context 'json形式' do
1349         before do
1350           @attr.merge!({:format => :json})
1351         end
1352         it 'ステータスコード401 Unauthorizedを返す' do
1353           post :create, @attr
1354           response.status.should eq 401
1355         end
1356         it '応答メッセージにUnauthorizedを返す' do
1357           post :create, @attr
1358           response.message.should match(/Unauthorized/)
1359         end
1360       end
1361     end
1362   end
1363
1364   describe '編集フォーム表示に於いて' do
1365     before do
1366       @pic = FactoryGirl.create :original_picture, :artist_id => @artist.id
1367       sign_in @user
1368       OriginalPicture.stub(:show).and_return(@pic)
1369     end
1370     context 'つつがなく終わるとき' do
1371       it 'ステータスコード200 OKを返す' do
1372         get :edit, :id => @pic.id
1373         response.should be_success 
1374       end
1375       context 'html形式' do
1376         it 'ページテンプレートeditを描画する' do
1377           get :edit, :id => @pic.id
1378           response.should render_template("edit")
1379         end
1380       end
1381       context 'js形式' do
1382         it '部分テンプレートedit.jsを描画する' do
1383           get :edit, :id => @pic.id, :format => :js
1384           response.should render_template("edit")
1385         end
1386       end
1387     end
1388     context '作家権限がないとき' do
1389       before do
1390         sign_out @user
1391       end
1392       context 'html形式' do
1393         it 'ステータスコード302 Foundを返す' do
1394           get :edit, :id => @pic.id
1395           response.status.should eq 302
1396         end
1397         it 'サインインページへ遷移する' do
1398           get :edit, :id => @pic.id
1399           response.body.should redirect_to '/users/sign_in'
1400         end
1401       end
1402       context 'js形式' do
1403         it 'ステータスコード401 Unauthorizedを返す' do
1404           get :edit, :id => @pic.id, :format => :js
1405           response.status.should eq 401
1406         end
1407         it '応答メッセージにUnauthorizedを返す' do
1408           get :edit, :id => @pic.id, :format => :js
1409           response.message.should match(/Unauthorized/)
1410         end
1411       end
1412     end
1413   end
1414
1415   describe '更新に於いて' do
1416     before do
1417       @op = FactoryGirl.create :original_picture, :artist_id => @artist.id
1418       OriginalPicture.stub(:edit).with(@op.id.to_s, @artist).and_return(@op)
1419       OriginalPicturesController.any_instance.stub(:set_image).with(any_args()).and_return("abc\ndef\nghi")
1420       sign_in @user
1421       @attr = {:file => "abc\ndef\nghi"}
1422       @imager = ImagerTest.load("abc\ndef\nghi")
1423     end
1424     context 'つつがなく終わるとき' do
1425       before do
1426         PettanImager.stub(:load).with("abc\ndef\nghi").and_return(@imager)
1427       end
1428       context 'html形式' do
1429         it 'ステータスコード302 Foundを返す' do
1430           put :update, :id => @op.id, :original_picture => @attr
1431           response.status.should eq 302
1432         end
1433         it '更新された原画の表示ページへ遷移する' do
1434           put :update, :id => @op.id, :original_picture => @attr
1435           response.should redirect_to(@pic)
1436         end
1437       end
1438       context 'json形式' do
1439         it 'ステータスコード200 OKを返す' do
1440           put :update, :id => @op.id, :original_picture => @attr, :format => :json
1441           response.should be_success 
1442         end
1443         it 'ページ本体は特に返さない' do
1444           put :update, :id => @op.id, :original_picture => @attr, :format => :json
1445           response.body.should match /./
1446         end
1447       end
1448     end
1449     context '作家権限がないとき' do
1450       before do
1451         sign_out @user
1452       end
1453       context 'html形式' do
1454         it 'ステータスコード302 Foundを返す' do
1455           put :update, :id => @op.id, :original_picture => @attr
1456           response.status.should eq 302
1457         end
1458         it 'サインインページへ遷移する' do
1459           put :update, :id => @op.id, :original_picture => @attr
1460           response.body.should redirect_to '/users/sign_in'
1461         end
1462       end
1463       context 'json形式' do
1464         it 'ステータスコード401 Unauthorizedを返す' do
1465           put :update, :id => @op.id, :original_picture => @attr, :format => :json
1466           response.status.should eq 401
1467         end
1468         it '応答メッセージにUnauthorizedを返す' do
1469           put :update, :id => @op.id, :original_picture => @attr, :format => :json
1470           response.message.should match(/Unauthorized/)
1471         end
1472       end
1473     end
1474     context '作家が絵師でないとき' do
1475       before do
1476         Author.any_instance.stub(:artist?).and_return(false)
1477       end
1478       context 'html形式' do
1479         it 'ステータスコード302 Foundを返す' do
1480           put :update, :id => @op.id, :original_picture => @attr
1481           response.status.should eq 302
1482         end
1483         it '絵師登録ページへ遷移する' do
1484           put :update, :id => @op.id, :original_picture => @attr
1485           response.should redirect_to new_artist_path
1486         end
1487       end
1488       context 'json形式' do
1489         it '例外403 forbiddenを返す' do
1490           lambda{
1491             put :update, :id => @op.id, :original_picture => @attr, :format => :json
1492           }.should raise_error(ActiveRecord::Forbidden)
1493         end
1494       end
1495     end
1496     context '検証、保存に失敗した' do
1497       before do
1498         PettanImager.stub(:load).with("abc\ndef\nghi").and_return(@imager)
1499         OriginalPicture.any_instance.stub(:store).with(@imager).and_return(false)
1500       end
1501       context 'html形式' do
1502         it 'ステータスコード200 Okを返す' do
1503           put :update, :id => @op.id, :original_picture => @attr
1504           response.status.should eq 200
1505         end
1506         it '編集ページを描画する' do
1507           put :update, :id => @op.id, :original_picture => @attr
1508           response.should render_template("edit")
1509         end
1510       end
1511       context 'json形式' do
1512         it 'ステータスコード422 unprocessable_entity を返す' do
1513           put :update, :id => @op.id, :original_picture => @attr, :format => :json
1514           response.status.should eq 422
1515         end
1516         it '応答メッセージUnprocessable Entityを返す' do
1517           put :update, :id => @op.id, :original_picture => @attr, :format => :json
1518           response.message.should match(/Unprocessable/)
1519         end
1520       end
1521     end
1522   end
1523
1524   describe '削除に於いて' do
1525     before do
1526       @op = FactoryGirl.create :original_picture, :artist_id => @artist.id
1527       sign_in @user
1528     end
1529     context '事前チェックしておく' do
1530       before do
1531         OriginalPicture.stub(:edit).with(any_args()).and_return @op
1532         OriginalPicture.any_instance.stub(:destroy_with_resource_picture).with(any_args()).and_return(true)
1533       end
1534       it '原画モデルに編集取得を問い合わせている' do
1535         OriginalPicture.should_receive(:edit).exactly(1)
1536         delete :destroy, :id => @op.id
1537       end
1538       it 'モデルに削除を依頼する' do
1539         OriginalPicture.any_instance.should_receive(:destroy_with_resource_picture).exactly(1)
1540         delete :destroy, :id => @op.id
1541       end
1542       it '@original_pictureにアレを取得している' do
1543         delete :destroy, :id => @op.id
1544         assigns(:original_picture).id.should eq(@op.id)
1545       end
1546     end
1547     context 'つつがなく終わるとき' do
1548       before do
1549         OriginalPicture.any_instance.stub(:destroy_with_resource_picture).with(any_args()).and_return(true)
1550       end
1551       context 'html形式' do
1552         it 'ステータスコード302 Foundを返す' do
1553           delete :destroy, :id => @op.id
1554           response.status.should eq 302
1555         end
1556         it '原画の一覧ページへ遷移する' do
1557           delete :destroy, :id => @op.id
1558           response.should redirect_to(original_pictures_path)
1559         end
1560       end
1561       context 'json形式' do
1562         it 'ステータスコード200 OKを返す' do
1563           delete :destroy, :id => @op.id, :format => :json
1564           response.should be_success 
1565         end
1566         it 'ページ本体は特に返さない' do
1567           delete :destroy, :id => @op.id, :format => :json
1568           response.body.should match /./
1569         end
1570       end
1571     end
1572     context '作家権限がないとき' do
1573       before do
1574         sign_out @user
1575       end
1576       it 'ステータスコード302 Foundを返す' do
1577         delete :destroy, :id => @op.id
1578         response.status.should eq 302
1579       end
1580       context 'html形式' do
1581         it 'サインインページへ遷移する' do
1582           delete :destroy, :id => @op.id
1583           response.body.should redirect_to '/users/sign_in'
1584         end
1585       end
1586       context 'json形式' do
1587         it '応答メッセージにUnauthorizedを返す' do
1588           delete :destroy, :id => @op.id, :format => :json
1589           response.message.should match(/Unauthorized/)
1590         end
1591       end
1592     end
1593     context '削除に失敗したとき' do
1594       before do
1595         OriginalPicture.any_instance.stub(:destroy_with_resource_picture).and_return(false)
1596       end
1597       context 'html形式' do
1598         it 'ステータスコード302 Foundを返す' do
1599           delete :destroy, :id => @op.id
1600           response.status.should eq 302
1601         end
1602         it 'その原画の詳細ページへ遷移する' do
1603           delete :destroy, :id => @op.id
1604           response.should redirect_to(original_picture_path(@op))
1605         end
1606       end
1607       context 'json形式' do
1608         it 'ステータスコード422 unprocessable_entity を返す' do
1609           delete :destroy, :id => @op.id, :format => :json
1610           response.status.should eq 422
1611         end
1612         it '応答メッセージUnprocessable Entityを返す' do
1613           delete :destroy, :id => @op.id, :format => :json
1614           response.message.should match(/Unprocessable/)
1615         end
1616       end
1617     end
1618   end
1619 end
1620
1621 end