OSDN Git Service

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