OSDN Git Service

t#31470:create pager
[pettanr/pettanr.git] / spec / models / artist_spec.rb
1 # -*- encoding: utf-8 -*-
2 #絵師
3 require 'spec_helper'
4
5 describe Artist 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     @other_user = FactoryGirl.create( :user_yas)
11     @other_author = FactoryGirl.create :author, :user_id => @other_user.id
12     @sp = FactoryGirl.create :system_picture
13     @lg = FactoryGirl.create :license_group
14     @license = FactoryGirl.create :license, :license_group_id => @lg.id, :system_picture_id => @sp.id
15   end
16
17   describe '検証に於いて' do
18     before do
19       @artist = FactoryGirl.build :artist, :author_id => @author.id
20     end
21     
22     context 'オーソドックスなデータのとき' do
23       it '下限データが通る' do
24         @artist.name = 'a'
25         @artist.should be_valid
26       end
27       it '上限データが通る' do
28         @artist.name = 'a'*30
29         @artist.should be_valid
30       end
31     end
32     
33     context 'nameを検証するとき' do
34       it 'nullなら失敗する' do
35         @artist.name = nil
36         @artist.should_not be_valid
37       end
38       it '30文字以上なら失敗する' do
39         @artist.name = 'a'*31
40         @artist.should_not be_valid
41       end
42     end
43     context 'author_idを検証するとき' do
44       #絵師は独立絵師があるので数値チェックだけ
45       it '数値でなければ失敗する' do
46         @artist.author_id = 'a'
47         @artist.should_not be_valid
48       end
49     end
50   end
51   
52   describe '文字コード検証に於いて' do
53     before do
54       @artist = FactoryGirl.build :artist, :author_id => @author.id
55     end
56     
57     context 'nameを検証するとき' do
58       it 'Shift JISなら失敗する' do
59         @artist.name = "\x83G\x83r\x83]\x83D"
60         lambda{
61           @artist.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       @artist = FactoryGirl.build :artist, :name => nil
71       @artist.supply_default
72       @artist.name.should eq 'no name'
73     end
74   end
75   
76   describe '上書き補充に於いて' do
77     it '作家idが設定されている' do
78       @artist = FactoryGirl.build :artist
79       @artist.overwrite @author
80       @artist.author_id.should eq @author.id
81     end
82   end
83   
84   describe '所持判定に於いて' do
85     before do
86       @artist = FactoryGirl.create :artist, :author_id => @author.id
87       @other_artist = FactoryGirl.create :artist_yas, :author_id => @other_author.id
88     end
89     context '事前チェックする' do
90       it '自身にロールリストからの作家取得を依頼している' do
91         Artist.should_receive(:get_author_from_roles).with(any_args).exactly(1)
92         r = @artist.own?([@author])
93       end
94     end
95     context 'ロール内作家が取得できるとき' do
96       before do
97       end
98       it 'ロール内作家のidが自身の作家idと一致するなら許可する' do
99         Artist.stub(:get_author_from_roles).with(any_args).and_return(@author)
100         r = @artist.own?([@author])
101         r.should be_true
102       end
103       it 'ロール内作家のidが自身の作家idと一致しないならno' do
104         Artist.stub(:get_author_from_roles).with(any_args).and_return(@other_author)
105         @artist.own?(@other_author).should be_false
106       end
107     end
108     context 'ロール内作家が取得できないとき' do
109       before do
110         Artist.stub(:get_author_from_roles).with(any_args).and_return(nil)
111       end
112       it 'Falseを返す' do
113         r = @artist.own?([@author])
114         r.should be_false
115       end
116     end
117   end
118   
119   describe '閲覧許可に於いて' do
120     before do
121       @artist = FactoryGirl.create :artist, :author_id => @author.id
122     end
123     context 'オープンモードのとき' do
124       before do
125         MagicNumber['run_mode'] = 0
126       end
127       it '自身にゲスト用ロールチェックを問い合わせしている' do
128         Artist.any_instance.stub(:guest_role_check).and_return(true)
129         Artist.any_instance.should_receive(:guest_role_check).with(any_args).exactly(1)
130         r = @artist.visible?([@author])
131       end
132       it 'ゲスト用ロールチェックが失敗したとき、falseを返す' do
133         Artist.any_instance.stub(:guest_role_check).and_return(false)
134         r = @artist.visible?([@author])
135         r.should be_false
136       end
137     end
138     context 'クローズドモードのとき' do
139       before do
140         MagicNumber['run_mode'] = 1
141       end
142       it '自身に素材読者用ロールチェックを問い合わせしている' do
143         Artist.any_instance.stub(:resource_reader_role_check).and_return(true)
144         Artist.any_instance.should_receive(:resource_reader_role_check).with(any_args).exactly(1)
145         r = @artist.visible?([@author])
146       end
147       it '素材読者用ロールチェックが失敗したとき、falseを返す' do
148         Artist.any_instance.stub(:resource_reader_role_check).and_return(false)
149         r = @artist.visible?([@author])
150         r.should be_false
151       end
152     end
153     context 'つつがなく終わるとき' do
154       before do
155         MagicNumber['run_mode'] = 1
156         Artist.any_instance.stub(:resource_reader_role_check).and_return(true)
157       end
158       it '許可する' do
159         r = @artist.visible?([@author])
160         r.should be_true
161       end
162     end
163   end
164   
165   describe '一覧取得に於いて' do
166     before do
167       @artist = FactoryGirl.create :artist, :author_id => @author.id
168     end
169     context 'page補正について' do
170       it '文字列から数値に変換される' do
171         Artist.page('8').should eq 8
172       end
173       it 'nilの場合は1になる' do
174         Artist.page().should eq 1
175       end
176       it '0以下の場合は1になる' do
177         Artist.page('0').should eq 1
178       end
179     end
180     context 'page_size補正について' do
181       it '文字列から数値に変換される' do
182         Artist.page_size('7').should eq 7
183       end
184       it 'nilの場合はArtist.default_page_sizeになる' do
185         Artist.page_size().should eq Artist.default_page_size
186       end
187       it '0以下の場合はArtist.default_page_sizeになる' do
188         Artist.page_size('0').should eq Artist.default_page_size
189       end
190       it 'Artist.max_page_sizeを超えた場合はArtist.max_page_sizeになる' do
191         Artist.page_size('1000').should eq Artist.max_page_size
192       end
193     end
194     context 'つつがなく終わるとき' do
195       it '一覧取得オプションを利用している' do
196         Artist.stub(:list_opt).with(any_args).and_return({})
197         Artist.should_receive(:list_opt).with(any_args).exactly(1)
198         r = Artist.list
199       end
200     end
201     it 'リストを返す' do
202       r = Artist.list
203       r.should eq [@artist]
204     end
205     it '作成時系列で並んでいる' do
206       n = FactoryGirl.create :artist, :author_id => @author.id, :name => 'artist', :created_at => Time.now + 100
207       l = Artist.list
208       l.should eq [n, @artist]
209     end
210     it 'ただし、内絵師(作家idが空)だけとする' do
211       n = FactoryGirl.create :artist, :author_id => nil
212       l = Artist.list
213       l.should eq [@artist]
214     end
215     context 'DBに5件あって1ページの件数を2件に変えたとして' do
216       before do
217         @artist2 = FactoryGirl.create :artist, :author_id => @author.id, :name => 'artist2', :created_at => Time.now + 100
218         @artist3 = FactoryGirl.create :artist, :author_id => @author.id, :name => 'artist3', :created_at => Time.now + 200
219         @artist4 = FactoryGirl.create :artist, :author_id => @author.id, :name => 'artist4', :created_at => Time.now + 300
220         @artist5 = FactoryGirl.create :artist, :author_id => @author.id, :name => 'artist5', :created_at => Time.now + 400
221         Artist.stub(:default_page_size).and_return(2)
222       end
223       it '通常は2件を返す' do
224         r = Artist.list
225         r.should have(2).items 
226       end
227       it 'page=1なら末尾2件を返す' do
228         #時系列で並んでいる
229         r = Artist.list(1)
230         r.should eq [@artist5, @artist4]
231       end
232       it 'page=2なら中間2件を返す' do
233         r = Artist.list(2)
234         r.should eq [@artist3, @artist2]
235       end
236       it 'page=3なら先頭1件を返す' do
237         r = Artist.list(3)
238         r.should eq [@artist]
239       end
240     end
241   end
242   
243   describe '一覧ページ制御に於いて' do
244     before do
245       Artist.stub(:count).with(any_args).and_return(100)
246     end
247     it 'ページ制御を返す' do
248       r = Artist.list_paginate
249       r.is_a?(Kaminari::PaginatableArray).should be_true
250     end
251     it '一覧の取得条件を利用している' do
252       Artist.stub(:list_where).with(any_args).and_return('')
253       Artist.should_receive(:list_where).with(any_args).exactly(1)
254       r = Artist.list_paginate
255     end
256     it 'ページ件数10のとき、3ページ目のオフセットは20から始まる' do
257       r = Artist.list_paginate 3, 10
258       r.limit_value.should eq 10
259       r.offset_value.should eq 20
260     end
261   end
262   
263   describe '一覧取得オプションに於いて' do
264     it '1つの項目を含んでいる' do
265       r = Artist.list_opt
266       r.should have(1).items
267     end
268     it '作家を含んでいる' do
269       r = Artist.list_opt
270       r.has_key?(:author).should be_true
271     end
272   end
273   describe 'json一覧出力オプションに於いて' do
274     before do
275       @artist = FactoryGirl.create :artist, :author_id => @author.id
276     end
277     it '作家を含んでいる' do
278       r = Artist.list.to_json Artist.list_json_opt
279       j = JSON.parse r
280       i = j.first
281       i.has_key?('author').should be_true
282     end
283   end
284   
285   describe '単体取得に於いて' do
286     before do
287       @artist = FactoryGirl.create :artist, :author_id => @author.id
288     end
289     context 'つつがなく終わるとき' do
290       it '単体取得オプションを利用している' do
291         Artist.stub(:show_opt).with(any_args).and_return({})
292         Artist.should_receive(:show_opt).with(any_args).exactly(1)
293         r = Artist.show @artist.id, @author
294       end
295       it '閲覧許可を問い合わせている' do
296         Artist.any_instance.stub(:visible?).with(any_args).and_return(true)
297         Artist.any_instance.should_receive(:visible?).with(any_args).exactly(1)
298         r = Artist.show @artist.id, @author
299       end
300     end
301     it '指定の絵師を返す' do
302       a = Artist.show @artist.id, @author
303       a.should eq @artist
304     end
305     context '閲覧許可が出なかったとき' do
306       it '403Forbidden例外を返す' do
307         Artist.any_instance.stub(:visible?).and_return(false)
308         lambda{
309           Artist.show @artist.id, @author
310         }.should raise_error(ActiveRecord::Forbidden)
311       end
312     end
313     context '存在しない絵師を開こうとしたとき' do
314       it '404RecordNotFound例外を返す' do
315         lambda{
316           Artist.show 110, @author
317         }.should raise_error(ActiveRecord::RecordNotFound)
318       end
319     end
320   end
321   describe '編集取得に於いて' do
322     before do
323       @artist = FactoryGirl.create :artist, :author_id => @author.id
324     end
325     context 'つつがなく終わるとき' do
326       it '単体取得オプションを利用している' do
327         Artist.stub(:show_opt).with(any_args).and_return({})
328         Artist.should_receive(:show_opt).with(any_args).exactly(1)
329         r = Artist.edit @artist.id, @author
330       end
331       it '所持判定を問い合わせている' do
332         Artist.any_instance.stub(:own?).with(any_args).and_return(true)
333         Artist.any_instance.should_receive(:own?).with(any_args).exactly(1)
334         r = Artist.edit @artist.id, @author
335       end
336     end
337     it '指定の絵師を返す' do
338       Artist.any_instance.stub(:own?).and_return(true)
339       r = Artist.edit @artist.id, @author.id
340       r.should eq @artist
341     end
342     context '他人の絵師を開こうとしたとき' do
343       it '403Forbidden例外を返す' do
344         Artist.any_instance.stub(:own?).and_return(false)
345         lambda{
346           Artist.edit @artist.id, @author
347         }.should raise_error(ActiveRecord::Forbidden)
348       end
349     end
350     context '存在しない絵師を開こうとしたとき' do
351       it '404RecordNotFound例外を返す' do
352         lambda{
353           Artist.edit 110, @author
354         }.should raise_error(ActiveRecord::RecordNotFound)
355       end
356     end
357   end
358   describe '単体取得オプションに於いて' do
359     it 'includeキーを含んでいる' do
360       r = Artist.show_opt
361       r.has_key?(:include).should be_true
362     end
363     it '1つの項目を含んでいる' do
364       r = Artist.show_opt[:include]
365       r.should have(1).items
366     end
367   end
368   describe 'json単体出力オプションに於いて' do
369     before do
370       @artist = FactoryGirl.create :artist, :author_id => @author.id
371     end
372     it '作家を含んでいる' do
373       r = Artist.show(@artist.id, @author).to_json Artist.show_json_opt
374       j = JSON.parse r
375       i = j
376       i.has_key?('author').should be_true
377     end
378   end
379   
380   describe '有効絵師数に於いて' do
381     before do
382       @artist = FactoryGirl.create :artist, :author_id => @author.id
383       @artist = FactoryGirl.create :artist, :author_id => @other_author.id
384       @artist = FactoryGirl.create :artist, :author_id => nil
385     end
386     it '内絵師数を返す' do
387       r = Artist.visible_count
388       r.should eq 2
389     end
390   end
391   
392   describe 'エクスポートに於いて' do
393     before do
394       @artist = FactoryGirl.create :artist, :author_id => @author.id
395       @artist2 = FactoryGirl.create :artist, :author_id => @other_author.id, :updated_at => Time.now - 3000
396       @artist3 = FactoryGirl.create :artist, :author_id => nil
397     end
398     it '開始日時が省略された場合はすべての内絵師を返す' do
399       r = Artist.export 
400       r.should eq [@artist, @artist2]
401     end
402     it '開始日時以降に更新された内絵師を返す' do
403       r = Artist.export @artist.updated_at - 100
404       r.should eq [@artist]
405     end
406   end
407   
408
409 end