OSDN Git Service

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