OSDN Git Service

t#30200:update i18n devise
[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     it '自分の絵師ならyes' do
74       @artist.own?(@author).should == true
75     end
76     it '他人の絵師ならno' do
77       @artist.own?(@other_author).should == false
78     end
79     it 'パラメータが作家でないならならno' do
80       @artist.own?(nil).should == false
81     end
82   end
83   
84   describe '閲覧許可に於いて' do
85     before do
86       @artist = FactoryGirl.create :artist, :author_id => @author.id
87     end
88     context '検査対象がnil(ゲスト)のとき' do
89       context 'クローズドモードのとき' do
90         before do
91           MagicNumber['run_mode'] = 1
92         end
93         it '不許可を返す。' do
94           r = @artist.visible?(nil)
95           r.should be_false
96         end
97       end
98       context 'オープンモードのとき' do
99         before do
100           MagicNumber['run_mode'] = 0
101         end
102         it '許可する' do
103           r = @artist.visible?(nil)
104           r.should be_true
105         end
106       end
107     end
108     context '検査対象が作家のとき' do
109       it '許可する' do
110         r = @artist.visible?(@author)
111         r.should == true
112       end
113     end
114     context '検査対象がそれ以外のとき' do
115       it '不許可を返す。' do
116         r = @artist.visible?(@admin)
117         r.should be_false
118       end
119     end
120   end
121   
122   describe '一覧取得に於いて' do
123     before do
124       @artist = FactoryGirl.create :artist, :author_id => @author.id
125     end
126     context 'page補正について' do
127       it '文字列から数値に変換される' do
128         Artist.page('8').should eq 8
129       end
130       it 'nilの場合は1になる' do
131         Artist.page().should eq 1
132       end
133       it '0以下の場合は1になる' do
134         Artist.page('0').should eq 1
135       end
136     end
137     context 'page_size補正について' do
138       it '文字列から数値に変換される' do
139         Artist.page_size('7').should eq 7
140       end
141       it 'nilの場合はArtist.default_page_sizeになる' do
142         Artist.page_size().should eq Artist.default_page_size
143       end
144       it '0以下の場合はArtist.default_page_sizeになる' do
145         Artist.page_size('0').should eq Artist.default_page_size
146       end
147       it 'Artist.max_page_sizeを超えた場合はArtist.max_page_sizeになる' do
148         Artist.page_size('1000').should eq Artist.max_page_size
149       end
150     end
151     context 'つつがなく終わるとき' do
152       it '一覧取得オプションを利用している' do
153         Artist.stub(:list_opt).with(any_args).and_return({})
154         Artist.should_receive(:list_opt).with(any_args).exactly(1)
155         r = Artist.list
156       end
157     end
158     it 'リストを返す' do
159       r = Artist.list
160       r.should eq [@artist]
161     end
162     it '作成時系列で並んでいる' do
163       n = FactoryGirl.create :artist, :author_id => @author.id, :name => 'artist', :created_at => Time.now + 100
164       l = Artist.list
165       l.should eq [n, @artist]
166     end
167     context 'DBに5件あって1ページの件数を2件に変えたとして' do
168       before do
169         @artist2 = FactoryGirl.create :artist, :author_id => @author.id, :name => 'artist2', :created_at => Time.now + 100
170         @artist3 = FactoryGirl.create :artist, :author_id => @author.id, :name => 'artist3', :created_at => Time.now + 200
171         @artist4 = FactoryGirl.create :artist, :author_id => @author.id, :name => 'artist4', :created_at => Time.now + 300
172         @artist5 = FactoryGirl.create :artist, :author_id => @author.id, :name => 'artist5', :created_at => Time.now + 400
173         Artist.stub(:default_page_size).and_return(2)
174       end
175       it '通常は2件を返す' do
176         r = Artist.list
177         r.should have(2).items 
178       end
179       it 'page=1なら末尾2件を返す' do
180         #時系列で並んでいる
181         r = Artist.list(1)
182         r.should eq [@artist5, @artist4]
183       end
184       it 'page=2なら中間2件を返す' do
185         r = Artist.list(2)
186         r.should eq [@artist3, @artist2]
187       end
188       it 'page=3なら先頭1件を返す' do
189         r = Artist.list(3)
190         r.should eq [@artist]
191       end
192     end
193     context 'DBに5件あって1ページの件数を0件に変えたとして' do
194       before do
195         @artist2 = FactoryGirl.create :artist, :author_id => @author.id, :name => 'artist2', :created_at => Time.now + 100
196         @artist3 = FactoryGirl.create :artist, :author_id => @author.id, :name => 'artist3', :created_at => Time.now + 200
197         @artist4 = FactoryGirl.create :artist, :author_id => @author.id, :name => 'artist4', :created_at => Time.now + 300
198         @artist5 = FactoryGirl.create :artist, :author_id => @author.id, :name => 'artist5', :created_at => Time.now + 400
199         Artist.stub(:default_page_size).and_return(2)
200       end
201       it '通常は全件(5件)を返す' do
202         r = Artist.list 5, 0
203         r.should have(5).items 
204       end
205     end
206   end
207   describe '一覧取得オプションに於いて' do
208     it 'includeキーを含んでいる' do
209       r = Artist.list_opt
210       r.has_key?(:include).should be_true
211     end
212     it '1つの項目を含んでいる' do
213       r = Artist.list_opt[:include]
214       r.should have(1).items
215     end
216     it '作家を含んでいる' do
217       r = Artist.list_opt[:include]
218       r.has_key?(:author).should be_true
219     end
220   end
221   describe 'json一覧出力オプションに於いて' do
222     before do
223       @artist = FactoryGirl.create :artist, :author_id => @author.id
224     end
225     it '作家を含んでいる' do
226       r = Artist.list.to_json Artist.list_json_opt
227       j = JSON.parse r
228       i = j.first
229       i.has_key?('author').should be_true
230     end
231   end
232   
233   describe '単体取得に於いて' do
234     before do
235       @artist = FactoryGirl.create :artist, :author_id => @author.id
236     end
237     context 'つつがなく終わるとき' do
238       it '単体取得オプションを利用している' do
239         Artist.stub(:show_opt).with(any_args).and_return({})
240         Artist.should_receive(:show_opt).with(any_args).exactly(1)
241         r = Artist.show @artist.id, @author
242       end
243       it '閲覧許可を問い合わせている' do
244         Artist.any_instance.stub(:visible?).with(any_args).and_return(true)
245         Artist.any_instance.should_receive(:visible?).with(any_args).exactly(1)
246         r = Artist.show @artist.id, @author
247       end
248     end
249     it '指定の絵師を返す' do
250       a = Artist.show @artist.id, @author
251       a.should eq @artist
252     end
253     context '閲覧許可が出なかったとき' do
254       it '403Forbidden例外を返す' do
255         Artist.any_instance.stub(:visible?).and_return(false)
256         lambda{
257           Artist.show @artist.id, @author
258         }.should raise_error(ActiveRecord::Forbidden)
259       end
260     end
261     context '存在しない絵師を開こうとしたとき' do
262       it '404RecordNotFound例外を返す' do
263         lambda{
264           Artist.show 110, @author
265         }.should raise_error(ActiveRecord::RecordNotFound)
266       end
267     end
268   end
269   describe '編集取得に於いて' do
270     before do
271       @artist = FactoryGirl.create :artist, :author_id => @author.id
272     end
273     context 'つつがなく終わるとき' do
274       it '単体取得オプションを利用している' do
275         Artist.stub(:show_opt).with(any_args).and_return({})
276         Artist.should_receive(:show_opt).with(any_args).exactly(1)
277         r = Artist.edit @artist.id, @author
278       end
279       it '所持判定を問い合わせている' do
280         Artist.any_instance.stub(:own?).with(any_args).and_return(true)
281         Artist.any_instance.should_receive(:own?).with(any_args).exactly(1)
282         r = Artist.edit @artist.id, @author
283       end
284     end
285     it '指定の絵師を返す' do
286       Artist.any_instance.stub(:own?).and_return(true)
287       r = Artist.edit @artist.id, @author.id
288       r.should eq @artist
289     end
290     context '他人の絵師を開こうとしたとき' do
291       it '403Forbidden例外を返す' do
292         Artist.any_instance.stub(:own?).and_return(false)
293         lambda{
294           Artist.edit @artist.id, @author
295         }.should raise_error(ActiveRecord::Forbidden)
296       end
297     end
298     context '存在しない絵師を開こうとしたとき' do
299       it '404RecordNotFound例外を返す' do
300         lambda{
301           Artist.edit 110, @author
302         }.should raise_error(ActiveRecord::RecordNotFound)
303       end
304     end
305   end
306   describe '単体取得オプションに於いて' do
307     it 'includeキーを含んでいる' do
308       r = Artist.show_opt
309       r.has_key?(:include).should be_true
310     end
311     it '1つの項目を含んでいる' do
312       r = Artist.show_opt[:include]
313       r.should have(1).items
314     end
315   end
316   describe 'json単体出力オプションに於いて' do
317     before do
318       @artist = FactoryGirl.create :artist, :author_id => @author.id
319     end
320     it '作家を含んでいる' do
321       r = Artist.show(@artist.id, @author).to_json Artist.show_json_opt
322       j = JSON.parse r
323       i = j
324       i.has_key?('author').should be_true
325     end
326   end
327 end