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, :name => 'peta2.0', :license_group_id => @lg.id, :system_picture_id => @sp.id
107 describe '上書き補充に於いて' do
109 @sp = FactoryGirl.create :system_picture
110 @lg = FactoryGirl.create :license_group
111 @l = FactoryGirl.build :license, :name => 'peta2.0', :license_group_id => @lg.id, :system_picture_id => @sp.id
116 describe '閲覧許可に於いて' do
117 #ライセンスは作家作成する前から存在するので、閲覧制限の意味がない
120 describe '一覧取得に於いて' do
122 @sp = FactoryGirl.create :system_picture
123 @lg = FactoryGirl.create :license_group
124 @l = FactoryGirl.create :license, :name => 'peta2.0', :license_group_id => @lg.id, :system_picture_id => @sp.id
125 @lg2 = FactoryGirl.create :license_group, :name => 'pubdm'
127 context 'つつがなく終わるとき' do
128 it '一覧取得オプションを利用している' do
129 License.stub(:list_opt).with(any_args).and_return({})
130 License.should_receive(:list_opt).with(any_args).exactly(1)
139 @l2 = FactoryGirl.create :license, :name => 'peta3.0', :url => 'http://pe.ta/3.0', :license_group_id => @lg.id, :system_picture_id => @sp.id
140 @l3 = FactoryGirl.create :license, :name => 'pd1.0', :url => 'http://pb.dm/1.0', :license_group_id => @lg2.id, :system_picture_id => @sp.id
142 l.should eq [@l3, @l, @l2]
145 describe '一覧取得オプションに於いて' do
146 it 'includeキーを含んでいる' do
148 r.has_key?(:include).should be_true
151 r = License.list_opt[:include]
152 r.should have(1).items
154 it 'ライセンスグループを含んでいる' do
155 r = License.list_opt[:include]
156 r.has_key?(:license_group).should be_true
159 describe 'json一覧出力オプションに於いて' do
161 @sp = FactoryGirl.create :system_picture
162 @lg = FactoryGirl.create :license_group
163 @l = FactoryGirl.create :license, :license_group_id => @lg.id, :system_picture_id => @sp.id
165 it 'ライセンスグループを含んでいる' do
166 r = License.list.to_json License.list_json_opt
169 i.has_key?('license_group').should be_true
173 describe '単体取得に於いて' do
175 @sp = FactoryGirl.create :system_picture
176 @lg = FactoryGirl.create :license_group
177 @l = FactoryGirl.create :license, :license_group_id => @lg.id, :system_picture_id => @sp.id
179 context 'つつがなく終わるとき' do
180 it '単体取得オプションを利用している' do
181 License.stub(:show_opt).with(any_args).and_return({})
182 License.should_receive(:show_opt).with(any_args).exactly(1)
183 r = License.show @l.id
187 l = License.show @l.id
190 context '存在しないライセンスを開こうとしたとき' do
191 it '404RecordNotFound例外を返す' do
194 }.should raise_error(ActiveRecord::RecordNotFound)
198 describe '単体取得オプションに於いて' do
199 it 'includeキーを含んでいる' do
201 r.has_key?(:include).should be_true
204 r = License.show_opt[:include]
205 r.should have(1).items
207 it 'ライセンスグループを含んでいる' do
208 r = License.show_opt[:include]
209 r.has_key?(:license_group).should be_true
212 describe 'json単体出力オプションに於いて' do
214 @sp = FactoryGirl.create :system_picture
215 @lg = FactoryGirl.create :license_group
216 @l = FactoryGirl.create :license, :license_group_id => @lg.id, :system_picture_id => @sp.id
218 it 'ライセンスグループを含んでいる' do
219 r = License.show(@l.id).to_json License.show_json_opt
222 i.has_key?('license_group').should be_true
228 @lg = FactoryGirl.create :license_group
229 @f = Rails.root + 'spec/json/license_group.json'
230 @t = File.open(@f, 'r').read
234 @attr = @a["licenses_attributes"]
235 @ln = @attr.keys.first
236 @la = @attr.values.first
237 @la["license_group_id"] = @lg.id
239 context 'つつがなく終わるとき' do
240 it 'システム画像置換を依頼する' do
241 License.stub(:replace_system_picture).with(any_args).and_return(true)
242 License.should_receive(:replace_system_picture).with(any_args).exactly(1)
243 License.stub(:modify_object).with(any_args).and_return(License.new)
244 License.any_instance.stub(:save).with(any_args).and_return(true)
245 r = License.store(@ln, @la)
248 License.stub(:replace_system_picture).with(any_args).and_return(true)
249 License.stub(:modify_object).with(any_args).and_return(License.new)
250 License.should_receive(:modify_object).with(any_args).exactly(1)
251 License.any_instance.stub(:save).with(any_args).and_return(true)
252 r = License.store(@ln, @la)
255 License.stub(:replace_system_picture).with(any_args).and_return(true)
256 License.stub(:modify_object).with(any_args).and_return(License.new)
257 License.any_instance.stub(:save).with(any_args).and_return(true)
258 License.any_instance.should_receive(:save).with(any_args).exactly(1)
259 r = License.store(@ln, @la)
262 r = License.store(@ln, @la)
263 r.is_a?(License).should be_true
265 r.url.should eq @la["url"]
266 cp = JSON.parse r.credit_pictures
267 cp['a_picture_id'].should_not be_nil
268 cp['b_picture_id'].should_not be_nil
270 it 'ライセンスが作成されている' do
272 r = License.store(@ln, @la)
273 }.should change License, :count
275 it 'システム画像が作成されている' do
277 r = License.store(@ln, @la)
278 }.should change SystemPicture, :count
281 context 'システム画像置換が失敗するとき' do
283 License.stub(:replace_system_picture).with(any_args).and_return(false)
284 License.stub(:modify_object).with(any_args).and_return(License.new)
286 it '全体エラーメッセージがセットされている' do
287 r = License.store(@ln, @la)
288 r.errors[:base].should_not be_blank
290 it 'ライセンスが作成されていない' do
292 r = License.store(@ln, @la)
293 }.should_not change License, :count
298 describe '複数の更新に於いて' do
300 @lg = FactoryGirl.create :license_group
301 @fs = Rails.root + 'spec/json/license_groups.json'
302 @ts = File.open(@fs, 'r').read
306 @attr = @a["licenses_attributes"]
308 context '2件データでつつがなく終わるとき' do
310 License.stub(:store).with(any_args).and_return(License.new)
311 License.should_receive(:store).with(any_args).exactly(2)
312 License.any_instance.stub(:valid?).with(any_args).and_return(true)
313 r = License.stores(@attr, @lg.id)
316 License.stub(:store).with(any_args).and_return(License.new)
317 License.any_instance.stub(:valid?).with(any_args).and_return(true)
318 r = License.stores(@attr, @lg.id)
322 context '2件データで失敗するとき' do
324 License.stub(:store).with(any_args).and_return(License.new)
325 License.should_receive(:store).with(any_args).exactly(2)
326 License.any_instance.stub(:valid?).with(any_args).and_return(false)
327 r = License.stores(@attr, @lg.id)
330 License.stub(:store).with(any_args).and_return(License.new)
331 License.any_instance.stub(:valid?).with(any_args).and_return(false)
332 r = License.stores(@attr, @lg.id)
336 context 'attrsがnilなどのHashでないとき' do
338 r = License.stores(nil, @lg.id)