OSDN Git Service

t#32046:
[pettanr/pettanr.git] / spec / models / original_picture_spec.rb
1 # -*- encoding: utf-8 -*-
2 #原画
3 require 'spec_helper'
4
5 describe OriginalPicture do
6   before do
7     SpeechBalloonTemplate.delete_all
8     @admin = FactoryGirl.create :admin
9     @user = FactoryGirl.create( :user_yas)
10     @author = FactoryGirl.create :author, :user_id => @user.id
11     @artist = FactoryGirl.create :artist_yas, :author_id => @author.id
12     @other_user = FactoryGirl.create( :user_yas)
13     @other_author = FactoryGirl.create :author, :user_id => @other_user.id
14     @other_artist = FactoryGirl.create :artist_yas, :author_id => @other_author.id
15     @sp = FactoryGirl.create :system_picture
16     @lg = FactoryGirl.create :license_group
17     @license = FactoryGirl.create :license, :license_group_id => @lg.id, :system_picture_id => @sp.id
18   end
19   
20   describe '検証に於いて' do
21     before do
22       @op = FactoryGirl.build :original_picture, :artist_id => @artist.id
23     end
24     
25     context 'オーソドックスなデータのとき' do
26       it '下限データが通る' do
27         @op.ext = 'png' #リストにない拡張子は通らないし
28         @op.width = 1
29         @op.height = 1
30         @op.filesize = 1
31         @op.md5 = 'a'*32
32         @op.should be_valid
33       end
34       it '上限データが通る' do
35         @op.ext = 'jpeg'
36         @op.width = 99999
37         @op.height = 99999
38         @op.filesize = 2000000
39         @op.md5 = 'a'*32
40         @op.should be_valid
41       end
42     end
43     
44     context 'extを検証するとき' do
45       it 'nullなら失敗する' do
46         @op.ext = ''
47         @op.should_not be_valid
48       end
49       it '5文字以上なら失敗する' do
50         @op.ext = 'a'*5
51         @op.should_not be_valid
52       end
53       it 'png,gif,jpeg以外なら失敗する' do
54         @op.ext = 'bmp'
55         @op.should_not be_valid
56       end
57     end
58     context 'widthを検証するとき' do
59       it 'nullなら失敗する' do
60         @op.width = nil
61         @op.should_not be_valid
62       end
63       it '数値でなければ失敗する' do
64         @op.width = 'a'
65         @op.should_not be_valid
66       end
67       it '0なら失敗する' do
68         @op.width = '0'
69         @op.should_not be_valid
70       end
71       it '負でも失敗する' do
72         @op.width = -1
73         @op.should_not be_valid
74       end
75     end
76     context 'heightを検証するとき' do
77       it 'nullなら失敗する' do
78         @op.height = nil
79         @op.should_not be_valid
80       end
81       it '数値でなければ失敗する' do
82         @op.height = 'a'
83         @op.should_not be_valid
84       end
85       it '0なら失敗する' do
86         @op.height = '0'
87         @op.should_not be_valid
88       end
89       it '負でも失敗する' do
90         @op.height = -1
91         @op.should_not be_valid
92       end
93     end
94     context 'filesizeを検証するとき' do
95       it 'nullなら失敗する' do
96         @op.filesize = nil
97         @op.should_not be_valid
98       end
99       it '数値でなければ失敗する' do
100         @op.filesize = 'a'
101         @op.should_not be_valid
102       end
103       it '負なら失敗する' do
104         @op.filesize = '-1'
105         @op.should_not be_valid
106       end
107       it '2MB以上なら失敗する' do
108         @op.filesize = 2000000+1
109         @op.should_not be_valid
110       end
111     end
112     context 'artist_idを検証するとき' do
113       it 'nullなら失敗する' do
114         @op.artist_id = nil
115         @op.should_not be_valid
116       end
117       it '数値でなければ失敗する' do
118         @op.artist_id = 'a'
119         @op.should_not be_valid
120       end
121       it '存在する絵師でなければ失敗する' do
122         @op.artist_id = 0
123         @op.should_not be_valid
124       end
125     end
126     context 'md5を検証するとき' do
127       it 'nullなら失敗する' do
128         @op.md5 = ''
129         @op.should_not be_valid
130       end
131       it '31文字なら失敗する' do
132         @op.md5 = 'a'*31
133         @op.should_not be_valid
134       end
135       it '33文字なら失敗する' do
136         @op.md5 = 'a'*33
137         @op.should_not be_valid
138       end
139     end
140   end
141   
142   describe 'デフォルト値補充に於いて' do
143     it 'defined' do
144       @op = FactoryGirl.build :original_picture, :artist_id => @artist.id
145       @op.supply_default
146     end
147   end
148   
149   describe '上書き補充に於いて' do
150     it '投稿日時が設定されている' do
151       @op = FactoryGirl.build :original_picture, :uploaded_at => nil
152       @op.overwrite @artist
153       @op.uploaded_at.should_not be_nil
154     end
155     it '絵師idが設定されている' do
156       @op = FactoryGirl.build :original_picture, :artist_id => nil
157       @op.overwrite @artist
158       @op.artist_id.should eq @artist.id
159     end
160   end
161   
162   describe '所持判定に於いて' do
163     before do
164       @op = FactoryGirl.create :original_picture, :artist_id => @artist.id
165     end
166     context '事前チェックする' do
167       it '自身にロールリストからの絵師取得を依頼している' do
168         OriginalPicture.should_receive(:get_artist_from_roles).with(any_args).exactly(1)
169         r = @op.own?([@artist])
170       end
171     end
172     context 'ロール内絵師が取得できるとき' do
173       before do
174       end
175       it 'ロール内絵師のidが自身の絵師idと一致するなら許可する' do
176         OriginalPicture.stub(:get_artist_from_roles).with(any_args).and_return(@artist)
177         r = @op.own?([@artist])
178         r.should be_true
179       end
180       it 'ロール内絵師のidが自身の絵師idと一致しないならno' do
181         OriginalPicture.stub(:get_artist_from_roles).with(any_args).and_return(@other_artist)
182         @op.own?(@other_artist).should be_false
183       end
184     end
185     context 'ロール内絵師が取得できないとき' do
186       before do
187         OriginalPicture.stub(:get_artist_from_roles).with(any_args).and_return(nil)
188       end
189       it 'Falseを返す' do
190         r = @op.own?([@artist])
191         r.should be_false
192       end
193     end
194   end
195   
196   describe '閲覧許可に於いて' do
197     before do
198       @op = FactoryGirl.create :original_picture, :artist_id => @artist.id
199     end
200     context '事前チェックする' do
201       before do
202         MagicNumber['run_mode'] = 1
203       end
204       it '自身の原画に所持判定を問い合わせしている' do
205         OriginalPicture.any_instance.stub(:own?).and_return(true)
206         OriginalPicture.any_instance.should_receive(:own?).with(any_args).exactly(1)
207         r = @op.visible?([@artist])
208       end
209     end
210     context 'つつがなく終わるとき' do
211       before do
212         MagicNumber['run_mode'] = 1
213       end
214       it '自分の原画なら許可する' do
215         OriginalPicture.any_instance.stub(:own?).and_return(true)
216         r = @op.visible?([@artist])
217         r.should be_true
218       end
219       it '他人の原画なら許可しない' do
220         OriginalPicture.any_instance.stub(:own?).and_return(false)
221         r = @op.visible?([@artist])
222         r.should be_false
223       end
224     end
225   end
226   
227   describe 'ファイル名に於いて' do
228     before do
229       @op = FactoryGirl.create :original_picture, :artist_id => @artist.id
230     end
231     it 'id+拡張子のフォーマットで返す' do
232       r = @op.filename
233       r.should eq "#{@op.id}.png"
234     end
235   end
236   
237   describe 'MimeTypeに於いて' do
238     before do
239       @op = FactoryGirl.create :original_picture, :artist_id => @artist.id
240     end
241     it 'image/拡張子のフォーマットで返す' do
242       r = @op.mime_type
243       r.should eq "image/png"
244     end
245   end
246   
247   describe 'ファイルのurlに於いて' do
248     before do
249       @op = FactoryGirl.create :original_picture, :artist_id => @artist.id
250       OriginalPicture.any_instance.stub(:filename).and_return('3.gif')
251     end
252     it 'ファイル名取得を依頼している' do
253       OriginalPicture.any_instance.should_receive(:filename).exactly(1)
254       @op.url
255     end
256     it '/original_pictures/3.gifのフォーマットで返す' do
257       r = @op.url
258       r.should eq "/original_pictures/3.gif"
259     end
260   end
261   
262   describe 'サムネイル画像タグオプションに於いて' do
263     before do
264       @op = FactoryGirl.create :original_picture, :artist_id => @artist.id
265       OriginalPicture.any_instance.stub(:url).and_return('/original_pictures/3.gif')
266       PettanImager.stub(:thumbnail_size).with(any_args).and_return([40, 30])
267     end
268     it 'サムネイル画像の幅高さ取得を依頼している' do
269       PettanImager.should_receive(:thumbnail_size).with(any_args).exactly(1)
270       @op.tmb_opt_img_tag
271     end
272     it '戻り値はHashで返す' do
273       r = @op.tmb_opt_img_tag
274       r.is_a?(Hash).should be_true
275     end
276     it 'srcキーを含んでいる' do
277       r = @op.tmb_opt_img_tag
278       r.has_key?(:src).should be_true
279       r[:src].should eq '/original_pictures/3.gif'
280     end
281     it 'widthキーを含んでいる' do
282       r = @op.tmb_opt_img_tag
283       r.has_key?(:width).should be_true
284       r[:width].should eq 40
285     end
286     it 'heightキーを含んでいる' do
287       r = @op.tmb_opt_img_tag
288       r.has_key?(:height).should be_true
289       r[:height].should eq 30
290     end
291   end
292   
293   describe '画像タグオプションに於いて' do
294     before do
295       @op = FactoryGirl.create :original_picture, :artist_id => @artist.id
296       OriginalPicture.any_instance.stub(:url).and_return('/original_pictures/3.gif')
297     end
298     it '戻り値はHashで返す' do
299       r = @op.opt_img_tag
300       r.is_a?(Hash).should be_true
301     end
302     it 'srcキーを含んでいる' do
303       r = @op.opt_img_tag
304       r.has_key?(:src).should be_true
305       r[:src].should eq '/original_pictures/3.gif'
306     end
307     it 'widthキーを含んでいる' do
308       r = @op.opt_img_tag
309       r.has_key?(:width).should be_true
310       r[:width].should eq @op.width
311     end
312     it 'heightキーを含んでいる' do
313       r = @op.opt_img_tag
314       r.has_key?(:height).should be_true
315       r[:height].should eq @op.height
316     end
317   end
318   
319   describe '未公開に於いて' do
320     before do
321       @op = FactoryGirl.create :original_picture, :artist_id => @artist.id
322     end
323     context '公開日時と停止日時がともに空のとき' do
324       it 'Trueを返す' do
325         r = @op.unpublished?
326         r.should be_true
327       end
328     end
329     context '公開日時と停止日時がともに空ではないとき' do
330       it 'Falseを返す' do
331         @op.stopped_at =Time.now
332         r = @op.unpublished?
333         r.should be_false
334       end
335       it 'Falseを返す' do
336         @op.published_at =Time.now
337         r = @op.unpublished?
338         r.should be_false
339       end
340     end
341   end
342
343   describe '停止中に於いて' do
344     before do
345       @op = FactoryGirl.create :original_picture, :artist_id => @artist.id
346     end
347     context '停止日時が空でないとき' do
348       it 'Trueを返す' do
349         @op.stopped_at =Time.now
350         r = @op.stopped?
351         r.should be_true
352       end
353     end
354     context '停止日時が空のとき' do
355       it 'Falseを返す' do
356         @op.stopped_at = nil
357         r = @op.stopped?
358         r.should be_false
359       end
360     end
361   end
362
363   describe 'ライセンス待ちに於いて' do
364     before do
365       @op = FactoryGirl.create :original_picture, :artist_id => @artist.id
366     end
367     context '公開日時と停止日時のうち、空でない方を取得して、投稿日時がそれより後のとき' do
368       it 'Trueを返す' do
369         @op.uploaded_at = Time.now
370         @op.stopped_at = Time.now - 400
371         r = @op.unlicensed?
372         r.should be_true
373       end
374       it 'Trueを返す' do
375         @op.uploaded_at = Time.now
376         @op.published_at =Time.now - 400
377         r = @op.unlicensed?
378         r.should be_true
379       end
380     end
381     context '公開日時と停止日時のうち、空でない方を取得して、投稿日時がそれより後ではないとき' do
382       it 'Falseを返す' do
383         @op.published_at = nil
384         @op.stopped_at = nil
385         r = @op.unlicensed?
386         r.should be_false
387       end
388       it 'Falseを返す' do
389         @op.uploaded_at = Time.now
390         @op.published_at =Time.now + 400
391         r = @op.unlicensed?
392         r.should be_false
393       end
394       it 'Falseを返す' do
395         @op.uploaded_at = Time.now
396         @op.stopped_at =Time.now + 400
397         r = @op.unlicensed?
398         r.should be_false
399       end
400     end
401   end
402
403   describe '公開中に於いて' do
404     before do
405       @op = FactoryGirl.create :original_picture, :artist_id => @artist.id
406     end
407     context '公開日時が空ではないとき' do
408       it 'Trueを返す' do
409         @op.published_at =Time.now
410         r = @op.published?
411         r.should be_true
412       end
413     end
414     context '公開日時が空のとき' do
415       it 'Falseを返す' do
416         @op.published_at = nil
417         r = @op.published?
418         r.should be_false
419       end
420     end
421   end
422
423   describe '一覧取得に於いて' do
424     before do
425       @op = FactoryGirl.create :original_picture, :artist_id => @artist.id
426     end
427     context 'page補正について' do
428       it '文字列から数値に変換される' do
429         OriginalPicture.page('8').should eq 8
430       end
431       it 'nilの場合は1になる' do
432         OriginalPicture.page().should eq 1
433       end
434       it '0以下の場合は1になる' do
435         OriginalPicture.page('0').should eq 1
436       end
437     end
438     context 'page_size補正について' do
439       it '文字列から数値に変換される' do
440         OriginalPicture.page_size('7').should eq 7
441       end
442       it 'nilの場合はOriginalPicture.default_page_sizeになる' do
443         OriginalPicture.page_size().should eq OriginalPicture.default_page_size
444       end
445       it '0以下の場合はOriginalPicture.default_page_sizeになる' do
446         OriginalPicture.page_size('0').should eq OriginalPicture.default_page_size
447       end
448       it 'OriginalPicture.max_page_sizeを超えた場合はOriginalPicture.max_page_sizeになる' do
449         OriginalPicture.page_size('1000').should eq OriginalPicture.max_page_size
450       end
451     end
452   end
453   
454   describe '自分の原画一覧取得に於いて' do
455     before do
456       @op = FactoryGirl.create :original_picture, :artist_id => @artist.id
457     end
458     context 'つつがなく終わるとき' do
459       it '一覧取得オプションを利用している' do
460         OriginalPicture.stub(:list_opt).with(any_args).and_return({})
461         OriginalPicture.should_receive(:list_opt).with(any_args).exactly(1)
462         r = OriginalPicture.mylist @artist
463       end
464     end
465     it 'リストを返す' do
466       r = OriginalPicture.mylist @artist
467       r.should eq [@op]
468     end
469     it '時系列で並んでいる' do
470       nc = FactoryGirl.create :original_picture, :artist_id => @artist.id, :updated_at => Time.now + 100
471       r = OriginalPicture.mylist @artist
472       r.should eq [nc, @op]
473     end
474     it '他人の原画は含まない' do
475       nc = FactoryGirl.create :original_picture, :artist_id => @other_artist.id
476       r = OriginalPicture.mylist @artist
477       r.should eq [@op]
478     end
479     context 'DBに5件あって1ページの件数を2件に変えたとして' do
480       before do
481         @op2 = FactoryGirl.create :original_picture, :artist_id => @artist.id, :updated_at => Time.now + 100
482         @op3 = FactoryGirl.create :original_picture, :artist_id => @artist.id, :updated_at => Time.now + 200
483         @op4 = FactoryGirl.create :original_picture, :artist_id => @artist.id, :updated_at => Time.now + 300
484         @op5 = FactoryGirl.create :original_picture, :artist_id => @artist.id, :updated_at => Time.now + 400
485       end
486       it '通常は2件を返す' do
487         r = OriginalPicture.mylist @artist, 1, 2
488         r.should have(2).items 
489       end
490       it 'page=1なら末尾2件を返す' do
491         #時系列で並んでいる
492         r = OriginalPicture.mylist @artist, 1, 2
493         r.should eq [@op5, @op4]
494       end
495       it 'page=2なら中間2件を返す' do
496         r = OriginalPicture.mylist @artist, 2, 2
497         r.should eq [@op3, @op2]
498       end
499       it 'page=3なら先頭1件を返す' do
500         r = OriginalPicture.mylist @artist, 3, 2
501         r.should eq [@op]
502       end
503     end
504   end
505   
506   describe '自分の原画一覧ページ制御に於いて' do
507     before do
508       OriginalPicture.stub(:count).with(any_args).and_return(100)
509     end
510     it 'ページ制御を返す' do
511       r = OriginalPicture.mylist_paginate @author
512       r.is_a?(Kaminari::PaginatableArray).should be_true
513     end
514     it '自分の原画一覧の取得条件を利用している' do
515       OriginalPicture.stub(:mylist_where).with(any_args).and_return('')
516       OriginalPicture.should_receive(:mylist_where).with(any_args).exactly(1)
517       r = OriginalPicture.mylist_paginate @author
518     end
519     it 'ページ件数10のとき、3ページ目のオフセットは20から始まる' do
520       r = OriginalPicture.mylist_paginate @author, 3, 10
521       r.limit_value.should eq 10
522       r.offset_value.should eq 20
523     end
524   end
525   
526   describe '一覧取得オプションに於いて' do
527     it '2つの項目を含んでいる' do
528       r = OriginalPicture.list_opt
529       r.should have(2).items
530     end
531     it '素材を含んでいる' do
532       r = OriginalPicture.list_opt
533       r.has_key?(:resource_picture).should be_true
534     end
535     it '実素材を含んでいる' do
536       r = OriginalPicture.list_opt
537       r.has_key?(:pictures).should be_true
538     end
539   end
540   describe 'json一覧出力オプションに於いて' do
541     before do
542       @op = FactoryGirl.create :original_picture, :artist_id => @artist.id
543       @p = FactoryGirl.create :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @artist.id
544       @rp = FactoryGirl.create :resource_picture, :artist_id => @artist.id, :license_id => @license.id, :original_picture_id => @op.id, :picture_id => @p.id
545       @sbt = FactoryGirl.create :speech_balloon_template
546       @scroll = FactoryGirl.create :scroll, :author_id => @author.id, :visible => 1
547       @panel = FactoryGirl.create :panel, :author_id => @author.id, :publish => 1
548       @scroll_panel = FactoryGirl.create :scroll_panel, :author_id => @author.id, :scroll_id => @scroll.id, :panel_id => @panel.id
549     end
550     it '素材を含んでいる' do
551       r = OriginalPicture.mylist(@artist).to_json OriginalPicture.list_json_opt
552       j = JSON.parse r
553       i = j.first
554       i.has_key?('resource_picture').should be_true
555     end
556     it '実素材を含んでいる' do
557       r = OriginalPicture.mylist(@artist).to_json OriginalPicture.list_json_opt
558       j = JSON.parse r
559       i = j.first
560       i.has_key?('pictures').should be_true
561     end
562   end
563   
564   describe '更新履歴一覧取得に於いて' do
565     before do
566       @op = FactoryGirl.create :original_picture, :artist_id => @artist.id
567       @p = FactoryGirl.create :picture, :artist_id => @artist.id, :license_id => @license.id, :revision => 0,
568         :original_picture_id => @op.id
569       @op2 = FactoryGirl.create :original_picture, :artist_id => @artist.id
570     end
571     it 'リストを返す' do
572       r = @op.history
573       r.should eq [@p]
574     end
575     it '他の原画の実素材は含んでいない' do
576       @p2 = FactoryGirl.create :picture, :artist_id => @artist.id, :license_id => @license.id, :revision => 0,
577         :original_picture_id => @op2.id
578       r = @op.history
579       r.should eq [@p]
580     end
581     it 'revisionで並んでいる' do
582       @p2 = FactoryGirl.create :picture, :artist_id => @artist.id, :license_id => @license.id, :revision => 1,
583         :original_picture_id => @op.id
584       r = @op.history
585       r.should eq [@p2, @p]
586     end
587   end
588   
589   describe '単体取得に於いて' do
590     before do
591       @op = FactoryGirl.create :original_picture, :artist_id => @artist.id
592     end
593     context 'つつがなく終わるとき' do
594       it '単体取得オプションを利用している' do
595         OriginalPicture.stub(:show_opt).with(any_args).and_return({})
596         OriginalPicture.should_receive(:show_opt).with(any_args).exactly(1)
597         r = OriginalPicture.show @op.id, @artist
598       end
599       it '閲覧許可を問い合わせている' do
600         OriginalPicture.any_instance.stub(:visible?).with(any_args).and_return(true)
601         OriginalPicture.any_instance.should_receive(:visible?).with(any_args).exactly(1)
602         r = OriginalPicture.show @op.id, @artist
603       end
604     end
605     it '指定の原画を返す' do
606       pic = OriginalPicture.show @op.id, @artist
607       pic.should eq @op
608     end
609     context '他人の原画を開こうとしたとき' do
610       it '403Forbidden例外を返す' do
611         OriginalPicture.any_instance.stub(:visible?).and_return(false)
612         lambda{
613           pic = OriginalPicture.show @op.id, @other_artist
614         }.should raise_error(ActiveRecord::Forbidden)
615       end
616     end
617     context '存在しない原画を開こうとしたとき' do
618       it '404RecordNotFound例外を返す' do
619         lambda{
620           pic = OriginalPicture.show 0, @artist
621         }.should raise_error(ActiveRecord::RecordNotFound)
622       end
623     end
624   end
625   describe '編集取得に於いて' do
626     before do
627       @op = FactoryGirl.create :original_picture, :artist_id => @artist.id
628     end
629     context 'つつがなく終わるとき' do
630       it '単体取得オプションを利用している' do
631         OriginalPicture.stub(:show_opt).with(any_args).and_return({})
632         OriginalPicture.should_receive(:show_opt).with(any_args).exactly(1)
633         r = OriginalPicture.edit @op.id, @artist
634       end
635       it '所持判定を問い合わせている' do
636         OriginalPicture.any_instance.stub(:own?).with(any_args).and_return(true)
637         OriginalPicture.any_instance.should_receive(:own?).with(any_args).exactly(1)
638         r = OriginalPicture.edit @op.id, @artist
639       end
640     end
641     it '指定の原画を返す' do
642       pic = OriginalPicture.edit @op.id, @artist
643       pic.should eq @op
644     end
645     context '他人の原画を開こうとしたとき' do
646       it '403Forbidden例外を返す' do
647         OriginalPicture.any_instance.stub(:own?).and_return(false)
648         lambda{
649           r = OriginalPicture.edit @op.id, @other_artist
650         }.should raise_error(ActiveRecord::Forbidden)
651       end
652     end
653     context '存在しない原画を開こうとしたとき' do
654       it '404RecordNotFound例外を返す' do
655         lambda{
656           r = OriginalPicture.edit 0, @artist
657         }.should raise_error(ActiveRecord::RecordNotFound)
658       end
659     end
660   end
661   describe '単体取得オプションに於いて' do
662     it 'includeキーを含んでいる' do
663       r = OriginalPicture.show_opt
664       r.has_key?(:include).should be_true
665     end
666     it '2つの項目を含んでいる' do
667       r = OriginalPicture.show_opt[:include]
668       r.should have(2).items
669     end
670     it '素材を含んでいる' do
671       r = OriginalPicture.show_opt[:include]
672       r.has_key?(:resource_picture).should be_true
673     end
674     it '実素材を含んでいる' do
675       r = OriginalPicture.show_opt[:include]
676       r.has_key?(:pictures).should be_true
677     end
678   end
679   describe 'json単体出力オプションに於いて' do
680     before do
681       @op = FactoryGirl.create :original_picture, :artist_id => @artist.id
682       @p = FactoryGirl.create :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @artist.id
683       @rp = FactoryGirl.create :resource_picture, :artist_id => @artist.id, :license_id => @license.id, :original_picture_id => @op.id, :picture_id => @p.id
684       @sbt = FactoryGirl.create :speech_balloon_template
685       @scroll = FactoryGirl.create :scroll, :author_id => @author.id, :visible => 1
686       @panel = FactoryGirl.create :panel, :author_id => @author.id, :publish => 1
687       @scroll_panel = FactoryGirl.create :scroll_panel, :author_id => @author.id, :scroll_id => @scroll.id, :panel_id => @panel.id
688     end
689     it '素材を含んでいる' do
690       r = OriginalPicture.show(@op.id, @artist).to_json OriginalPicture.show_json_opt
691       j = JSON.parse r
692       i = j
693       i.has_key?('resource_picture').should be_true
694     end
695     it '実素材を含んでいる' do
696       r = OriginalPicture.show(@op.id, @artist).to_json OriginalPicture.show_json_opt
697       j = JSON.parse r
698       i = j
699       i.has_key?('pictures').should be_true
700     end
701   end
702   describe '作成・更新に於いて' do
703     before do
704       @op = FactoryGirl.build :original_picture, :artist_id => @artist.id
705       @imager = ImagerTest.load "abc\ndef\nghi"
706     end
707     context '事前チェック' do
708       before do
709         #すべての処理が正常パターンで通過すれば、一番深い分岐まで通る。
710         #それで外部のメソッド呼び出しだけに着目してテストする。
711         OriginalPicture.any_instance.stub(:save).with(any_args).and_return(true)
712         PictureIO.original_picture_io.stub(:put).with(any_args).and_return(true)
713       end
714       it '自身に属性をセットしている' do
715         OriginalPicture.any_instance.should_receive(:attributes=).exactly(1)
716         @op.store @imager
717       end
718       it '自身が保存されている' do
719         OriginalPicture.any_instance.should_receive(:save).exactly(1)
720         @op.store @imager
721       end
722       it 'PictureIoに画像データの保存を依頼している' do
723         PictureIO.original_picture_io.should_receive(:put).with(any_args).exactly(1)
724         @op.store @imager
725       end
726     end
727     context 'つつがなく終わるとき' do
728       before do
729       end
730       it '自身に属性をセットしている' do
731         @op.store @imager
732         @op.width.should eq 3
733         @op.height.should eq 3
734         @op.filesize.should eq 9
735       end
736       it '原画モデルが作成されている' do
737         lambda {
738           @op.store @imager
739         }.should change OriginalPicture, :count
740       end
741       it '原画が保存されている' do
742         @op.store @imager
743         OriginalPicture.find(@op).should_not be_nil
744       end
745       it 'Trueを返す' do
746         @op.store(@imager).should eq true
747       end
748     end
749     #以下から例外ケース。処理先頭から失敗させていく
750     context 'imagerが初期化に失敗したとき' do
751       before do
752       end
753       it 'falseを返す' do
754         @op.store(false).should be_false
755       end
756       it '自身の保存は呼ばれていない' do
757         OriginalPicture.any_instance.should_not_receive(:save)
758         @op.store(false)
759       end
760       it '全体エラーメッセージがセットされている' do
761         lambda {
762           @op.store(false)
763         }.should change(@op.errors[:base], :count)
764       end
765     end
766     context '自身の保存に失敗したとき' do
767       before do
768         OriginalPicture.any_instance.stub(:save).with(any_args).and_return(false)
769       end
770       it 'falseを返す' do
771         @op.store(@imager).should be_false
772       end
773       it '更新されていない' do
774         @op.store(@imager)
775         @op.should be_a_new OriginalPicture
776       end
777       it '原画の保存は呼ばれていない' do
778         PictureIO::LocalPicture.any_instance.should_not_receive(:put)
779       end
780     end
781     context '原画の保存に失敗したとき' do
782       before do
783         OriginalPicture.any_instance.stub(:save).with(any_args).and_return(true)
784         PictureIO.original_picture_io.stub(:put).with(any_args).and_raise(PictureIO::Error)
785       end
786       it 'falseを返す' do
787         @op.store(@imager).should be_false
788       end
789       it '更新されていない' do
790         @op.store(@imager)
791         @op.should be_a_new OriginalPicture
792       end
793       it '全体エラーメッセージがセットされている' do
794         lambda {
795           @op.store(@imager)
796         }.should change(@op.errors[:base], :count)
797       end
798     end
799   end
800   
801   describe '削除に於いて' do
802     before do
803       @op = FactoryGirl.create :original_picture, :artist_id => @artist.id
804     end
805     context '事前チェックしておく' do
806       before do
807         OriginalPicture.any_instance.stub(:destroy).and_return(true)
808         ResourcePicture.any_instance.stub(:unpublish).and_return(true)
809         Picture.any_instance.stub(:unpublish).with(any_args).and_return(true)
810         PictureIO.original_picture_io.stub(:delete).with(any_args).and_return(true)
811       end
812       it '原画モデルに削除を依頼している' do
813         OriginalPicture.any_instance.should_receive(:destroy).exactly(1)
814         r = @op.destroy_with_resource_picture
815       end
816       it '保管庫に原画の画像データ削除を依頼している' do
817         PictureIO.original_picture_io.should_receive(:delete).with(@op.filename).exactly(1)
818         r = @op.destroy_with_resource_picture
819       end
820       context '自身にリンクされた素材があるとき' do
821         it '素材モデルに削除を依頼している' do
822           @p = FactoryGirl.create :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @artist.id, :revision => 0
823           @rp = FactoryGirl.create :resource_picture, :artist_id => @artist.id, :license_id => @license.id, :original_picture_id => @op.id, :picture_id => @p.id
824           ResourcePicture.any_instance.should_receive(:unpublish).exactly(1)
825           r = @op.destroy_with_resource_picture
826         end
827       end
828       context '自身にリンクされた素材がないとき' do
829         it '素材モデルに削除を依頼しない' do
830           ResourcePicture.any_instance.should_not_receive(:unpublish)
831           r = @op.destroy_with_resource_picture
832         end
833       end
834       context '自身にリンクされた実素材があるとき' do
835         it 'すべての実素材に墨塗を依頼している' do
836           @p = FactoryGirl.create :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @artist.id, :revision => 0
837           Picture.any_instance.should_receive(:unpublish).with(any_args).exactly(1)
838           r = @op.destroy_with_resource_picture
839         end
840       end
841       context '自身にリンクされた実素材がないとき' do
842         it '実素材に墨塗を依頼しない' do
843           Picture.any_instance.should_not_receive(:unpublish)
844           r = @op.destroy_with_resource_picture
845         end
846       end
847     end
848     context 'つつがなく終わるとき' do
849       before do
850         PictureIO.original_picture_io.stub(:delete).with(any_args).and_return(true)
851         @p = FactoryGirl.create :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @artist.id, :revision => 0
852         @rp = FactoryGirl.create :resource_picture, :artist_id => @artist.id, :license_id => @license.id, :original_picture_id => @op.id, :picture_id => @p.id
853       end
854       it '自身を削除する' do
855         lambda {
856           r = @op.destroy_with_resource_picture
857         }.should change(OriginalPicture, :count).by(-1)
858         lambda {
859           r = OriginalPicture.find @op.id
860         }.should raise_error
861       end
862       it 'Trueを返す' do
863         r = @op.destroy_with_resource_picture
864         r.should be_true
865       end
866     end
867     context '自身の削除に失敗したとき' do
868       before do
869         OriginalPicture.any_instance.stub(:destroy).with(any_args).and_return(false)
870       end
871       it 'Falseを返す' do
872         r = @op.destroy_with_resource_picture
873         r.should be_false
874       end
875       it 'ロールバックしている' do
876         lambda {
877           r = @op.destroy_with_resource_picture
878         }.should_not change(OriginalPicture, :count)
879       end
880     end
881     context '画像の削除に失敗したとき' do
882       before do
883         PictureIO.original_picture_io.stub(:exist?).with(@op.filename).and_return(true)
884         PictureIO.original_picture_io.stub(:delete).with(@op.filename).and_raise(PictureIO::Error)
885       end
886       it 'Falseを返す' do
887         r = @op.destroy_with_resource_picture
888         r.should be_false
889       end
890       it 'ロールバックしている' do
891         lambda {
892           r = @op.destroy_with_resource_picture
893         }.should_not change(OriginalPicture, :count)
894       end
895     end
896     context '素材の削除に失敗とき' do
897       before do
898         @p = FactoryGirl.create :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @artist.id, :revision => 0
899         @rp = FactoryGirl.create :resource_picture, :artist_id => @artist.id, :license_id => @license.id, :original_picture_id => @op.id, :picture_id => @p.id
900         ResourcePicture.any_instance.stub(:unpublish).with(any_args).and_return(false)
901       end
902       it 'Falseを返す' do
903         r = @op.destroy_with_resource_picture
904         r.should be_false
905       end
906       it 'ロールバックしている' do
907         lambda {
908           r = @op.destroy_with_resource_picture
909         }.should_not change(OriginalPicture, :count)
910         lambda {
911           r = @op.destroy_with_resource_picture
912         }.should_not change(ResourcePicture, :count)
913       end
914     end
915     context '実素材の墨塗に失敗とき' do
916       before do
917         @p = FactoryGirl.create :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @artist.id, :revision => 0
918         @rp = FactoryGirl.create :resource_picture, :artist_id => @artist.id, :license_id => @license.id, :original_picture_id => @op.id, :picture_id => @p.id
919         Picture.any_instance.stub(:unpublish).with(any_args).and_return(false)
920       end
921       it 'Falseを返す' do
922         r = @op.destroy_with_resource_picture
923         r.should be_false
924       end
925       it 'ロールバックしている' do
926         lambda {
927           r = @op.destroy_with_resource_picture
928         }.should_not change(OriginalPicture, :count)
929         lambda {
930           r = @op.destroy_with_resource_picture
931         }.should_not change(ResourcePicture, :count)
932       end
933     end
934   end
935   
936   describe 'エクスポートに於いて' do
937     before do
938       @op = FactoryGirl.create :original_picture, :artist_id => @artist.id
939       @p = FactoryGirl.create :picture, :artist_id => @artist.id, :original_picture_id => @op.id, :license_id => @license.id, :revision => 0
940       @rp = FactoryGirl.create :resource_picture, :artist_id => @artist.id, :original_picture_id => @op.id, :license_id => @license.id, :picture_id => @p.id
941       #開始日時以前の原画
942       @old_op = FactoryGirl.create :original_picture, :artist_id => @artist.id, :updated_at => Time.now - 1000
943       #素材がライセンスされていない原画
944       @stopped_op = FactoryGirl.create :original_picture, :artist_id => @artist.id
945       #他人の原画
946       @other_op = FactoryGirl.create :original_picture, :artist_id => @other_artist.id
947       #貸手からの原画排除
948       @outer_artist = FactoryGirl.create :artist_yas, :author_id => nil
949       @outer_op = FactoryGirl.create :original_picture, :artist_id => @outer_artist.id
950     end
951     context 'つつがなく終わるとき' do
952       it '開始日時が省略された場合はすべての内原画を返す' do
953         r = OriginalPicture.export 
954         r.should eq [@op, @old_op, @stopped_op, @other_op]
955       end
956       it '開始日時以降に更新された内原画を返す' do
957         r = OriginalPicture.export @op.updated_at - 100
958         r.should eq [@op, @stopped_op, @other_op]
959       end
960       it '素材を含んでいる' do
961         r = OriginalPicture.export 
962         r.first.resource_picture.should_not be_nil
963       end
964       #素材がライセンスされていないケースもある
965       it '停止中の原画は素材を含んでいない' do
966         r = OriginalPicture.export
967         r[2].resource_picture.should be_nil
968       end
969     end
970   end
971   
972   describe 'リストのjson化に於いて' do
973     before do
974       @op = FactoryGirl.create :original_picture, :artist_id => @artist.id
975       @p = FactoryGirl.create :picture, :artist_id => @artist.id, :original_picture_id => @op.id, :license_id => @license.id, :revision => 0
976       @rp = FactoryGirl.create :resource_picture, :artist_id => @artist.id, :original_picture_id => @op.id, :license_id => @license.id, :picture_id => @p.id
977       @other_op = FactoryGirl.create :original_picture, :artist_id => @other_artist.id
978       @other_p = FactoryGirl.create :picture, :artist_id => @other_artist.id, :original_picture_id => @other_op.id, :license_id => @license.id, :revision => 0, :updated_at => Time.now + 100
979       @other_rp = FactoryGirl.create :resource_picture, :artist_id => @other_artist.id, :original_picture_id => @other_op.id, :license_id => @license.id, :picture_id => @other_p.id
980       ResourcePicture.any_instance.stub(:restore).with(any_args).and_return('picture binary data')
981     end
982     context 'つつがなく終わるとき' do
983       it 'json文字列を返す' do
984         r = OriginalPicture.list_as_json_text OriginalPicture.all 
985         lambda {
986           j = JSON.parse r
987         }.should_not raise_error(JSON::ParserError)
988       end
989       it '原画リストを返す' do
990         r = OriginalPicture.list_as_json_text OriginalPicture.all 
991         j = JSON.parse r
992         j.size.should eq 2
993       end
994       it '素材を含んでいる' do
995         r = OriginalPicture.list_as_json_text OriginalPicture.all 
996         j = JSON.parse r
997         j.first['resource_picture'].should_not be_nil
998       end
999       it '各素材に画像データを添えて返す' do
1000         r = OriginalPicture.list_as_json_text OriginalPicture.all 
1001         j = JSON.parse r
1002         j.first['resource_picture']['picture_data'].should_not be_nil
1003         j.last['resource_picture']['picture_data'].should_not be_nil
1004       end
1005     end
1006   end
1007   
1008   describe 'インポートに於いて' do
1009     before do
1010       @imports = {:licenses => {}, :artist_id => @artist.id}
1011     end
1012     context '事前チェックしておく' do
1013     end
1014   end
1015   
1016 end