OSDN Git Service

t#30328:pull porting
[pettanr/pettanr.git] / spec / models / author_spec.rb
1 # -*- encoding: utf-8 -*-
2 #作家
3 require 'spec_helper'
4
5 describe Author 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   end
11
12   describe '検証に於いて' do
13     before do
14     end
15     
16     context 'オーソドックスなデータのとき' do
17       it '下限データが通る' do
18         @author.name = 'a'
19         @author.should be_valid
20       end
21       it '上限データが通る' do
22         @author.name = 'a'*30
23         @author.should be_valid
24       end
25     end
26     
27     context 'nameを検証するとき' do
28       it 'nullなら失敗する' do
29         @author.name = nil
30         @author.should_not be_valid
31       end
32       it '30文字以上なら失敗する' do
33         @author.name = 'a'*31
34         @author.should_not be_valid
35       end
36     end
37     context 'user_idを検証するとき' do
38       it 'nullなら失敗する' do
39         @author.user_id = nil
40         @author.should_not be_valid
41       end
42       it '数値でなければ失敗する' do
43         @author.user_id = 'a'
44         @author.should_not be_valid
45       end
46       it '存在するアカウントでなければ失敗する' do
47         @author.user_id = 0
48         @author.should_not be_valid
49       end
50     end
51   end
52   
53   describe 'デフォルト値補充に於いて' do
54     it '名前がno nameになっている' do
55       @author = FactoryGirl.build :author, :name => nil
56       @author.supply_default
57       @author.name.should eq 'no name'
58     end
59   end
60   
61   describe '上書き補充に於いて' do
62   end
63   
64   describe '所持判定に於いて' do
65     before do
66       @other_user = FactoryGirl.create :user_yas
67       @other_author = @other_user.author
68     end
69     context '事前チェックする' do
70       it '自身にロールリストからの作家取得を依頼している' do
71         Author.should_receive(:get_author_from_roles).with(any_args).exactly(1)
72         r = @author.own?([@author])
73       end
74     end
75     context 'ロール内作家が取得できるとき' do
76       before do
77       end
78       it 'ロール内作家のidが自身の作家idと一致するなら許可する' do
79         Author.stub(:get_author_from_roles).with(any_args).and_return(@author)
80         r = @author.own?([@author])
81         r.should be_true
82       end
83       it 'ロール内作家のidが自身の作家idと一致しないならno' do
84         Author.stub(:get_author_from_roles).with(any_args).and_return(@other_author)
85         @author.own?(@other_author).should be_false
86       end
87     end
88     context 'ロール内作家が取得できないとき' do
89       before do
90         Author.stub(:get_author_from_roles).with(any_args).and_return(nil)
91       end
92       it 'Falseを返す' do
93         r = @author.own?([@author])
94         r.should be_false
95       end
96     end
97   end
98   
99   describe '閲覧許可に於いて' do
100     before do
101     end
102     context 'オープンモードのとき' do
103       before do
104         MagicNumber['run_mode'] = 0
105       end
106       it '自身にゲスト用ロールチェックを問い合わせしている' do
107         Author.any_instance.stub(:guest_role_check).and_return(true)
108         Author.any_instance.should_receive(:guest_role_check).with(any_args).exactly(1)
109         r = @author.visible?([@author])
110       end
111       it 'ゲスト用ロールチェックが失敗したとき、falseを返す' do
112         Author.any_instance.stub(:guest_role_check).and_return(false)
113         r = @author.visible?([@author])
114         r.should be_false
115       end
116     end
117     context 'クローズドモードのとき' do
118       before do
119         MagicNumber['run_mode'] = 1
120       end
121       it '自身に読者用ロールチェックを問い合わせしている' do
122         Author.any_instance.stub(:reader_role_check).and_return(true)
123         Author.any_instance.should_receive(:reader_role_check).with(any_args).exactly(1)
124         r = @author.visible?([@author])
125       end
126       it '読者用ロールチェックが失敗したとき、falseを返す' do
127         Author.any_instance.stub(:reader_role_check).and_return(false)
128         r = @author.visible?([@author])
129         r.should be_false
130       end
131     end
132     context '事前チェックする' do
133       before do
134         MagicNumber['run_mode'] = 1
135         Author.any_instance.stub(:reader_role_check).and_return(true)
136       end
137     end
138     context 'つつがなく終わるとき' do
139       before do
140         MagicNumber['run_mode'] = 1
141         Author.any_instance.stub(:reader_role_check).and_return(true)
142       end
143       it '許可する' do
144         r = @author.visible?([@author])
145         r.should be_true
146       end
147     end
148   end
149   
150   describe '絵師作家判定に於いて' do
151     before do
152     end
153     context 'Trueのケース' do
154       it 'リンクされた絵師がある' do
155         artist = FactoryGirl.create :artist, :author_id => @author.id
156         @author.artist?.should eq true
157       end
158     end
159     context 'Falseのケース' do
160       it 'リンクされた絵師がない' do
161         @author.artist?.should eq false
162       end
163     end
164   end
165   
166   describe '一覧取得に於いて' do
167     before do
168     end
169     context 'page補正について' do
170       it '文字列から数値に変換される' do
171         Author.page('8').should eq 8
172       end
173       it 'nilの場合は1になる' do
174         Author.page().should eq 1
175       end
176       it '0以下の場合は1になる' do
177         Author.page('0').should eq 1
178       end
179     end
180     context 'page_size補正について' do
181       it '文字列から数値に変換される' do
182         Author.page_size('7').should eq 7
183       end
184       it 'nilの場合はAuthor.default_page_sizeになる' do
185         Author.page_size().should eq Author.default_page_size
186       end
187       it '0以下の場合はAuthor.default_page_sizeになる' do
188         Author.page_size('0').should eq Author.default_page_size
189       end
190       it 'Author.max_page_sizeを超えた場合はAuthor.max_page_sizeになる' do
191         Author.page_size('1000').should eq Author.max_page_size
192       end
193     end
194     context 'つつがなく終わるとき' do
195       it '一覧取得オプションを利用している' do
196         Author.stub(:list_opt).with(any_args).and_return({})
197         Author.should_receive(:list_opt).with(any_args).exactly(1)
198         r = Author.list
199       end
200     end
201     it 'リストを返す' do
202       r = Author.list
203       r.should eq [@author]
204     end
205     it '作成時系列で並んでいる' do
206       @other_user = FactoryGirl.create :user_yas
207       @other_author = FactoryGirl.create :author, :user_id => @other_user.id
208       n = @other_user.author
209       n.created_at = Time.now + 100
210       n.save
211       l = Author.list
212       l.should eq [n, @author]
213     end
214     context 'DBに5件あって1ページの件数を2件に変えたとして' do
215       before do
216         @other_user2 = FactoryGirl.create :user_yas
217         @author2 = FactoryGirl.create :author, :user_id => @other_user2.id
218         @author2.created_at = Time.now + 100
219         @author2.save
220         @other_user3 = FactoryGirl.create :user_yas
221         @author3 = FactoryGirl.create :author, :user_id => @other_user3.id
222         @author3.created_at = Time.now + 200
223         @author3.save
224         @other_user4 = FactoryGirl.create :user_yas
225         @author4 = FactoryGirl.create :author, :user_id => @other_user4.id
226         @author4.created_at = Time.now + 300
227         @author4.save
228         @other_user5 = FactoryGirl.create :user_yas
229         @author5 = FactoryGirl.create :author, :user_id => @other_user5.id
230         @author5.created_at = Time.now + 400
231         @author5.save
232         Author.stub(:default_page_size).and_return(2)
233       end
234       it '通常は2件を返す' do
235         r = Author.list
236         r.should have(2).items 
237       end
238       it 'page=1なら末尾2件を返す' do
239         #時系列で並んでいる
240         r = Author.list(1)
241         r.should eq [@author5, @author4]
242       end
243       it 'page=2なら中間2件を返す' do
244         r = Author.list(2)
245         r.should eq [@author3, @author2]
246       end
247       it 'page=3なら先頭1件を返す' do
248         r = Author.list(3)
249         r.should eq [@author]
250       end
251     end
252     context 'DBに5件あって1ページの件数を0件に変えたとして' do
253       before do
254         @other_user2 = FactoryGirl.create :user_yas
255         @author2 = FactoryGirl.create :author, :user_id => @other_user2.id
256         @author2.created_at = Time.now + 100
257         @author2.save
258         @other_user3 = FactoryGirl.create :user_yas
259         @author3 = FactoryGirl.create :author, :user_id => @other_user3.id
260         @author3.created_at = Time.now + 200
261         @author3.save
262         @other_user4 = FactoryGirl.create :user_yas
263         @author4 = FactoryGirl.create :author, :user_id => @other_user4.id
264         @author4.created_at = Time.now + 300
265         @author4.save
266         @other_user5 = FactoryGirl.create :user_yas
267         @author5 = FactoryGirl.create :author, :user_id => @other_user5.id
268         @author5.created_at = Time.now + 400
269         @author5.save
270         Author.stub(:default_page_size).and_return(2)
271       end
272       it '通常は全件(5件)を返す' do
273         r = Author.list 5, 0
274         r.should have(5).items 
275       end
276     end
277   end
278   describe '一覧取得オプションに於いて' do
279     it 'includeキーを含んでいる' do
280       r = Author.list_opt
281       r.has_key?(:include).should be_true
282     end
283     it '1つの項目を含んでいる' do
284       r = Author.list_opt[:include]
285       r.should have(1).items
286     end
287     it '絵師を含んでいる' do
288       r = Author.list_opt[:include]
289       r.has_key?(:artist).should be_true
290     end
291   end
292   describe 'json一覧出力オプションに於いて' do
293     before do
294       @artist = FactoryGirl.create :artist_yas, :author_id => @author.id
295     end
296     it '絵師を含んでいる' do
297       r = Author.list.to_json Author.list_json_opt
298       j = JSON.parse r
299       i = j.first
300       i.has_key?('artist').should be_true
301     end
302   end
303   
304   describe '単体取得に於いて' do
305     before do
306     end
307     context 'つつがなく終わるとき' do
308       it '単体取得オプションを利用している' do
309         Author.stub(:show_opt).with(any_args).and_return({})
310         Author.should_receive(:show_opt).with(any_args).exactly(1)
311         r = Author.show @author.id, @author
312       end
313       it '閲覧許可を問い合わせている' do
314         Author.any_instance.stub(:visible?).with(any_args).and_return(true)
315         Author.any_instance.should_receive(:visible?).with(any_args).exactly(1)
316         r = Author.show @author.id, @author
317       end
318     end
319     it '指定の作家を返す' do
320       r = Author.show @author.id, @author
321       r.should eq @author
322     end
323     context '閲覧許可が出なかったとき' do
324       it '403Forbidden例外を返す' do
325         Author.any_instance.stub(:visible?).and_return(false)
326         lambda{
327           Author.show @author.id, @author
328         }.should raise_error(ActiveRecord::Forbidden)
329       end
330     end
331     context '存在しない作家を開こうとしたとき' do
332       it '404RecordNotFound例外を返す' do
333         lambda{
334           Author.show 110, @author
335         }.should raise_error(ActiveRecord::RecordNotFound)
336       end
337     end
338   end
339   describe '編集取得に於いて' do
340     before do
341     end
342     context 'つつがなく終わるとき' do
343       it '単体取得オプションを利用している' do
344         Author.stub(:show_opt).with(any_args).and_return({})
345         Author.should_receive(:show_opt).with(any_args).exactly(1)
346         r = Author.edit @author.id, @author
347       end
348       it '所持判定を問い合わせている' do
349         Author.any_instance.stub(:own?).with(any_args).and_return(true)
350         Author.any_instance.should_receive(:own?).with(any_args).exactly(1)
351         r = Author.edit @author.id, @author
352       end
353     end
354     it '指定の作家を返す' do
355       Author.any_instance.stub(:own?).and_return(true)
356       r = Author.edit @author.id, @author.id
357       r.should eq @author
358     end
359     context '他人の作家を開こうとしたとき' do
360       it '403Forbidden例外を返す' do
361         Author.any_instance.stub(:own?).and_return(false)
362         lambda{
363           Author.edit @author.id, @author
364         }.should raise_error(ActiveRecord::Forbidden)
365       end
366     end
367     context '存在しない作家を開こうとしたとき' do
368       it '404RecordNotFound例外を返す' do
369         lambda{
370           Author.edit 110, @author
371         }.should raise_error(ActiveRecord::RecordNotFound)
372       end
373     end
374   end
375   describe '単体取得オプションに於いて' do
376     it 'includeキーを含んでいる' do
377       r = Author.show_opt
378       r.has_key?(:include).should be_true
379     end
380     it '1つの項目を含んでいる' do
381       r = Author.show_opt[:include]
382       r.should have(1).items
383     end
384     it '絵師を含んでいる' do
385       r = Author.show_opt[:include]
386       r.has_key?(:artist).should be_true
387     end
388   end
389   describe 'json単体出力オプションに於いて' do
390     before do
391       @artist = FactoryGirl.create :artist_yas, :author_id => @author.id
392     end
393     it '絵師を含んでいる' do
394       r = Author.show(@author.id, @author).to_json Author.show_json_opt
395       j = JSON.parse r
396       i = j
397       i.has_key?('artist').should be_true
398     end
399   end
400   
401   describe 'マイリストページ制御パラメータに於いて' do
402     before do
403     end
404     context 'コミックpage_size補正について' do
405       it '文字列から数値に変換される' do
406         Author.comic_page_size('7').should eq 7
407       end
408       it 'nilの場合はAuthor.default_comic_page_sizeになる' do
409         Author.comic_page_size().should eq Author.default_comic_page_size
410       end
411       it '0以下の場合はAuthor.default_comic_page_sizeになる' do
412         Author.comic_page_size('0').should eq Author.default_comic_page_size
413       end
414       it 'Author.comic_max_page_sizeを超えた場合はAuthor.comic_max_page_sizeになる' do
415         Author.comic_page_size('1000').should eq Author.comic_max_page_size
416       end
417     end
418     context 'ストーリーpage_size補正について' do
419       it '文字列から数値に変換される' do
420         Author.story_page_size('7').should eq 7
421       end
422       it 'nilの場合はAuthor.default_story_page_sizeになる' do
423         Author.story_page_size().should eq Author.default_story_page_size
424       end
425       it '0以下の場合はAuthor.default_story_page_sizeになる' do
426         Author.story_page_size('0').should eq Author.default_story_page_size
427       end
428       it 'Author.story_max_page_sizeを超えた場合はAuthor.story_max_page_sizeになる' do
429         Author.story_page_size('1000').should eq Author.story_max_page_size
430       end
431     end
432     context 'コマ絵page_size補正について' do
433       it '文字列から数値に変換される' do
434         Author.panel_page_size('7').should eq 7
435       end
436       it 'nilの場合はAuthor.default_panel_page_sizeになる' do
437         Author.panel_page_size().should eq Author.default_panel_page_size
438       end
439       it '0以下の場合はAuthor.default_panel_page_sizeになる' do
440         Author.panel_page_size('0').should eq Author.default_panel_page_size
441       end
442       it 'Author.panel_max_page_sizeを超えた場合はAuthor.panel_max_page_sizeになる' do
443         Author.panel_page_size('1000').should eq Author.panel_max_page_size
444       end
445     end
446     context '景色素材page_size補正について' do
447       it '文字列から数値に変換される' do
448         Author.panel_picture_page_size('7').should eq 7
449       end
450       it 'nilの場合はAuthor.default_panel_picture_page_sizeになる' do
451         Author.panel_picture_page_size().should eq Author.default_panel_picture_page_size
452       end
453       it '0以下の場合はAuthor.default_panel_picture_page_sizeになる' do
454         Author.panel_picture_page_size('0').should eq Author.default_panel_picture_page_size
455       end
456       it 'Author.panel_picture_max_page_sizeを超えた場合はAuthor.panel_picture_max_page_sizeになる' do
457         Author.panel_picture_page_size('1000').should eq Author.panel_picture_max_page_size
458       end
459     end
460     context '景色カラーpage_size補正について' do
461       it '文字列から数値に変換される' do
462         Author.ground_picture_page_size('7').should eq 7
463       end
464       it 'nilの場合はAuthor.default_ground_picture_page_sizeになる' do
465         Author.ground_picture_page_size().should eq Author.default_ground_picture_page_size
466       end
467       it '0以下の場合はAuthor.default_ground_picture_page_sizeになる' do
468         Author.ground_picture_page_size('0').should eq Author.default_ground_picture_page_size
469       end
470       it 'Author.ground_picture_max_page_sizeを超えた場合はAuthor.ground_picture_max_page_sizeになる' do
471         Author.ground_picture_page_size('1000').should eq Author.ground_picture_max_page_size
472       end
473     end
474     context '景色カラーコードpage_size補正について' do
475       it '文字列から数値に変換される' do
476         Author.ground_color_page_size('7').should eq 7
477       end
478       it 'nilの場合はAuthor.default_ground_color_page_sizeになる' do
479         Author.ground_color_page_size().should eq Author.default_ground_color_page_size
480       end
481       it '0以下の場合はAuthor.default_ground_color_page_sizeになる' do
482         Author.ground_color_page_size('0').should eq Author.default_ground_color_page_size
483       end
484       it 'Author.ground_color_max_page_sizeを超えた場合はAuthor.ground_color_max_page_sizeになる' do
485         Author.ground_color_page_size('1000').should eq Author.ground_color_max_page_size
486       end
487     end
488     context '素材page_size補正について' do
489       it '文字列から数値に変換される' do
490         Author.resource_picture_page_size('7').should eq 7
491       end
492       it 'nilの場合はAuthor.default_resource_picture_page_sizeになる' do
493         Author.resource_picture_page_size().should eq Author.default_resource_picture_page_size
494       end
495       it '0以下の場合はAuthor.default_resource_picture_page_sizeになる' do
496         Author.resource_picture_page_size('0').should eq Author.default_resource_picture_page_size
497       end
498       it 'Author.resource_picture_max_page_sizeを超えた場合はAuthor.resource_picture_max_page_sizeになる' do
499         Author.resource_picture_page_size('1000').should eq Author.resource_picture_max_page_size
500       end
501     end
502   end
503 end