OSDN Git Service

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