OSDN Git Service

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