OSDN Git Service

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