OSDN Git Service

782b8eba55dc27e7ff012a32541957f63f095211
[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   describe '一覧表示に於いて' do
17     before do
18       @op = FactoryGirl.create :original_picture, :artist_id => @artist.id
19       sign_in @user
20       OriginalPicture.stub(:mylist).and_return([@op, @op, @op])
21     end
22     context 'パラメータpageについて' do
23       it '@pageに値が入る' do
24         get :index, :page => 5
25         assigns(:page).should eq 5
26       end
27       it '省略されると@pageに1値が入る' do
28         get :index
29         assigns(:page).should eq 1
30       end
31       it '与えられたpage_sizeがセットされている' do
32         get :index, :page_size => 15
33         assigns(:page_size).should eq 15
34       end
35       it '省略されると@page_sizeにデフォルト値が入る' do
36         get :index
37         assigns(:page_size).should eq OriginalPicture.default_page_size
38       end
39       it '最大を超えると@page_sizeにデフォルト最大値が入る' do
40         get :index, :page_size => 1500
41         assigns(:page_size).should eq OriginalPicture.max_page_size
42       end
43       it '不正な値が入ると@page_sizeにデフォルト最大値が入る' do
44         get :index, :page_size => 0
45         assigns(:page_size).should eq OriginalPicture.default_page_size
46       end
47     end
48     context 'つつがなく終わるとき' do
49       it 'ステータスコード200 OKを返す' do
50         get :index
51         response.should be_success 
52       end
53       it '原画モデルにマイリストを問い合わせている' do
54         OriginalPicture.should_receive(:mylist).exactly(1)
55         get :index
56       end
57       it '@original_picturesにリストを取得している' do
58         get :index
59         assigns(:original_pictures).should have_at_least(3).items
60       end
61       context 'html形式' do
62         it 'indexテンプレートを描画する' do
63           get :index
64           response.should render_template("index")
65         end
66       end
67       context 'json形式' do
68         it 'jsonデータを返す' do
69           get :index, :format => :json
70           lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
71         end
72         it '原画モデルにjson一覧出力オプションを問い合わせている' do
73           OriginalPicture.should_receive(:list_json_opt).exactly(1)
74           get :index, :format => :json
75         end
76         it 'データがリスト構造になっている' do
77           get :index, :format => :json
78           json = JSON.parse response.body
79           json.should have_at_least(3).items
80         end
81         it 'リストの先頭くらいは原画っぽいものであって欲しい' do
82           get :index, :format => :json
83           json = JSON.parse response.body
84           json.first.has_key?("ext").should be_true
85           json.first.has_key?("md5").should be_true
86           json.first.has_key?("artist_id").should be_true
87           json.first.has_key?("width").should be_true
88         end
89       end
90     end
91     context '作家権限がないとき' do
92       before do
93         sign_out @user
94       end
95       context 'html形式' do
96         it 'ステータスコード302 Foundを返す' do
97           get :index
98           response.status.should eq 302
99         end
100         it 'サインインページへ遷移する' do
101           get :index
102           response.should redirect_to '/users/sign_in'
103         end
104       end
105       context 'json形式' do
106         it 'ステータスコード401 Unauthorizedを返す' do
107           get :index, :format => :json
108           response.status.should eq 401
109         end
110         it '応答メッセージにUnauthorizedを返す' do
111           get :index, :format => :json
112           response.message.should match(/Unauthorized/)
113         end
114       end
115     end
116     context '作家が絵師でないとき' do
117       before do
118         Author.any_instance.stub(:artist?).and_return(false)
119       end
120       context 'html形式' do
121         it 'ステータスコード302 Foundを返す' do
122           get :index
123           response.status.should eq 302
124         end
125         it '絵師登録ページへ遷移する' do
126           get :index
127           response.should redirect_to new_artist_path
128         end
129       end
130       context 'json形式' do
131         it '例外403 forbiddenを返す' do
132           lambda{
133             get :index, :format => :json
134           }.should raise_error(ActiveRecord::Forbidden)
135         end
136       end
137     end
138   end
139   
140   describe '単体表示に於いて' do
141     before do
142       @pic = FactoryGirl.create :original_picture, :artist_id => @artist.id
143       sign_in @user
144       OriginalPicture.stub(:show).and_return(@pic)
145     end
146     context 'つつがなく終わるとき' do
147       it 'ステータスコード200 OKを返す' do
148         get :show, :id => @pic.id
149         response.should be_success
150       end
151       it '原画モデルに単体取得を問い合わせている' do
152         OriginalPicture.should_receive(:show).exactly(1)
153         get :show
154       end
155       it '@original_pictureにアレを取得している' do
156         get :show, :id => @pic.id
157         assigns(:original_picture).id.should eq(@pic.id)
158       end
159       context 'html形式' do
160         it 'showテンプレートを描画する' do
161           get :show, :id => @pic.id
162           response.should render_template("show")
163         end
164       end
165       context 'json形式' do
166         it 'jsonデータを返す' do
167           get :show, :id => @pic.id, :format => :json
168           lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
169         end
170         it '原画モデルにjson単体出力オプションを問い合わせている' do
171           OriginalPicture.should_receive(:show_json_opt).exactly(1)
172           get :show, :id => @pic.id, :format => :json
173         end
174         it 'データがアレになっている' do
175           get :show, :id => @pic.id, :format => :json
176           json = JSON.parse response.body
177           json["ext"].should match(/png/)
178           json["md5"].should be_true
179           json["artist_id"].should be_true
180           json["width"].should be_true
181         end
182       end
183       #画像送信では、send_dataにスタブをおいてテストしたいが、ここに噛ませると
184       #renderが働かず、エラーとなってしまう。そこで、原画のファイル取得部分に
185       #スタブをおいてsend_dataがデータを返す体裁でテストする。
186       context 'png形式' do
187         before do
188           OriginalPicture.any_instance.stub(:mime_type).and_return('image/png')
189           OriginalPicture.any_instance.stub(:restore).and_return('aaa')
190         end
191         it '画像モデルに画像データを問い合わせる' do
192           OriginalPicture.any_instance.should_receive(:restore).exactly(1)
193           get :show, :id => @pic.id, :format => :png
194         end
195         it '画像モデルにMimeTypeを問い合わせる' do
196           OriginalPicture.any_instance.should_receive(:mime_type).exactly(1)
197           get :show, :id => @pic.id, :format => :png
198         end
199         it '画像を送信する' do
200           get :show, :id => @pic.id, :format => :png
201           response.body.should eq 'aaa'
202         end
203       end
204       context 'gif形式' do
205         before do
206           OriginalPicture.any_instance.stub(:mime_type).and_return('image/gif')
207           OriginalPicture.any_instance.stub(:restore).and_return('bbb')
208         end
209         it '画像モデルに画像データを問い合わせる' do
210           OriginalPicture.any_instance.should_receive(:restore).exactly(1)
211           get :show, :id => @pic.id, :format => :gif
212         end
213         it '画像モデルにMimeTypeを問い合わせる' do
214           OriginalPicture.any_instance.should_receive(:mime_type).exactly(1)
215           get :show, :id => @pic.id, :format => :gif
216         end
217         it '画像を送信する' do
218           get :show, :id => @pic.id, :format => :gif
219           response.body.should eq 'bbb'
220         end
221       end
222       context 'jpeg形式' do
223         before do
224           OriginalPicture.any_instance.stub(:mime_type).and_return('image/jpeg')
225           OriginalPicture.any_instance.stub(:restore).and_return('ccc')
226         end
227         it '画像モデルに画像データを問い合わせる' do
228           OriginalPicture.any_instance.should_receive(:restore).exactly(1)
229           get :show, :id => @pic.id, :format => :jpeg
230         end
231         it '画像モデルにMimeTypeを問い合わせる' do
232           OriginalPicture.any_instance.should_receive(:mime_type).exactly(1)
233           get :show, :id => @pic.id, :format => :jpeg
234         end
235         it '画像を送信する' do
236           get :show, :id => @pic.id, :format => :jpeg
237           response.body.should eq 'ccc'
238         end
239       end
240     end
241     context '作家権限がないとき' do
242       before do
243         sign_out @user
244       end
245       context 'html形式' do
246         it 'ステータスコード302 Foundを返す' do
247           get :show, :id => @pic.id
248           response.status.should eq 302
249         end
250         it 'サインインページへ遷移する' do
251           get :show, :id => @pic.id
252           response.body.should redirect_to '/users/sign_in'
253         end
254       end
255       context 'json形式' do
256         it 'ステータスコード401 Unauthorizedを返す' do
257           get :show, :id => @pic.id, :format => :json
258           response.status.should eq 401
259         end
260         it '応答メッセージにUnauthorizedを返す' do
261           get :show, :id => @pic.id, :format => :json
262           response.message.should match(/Unauthorized/)
263         end
264       end
265     end
266     context '作家が絵師でないとき' do
267       before do
268         Author.any_instance.stub(:artist?).and_return(false)
269       end
270       context 'html形式' do
271         it 'ステータスコード302 Foundを返す' do
272           get :show, :id => @pic.id
273           response.status.should eq 302
274         end
275         it '絵師登録ページへ遷移する' do
276           get :show, :id => @pic.id
277           response.should redirect_to new_artist_path
278         end
279       end
280       context 'json形式' do
281         it '例外403 forbiddenを返す' do
282           lambda{
283             get :show, :id => @pic.id, :format => :json
284           }.should raise_error(ActiveRecord::Forbidden)
285         end
286       end
287     end
288 =begin
289     context '対象原画がないとき' do
290       before do
291         OriginalPicture.unstub(:show)
292       end
293       context 'html形式' do
294         it '例外404 not_foundを返す' do
295           lambda{
296             get :show, :id => 0
297           }.should raise_error(ActiveRecord::RecordNotFound)
298         end
299       end
300       context 'json形式' do
301         it '例外404 not_foundを返す' do
302           lambda{ 
303             get :show, :id => 0, :format => :json
304           }.should raise_error(ActiveRecord::RecordNotFound)
305         end
306       end
307     end
308     context '他人の原画を見ようとしたとき' do
309       before do
310         OriginalPicture.stub(:show).and_return(@pic)
311         OriginalPicture.any_instance.stub(:own?).with(any_args()).and_return(false)
312       end
313       context 'html形式' do
314         it '例外403 forbiddenを返す' do
315           lambda{
316             get :show, :id => @pic.id
317           }.should raise_error(ActiveRecord::Forbidden)
318         end
319       end
320       context 'json形式' do
321         it '例外403 forbiddenを返す' do
322           lambda{
323             get :show, :id => @pic.id, :format => :json
324           }.should raise_error(ActiveRecord::Forbidden)
325         end
326       end
327     end
328 =end
329   end
330
331   describe '履歴一覧表示に於いて' do
332     before do
333       @op = FactoryGirl.create :original_picture, :artist_id => @artist.id
334       @p = FactoryGirl.create :picture, :artist_id => @artist.id, :license_id => @license.id, :revision => 0,
335         :original_picture_id => @op.id
336       sign_in @user
337       OriginalPicture.stub(:show).and_return(@op)
338       OriginalPicture.any_instance.stub(:history).and_return([@p, @p, @p])
339     end
340     context 'つつがなく終わるとき' do
341       it 'ステータスコード200 OKを返す' do
342         get :history, :id => @op.id
343         response.should be_success 
344       end
345       it '原画モデルに単体取得を問い合わせている' do
346         OriginalPicture.should_receive(:show).exactly(1)
347         get :history, :id => @op.id
348       end
349       it '自身に履歴一覧を問い合わせている' do
350         OriginalPicture.any_instance.should_receive(:history).exactly(1)
351         get :history, :id => @op.id
352       end
353       it '@historyにリストを取得している' do
354         get :history, :id => @op.id
355         assigns(:history).should have_at_least(3).items
356       end
357       context 'html形式' do
358         it 'historyテンプレートを描画する' do
359           get :history, :id => @op.id
360           response.should render_template("history")
361         end
362       end
363       context 'json形式' do
364         it 'jsonデータを返す' do
365           get :history, :id => @op.id, :format => :json
366           lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
367         end
368         it 'データがリスト構造になっている' do
369           get :history, :id => @op.id, :format => :json
370           json = JSON.parse response.body
371           json.should have_at_least(3).items
372         end
373         it 'リストの先頭くらいは原画っぽいものであって欲しい' do
374           get :history, :id => @op.id, :format => :json
375           json = JSON.parse response.body
376           json.first.has_key?("ext").should be_true
377         end
378       end
379     end
380     context '作家権限がないとき' do
381       before do
382         sign_out @user
383       end
384       context 'html形式' do
385         it 'ステータスコード302 Foundを返す' do
386           get :history, :id => @op.id
387           response.status.should eq 302
388         end
389         it 'サインインページへ遷移する' do
390           get :history, :id => @op.id
391           response.should redirect_to '/users/sign_in'
392         end
393       end
394       context 'json形式' do
395         it 'ステータスコード401 Unauthorizedを返す' do
396           get :history, :id => @op.id, :format => :json
397           response.status.should eq 401
398         end
399         it '応答メッセージにUnauthorizedを返す' do
400           get :history, :id => @op.id, :format => :json
401           response.message.should match(/Unauthorized/)
402         end
403       end
404     end
405     context '作家が絵師でないとき' do
406       before do
407         Author.any_instance.stub(:artist?).and_return(false)
408       end
409       context 'html形式' do
410         it 'ステータスコード302 Foundを返す' do
411           get :history, :id => @op.id
412           response.status.should eq 302
413         end
414         it '絵師登録ページへ遷移する' do
415           get :history, :id => @op.id
416           response.should redirect_to new_artist_path
417         end
418       end
419       context 'json形式' do
420         it '例外403 forbiddenを返す' do
421           lambda{
422             get :history, :id => @op.id, :format => :json
423           }.should raise_error(ActiveRecord::Forbidden)
424         end
425       end
426     end
427   end
428   
429   describe '新規作成フォーム表示に於いて' do
430     before do
431       sign_in @user
432     end
433     context 'つつがなく終わるとき' do
434       it '@original_pictureに新規データを用意している' do
435         get :new
436         assigns(:original_picture).should be_a_new(OriginalPicture)
437       end
438       it '原画モデルにデフォルト値補充を依頼している' do
439         OriginalPicture.any_instance.should_receive(:supply_default).exactly(1)
440         get :new
441       end
442       context 'html形式' do
443         it 'ステータスコード200 OKを返す' do
444           get :new
445           response.should be_success 
446         end
447         it 'ページテンプレートnewを描画する' do
448           get :new
449           response.should render_template("new")
450         end
451       end
452       context 'js形式' do
453         it 'ステータスコード200 OKを返す' do
454           get :new, :format => :js
455           response.should be_success 
456         end
457         it '部分テンプレートnew.jsを描画する' do
458           get :new, :format => :js
459           response.should render_template("new")
460         end
461       end
462       context 'json形式' do
463         it 'jsonデータを返す' do
464           get :new, :format => :json
465           lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
466         end
467         it '原画モデルにjson単体出力オプションを問い合わせている' do
468           OriginalPicture.should_receive(:show_json_opt).exactly(1)
469           get :new, :format => :json
470         end
471       end
472     end
473     context '作家権限がないとき' do
474       before do
475         sign_out @user
476       end
477       context 'html形式' do
478         it 'ステータスコード302 Foundを返す' do
479           get :new
480           response.status.should eq 302
481         end
482         it 'サインインページへ遷移する' do
483           get :new
484           response.body.should redirect_to '/users/sign_in'
485         end
486       end
487       context 'js形式' do
488         it 'ステータスコード401 Unauthorizedを返す' do
489           get :new, :format => :js
490           response.status.should eq 401
491         end
492         it '応答メッセージにUnauthorizedを返す' do
493           get :new, :format => :js
494           response.message.should match(/Unauthorized/)
495         end
496       end
497     end
498     context '作家が絵師でないとき' do
499       before do
500         Author.any_instance.stub(:artist?).and_return(false)
501       end
502       context 'html形式' do
503         it 'ステータスコード302 Foundを返す' do
504           get :new
505           response.status.should eq 302
506         end
507         it '絵師登録ページへ遷移する' do
508           get :new
509           response.should redirect_to new_artist_path
510         end
511       end
512       context 'js形式' do
513         it 'ステータスコード200 Okを返す' do
514           get :new, :format => :js
515           response.status.should eq 200
516         end
517         it '絵師登録部分テンプレートartists/new.jsを描画する' do
518           get :new, :format => :js
519           response.should render_template("artists/new")
520         end
521       end
522       context 'json形式' do
523         it '例外403 forbiddenを返す' do
524           lambda{
525           get :new, :format => :json
526           }.should raise_error(ActiveRecord::Forbidden)
527         end
528       end
529     end
530   end
531
532   describe '新規作成に於いて' do
533     before do
534       sign_in @user
535       @attr = {:original_picture => {:file => "abc\ndef\nghi"}}
536       @imager = ImagerTest.load("abc\ndef\nghi")
537       OriginalPicturesController.any_instance.stub(:set_image).with(any_args()).and_return("abc\ndef\nghi")
538     end
539     context '事前チェックしておく' do
540       before do
541         PettanImager.stub(:load).with("abc\ndef\nghi").and_return(@imager)
542         OriginalPicture.any_instance.stub(:store).with(@imager).and_return(true)
543       end
544       it '画像ライブラリをロードする' do
545         PettanImager.should_receive(:load).with(any_args()).exactly(1)
546         post :create, @attr
547       end
548       it '原画モデルにデフォルト値補充を依頼している' do
549         OriginalPicture.any_instance.should_receive(:supply_default).with(any_args()).exactly(1)
550         post :create, @attr
551       end
552       it '原画モデルに上書き補充を依頼している' do
553         OriginalPicture.any_instance.should_receive(:overwrite).with(@artist).exactly(1)
554         post :create, @attr
555       end
556       it 'モデルに保存依頼する' do
557         OriginalPicture.any_instance.should_receive(:store).with(@imager).exactly(1)
558         post :create, @attr
559       end
560     end
561     context 'つつがなく終わるとき' do
562       before do
563         PettanImager.stub(:load).with("abc\ndef\nghi").and_return(@imager)
564       end
565       it "@original_pictureに作成された原画を保持している" do
566         post :create, @attr
567         assigns(:original_picture).should be_a(OriginalPicture)
568       end
569       context 'html形式' do
570         it 'ステータスコード302 Foundを返す' do
571           post :create, @attr
572           response.status.should eq 302
573         end
574         it '作成された原画の表示ページへ遷移する' do
575           post :create, @attr
576           response.should redirect_to(OriginalPicture.last)
577         end
578       end
579       context 'json形式' do
580         before do
581           @attr.merge!({:format => :json})
582         end
583         it 'ステータスコード200 OKを返す' do
584           post :create, @attr
585           response.should be_success 
586         end
587         it '作成された原画をjsonデータで返す' do
588           post :create, @attr
589           lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
590         end
591         it '原画モデルにjson単体出力オプションを問い合わせている' do
592           OriginalPicture.should_receive(:show_json_opt).exactly(1)
593           post :create, @attr
594         end
595         it 'データがアレになっている' do
596           post :create, @attr
597           json = JSON.parse response.body
598           json["ext"].should match(/png/)
599           json["md5"].should be_true
600           json["artist_id"].should be_true
601           json["width"].should be_true
602         end
603       end
604     end
605     context '作家権限がないとき' do
606       before do
607         sign_out @user
608       end
609       context 'html形式' do
610         it 'ステータスコード302 Foundを返す' do
611           post :create, @attr
612           response.status.should eq 302
613         end
614         it 'サインインページへ遷移する' do
615           post :create, @attr
616           response.body.should redirect_to '/users/sign_in'
617         end
618       end
619       context 'json形式' do
620         before do
621           @attr.merge!({:format => :json})
622         end
623         it 'ステータスコード401 Unauthorizedを返す' do
624           post :create, @attr
625           response.status.should eq 401
626         end
627         it '応答メッセージにUnauthorizedを返す' do
628           post :create, @attr
629           response.message.should match(/Unauthorized/)
630         end
631       end
632     end
633     context '作家が絵師でないとき' do
634       before do
635         Author.any_instance.stub(:artist?).and_return(false)
636       end
637       context 'html形式' do
638         it 'ステータスコード302 Foundを返す' do
639           post :create, @attr
640           response.status.should eq 302
641         end
642         it '絵師登録ページへ遷移する' do
643           post :create, @attr
644           response.should redirect_to new_artist_path
645         end
646       end
647       context 'json形式' do
648         before do
649           @attr.merge!({:format => :json})
650         end
651         it '例外403 forbiddenを返す' do
652           lambda{
653             post :create, @attr
654           }.should raise_error(ActiveRecord::Forbidden)
655         end
656       end
657     end
658     context '検証、保存に失敗した' do
659       before do
660         PettanImager.stub(:load).with("abc\ndef\nghi").and_return(@imager)
661         OriginalPicture.any_instance.stub(:store).with(@imager).and_return(false)
662       end
663       it "未保存の原画を保持している" do
664         post :create, @attr
665         assigns(:original_picture).should be_a_new(OriginalPicture)
666       end
667       context 'html形式' do
668         it 'ステータスコード200 OKを返す' do
669           post :create, @attr
670           response.status.should eq 200
671         end
672         it '新規ページを描画する' do
673           post :create, @attr
674           response.should render_template("new")
675         end
676       end
677       context 'json形式' do
678         before do
679           @attr.merge!({:format => :json})
680         end
681         it 'ステータスコード422 unprocessable_entity を返す' do
682           post :create, @attr
683           response.status.should eq 422
684         end
685         it '応答メッセージUnprocessable Entityを返す' do
686           post :create, @attr
687           response.message.should match(/Unprocessable/)
688         end
689       end
690     end
691   end
692
693   describe '編集フォーム表示に於いて' do
694     before do
695       @pic = FactoryGirl.create :original_picture, :artist_id => @artist.id
696       sign_in @user
697       OriginalPicture.stub(:show).and_return(@pic)
698     end
699     context 'つつがなく終わるとき' do
700       it 'ステータスコード200 OKを返す' do
701         get :edit, :id => @pic.id
702         response.should be_success 
703       end
704       it '原画モデルに編集取得を問い合わせている' do
705         OriginalPicture.should_receive(:edit).exactly(1)
706         get :edit, :id => @pic.id
707       end
708       it '@original_pictureにデータを用意している' do
709         get :edit, :id => @pic.id
710         assigns(:original_picture).should eq @pic
711       end
712       context 'html形式' do
713         it 'ページテンプレートeditを描画する' do
714           get :edit, :id => @pic.id
715           response.should render_template("edit")
716         end
717       end
718       context 'js形式' do
719         it '部分テンプレートedit.jsを描画する' do
720           get :edit, :id => @pic.id, :format => :js
721           response.should render_template("edit")
722         end
723       end
724     end
725     context '作家権限がないとき' do
726       before do
727         sign_out @user
728       end
729       context 'html形式' do
730         it 'ステータスコード302 Foundを返す' do
731           get :edit, :id => @pic.id
732           response.status.should eq 302
733         end
734         it 'サインインページへ遷移する' do
735           get :edit, :id => @pic.id
736           response.body.should redirect_to '/users/sign_in'
737         end
738       end
739       context 'js形式' do
740         it 'ステータスコード401 Unauthorizedを返す' do
741           get :edit, :id => @pic.id, :format => :js
742           response.status.should eq 401
743         end
744         it '応答メッセージにUnauthorizedを返す' do
745           get :edit, :id => @pic.id, :format => :js
746           response.message.should match(/Unauthorized/)
747         end
748       end
749     end
750     context '作家が絵師でないとき' do
751       before do
752         Author.any_instance.stub(:artist?).and_return(false)
753       end
754       context 'html形式' do
755         it 'ステータスコード302 Foundを返す' do
756           get :edit, :id => @pic.id
757           response.status.should eq 302
758         end
759         it '絵師登録ページへ遷移する' do
760           get :edit, :id => @pic.id
761           response.should redirect_to new_artist_path
762         end
763       end
764       context 'js形式' do
765         it 'ステータスコード200 Okを返す' do
766           get :edit, :id => @pic.id, :format => :js
767           response.status.should eq 200
768         end
769         it '絵師登録部分テンプレートartists/new.jsを描画する' do
770           get :edit, :id => @pic.id, :format => :js
771           response.should render_template("artists/new")
772         end
773       end
774     end
775   end
776
777   describe '更新に於いて' do
778     before do
779       @op = FactoryGirl.create :original_picture, :artist_id => @artist.id
780       OriginalPicture.stub(:edit).with(@op.id.to_s, @artist).and_return(@op)
781       OriginalPicturesController.any_instance.stub(:set_image).with(any_args()).and_return("abc\ndef\nghi")
782       sign_in @user
783       @attr = {:file => "abc\ndef\nghi"}
784       @imager = ImagerTest.load("abc\ndef\nghi")
785     end
786     context '事前チェックしておく' do
787       before do
788         PettanImager.stub(:load).with("abc\ndef\nghi").and_return(@imager)
789         OriginalPicture.any_instance.stub(:store).with(@imager).and_return(true)
790       end
791       it '画像ライブラリをロードする' do
792         PettanImager.should_receive(:load).with(any_args()).exactly(1)
793         put :update, :id => @op.id, :original_picture => @attr
794       end
795       it '原画モデルに編集取得を問い合わせている' do
796         OriginalPicture.should_receive(:edit).with(@op.id.to_s, @artist).exactly(1)
797         put :update, :id => @op.id, :original_picture => @attr
798       end
799       it '原画モデルに上書き補充を依頼している' do
800         OriginalPicture.any_instance.should_receive(:overwrite).with(@artist).exactly(1)
801         put :update, :id => @op.id, :original_picture => @attr
802       end
803       it 'モデルに更新を依頼する' do
804         OriginalPicture.any_instance.should_receive(:store).with(@imager).exactly(1)
805         put :update, :id => @op.id, :original_picture => @attr
806       end
807     end
808     context 'つつがなく終わるとき' do
809       before do
810         PettanImager.stub(:load).with("abc\ndef\nghi").and_return(@imager)
811       end
812       it '@original_pictureにアレを取得している' do
813         put :update, :id => @op.id, :original_picture => @attr
814         assigns(:original_picture).should eq(@op)
815       end
816       context 'html形式' do
817         it 'ステータスコード302 Foundを返す' do
818           put :update, :id => @op.id, :original_picture => @attr
819           response.status.should eq 302
820         end
821         it '更新された原画の表示ページへ遷移する' do
822           put :update, :id => @op.id, :original_picture => @attr
823           response.should redirect_to(@pic)
824         end
825       end
826       context 'json形式' do
827         it 'ステータスコード200 OKを返す' do
828           put :update, :id => @op.id, :original_picture => @attr, :format => :json
829           response.should be_success 
830         end
831         it 'ページ本体は特に返さない' do
832           put :update, :id => @op.id, :original_picture => @attr, :format => :json
833           response.body.should match /./
834         end
835       end
836     end
837     context '作家権限がないとき' do
838       before do
839         sign_out @user
840       end
841       context 'html形式' do
842         it 'ステータスコード302 Foundを返す' do
843           put :update, :id => @op.id, :original_picture => @attr
844           response.status.should eq 302
845         end
846         it 'サインインページへ遷移する' do
847           put :update, :id => @op.id, :original_picture => @attr
848           response.body.should redirect_to '/users/sign_in'
849         end
850       end
851       context 'json形式' do
852         it 'ステータスコード401 Unauthorizedを返す' do
853           put :update, :id => @op.id, :original_picture => @attr, :format => :json
854           response.status.should eq 401
855         end
856         it '応答メッセージにUnauthorizedを返す' do
857           put :update, :id => @op.id, :original_picture => @attr, :format => :json
858           response.message.should match(/Unauthorized/)
859         end
860       end
861     end
862     context '作家が絵師でないとき' do
863       before do
864         Author.any_instance.stub(:artist?).and_return(false)
865       end
866       context 'html形式' do
867         it 'ステータスコード302 Foundを返す' do
868           put :update, :id => @op.id, :original_picture => @attr
869           response.status.should eq 302
870         end
871         it '絵師登録ページへ遷移する' do
872           put :update, :id => @op.id, :original_picture => @attr
873           response.should redirect_to new_artist_path
874         end
875       end
876       context 'json形式' do
877         it '例外403 forbiddenを返す' do
878           lambda{
879             put :update, :id => @op.id, :original_picture => @attr, :format => :json
880           }.should raise_error(ActiveRecord::Forbidden)
881         end
882       end
883     end
884     context '検証、保存に失敗した' do
885       before do
886         PettanImager.stub(:load).with("abc\ndef\nghi").and_return(@imager)
887         OriginalPicture.any_instance.stub(:store).with(@imager).and_return(false)
888       end
889       context 'html形式' do
890         it 'ステータスコード200 Okを返す' do
891           put :update, :id => @op.id, :original_picture => @attr
892           response.status.should eq 200
893         end
894         it '編集ページを描画する' do
895           put :update, :id => @op.id, :original_picture => @attr
896           response.should render_template("edit")
897         end
898       end
899       context 'json形式' do
900         it 'ステータスコード422 unprocessable_entity を返す' do
901           put :update, :id => @op.id, :original_picture => @attr, :format => :json
902           response.status.should eq 422
903         end
904         it '応答メッセージUnprocessable Entityを返す' do
905           put :update, :id => @op.id, :original_picture => @attr, :format => :json
906           response.message.should match(/Unprocessable/)
907         end
908       end
909     end
910   end
911
912   describe '削除に於いて' do
913     before do
914       @op = FactoryGirl.create :original_picture, :artist_id => @artist.id
915       sign_in @user
916     end
917     context '事前チェックしておく' do
918       before do
919         OriginalPicture.stub(:edit).with(any_args()).and_return @op
920         OriginalPicture.any_instance.stub(:destroy_with_resource_picture).with(any_args()).and_return(true)
921       end
922       it '原画モデルに編集取得を問い合わせている' do
923         OriginalPicture.should_receive(:edit).exactly(1)
924         delete :destroy, :id => @op.id
925       end
926       it 'モデルに削除を依頼する' do
927         OriginalPicture.any_instance.should_receive(:destroy_with_resource_picture).exactly(1)
928         delete :destroy, :id => @op.id
929       end
930       it '@original_pictureにアレを取得している' do
931         delete :destroy, :id => @op.id
932         assigns(:original_picture).id.should eq(@op.id)
933       end
934     end
935     context 'つつがなく終わるとき' do
936       before do
937         OriginalPicture.any_instance.stub(:destroy_with_resource_picture).with(any_args()).and_return(true)
938       end
939       context 'html形式' do
940         it 'ステータスコード302 Foundを返す' do
941           delete :destroy, :id => @op.id
942           response.status.should eq 302
943         end
944         it '原画の一覧ページへ遷移する' do
945           delete :destroy, :id => @op.id
946           response.should redirect_to(original_pictures_path)
947         end
948       end
949       context 'json形式' do
950         it 'ステータスコード200 OKを返す' do
951           delete :destroy, :id => @op.id, :format => :json
952           response.should be_success 
953         end
954         it 'ページ本体は特に返さない' do
955           delete :destroy, :id => @op.id, :format => :json
956           response.body.should match /./
957         end
958       end
959     end
960     context '作家権限がないとき' do
961       before do
962         sign_out @user
963       end
964       it 'ステータスコード302 Foundを返す' do
965         delete :destroy, :id => @op.id
966         response.status.should eq 302
967       end
968       context 'html形式' do
969         it 'サインインページへ遷移する' do
970           delete :destroy, :id => @op.id
971           response.body.should redirect_to '/users/sign_in'
972         end
973       end
974       context 'json形式' do
975         it '応答メッセージにUnauthorizedを返す' do
976           delete :destroy, :id => @op.id, :format => :json
977           response.message.should match(/Unauthorized/)
978         end
979       end
980     end
981     context '削除に失敗したとき' do
982       before do
983         OriginalPicture.any_instance.stub(:destroy_with_resource_picture).and_return(false)
984       end
985       context 'html形式' do
986         it 'ステータスコード302 Foundを返す' do
987           delete :destroy, :id => @op.id
988           response.status.should eq 302
989         end
990         it 'その原画の詳細ページへ遷移する' do
991           delete :destroy, :id => @op.id
992           response.should redirect_to(original_picture_path(@op))
993         end
994       end
995       context 'json形式' do
996         it 'ステータスコード422 unprocessable_entity を返す' do
997           delete :destroy, :id => @op.id, :format => :json
998           response.status.should eq 422
999         end
1000         it '応答メッセージUnprocessable Entityを返す' do
1001           delete :destroy, :id => @op.id, :format => :json
1002           response.message.should match(/Unprocessable/)
1003         end
1004       end
1005     end
1006   end
1007 end