OSDN Git Service

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