OSDN Git Service

t#29510:
[pettanr/pettanr.git] / spec / models / comic_spec.rb
1 # -*- encoding: utf-8 -*-
2 require 'spec_helper'
3 #コミック
4
5 describe Comic do
6   before do
7     FactoryGirl.create :admin
8     @sp = FactoryGirl.create :system_picture
9     @lg = FactoryGirl.create :license_group
10     @license = FactoryGirl.create :license, :license_group_id => @lg.id, :system_picture_id => @sp.id
11     @user = FactoryGirl.create( :user_yas)
12     @author = @user.author
13     @artist = FactoryGirl.create :artist_yas, :author_id => @author.id
14     @other_user = FactoryGirl.create( :user_yas)
15     @other_author = @other_user.author
16     @other_artist = FactoryGirl.create :artist_yas, :author_id => @other_author.id
17   end
18   
19   describe '検証に於いて' do
20     before do
21       @comic = FactoryGirl.build :comic, :author_id => @author.id
22     end
23     
24     context 'オーソドックスなデータのとき' do
25       it '下限データが通る' do
26         @comic.title = 'a'
27         @comic.visible = 0
28         @comic.should be_valid
29       end
30       it '上限データが通る' do
31         @comic.title = 'a'*100
32         @comic.visible = 1
33         @comic.should be_valid
34       end
35     end
36     
37     context 'titleを検証するとき' do
38       it 'nullなら失敗する' do
39         @comic.title = nil
40         @comic.should_not be_valid
41       end
42       it '100文字以上なら失敗する' do
43         @comic.title = 'a'*101
44         @comic.should_not be_valid
45       end
46     end
47     context 'visibleを検証するとき' do
48       it 'nullなら失敗する' do
49         @comic.visible = nil
50         @comic.should_not be_valid
51       end
52       it '負なら失敗する' do
53         @comic.visible = -1
54         @comic.should_not be_valid
55       end
56       it '2以上なら失敗する' do
57         @comic.visible = 2
58         @comic.should_not be_valid
59       end
60     end
61   end
62   
63   describe 'デフォルト値補充に於いて' do
64     it 'visibleが0になっている' do
65       @comic = FactoryGirl.build :comic, :visible => nil
66       @comic.supply_default
67       @comic.visible.should eq 0
68     end
69   end
70   
71   describe '上書き補充に於いて' do
72     it '作家idが設定されている' do
73       @comic = FactoryGirl.build :comic, :author_id => nil
74       @comic.overwrite @author
75       @comic.author_id.should eq @author.id
76     end
77   end
78   
79   describe '所持判定に於いて' do
80     before do
81       @comic = FactoryGirl.build :comic, :author_id => @author.id
82     end
83     it '自分のコミックならyes' do
84       @comic.own?(@author).should == true
85     end
86     it '他人のコミックならno' do
87       @comic.own?(@other_author).should == false
88     end
89     it '作家が不明ならno' do
90       @comic.own?(nil).should == false
91     end
92   end
93   
94   describe '閲覧許可に於いて' do
95     before do
96       @comic = FactoryGirl.build :comic, :author_id => @author.id
97     end
98     it '自分の公開コミックを見るときは許可する' do\r
99       Comic.any_instance.stub(:own?).and_return(true)\r
100       Comic.any_instance.stub(:visible).and_return(1)\r
101       r = @comic.visible?(@author)
102       r.should == true
103     end\r
104     it '自分のコミックなら非公開でも許可する' do\r
105       Comic.any_instance.stub(:own?).and_return(true)\r
106       Comic.any_instance.stub(:visible).and_return(0)\r
107       r = @comic.visible?(@author)
108       r.should == true
109     end\r
110     it '他人のコミックでも公開なら許可する' do\r
111       Comic.any_instance.stub(:own?).and_return(false)\r
112       Comic.any_instance.stub(:visible).and_return(1)\r
113       r = @comic.visible?(@author)
114       r.should == true
115     end\r
116     it '他人のコミックで非公開なら許可しない' do\r
117       Comic.any_instance.stub(:own?).and_return(false)\r
118       Comic.any_instance.stub(:visible).and_return(0)\r
119       r = @comic.visible?(@author)
120       r.should == false
121     end\r
122   end
123   
124   describe '一覧取得に於いて' do
125     before do
126       @comic = FactoryGirl.create :comic, :author_id => @author.id
127     end
128     context 'page補正について' do
129       it '文字列から数値に変換される' do
130         Comic.page('8').should eq 8
131       end
132       it 'nilの場合は1になる' do
133         Comic.page().should eq 1
134       end
135       it '0以下の場合は1になる' do
136         Comic.page('0').should eq 1
137       end
138     end
139     context 'page_size補正について' do
140       it '文字列から数値に変換される' do
141         Comic.page_size('7').should eq 7
142       end
143       it 'nilの場合はComic.default_page_sizeになる' do
144         Comic.page_size().should eq Comic.default_page_size
145       end
146       it '0以下の場合はComic.default_page_sizeになる' do
147         Comic.page_size('0').should eq Comic.default_page_size
148       end
149       it 'Comic.max_page_sizeを超えた場合はComic.max_page_sizeになる' do
150         Comic.page_size('1000').should eq Comic.max_page_size
151       end
152     end
153     context 'つつがなく終わるとき' do\r
154       it '一覧取得オプションを利用している' do\r
155         Comic.stub(:list_opt).with(any_args).and_return({})\r
156         Comic.should_receive(:list_opt).with(any_args).exactly(1)\r
157         r = Comic.list
158       end\r
159     end\r
160     it 'リストを返す' do
161       c = Comic.list
162       c.should eq [@comic]
163     end
164     it '非公開コミックは(自分のコミックであっても)含んでいない' do
165       FactoryGirl.create :comic, :author_id => @author.id, :visible => 0
166       c = Comic.list
167       c.should eq [@comic]
168     end
169     it '時系列で並んでいる' do
170       #公開コミックは(他人のコミックであっても)含んでいる
171       v = FactoryGirl.create :comic, :author_id => @other_author.id, :updated_at => Time.now + 100
172       c = Comic.list
173       c.should eq [v, @comic]
174     end
175     context 'DBに5件あって1ページの件数を2件に変えたとして' do
176       before do
177         @comic2 = FactoryGirl.create :comic, :author_id => @author.id, :updated_at => Time.now + 100
178         @comic3 = FactoryGirl.create :comic, :author_id => @author.id, :updated_at => Time.now + 200
179         @comic4 = FactoryGirl.create :comic, :author_id => @author.id, :updated_at => Time.now + 300
180         @comic5 = FactoryGirl.create :comic, :author_id => @author.id, :updated_at => Time.now + 400
181         Comic.stub(:default_page_size).and_return(2)
182       end
183       it '通常は2件を返す' do
184         c = Comic.list
185         c.should have(2).items 
186       end
187       it 'page=1なら末尾2件を返す' do
188         #時系列で並んでいる
189         c = Comic.list(1)
190         c.should eq [@comic5, @comic4]
191       end
192       it 'page=2なら中間2件を返す' do
193         c = Comic.list(2)
194         c.should eq [@comic3, @comic2]
195       end
196       it 'page=3なら先頭1件を返す' do
197         c = Comic.list(3)
198         c.should eq [@comic]
199       end
200     end
201     context 'DBに5件あって1ページの件数を2件に変えたとして' do
202       before do
203         @comic2 = FactoryGirl.create :comic, :author_id => @author.id, :updated_at => Time.now + 100
204         @comic3 = FactoryGirl.create :comic, :author_id => @author.id, :updated_at => Time.now + 200
205         @comic4 = FactoryGirl.create :comic, :author_id => @author.id, :updated_at => Time.now + 300
206         @comic5 = FactoryGirl.create :comic, :author_id => @author.id, :updated_at => Time.now + 400
207         Comic.stub(:default_page_size).and_return(2)
208       end
209       it '件数0は全件(5件)を返す' do
210         r = Comic.list 5, 0
211         r.should have(5).items 
212       end
213     end
214   end
215   describe '一覧取得オプションに於いて' do
216     it 'includeキーを含んでいる' do
217       r = Comic.list_opt
218       r.has_key?(:include).should be_true
219     end
220     it '2つの項目を含んでいる' do
221       r = Comic.list_opt[:include]
222       r.should have(2).items
223     end
224     it 'ストーリーを含んでいる' do
225       r = Comic.list_opt[:include]
226       r.has_key?(:stories).should be_true
227     end
228       it 'ストーリーはコマを含んでいる' do
229         r = Comic.list_opt[:include]
230         r[:stories].has_key?(:panel).should be_true
231       end
232     it '作家を含んでいる' do
233       r = Comic.list_opt[:include]
234       r.has_key?(:author).should be_true
235     end
236   end
237   describe 'json一覧出力オプションに於いて' do
238     before do
239       @op = FactoryGirl.create :original_picture, :artist_id => @artist.id\r
240       @p = FactoryGirl.create :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @artist.id\r
241       @rp = FactoryGirl.create :resource_picture, :artist_id => @artist.id, :license_id => @license.id, :original_picture_id => @op.id, :picture_id => @p.id\r
242       @sbt = FactoryGirl.create :speech_balloon_template\r
243       @comic = FactoryGirl.create :comic, :author_id => @author.id, :visible => 1
244       @panel = FactoryGirl.create :panel, :author_id => @author.id, :publish => 1
245       @story = FactoryGirl.create :story, :author_id => @author.id, :comic_id => @comic.id, :panel_id => @panel.id
246     end
247     it 'ストーリーを含んでいる' do
248       r = Comic.list.to_json Comic.list_json_opt
249       j = JSON.parse r
250       i = j.first
251       i.has_key?('stories').should be_true
252     end
253       it 'ストーリーはコマを含んでいる' do
254         r = Comic.list.to_json Comic.list_json_opt
255         j = JSON.parse r
256         i = j.first
257         s = i['stories'].first
258         s.has_key?('panel').should be_true
259       end
260     it '作家を含んでいる' do
261       r = Comic.list.to_json Comic.list_json_opt
262       j = JSON.parse r
263       i = j.first
264       i.has_key?('author').should be_true
265     end
266   end
267   
268   describe '自分のコミック一覧取得に於いて' do
269     before do
270       @comic = FactoryGirl.create :comic, :author_id => @author.id
271     end
272     context 'つつがなく終わるとき' do\r
273       it '一覧取得オプションを利用している' do\r
274         Comic.stub(:list_opt).with(any_args).and_return({})\r
275         Comic.should_receive(:list_opt).with(any_args).exactly(1)\r
276         r = Comic.mylist @author
277       end\r
278     end\r
279     it 'リストを返す' do
280       c = Comic.mylist @author
281       c.should eq [@comic]
282     end
283     it '時系列で並んでいる' do
284       nc = FactoryGirl.create :comic, :author_id => @author.id, :updated_at => Time.now + 100
285       cl = Comic.mylist @author
286       cl.should eq [nc, @comic]
287     end
288     it '他人のコミックは公開でも含まない' do
289       nc = FactoryGirl.create :comic, :author_id => @other_author.id, :visible => 1
290       cl = Comic.mylist @author
291       cl.should eq [@comic]
292     end
293     it '自分のコミックは非公開でも含んでいる' do
294       nc = FactoryGirl.create :comic, :author_id => @author.id, :visible => 0, :updated_at => Time.now + 100
295       cl = Comic.mylist @author
296       cl.should eq [nc, @comic]
297     end
298     context 'DBに5件あって1ページの件数を2件に変えたとして' do
299       before do
300         @comic2 = FactoryGirl.create :comic, :author_id => @author.id, :updated_at => Time.now + 100
301         @comic3 = FactoryGirl.create :comic, :author_id => @author.id, :updated_at => Time.now + 200
302         @comic4 = FactoryGirl.create :comic, :author_id => @author.id, :updated_at => Time.now + 300
303         @comic5 = FactoryGirl.create :comic, :author_id => @author.id, :updated_at => Time.now + 400
304       end
305       it '通常は2件を返す' do
306         c = Comic.mylist @author, 1, 2
307         c.should have(2).items 
308       end
309       it 'page=1なら末尾2件を返す' do
310         #時系列で並んでいる
311         c = Comic.mylist(@author, 1, 2)
312         c.should eq [@comic5, @comic4]
313       end
314       it 'page=2なら中間2件を返す' do
315         c = Comic.mylist(@author, 2, 2)
316         c.should eq [@comic3, @comic2]
317       end
318       it 'page=3なら先頭1件を返す' do
319         c = Comic.mylist(@author, 3, 2)
320         c.should eq [@comic]
321       end
322     end
323     context 'DBに5件あって1ページの件数を0件に変えたとして' do
324       before do
325         @comic2 = FactoryGirl.create :comic, :author_id => @author.id, :updated_at => Time.now + 100
326         @comic3 = FactoryGirl.create :comic, :author_id => @author.id, :updated_at => Time.now + 200
327         @comic4 = FactoryGirl.create :comic, :author_id => @author.id, :updated_at => Time.now + 300
328         @comic5 = FactoryGirl.create :comic, :author_id => @author.id, :updated_at => Time.now + 400
329         Author.stub(:default_comic_page_size).and_return(2)\r
330       end
331       it '通常は全件(5件)を返す' do
332         r = Comic.mylist @author, 5, 0
333         r.should have(5).items 
334       end
335     end
336   end
337   
338   describe '単体取得に於いて' do
339     before do
340       @comic = FactoryGirl.create :comic, :author_id => @author.id
341     end
342     context 'つつがなく終わるとき' do\r
343       it '単体取得オプションを利用している' do\r
344         Comic.stub(:show_opt).with(any_args).and_return({})\r
345         Comic.should_receive(:show_opt).with(any_args).exactly(1)\r
346         r = Comic.show @comic.id, @author
347       end\r
348       it '閲覧許可を問い合わせている' do\r
349         Comic.any_instance.stub(:visible?).with(any_args).and_return(true)\r
350         Comic.any_instance.should_receive(:visible?).with(any_args).exactly(1)\r
351         r = Comic.show @comic.id, @author
352       end\r
353     end\r
354     it '指定のコミックを返す' do
355       c = Comic.show @comic.id, @author.id
356       c.should eq @comic
357     end
358     context '閲覧許可が出なかったとき' do\r
359       it '403Forbidden例外を返す' do\r
360         Comic.any_instance.stub(:visible?).and_return(false)\r
361         lambda{\r
362           Comic.show @comic.id, @author\r
363         }.should raise_error(ActiveRecord::Forbidden)\r
364       end\r
365     end\r
366     context '存在しない作家を開こうとしたとき' do\r
367       it '404RecordNotFound例外を返す' do\r
368         lambda{\r
369           Comic.show 110, @author\r
370         }.should raise_error(ActiveRecord::RecordNotFound)\r
371       end\r
372     end\r
373   end
374   
375   describe '編集取得に於いて' do
376     before do
377       @comic = FactoryGirl.create :comic, :author_id => @author.id
378     end
379     context 'つつがなく終わるとき' do\r
380       it '単体取得オプションを利用している' do\r
381         Comic.stub(:show_opt).with(any_args).and_return({})\r
382         Comic.should_receive(:show_opt).with(any_args).exactly(1)\r
383         r = Comic.edit @comic.id, @author
384       end\r
385       it '所持判定を問い合わせている' do\r
386         Comic.any_instance.stub(:own?).with(any_args).and_return(true)\r
387         Comic.any_instance.should_receive(:own?).with(any_args).exactly(1)\r
388         r = Comic.edit @comic.id, @author
389       end\r
390     end\r
391     it '指定のコミックを返す' do
392       Comic.any_instance.stub(:own?).and_return(true)
393       c = Comic.edit @comic.id, @author.id
394       c.should eq @comic
395     end
396     context '他人のコミックを開こうとしたとき' do
397       it '403Forbidden例外を返す' do
398         Comic.any_instance.stub(:own?).and_return(false)
399         lambda{
400           Comic.edit @comic.id, @author
401         }.should raise_error(ActiveRecord::Forbidden)
402       end
403     end
404     context '存在しないコミックを開こうとしたとき' do
405       it '404RecordNotFound例外を返す' do
406         lambda{
407           Comic.edit 110, @author
408         }.should raise_error(ActiveRecord::RecordNotFound)
409       end
410     end
411   end
412   describe '単体取得オプションに於いて' do
413     it 'includeキーを含んでいる' do
414       r = Comic.show_opt
415       r.has_key?(:include).should be_true
416     end
417     it '2つの項目を含んでいる' do
418       r = Comic.show_opt[:include]
419       r.should have(2).items
420     end
421     it '作家を含んでいる' do
422       r = Comic.show_opt[:include]
423       r.has_key?(:author).should be_true
424     end
425     it 'ストーリーを含んでいる' do
426       r = Comic.show_opt[:include]
427       r.has_key?(:stories).should be_true
428     end
429       it 'ストーリーはコマを含んでいる' do
430         r = Comic.show_opt[:include]
431         r[:stories].has_key?(:panel).should be_true
432       end
433   end
434   describe 'json単体出力オプションに於いて' do
435     before do
436       @op = FactoryGirl.create :original_picture, :artist_id => @artist.id\r
437       @p = FactoryGirl.create :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @artist.id\r
438       @rp = FactoryGirl.create :resource_picture, :artist_id => @artist.id, :license_id => @license.id, :original_picture_id => @op.id, :picture_id => @p.id\r
439       @sbt = FactoryGirl.create :speech_balloon_template\r
440       @comic = FactoryGirl.create :comic, :author_id => @author.id, :visible => 1
441       @panel = FactoryGirl.create :panel, :author_id => @author.id, :publish => 1
442       @story = FactoryGirl.create :story, :author_id => @author.id, :comic_id => @comic.id, :panel_id => @panel.id
443     end
444     it 'ストーリーを含んでいる' do
445       r = Comic.show(@comic.id, @author).to_json Comic.show_json_opt
446       j = JSON.parse r
447       i = j
448       i.has_key?('stories').should be_true
449     end
450       it 'ストーリーはコマを含んでいる' do
451         r = Comic.show(@comic.id, @author).to_json Comic.show_json_opt
452         j = JSON.parse r
453         i = j
454         s = i['stories'].first
455         s.has_key?('panel').should be_true
456       end
457     it '作家を含んでいる' do
458       r = Comic.show(@comic.id, @author).to_json Comic.show_json_opt
459       j = JSON.parse r
460       i = j
461       i.has_key?('author').should be_true
462     end
463   end
464 end