OSDN Git Service

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