1 # -*- encoding: utf-8 -*-
5 describe LicenseGroup do
7 SpeechBalloonTemplate.delete_all
9 @f = Rails.root + 'spec/json/license_group.json'
10 @t = File.open(@f, 'r').read
12 @fs = Rails.root + 'spec/json/license_groups.json'
13 @fes = Rails.root + 'spec/json/invalid_license_groups.json'
17 @lg = FactoryGirl.build :license_group
20 context 'オーソドックスなデータのとき' do
25 @lg.url = 'http://test.jp/'
30 @lg.classname = 'a'*50
32 @lg.url = 'http://test.jp/aaaaa' + 'a' * 180
37 context 'nameを検証するとき' do
40 @lg.should_not be_valid
44 @lg.should_not be_valid
47 l = FactoryGirl.create :license_group
48 @lg.should_not be_valid
51 context 'classnameを検証するとき' do
54 @lg.should_not be_valid
57 @lg.classname = 'a'*51
58 @lg.should_not be_valid
61 context 'captionを検証するとき' do
64 @lg.should_not be_valid
68 @lg.should_not be_valid
71 context 'urlを検証するとき' do
74 @lg.should_not be_valid
78 @lg.should_not be_valid
80 it 'url形式でないなら失敗する' do
82 @lg.should_not be_valid
87 describe '文字コード検証に於いて' do
89 @lg = FactoryGirl.build :license_group
92 context 'nameを検証するとき' do
93 it 'Shift JISなら失敗する' do
94 @lg.name = "\x83G\x83r\x83]\x83D"
97 }.should raise_error(Pettanr::BadRequest)
101 context 'classnameを検証するとき' do
102 it 'Shift JISなら失敗する' do
103 @lg.classname = "\x83G\x83r\x83]\x83D"
106 }.should raise_error(Pettanr::BadRequest)
110 context 'captionを検証するとき' do
111 it 'Shift JISなら失敗する' do
112 @lg.caption = "\x83G\x83r\x83]\x83D"
115 }.should raise_error(Pettanr::BadRequest)
119 context 'urlを検証するとき' do
120 it 'Shift JISなら失敗する' do
121 @lg.url = "\x83G\x83r\x83]\x83D"
124 }.should raise_error(Pettanr::BadRequest)
129 describe 'デフォルト値補充に於いて' do
131 @lg = FactoryGirl.build :license_group, :name => "1"
136 describe '上書き補充に於いて' do
138 @lg = FactoryGirl.build :license_group, :name => "1"
143 describe '閲覧許可に於いて' do
144 #ライセンスグループは作家作成する前から存在するので、閲覧制限の意味がない
147 describe '一覧取得に於いて' do
149 @lg = FactoryGirl.create :license_group, :name => "1"
151 context 'つつがなく終わるとき' do
152 it '一覧取得オプションを利用している' do
153 LicenseGroup.stub(:list_opt).with(any_args).and_return({})
154 LicenseGroup.should_receive(:list_opt).with(any_args).exactly(1)
155 r = LicenseGroup.list
159 l = LicenseGroup.list
163 @lg2 = FactoryGirl.create :license_group, :name => "5", :url => 'http://test.ptn/10'
164 l = LicenseGroup.list
165 l.should eq [@lg, @lg2]
168 describe '一覧取得オプションに於いて' do
169 it 'includeキーを含んでいる' do
170 r = LicenseGroup.list_opt
171 r.has_key?(:include).should be_true
174 r = LicenseGroup.list_opt[:include]
175 r.should have(1).items
178 r = LicenseGroup.list_opt[:include]
179 r.has_key?(:licenses).should be_true
182 describe 'json一覧出力オプションに於いて' do
184 @sp = FactoryGirl.create :system_picture
185 @lg = FactoryGirl.create :license_group
186 @license = FactoryGirl.create :license, :license_group_id => @lg.id, :system_picture_id => @sp.id
187 @user = FactoryGirl.create( :user_yas)
188 @author = FactoryGirl.create :author, :user_id => @user.id
189 @artist = FactoryGirl.create :artist_yas, :author_id => @author.id
190 @op = FactoryGirl.create :original_picture, :artist_id => @artist.id
191 @p = FactoryGirl.create :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @artist.id
192 @rp = FactoryGirl.create :resource_picture, :artist_id => @artist.id, :license_id => @license.id, :original_picture_id => @op.id, :picture_id => @p.id
193 @sbt = FactoryGirl.create :speech_balloon_template
194 @scroll = FactoryGirl.create :scroll, :author_id => @author.id, :visible => 1
195 @panel = FactoryGirl.create :panel, :author_id => @author.id, :publish => 1
196 @scroll_panel = FactoryGirl.create :scroll_panel, :author_id => @author.id, :scroll_id => @scroll.id, :panel_id => @panel.id
199 r = LicenseGroup.list.to_json LicenseGroup.list_json_opt
202 i.has_key?('licenses').should be_true
206 describe '単体取得に於いて' do
208 @lg = FactoryGirl.create :license_group
210 context 'つつがなく終わるとき' do
211 it '単体取得オプションを利用している' do
212 LicenseGroup.stub(:show_opt).with(any_args).and_return({})
213 LicenseGroup.should_receive(:show_opt).with(any_args).exactly(1)
214 r = LicenseGroup.show @lg.id
218 l = LicenseGroup.show @lg.id
221 context '存在しないライセンスグループを開こうとしたとき' do
222 it '404RecordNotFound例外を返す' do
224 LicenseGroup.show 110
225 }.should raise_error(ActiveRecord::RecordNotFound)
229 describe '単体取得オプションに於いて' do
230 it 'includeキーを含んでいる' do
231 r = LicenseGroup.show_opt
232 r.has_key?(:include).should be_true
235 r = LicenseGroup.show_opt[:include]
236 r.should have(1).items
239 r = LicenseGroup.show_opt[:include]
240 r.has_key?(:licenses).should be_true
243 describe 'json単体出力オプションに於いて' do
245 @sp = FactoryGirl.create :system_picture
246 @lg = FactoryGirl.create :license_group
247 @license = FactoryGirl.create :license, :license_group_id => @lg.id, :system_picture_id => @sp.id
248 @user = FactoryGirl.create( :user_yas)
249 @author = FactoryGirl.create :author, :user_id => @user.id
250 @artist = FactoryGirl.create :artist_yas, :author_id => @author.id
251 @op = FactoryGirl.create :original_picture, :artist_id => @artist.id
252 @p = FactoryGirl.create :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @artist.id
253 @rp = FactoryGirl.create :resource_picture, :artist_id => @artist.id, :license_id => @license.id, :original_picture_id => @op.id, :picture_id => @p.id
254 @sbt = FactoryGirl.create :speech_balloon_template
255 @scroll = FactoryGirl.create :scroll, :author_id => @author.id, :visible => 1
256 @panel = FactoryGirl.create :panel, :author_id => @author.id, :publish => 1
257 @scroll_panel = FactoryGirl.create :scroll_panel, :author_id => @author.id, :scroll_id => @scroll.id, :panel_id => @panel.id
260 r = LicenseGroup.show(@lg.id).to_json LicenseGroup.show_json_opt
263 i.has_key?('licenses').should be_true
271 @imager = ImagerTest.load("abc\ndef\nghi")
272 PettanImager.stub(:load).with(any_args).and_return(@imager)
274 context 'つつがなく終わるとき' do
276 LicenseGroup.stub(:modify_object).with(any_args).and_return(LicenseGroup.new)
277 LicenseGroup.should_receive(:modify_object).with(any_args).exactly(1)
278 License.stub(:stores).with(any_args).and_return(0)
279 LicenseGroup.any_instance.stub(:save).with(any_args).and_return(true)
280 r = LicenseGroup.store(@n, @a)
282 it 'ライセンスに複数の更新を依頼する' do
283 LicenseGroup.stub(:modify_object).with(any_args).and_return(LicenseGroup.new)
284 License.stub(:stores).with(any_args).and_return(0)
285 License.should_receive(:stores).with(any_args).exactly(1)
286 LicenseGroup.any_instance.stub(:save).with(any_args).and_return(true)
287 r = LicenseGroup.store(@n, @a)
290 LicenseGroup.stub(:modify_object).with(any_args).and_return(LicenseGroup.new)
291 License.stub(:stores).with(any_args).and_return(0)
292 LicenseGroup.any_instance.stub(:save).with(any_args).and_return(true)
293 LicenseGroup.any_instance.should_receive(:save).with(any_args).exactly(1)
294 r = LicenseGroup.store(@n, @a)
297 r = LicenseGroup.store(@n, @a)
298 r.is_a?(LicenseGroup).should be_true
300 r.url.should eq @a["url"]
302 it 'カラム値からlicenses_attributesが削除されている' do
303 @a["licenses_attributes"].should_not be_nil
304 r = LicenseGroup.store(@n, @a)
305 @a["licenses_attributes"].should be_nil
308 context 'ライセンス複数更新が失敗するとき' do
310 LicenseGroup.stub(:modify_object).with(any_args).and_return(LicenseGroup.new)
311 License.stub(:stores).with(any_args).and_return(1)
312 LicenseGroup.any_instance.stub(:save).with(any_args).and_return(true)
314 it '全体エラーメッセージがセットされている' do
315 r = LicenseGroup.store(@n, @a)
316 r.errors[:base].should_not be_blank
318 it 'ライセンスが作成されていない' do
320 r = LicenseGroup.store(@n, @a)
321 }.should_not change License, :count
323 it 'ライセンスグループが作成されていない' do
325 r = LicenseGroup.store(@n, @a)
326 }.should_not change LicenseGroup, :count
331 describe 'インポートに於いて' do
334 context 'つつがなく終わるとき' do
335 it 'ファイルインポートを依頼する' do
336 LicenseGroup.should_receive(:import_file).with(any_args).exactly(1)
337 LicenseGroup.stub(:import_file).with(any_args).and_return([])
338 LicenseGroup.import(@f)
340 it 'ライセンスグループ更新を一回依頼する' do
341 LicenseGroup.stub(:store).with(any_args).and_return(LicenseGroup.new)
342 LicenseGroup.should_receive(:store).with(any_args).exactly(1)
343 LicenseGroup.import(@f)
345 it 'ライセンスグループが追加される' do
347 LicenseGroup.import(@f)
348 }.should change LicenseGroup, :count
352 LicenseGroup.import(@f)
353 }.should change License, :count
356 r = LicenseGroup.import(@f)
360 context '複数データがつつがなく終わるとき' do
361 it 'ライセンスグループ更新を二回依頼する' do
362 LicenseGroup.stub(:store).with(any_args).and_return(LicenseGroup.new)
363 LicenseGroup.should_receive(:store).with(any_args).exactly(2)
364 LicenseGroup.import(@fs)
366 it 'ライセンスグループが二個追加される' do
368 r = LicenseGroup.import(@fs)
369 }.should change(LicenseGroup, :count).by 2
373 r = LicenseGroup.import(@fs)
374 }.should change(License, :count)
377 r = LicenseGroup.import(@fs)
381 context 'ライセンスグループ作成に失敗したとき' do
383 LicenseGroup.any_instance.stub(:save).with(any_args).and_return(false)
384 LicenseGroup.any_instance.stub(:valid?).with(any_args).and_return(false)
386 it 'ライセンスグループの数に変化がない' do
388 LicenseGroup.import(@f)
389 }.should_not change LicenseGroup, :count
392 r = LicenseGroup.import(@f)
393 r.is_a?(Array).should be_true
396 r = LicenseGroup.import(@f)
397 r.should have(1).items
399 it 'ライセンスグループオブジェクトが入っている' do
400 r = LicenseGroup.import(@f)
401 r.first.is_a?(LicenseGroup).should be_true
404 context '複数のライセンスグループ作成に失敗したとき' do
405 #三件中、二件の失敗、一件を成功させ、成功データは戻り値に含まないことを確認する
406 it 'ライセンスグループの数に変化がない' do
408 LicenseGroup.import(@fes)
409 }.should_not change LicenseGroup, :count
411 it '途中で保存に失敗しても全件更新依頼する' do
412 LicenseGroup.stub(:store).with(any_args).and_return(LicenseGroup.new)
413 LicenseGroup.should_receive(:store).with(any_args).exactly(3)
414 LicenseGroup.import(@fes)
417 r = LicenseGroup.import(@fes)
418 r.is_a?(Array).should be_true
421 r = LicenseGroup.import(@fes)
422 r.should have(2).items
424 it '配列の中身は失敗したライセンスグループオブジェクトが入っている' do
425 r = LicenseGroup.import(@fes)
426 r[0].is_a?(LicenseGroup).should be_true
427 r[0]["name"].should eq 'UnknownUrl'
428 r[1].is_a?(LicenseGroup).should be_true
429 r[1]["name"].should eq 'UnknownClassname'