OSDN Git Service

t#29400:itr3?
[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     FactoryGirl.create :admin
8     @user = FactoryGirl.create( :user_yas)
9     @author = @user.author
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
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     end
536     context '事前チェックしておく' do
537       before do
538         OriginalPicture.any_instance.stub(:store).with(any_args()).and_return(true)
539       end
540       it 'モデルに保存依頼する' do
541         OriginalPicture.any_instance.should_receive(:store).exactly(1)
542         post :create, :original_picture => FactoryGirl.attributes_for(:original_picture)
543       end
544       it "@original_pictureに作成された原画を保持している" do
545         post :create, :original_picture => FactoryGirl.attributes_for(:original_picture)
546         assigns(:original_picture).should be_a(OriginalPicture)
547       end
548     end
549     context 'つつがなく終わるとき' do
550       before do
551         OriginalPicture.any_instance.stub(:store).with(any_args()).and_return {
552           assigns(:original_picture).attributes = FactoryGirl.attributes_for(:original_picture, :artist_id => @artist.id, :ext => 'jpeg')
553           assigns(:original_picture).save
554           true
555         }
556       end
557       it "作成された原画がDBにある" do
558         post :create, :original_picture => FactoryGirl.attributes_for(:original_picture)
559         assigns(:original_picture).should_not be_a_new OriginalPicture
560       end
561       context 'html形式' do
562         it 'ステータスコード302 Foundを返す' do
563           post :create, :original_picture => FactoryGirl.attributes_for(:original_picture)
564           response.status.should eq 302
565         end
566         it '作成された原画の表示ページへ遷移する' do
567           post :create, :original_picture => FactoryGirl.attributes_for(:original_picture)
568           response.should redirect_to(OriginalPicture.last)
569         end
570       end
571       context 'json形式' do
572         it 'ステータスコード200 OKを返す' do
573           post :create, :original_picture => FactoryGirl.attributes_for(:original_picture), :format => :json
574           response.should be_success 
575         end
576         it '作成された原画をjsonデータで返す' do
577           post :create, :original_picture => FactoryGirl.attributes_for(:original_picture), :format => :json
578           lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
579         end
580         it '原画モデルにjson単体出力オプションを問い合わせている' do
581           OriginalPicture.should_receive(:show_json_opt).exactly(1)
582           post :create, :original_picture => FactoryGirl.attributes_for(:original_picture), :format => :json
583         end
584         it 'データがアレになっている' do
585           post :create, :original_picture => FactoryGirl.attributes_for(:original_picture, :ext => 'jpeg'), :format => :json
586           json = JSON.parse response.body
587           json["ext"].should match(/jpeg/)
588           json["md5"].should be_true
589           json["artist_id"].should be_true
590           json["width"].should be_true
591         end
592       end
593     end
594     context '作家権限がないとき' do
595       before do
596         sign_out @user
597       end
598       context 'html形式' do
599         it 'ステータスコード302 Foundを返す' do
600           post :create, :original_picture => FactoryGirl.attributes_for(:original_picture)
601           response.status.should eq 302
602         end
603         it 'サインインページへ遷移する' do
604           post :create, :original_picture => FactoryGirl.attributes_for(:original_picture)
605           response.body.should redirect_to '/users/sign_in'
606         end
607       end
608       context 'json形式' do
609         it 'ステータスコード401 Unauthorizedを返す' do
610           post :create, :original_picture => FactoryGirl.attributes_for(:original_picture), :format => :json
611           response.status.should eq 401
612         end
613         it '応答メッセージにUnauthorizedを返す' do
614           post :create, :original_picture => FactoryGirl.attributes_for(:original_picture), :format => :json
615           response.message.should match(/Unauthorized/)
616         end
617       end
618     end
619     context '作家が絵師でないとき' do
620       before do
621         Author.any_instance.stub(:artist?).and_return(false)
622       end
623       context 'html形式' do
624         it 'ステータスコード302 Foundを返す' do
625           post :create, :original_picture => FactoryGirl.attributes_for(:original_picture)
626           response.status.should eq 302
627         end
628         it '絵師登録ページへ遷移する' do
629           post :create, :original_picture => FactoryGirl.attributes_for(:original_picture)
630           response.should redirect_to new_artist_path
631         end
632       end
633       context 'json形式' do
634         it '例外403 forbiddenを返す' do
635           lambda{
636             post :create, :original_picture => FactoryGirl.attributes_for(:original_picture), :format => :json
637           }.should raise_error(ActiveRecord::Forbidden)
638         end
639       end
640     end
641     context '検証、保存に失敗した' do
642       before do
643         OriginalPicture.any_instance.stub(:store).and_return(false)
644       end
645       it "未保存の原画を保持している" do
646         post :create, :original_picture => FactoryGirl.attributes_for(:original_picture)
647         assigns(:original_picture).should be_a_new(OriginalPicture)
648       end
649       context 'html形式' do
650         it 'ステータスコード200 OKを返す' do
651           post :create, :original_picture => FactoryGirl.attributes_for(:original_picture)
652           response.status.should eq 200
653         end
654         it '新規ページを描画する' do
655           post :create, :original_picture => FactoryGirl.attributes_for(:original_picture)
656           response.should render_template("new")
657         end
658       end
659       context 'json形式' do
660         it 'ステータスコード422 unprocessable_entity を返す' do
661           post :create, :original_picture => FactoryGirl.attributes_for(:original_picture), :format => :json
662           response.status.should eq 422
663         end
664         it '応答メッセージUnprocessable Entityを返す' do
665           post :create, :original_picture => FactoryGirl.attributes_for(:original_picture), :format => :json
666           response.message.should match(/Unprocessable/)
667         end
668       end
669     end
670   end
671
672   describe '編集フォーム表示に於いて' do
673     before do
674       @pic = FactoryGirl.create :original_picture, :artist_id => @artist.id
675       sign_in @user
676       OriginalPicture.stub(:show).and_return(@pic)
677     end
678     context 'つつがなく終わるとき' do
679       it 'ステータスコード200 OKを返す' do
680         get :edit, :id => @pic.id
681         response.should be_success 
682       end
683       it '原画モデルに編集取得を問い合わせている' do
684         OriginalPicture.should_receive(:edit).exactly(1)
685         get :edit, :id => @pic.id
686       end
687       it '@original_pictureにデータを用意している' do
688         get :edit, :id => @pic.id
689         assigns(:original_picture).should eq @pic
690       end
691       context 'html形式' do
692         it 'ページテンプレートeditを描画する' do
693           get :edit, :id => @pic.id
694           response.should render_template("edit")
695         end
696       end
697       context 'js形式' do
698         it '部分テンプレートedit.jsを描画する' do
699           get :edit, :id => @pic.id, :format => :js
700           response.should render_template("edit")
701         end
702       end
703     end
704     context '作家権限がないとき' do
705       before do
706         sign_out @user
707       end
708       context 'html形式' do
709         it 'ステータスコード302 Foundを返す' do
710           get :edit, :id => @pic.id
711           response.status.should eq 302
712         end
713         it 'サインインページへ遷移する' do
714           get :edit, :id => @pic.id
715           response.body.should redirect_to '/users/sign_in'
716         end
717       end
718       context 'js形式' do
719         it 'ステータスコード401 Unauthorizedを返す' do
720           get :edit, :id => @pic.id, :format => :js
721           response.status.should eq 401
722         end
723         it '応答メッセージにUnauthorizedを返す' do
724           get :edit, :id => @pic.id, :format => :js
725           response.message.should match(/Unauthorized/)
726         end
727       end
728     end
729     context '作家が絵師でないとき' do
730       before do
731         Author.any_instance.stub(:artist?).and_return(false)
732       end
733       context 'html形式' do
734         it 'ステータスコード302 Foundを返す' do
735           get :edit, :id => @pic.id
736           response.status.should eq 302
737         end
738         it '絵師登録ページへ遷移する' do
739           get :edit, :id => @pic.id
740           response.should redirect_to new_artist_path
741         end
742       end
743       context 'js形式' do
744         it 'ステータスコード200 Okを返す' do
745           get :edit, :id => @pic.id, :format => :js
746           response.status.should eq 200
747         end
748         it '絵師登録部分テンプレートartists/new.jsを描画する' do
749           get :edit, :id => @pic.id, :format => :js
750           response.should render_template("artists/new")
751         end
752       end
753     end
754   end
755
756   describe '更新に於いて' do
757     before do
758       @pic = FactoryGirl.create :original_picture, :artist_id => @artist.id
759       OriginalPicture.stub(:edit).with(any_args()).and_return(@pic)
760       sign_in @user
761     end
762     context '事前チェックしておく' do
763       before do
764         OriginalPicture.any_instance.stub(:store).with(any_args()).and_return(true)
765       end
766       it '原画モデルに単体取得を問い合わせている' do
767         OriginalPicture.should_receive(:edit).exactly(1)
768         put :update, :id => @pic.id, :original_picture => FactoryGirl.attributes_for(:original_picture)
769       end
770       it 'モデルに更新を依頼する' do
771         OriginalPicture.any_instance.should_receive(:store).exactly(1)
772         put :update, :id => @pic.id, :original_picture => FactoryGirl.attributes_for(:original_picture)
773       end
774       it '@original_pictureにアレを取得している' do
775         put :update, :id => @pic.id, :original_picture => FactoryGirl.attributes_for(:original_picture)
776         assigns(:original_picture).should eq(@pic)
777       end
778     end
779     context 'つつがなく終わるとき' do
780       before do
781         OriginalPicture.any_instance.stub(:store).with(any_args()).and_return {
782           assigns(:original_picture).attributes = FactoryGirl.attributes_for(:original_picture, :artist_id => @artist.id, :ext => 'jpeg')
783           assigns(:original_picture).save
784           true
785         }
786       end
787       it '更新される' do
788         put :update, :id => @pic.id, :original_picture => FactoryGirl.attributes_for(:original_picture, :ext => 'jpeg')
789         OriginalPicture.find(@pic.id).ext.should eq 'jpeg'
790       end
791       context 'html形式' do
792         it 'ステータスコード302 Foundを返す' do
793           put :update, :id => @pic.id, :original_picture => FactoryGirl.attributes_for(:original_picture)
794           response.status.should eq 302
795         end
796         it '更新された原画の表示ページへ遷移する' do
797           put :update, :id => @pic.id, :original_picture => FactoryGirl.attributes_for(:original_picture)
798           response.should redirect_to(@pic)
799         end
800       end
801       context 'json形式' do
802         it 'ステータスコード200 OKを返す' do
803           put :update, :id => @pic.id, :original_picture => FactoryGirl.attributes_for(:original_picture), :format => :json
804           response.should be_success 
805         end
806         it 'ページ本体は特に返さない' do
807           put :update, :id => @pic.id, :original_picture => FactoryGirl.attributes_for(:original_picture), :format => :json
808           response.body.should match /./
809         end
810       end
811     end
812     context '作家権限がないとき' do
813       before do
814         sign_out @user
815       end
816       context 'html形式' do
817         it 'ステータスコード302 Foundを返す' do
818           post :create, :original_picture => FactoryGirl.attributes_for(:original_picture)
819           response.status.should eq 302
820         end
821         it 'サインインページへ遷移する' do
822           post :create, :original_picture => FactoryGirl.attributes_for(:original_picture)
823           response.body.should redirect_to '/users/sign_in'
824         end
825       end
826       context 'json形式' do
827         it 'ステータスコード401 Unauthorizedを返す' do
828           post :create, :original_picture => FactoryGirl.attributes_for(:original_picture), :format => :json
829           response.status.should eq 401
830         end
831         it '応答メッセージにUnauthorizedを返す' do
832           post :create, :original_picture => FactoryGirl.attributes_for(:original_picture), :format => :json
833           response.message.should match(/Unauthorized/)
834         end
835       end
836     end
837     context '作家が絵師でないとき' do
838       before do
839         Author.any_instance.stub(:artist?).and_return(false)
840       end
841       context 'html形式' do
842         it 'ステータスコード302 Foundを返す' do
843           post :create, :original_picture => FactoryGirl.attributes_for(:original_picture)
844           response.status.should eq 302
845         end
846         it '絵師登録ページへ遷移する' do
847           post :create, :original_picture => FactoryGirl.attributes_for(:original_picture)
848           response.should redirect_to new_artist_path
849         end
850       end
851       context 'json形式' do
852         it '例外403 forbiddenを返す' do
853           lambda{
854             post :create, :original_picture => FactoryGirl.attributes_for(:original_picture), :format => :json
855           }.should raise_error(ActiveRecord::Forbidden)
856         end
857       end
858     end
859     context '検証、保存に失敗した' do
860       before do
861         OriginalPicture.any_instance.stub(:store).and_return(false)
862       end
863       context 'html形式' do
864         it 'ステータスコード200 Okを返す' do
865           put :update, :id => @pic.id, :original_picture => FactoryGirl.attributes_for(:original_picture)
866           response.status.should eq 200
867         end
868         it '編集ページを描画する' do
869           put :update, :id => @pic.id, :original_picture => FactoryGirl.attributes_for(:original_picture)
870           response.should render_template("edit")
871         end
872       end
873       context 'json形式' do
874         it 'ステータスコード422 unprocessable_entity を返す' do
875           put :update, :id => @pic.id, :original_picture => FactoryGirl.attributes_for(:original_picture), :format => :json
876           response.status.should eq 422
877         end
878         it '応答メッセージUnprocessable Entityを返す' do
879           put :update, :id => @pic.id, :original_picture => FactoryGirl.attributes_for(:original_picture), :format => :json
880           response.message.should match(/Unprocessable/)
881         end
882       end
883     end
884   end
885
886 end