OSDN Git Service

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