1 # -*- encoding: utf-8 -*-
5 describe LicenseGroup do
8 @f = Rails.root + 'spec/json/license_group.json'
9 @t = File.open(@f, 'r').read
11 @fs = Rails.root + 'spec/json/license_groups.json'
12 @fes = Rails.root + 'spec/json/invalid_license_groups.json'
16 @lg = FactoryGirl.build :license_group
19 context 'オーソドックスなデータのとき' do
24 @lg.url = 'http://test.jp/'
29 @lg.classname = 'a'*50
31 @lg.url = 'http://test.jp/aaaaa' + 'a' * 180
36 context 'nameを検証するとき' do
39 @lg.should_not be_valid
43 @lg.should_not be_valid
46 l = FactoryGirl.create :license_group
47 @lg.should_not be_valid
50 context 'classnameを検証するとき' do
53 @lg.should_not be_valid
56 @lg.classname = 'a'*51
57 @lg.should_not be_valid
60 context 'captionを検証するとき' do
63 @lg.should_not be_valid
67 @lg.should_not be_valid
70 context 'urlを検証するとき' do
73 @lg.should_not be_valid
77 @lg.should_not be_valid
79 it 'url形式でないなら失敗する' do
81 @lg.should_not be_valid
86 describe '文字コード検証に於いて' do
88 @lg = FactoryGirl.build :license_group
91 context 'nameを検証するとき' do
92 it 'Shift JISなら失敗する' do
93 @lg.name = "\x83G\x83r\x83]\x83D"
96 }.should raise_error(Pettanr::BadRequest)
100 context 'classnameを検証するとき' do
101 it 'Shift JISなら失敗する' do
102 @lg.classname = "\x83G\x83r\x83]\x83D"
105 }.should raise_error(Pettanr::BadRequest)
109 context 'captionを検証するとき' do
110 it 'Shift JISなら失敗する' do
111 @lg.caption = "\x83G\x83r\x83]\x83D"
114 }.should raise_error(Pettanr::BadRequest)
118 context 'urlを検証するとき' do
119 it 'Shift JISなら失敗する' do
120 @lg.url = "\x83G\x83r\x83]\x83D"
123 }.should raise_error(Pettanr::BadRequest)
128 describe 'デフォルト値補充に於いて' do
130 @lg = FactoryGirl.build :license_group, :name => "1"
135 describe '上書き補充に於いて' do
137 @lg = FactoryGirl.build :license_group, :name => "1"
142 describe '閲覧許可に於いて' do
143 #ライセンスグループは作家作成する前から存在するので、閲覧制限の意味がない
146 describe '一覧取得に於いて' do
148 @lg = FactoryGirl.create :license_group, :name => "1"
150 context 'つつがなく終わるとき' do
151 it '一覧取得オプションを利用している' do
152 LicenseGroup.stub(:list_opt).with(any_args).and_return({})
153 LicenseGroup.should_receive(:list_opt).with(any_args).exactly(1)
154 r = LicenseGroup.list
158 l = LicenseGroup.list
162 @lg2 = FactoryGirl.create :license_group, :name => "5", :url => 'http://test.ptn/10'
163 l = LicenseGroup.list
164 l.should eq [@lg, @lg2]
167 describe '一覧取得オプションに於いて' do
168 it 'includeキーを含んでいる' do
169 r = LicenseGroup.list_opt
170 r.has_key?(:include).should be_true
173 r = LicenseGroup.list_opt[:include]
174 r.should have(1).items
177 r = LicenseGroup.list_opt[:include]
178 r.has_key?(:licenses).should be_true
181 describe 'json一覧出力オプションに於いて' do
183 @sp = FactoryGirl.create :system_picture
184 @lg = FactoryGirl.create :license_group
185 @license = FactoryGirl.create :license, :license_group_id => @lg.id, :system_picture_id => @sp.id
186 @user = FactoryGirl.create( :user_yas)
187 @author = FactoryGirl.create :author, :user_id => @user.id
188 @artist = FactoryGirl.create :artist_yas, :author_id => @author.id
189 @op = FactoryGirl.create :original_picture, :artist_id => @artist.id
190 @p = FactoryGirl.create :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @artist.id
191 @rp = FactoryGirl.create :resource_picture, :artist_id => @artist.id, :license_id => @license.id, :original_picture_id => @op.id, :picture_id => @p.id
192 @sbt = FactoryGirl.create :speech_balloon_template
193 @comic = FactoryGirl.create :comic, :author_id => @author.id, :visible => 1
194 @panel = FactoryGirl.create :panel, :author_id => @author.id, :publish => 1
195 @story = FactoryGirl.create :story, :author_id => @author.id, :comic_id => @comic.id, :panel_id => @panel.id
198 r = LicenseGroup.list.to_json LicenseGroup.list_json_opt
201 i.has_key?('licenses').should be_true
205 describe '単体取得に於いて' do
207 @lg = FactoryGirl.create :license_group
209 context 'つつがなく終わるとき' do
210 it '単体取得オプションを利用している' do
211 LicenseGroup.stub(:show_opt).with(any_args).and_return({})
212 LicenseGroup.should_receive(:show_opt).with(any_args).exactly(1)
213 r = LicenseGroup.show @lg.id
217 l = LicenseGroup.show @lg.id
220 context '存在しないライセンスグループを開こうとしたとき' do
221 it '404RecordNotFound例外を返す' do
223 LicenseGroup.show 110
224 }.should raise_error(ActiveRecord::RecordNotFound)
228 describe '単体取得オプションに於いて' do
229 it 'includeキーを含んでいる' do
230 r = LicenseGroup.show_opt
231 r.has_key?(:include).should be_true
234 r = LicenseGroup.show_opt[:include]
235 r.should have(1).items
238 r = LicenseGroup.show_opt[:include]
239 r.has_key?(:licenses).should be_true
242 describe 'json単体出力オプションに於いて' do
244 @sp = FactoryGirl.create :system_picture
245 @lg = FactoryGirl.create :license_group
246 @license = FactoryGirl.create :license, :license_group_id => @lg.id, :system_picture_id => @sp.id
247 @user = FactoryGirl.create( :user_yas)
248 @author = FactoryGirl.create :author, :user_id => @user.id
249 @artist = FactoryGirl.create :artist_yas, :author_id => @author.id
250 @op = FactoryGirl.create :original_picture, :artist_id => @artist.id
251 @p = FactoryGirl.create :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @artist.id
252 @rp = FactoryGirl.create :resource_picture, :artist_id => @artist.id, :license_id => @license.id, :original_picture_id => @op.id, :picture_id => @p.id
253 @sbt = FactoryGirl.create :speech_balloon_template
254 @comic = FactoryGirl.create :comic, :author_id => @author.id, :visible => 1
255 @panel = FactoryGirl.create :panel, :author_id => @author.id, :publish => 1
256 @story = FactoryGirl.create :story, :author_id => @author.id, :comic_id => @comic.id, :panel_id => @panel.id
259 r = LicenseGroup.show(@lg.id).to_json LicenseGroup.show_json_opt
262 i.has_key?('licenses').should be_true
270 @imager = ImagerTest.load("abc\ndef\nghi")
271 PettanImager.stub(:load).with(any_args).and_return(@imager)
273 context 'つつがなく終わるとき' do
275 LicenseGroup.stub(:modify_object).with(any_args).and_return(LicenseGroup.new)
276 LicenseGroup.should_receive(:modify_object).with(any_args).exactly(1)
277 License.stub(:stores).with(any_args).and_return(0)
278 LicenseGroup.any_instance.stub(:save).with(any_args).and_return(true)
279 r = LicenseGroup.store(@n, @a)
281 it 'ライセンスに複数の更新を依頼する' do
282 LicenseGroup.stub(:modify_object).with(any_args).and_return(LicenseGroup.new)
283 License.stub(:stores).with(any_args).and_return(0)
284 License.should_receive(:stores).with(any_args).exactly(1)
285 LicenseGroup.any_instance.stub(:save).with(any_args).and_return(true)
286 r = LicenseGroup.store(@n, @a)
289 LicenseGroup.stub(:modify_object).with(any_args).and_return(LicenseGroup.new)
290 License.stub(:stores).with(any_args).and_return(0)
291 LicenseGroup.any_instance.stub(:save).with(any_args).and_return(true)
292 LicenseGroup.any_instance.should_receive(:save).with(any_args).exactly(1)
293 r = LicenseGroup.store(@n, @a)
296 r = LicenseGroup.store(@n, @a)
297 r.is_a?(LicenseGroup).should be_true
299 r.url.should eq @a["url"]
301 it 'カラム値からlicenses_attributesが削除されている' do
302 @a["licenses_attributes"].should_not be_nil
303 r = LicenseGroup.store(@n, @a)
304 @a["licenses_attributes"].should be_nil
307 context 'ライセンス複数更新が失敗するとき' do
309 LicenseGroup.stub(:modify_object).with(any_args).and_return(LicenseGroup.new)
310 License.stub(:stores).with(any_args).and_return(1)
311 LicenseGroup.any_instance.stub(:save).with(any_args).and_return(true)
313 it '全体エラーメッセージがセットされている' do
314 r = LicenseGroup.store(@n, @a)
315 r.errors[:base].should_not be_blank
317 it 'ライセンスが作成されていない' do
319 r = LicenseGroup.store(@n, @a)
320 }.should_not change License, :count
322 it 'ライセンスグループが作成されていない' do
324 r = LicenseGroup.store(@n, @a)
325 }.should_not change LicenseGroup, :count
330 describe 'インポートに於いて' do
333 context 'つつがなく終わるとき' do
334 it 'ファイルインポートを依頼する' do
335 LicenseGroup.should_receive(:import_file).with(any_args).exactly(1)
336 LicenseGroup.stub(:import_file).with(any_args).and_return([])
337 LicenseGroup.import(@f)
339 it 'ライセンスグループ更新を一回依頼する' do
340 LicenseGroup.stub(:store).with(any_args).and_return(LicenseGroup.new)
341 LicenseGroup.should_receive(:store).with(any_args).exactly(1)
342 LicenseGroup.import(@f)
344 it 'ライセンスグループが追加される' do
346 LicenseGroup.import(@f)
347 }.should change LicenseGroup, :count
351 LicenseGroup.import(@f)
352 }.should change License, :count
355 r = LicenseGroup.import(@f)
359 context '複数データがつつがなく終わるとき' do
360 it 'ライセンスグループ更新を二回依頼する' do
361 LicenseGroup.stub(:store).with(any_args).and_return(LicenseGroup.new)
362 LicenseGroup.should_receive(:store).with(any_args).exactly(2)
363 LicenseGroup.import(@fs)
365 it 'ライセンスグループが二個追加される' do
367 r = LicenseGroup.import(@fs)
368 }.should change(LicenseGroup, :count).by 2
372 r = LicenseGroup.import(@fs)
373 }.should change(License, :count)
376 r = LicenseGroup.import(@fs)
380 context 'ライセンスグループ作成に失敗したとき' do
382 LicenseGroup.any_instance.stub(:save).with(any_args).and_return(false)
383 LicenseGroup.any_instance.stub(:valid?).with(any_args).and_return(false)
385 it 'ライセンスグループの数に変化がない' do
387 LicenseGroup.import(@f)
388 }.should_not change LicenseGroup, :count
391 r = LicenseGroup.import(@f)
392 r.is_a?(Array).should be_true
395 r = LicenseGroup.import(@f)
396 r.should have(1).items
398 it 'ライセンスグループオブジェクトが入っている' do
399 r = LicenseGroup.import(@f)
400 r.first.is_a?(LicenseGroup).should be_true
403 context '複数のライセンスグループ作成に失敗したとき' do
404 #三件中、二件の失敗、一件を成功させ、成功データは戻り値に含まないことを確認する
405 it 'ライセンスグループの数に変化がない' do
407 LicenseGroup.import(@fes)
408 }.should_not change LicenseGroup, :count
410 it '途中で保存に失敗しても全件更新依頼する' do
411 LicenseGroup.stub(:store).with(any_args).and_return(LicenseGroup.new)
412 LicenseGroup.should_receive(:store).with(any_args).exactly(3)
413 LicenseGroup.import(@fes)
416 r = LicenseGroup.import(@fes)
417 r.is_a?(Array).should be_true
420 r = LicenseGroup.import(@fes)
421 r.should have(2).items
423 it '配列の中身は失敗したライセンスグループオブジェクトが入っている' do
424 r = LicenseGroup.import(@fes)
425 r[0].is_a?(LicenseGroup).should be_true
426 r[0]["name"].should eq 'UnknownUrl'
427 r[1].is_a?(LicenseGroup).should be_true
428 r[1]["name"].should eq 'UnknownClassname'