1 # -*- encoding: utf-8 -*-
10 @sp = FactoryGirl.create :system_picture
11 @lg = FactoryGirl.create :license_group
12 @l = FactoryGirl.build :license, :license_group_id => @lg.id, :system_picture_id => @sp.id
15 context 'オーソドックスなデータのとき' do
19 @l.url = 'http://test.jp/'
25 @l.url = 'http://test.jp/aaaaa' + 'a' * 180
30 context 'license_group_idを検証するとき' do
32 @l.license_group_id = ''
33 @l.should_not be_valid
36 @l.license_group_id = 'a'
37 @l.should_not be_valid
39 it '存在するライセンスグループでなければ失敗する' do
40 @l.license_group_id = 0
41 @l.should_not be_valid
44 context 'nameを検証するとき' do
47 @l.should_not be_valid
51 @l.should_not be_valid
54 lc = FactoryGirl.create :license
55 @l.should_not be_valid
58 context 'captionを検証するとき' do
61 @l.should_not be_valid
65 @l.should_not be_valid
68 context 'urlを検証するとき' do
71 @l.should_not be_valid
74 @l.url = 'http://test.jp/aaaaa' + 'a' * 181
75 @l.should_not be_valid
77 it 'url形式でないなら失敗する' do
79 @l.should_not be_valid
82 context 'system_picture_idを検証するとき' do
84 @l.system_picture_id = ''
85 @l.should_not be_valid
88 @l.system_picture_id = 'a'
89 @l.should_not be_valid
91 it '存在するシステム画像でなければ失敗する' do
92 @l.system_picture_id = 0
93 @l.should_not be_valid
98 describe '文字コード検証に於いて' do
100 @sp = FactoryGirl.create :system_picture
101 @lg = FactoryGirl.create :license_group
102 @l = FactoryGirl.build :license, :license_group_id => @lg.id, :system_picture_id => @sp.id
105 context 'nameを検証するとき' do
106 it 'Shift JISなら失敗する' do
107 @l.name = "\x83G\x83r\x83]\x83D"
110 }.should raise_error(Pettanr::BadRequest)
114 context 'captionを検証するとき' do
115 it 'Shift JISなら失敗する' do
116 @l.caption = "\x83G\x83r\x83]\x83D"
119 }.should raise_error(Pettanr::BadRequest)
123 context 'urlを検証するとき' do
124 it 'Shift JISなら失敗する' do
125 @l.url = "\x83G\x83r\x83]\x83D"
128 }.should raise_error(Pettanr::BadRequest)
132 context 'settingsを検証するとき' do
133 it 'Shift JISなら失敗する' do
134 @l.settings = "\x83G\x83r\x83]\x83D"
137 }.should raise_error(Pettanr::BadRequest)
141 context 'credit_picturesを検証するとき' do
142 it 'Shift JISなら失敗する' do
143 @l.credit_pictures = "\x83G\x83r\x83]\x83D"
146 }.should raise_error(Pettanr::BadRequest)
152 describe 'デフォルト値補充に於いて' do
154 @sp = FactoryGirl.create :system_picture
155 @lg = FactoryGirl.create :license_group
156 @l = FactoryGirl.build :license, :name => 'peta2.0', :license_group_id => @lg.id, :system_picture_id => @sp.id
161 describe '上書き補充に於いて' do
163 @sp = FactoryGirl.create :system_picture
164 @lg = FactoryGirl.create :license_group
165 @l = FactoryGirl.build :license, :name => 'peta2.0', :license_group_id => @lg.id, :system_picture_id => @sp.id
170 describe '閲覧許可に於いて' do
171 #ライセンスは作家作成する前から存在するので、閲覧制限の意味がない
174 describe '一覧取得に於いて' do
176 @sp = FactoryGirl.create :system_picture
177 @lg = FactoryGirl.create :license_group
178 @l = FactoryGirl.create :license, :name => 'peta2.0', :license_group_id => @lg.id, :system_picture_id => @sp.id
179 @lg2 = FactoryGirl.create :license_group, :name => 'pubdm'
181 context 'つつがなく終わるとき' do
182 it '一覧取得オプションを利用している' do
183 License.stub(:list_opt).with(any_args).and_return({})
184 License.should_receive(:list_opt).with(any_args).exactly(1)
193 @l2 = FactoryGirl.create :license, :name => 'peta3.0', :url => 'http://pe.ta/3.0', :license_group_id => @lg.id, :system_picture_id => @sp.id
194 @l3 = FactoryGirl.create :license, :name => 'pd1.0', :url => 'http://pb.dm/1.0', :license_group_id => @lg2.id, :system_picture_id => @sp.id
196 l.should eq [@l3, @l, @l2]
199 describe '一覧取得オプションに於いて' do
200 it 'includeキーを含んでいる' do
202 r.has_key?(:include).should be_true
205 r = License.list_opt[:include]
206 r.should have(1).items
208 it 'ライセンスグループを含んでいる' do
209 r = License.list_opt[:include]
210 r.has_key?(:license_group).should be_true
213 describe 'json一覧出力オプションに於いて' do
215 @sp = FactoryGirl.create :system_picture
216 @lg = FactoryGirl.create :license_group
217 @l = FactoryGirl.create :license, :license_group_id => @lg.id, :system_picture_id => @sp.id
219 it 'ライセンスグループを含んでいる' do
220 r = License.list.to_json License.list_json_opt
223 i.has_key?('license_group').should be_true
227 describe '単体取得に於いて' do
229 @sp = FactoryGirl.create :system_picture
230 @lg = FactoryGirl.create :license_group
231 @l = FactoryGirl.create :license, :license_group_id => @lg.id, :system_picture_id => @sp.id
233 context 'つつがなく終わるとき' do
234 it '単体取得オプションを利用している' do
235 License.stub(:show_opt).with(any_args).and_return({})
236 License.should_receive(:show_opt).with(any_args).exactly(1)
237 r = License.show @l.id
241 l = License.show @l.id
244 context '存在しないライセンスを開こうとしたとき' do
245 it '404RecordNotFound例外を返す' do
248 }.should raise_error(ActiveRecord::RecordNotFound)
252 describe '単体取得オプションに於いて' do
253 it 'includeキーを含んでいる' do
255 r.has_key?(:include).should be_true
258 r = License.show_opt[:include]
259 r.should have(1).items
261 it 'ライセンスグループを含んでいる' do
262 r = License.show_opt[:include]
263 r.has_key?(:license_group).should be_true
266 describe 'json単体出力オプションに於いて' do
268 @sp = FactoryGirl.create :system_picture
269 @lg = FactoryGirl.create :license_group
270 @l = FactoryGirl.create :license, :license_group_id => @lg.id, :system_picture_id => @sp.id
272 it 'ライセンスグループを含んでいる' do
273 r = License.show(@l.id).to_json License.show_json_opt
276 i.has_key?('license_group').should be_true
280 describe '管理名検索に於いて' do
282 @sp = FactoryGirl.create :system_picture
283 @lg = FactoryGirl.create :license_group
284 @l = FactoryGirl.create :license, :name => 'peta2.0', :license_group_id => @lg.id, :system_picture_id => @sp.id
285 @lg2 = FactoryGirl.create :license_group, :name => 'pubdm'
286 @l2 = FactoryGirl.create :license, :name => 'pettan2.2b', :url => @l.url + 'l2', :license_group_id => @lg.id, :system_picture_id => @sp.id
288 context 'つつがなく終わるとき' do
290 l = License.list_by_name @l2.name
298 @lg = FactoryGirl.create :license_group
299 @f = Rails.root + 'spec/json/license_group.json'
300 @t = File.open(@f, 'r').read
304 @attr = @a["licenses_attributes"]
305 @ln = @attr.keys.first
306 @la = @attr.values.first
307 @la["license_group_id"] = @lg.id
309 context 'つつがなく終わるとき' do
310 it 'システム画像置換を依頼する' do
311 License.stub(:replace_system_picture).with(any_args).and_return(true)
312 License.should_receive(:replace_system_picture).with(any_args).exactly(1)
313 License.stub(:modify_object).with(any_args).and_return(License.new)
314 License.any_instance.stub(:save).with(any_args).and_return(true)
315 r = License.store(@ln, @la)
318 License.stub(:replace_system_picture).with(any_args).and_return(true)
319 License.stub(:modify_object).with(any_args).and_return(License.new)
320 License.should_receive(:modify_object).with(any_args).exactly(1)
321 License.any_instance.stub(:save).with(any_args).and_return(true)
322 r = License.store(@ln, @la)
325 License.stub(:replace_system_picture).with(any_args).and_return(true)
326 License.stub(:modify_object).with(any_args).and_return(License.new)
327 License.any_instance.stub(:save).with(any_args).and_return(true)
328 License.any_instance.should_receive(:save).with(any_args).exactly(1)
329 r = License.store(@ln, @la)
332 r = License.store(@ln, @la)
333 r.is_a?(License).should be_true
335 r.url.should eq @la["url"]
336 cp = JSON.parse r.credit_pictures
337 cp['a_picture_id'].should_not be_nil
338 cp['b_picture_id'].should_not be_nil
340 it 'ライセンスが作成されている' do
342 r = License.store(@ln, @la)
343 }.should change License, :count
345 it 'システム画像が作成されている' do
347 r = License.store(@ln, @la)
348 }.should change SystemPicture, :count
351 context 'システム画像置換が失敗するとき' do
353 License.stub(:replace_system_picture).with(any_args).and_return(false)
354 License.stub(:modify_object).with(any_args).and_return(License.new)
356 it '全体エラーメッセージがセットされている' do
357 r = License.store(@ln, @la)
358 r.errors[:base].should_not be_blank
360 it 'ライセンスが作成されていない' do
362 r = License.store(@ln, @la)
363 }.should_not change License, :count
368 describe '複数の更新に於いて' do
370 @lg = FactoryGirl.create :license_group
371 @fs = Rails.root + 'spec/json/license_groups.json'
372 @ts = File.open(@fs, 'r').read
376 @attr = @a["licenses_attributes"]
378 context '2件データでつつがなく終わるとき' do
380 License.stub(:store).with(any_args).and_return(License.new)
381 License.should_receive(:store).with(any_args).exactly(2)
382 License.any_instance.stub(:valid?).with(any_args).and_return(true)
383 r = License.stores(@attr, @lg.id)
386 License.stub(:store).with(any_args).and_return(License.new)
387 License.any_instance.stub(:valid?).with(any_args).and_return(true)
388 r = License.stores(@attr, @lg.id)
392 context '2件データで失敗するとき' do
394 License.stub(:store).with(any_args).and_return(License.new)
395 License.should_receive(:store).with(any_args).exactly(2)
396 License.any_instance.stub(:valid?).with(any_args).and_return(false)
397 r = License.stores(@attr, @lg.id)
400 License.stub(:store).with(any_args).and_return(License.new)
401 License.any_instance.stub(:valid?).with(any_args).and_return(false)
402 r = License.stores(@attr, @lg.id)
406 context 'attrsがnilなどのHashでないとき' do
408 r = License.stores(nil, @lg.id)
414 describe 'エクスポートに於いて' do
416 @sp = FactoryGirl.create :system_picture
417 @lg = FactoryGirl.create :license_group
418 @l = FactoryGirl.create :license, :license_group_id => @lg.id, :system_picture_id => @sp.id
419 @l2 = FactoryGirl.create :license, :name => 'l2[old ls]', :url => @l.url+'2', :license_group_id => @lg.id, :system_picture_id => @sp.id, :updated_at => Time.now - 3000
421 it '開始日時が省略された場合はすべてのライセンスを返す' do
423 r.should eq [@l, @l2]
425 it '開始日時以降に更新されたライセンスを返す' do
426 r = License.export @l.updated_at - 100