OSDN Git Service

t#31725:add panel inspire
[pettanr/pettanr.git] / spec / models / license_group_spec.rb
1 # -*- encoding: utf-8 -*-
2 #ライセンスグループ
3 require 'spec_helper'
4
5 describe LicenseGroup do
6   before do
7     #テストデータを用意してね
8     @f = Rails.root + 'spec/json/license_group.json'
9     @t = File.open(@f, 'r').read
10     @j = JSON.parse @t
11     @fs = Rails.root + 'spec/json/license_groups.json'
12     @fes = Rails.root + 'spec/json/invalid_license_groups.json'
13   end
14   describe '検証に於いて' do
15     before do
16       @lg = FactoryGirl.build :license_group
17     end
18     
19     context 'オーソドックスなデータのとき' do
20       it '下限データが通る' do
21         @lg.name = 'a'
22         @lg.classname = 'a'
23         @lg.caption = 'a'
24         @lg.url = 'http://test.jp/'
25         @lg.should be_valid
26       end
27       it '上限データが通る' do
28         @lg.name = 'a'*50
29         @lg.classname = 'a'*50
30         @lg.caption = 'a'*30
31         @lg.url = 'http://test.jp/aaaaa' + 'a' * 180
32         @lg.should be_valid
33       end
34     end
35     
36     context 'nameを検証するとき' do
37       it 'nullなら失敗する' do
38         @lg.name = ''
39         @lg.should_not be_valid
40       end
41       it '51文字以上なら失敗する' do
42         @lg.name = 'a'*51
43         @lg.should_not be_valid
44       end
45       it '重複していたら失敗する' do
46         l = FactoryGirl.create :license_group
47         @lg.should_not be_valid
48       end
49     end
50     context 'classnameを検証するとき' do
51       it 'nullなら失敗する' do
52         @lg.classname = ''
53         @lg.should_not be_valid
54       end
55       it '51文字以上なら失敗する' do
56         @lg.classname = 'a'*51
57         @lg.should_not be_valid
58       end
59     end
60     context 'captionを検証するとき' do
61       it 'nullなら失敗する' do
62         @lg.caption = ''
63         @lg.should_not be_valid
64       end
65       it '51文字以上なら失敗する' do
66         @lg.caption = 'a'*51
67         @lg.should_not be_valid
68       end
69     end
70     context 'urlを検証するとき' do
71       it 'nullなら失敗する' do
72         @lg.url = ''
73         @lg.should_not be_valid
74       end
75       it '201文字以上なら失敗する' do
76         @lg.url = 'a'*201
77         @lg.should_not be_valid
78       end
79       it 'url形式でないなら失敗する' do
80         @lg.url = 'aaaaaaa'
81         @lg.should_not be_valid
82       end
83     end
84   end
85   
86   describe '文字コード検証に於いて' do
87     before do
88       @lg = FactoryGirl.build :license_group
89     end
90     
91     context 'nameを検証するとき' do
92       it 'Shift JISなら失敗する' do
93         @lg.name = "\x83G\x83r\x83]\x83D"
94         lambda{
95           @lg.valid_encode
96         }.should raise_error(Pettanr::BadRequest)
97       end
98     end
99     
100     context 'classnameを検証するとき' do
101       it 'Shift JISなら失敗する' do
102         @lg.classname = "\x83G\x83r\x83]\x83D"
103         lambda{
104           @lg.valid_encode
105         }.should raise_error(Pettanr::BadRequest)
106       end
107     end
108     
109     context 'captionを検証するとき' do
110       it 'Shift JISなら失敗する' do
111         @lg.caption = "\x83G\x83r\x83]\x83D"
112         lambda{
113           @lg.valid_encode
114         }.should raise_error(Pettanr::BadRequest)
115       end
116     end
117     
118     context 'urlを検証するとき' do
119       it 'Shift JISなら失敗する' do
120         @lg.url = "\x83G\x83r\x83]\x83D"
121         lambda{
122           @lg.valid_encode
123         }.should raise_error(Pettanr::BadRequest)
124       end
125     end
126   end
127   
128   describe 'デフォルト値補充に於いて' do
129     it 'defined' do
130       @lg = FactoryGirl.build :license_group, :name => "1"
131       @lg.supply_default
132     end
133   end
134   
135   describe '上書き補充に於いて' do
136     it 'defined' do
137       @lg = FactoryGirl.build :license_group, :name => "1"
138       @lg.overwrite
139     end
140   end
141   
142   describe '閲覧許可に於いて' do
143     #ライセンスグループは作家作成する前から存在するので、閲覧制限の意味がない
144   end
145   
146   describe '一覧取得に於いて' do
147     before do
148       @lg = FactoryGirl.create :license_group, :name => "1"
149     end
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
155       end
156     end
157     it 'リストを返す' do
158       l = LicenseGroup.list
159       l.should eq [@lg]
160     end
161     it '名前順で並んでいる' do
162       @lg2 = FactoryGirl.create :license_group, :name => "5", :url => 'http://test.ptn/10'
163       l = LicenseGroup.list
164       l.should eq [@lg, @lg2]
165     end
166   end
167   describe '一覧取得オプションに於いて' do
168     it 'includeキーを含んでいる' do
169       r = LicenseGroup.list_opt
170       r.has_key?(:include).should be_true
171     end
172     it '1つの項目を含んでいる' do
173       r = LicenseGroup.list_opt[:include]
174       r.should have(1).items
175     end
176     it 'ライセンスを含んでいる' do
177       r = LicenseGroup.list_opt[:include]
178       r.has_key?(:licenses).should be_true
179     end
180   end
181   describe 'json一覧出力オプションに於いて' do
182     before 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
196     end
197     it 'ライセンスを含んでいる' do
198       r = LicenseGroup.list.to_json LicenseGroup.list_json_opt
199       j = JSON.parse r
200       i = j.first
201       i.has_key?('licenses').should be_true
202     end
203   end
204   
205   describe '単体取得に於いて' do
206     before do
207       @lg = FactoryGirl.create :license_group
208     end
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
214       end
215     end
216     it '指定のコマを返す' do
217       l = LicenseGroup.show @lg.id
218       l.should eq @lg
219     end
220     context '存在しないライセンスグループを開こうとしたとき' do
221       it '404RecordNotFound例外を返す' do
222         lambda{
223           LicenseGroup.show 110
224         }.should raise_error(ActiveRecord::RecordNotFound)
225       end
226     end
227   end
228   describe '単体取得オプションに於いて' do
229     it 'includeキーを含んでいる' do
230       r = LicenseGroup.show_opt
231       r.has_key?(:include).should be_true
232     end
233     it '1つの項目を含んでいる' do
234       r = LicenseGroup.show_opt[:include]
235       r.should have(1).items
236     end
237     it 'ライセンスを含んでいる' do
238       r = LicenseGroup.show_opt[:include]
239       r.has_key?(:licenses).should be_true
240     end
241   end
242   describe 'json単体出力オプションに於いて' do
243     before 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
257     end
258     it 'ライセンスを含んでいる' do
259       r = LicenseGroup.show(@lg.id).to_json LicenseGroup.show_json_opt
260       j = JSON.parse r
261       i = j
262       i.has_key?('licenses').should be_true
263     end
264   end
265   
266   describe '更新に於いて' do
267     before do
268       @n = @j.keys.first
269       @a = @j.values.first
270       @imager = ImagerTest.load("abc\ndef\nghi")
271       PettanImager.stub(:load).with(any_args).and_return(@imager)
272     end
273     context 'つつがなく終わるとき' do
274       it 'データ更新準備を依頼する' 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)
280       end
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)
287       end
288       it '保存を依頼する' do
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)
294       end
295       it 'オブジェクトを返す' do
296         r = LicenseGroup.store(@n, @a)
297         r.is_a?(LicenseGroup).should be_true
298         r.name.should eq @n
299         r.url.should eq @a["url"]
300       end
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
305       end
306     end
307     context 'ライセンス複数更新が失敗するとき' do
308       before 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)
312       end
313       it '全体エラーメッセージがセットされている' do
314         r = LicenseGroup.store(@n, @a)
315         r.errors[:base].should_not be_blank
316       end
317       it 'ライセンスが作成されていない' do
318         lambda {
319           r = LicenseGroup.store(@n, @a)
320         }.should_not change License, :count
321       end
322       it 'ライセンスグループが作成されていない' do
323         lambda {
324           r = LicenseGroup.store(@n, @a)
325         }.should_not change LicenseGroup, :count
326       end
327     end
328   end
329   
330   describe 'インポートに於いて' do
331     before do
332     end
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)
338       end
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)
343       end
344       it 'ライセンスグループが追加される' do
345         lambda {
346           LicenseGroup.import(@f)
347         }.should change LicenseGroup, :count
348       end
349       it 'ライセンスが追加される' do
350         lambda {
351           LicenseGroup.import(@f)
352         }.should change License, :count
353       end
354       it '[]を返す' do
355         r = LicenseGroup.import(@f)
356         r.should eq []
357       end
358     end
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)
364       end
365       it 'ライセンスグループが二個追加される' do
366         lambda {
367           r = LicenseGroup.import(@fs)
368         }.should change(LicenseGroup, :count).by 2
369       end
370       it 'ライセンスが追加される' do
371         lambda {
372           r = LicenseGroup.import(@fs)
373         }.should change(License, :count)
374       end
375       it '[]を返す' do
376         r = LicenseGroup.import(@fs)
377         r.should eq []
378       end
379     end
380     context 'ライセンスグループ作成に失敗したとき' do
381       before 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)
384       end
385       it 'ライセンスグループの数に変化がない' do
386         lambda {
387           LicenseGroup.import(@f)
388         }.should_not change LicenseGroup, :count
389       end
390       it '配列を返す' do
391         r = LicenseGroup.import(@f)
392         r.is_a?(Array).should be_true
393       end
394       it '配列の中身は一件' do
395         r = LicenseGroup.import(@f)
396         r.should have(1).items
397       end
398       it 'ライセンスグループオブジェクトが入っている' do
399         r = LicenseGroup.import(@f)
400         r.first.is_a?(LicenseGroup).should be_true
401       end
402     end
403     context '複数のライセンスグループ作成に失敗したとき' do
404       #三件中、二件の失敗、一件を成功させ、成功データは戻り値に含まないことを確認する
405       it 'ライセンスグループの数に変化がない' do
406         lambda {
407           LicenseGroup.import(@fes)
408         }.should_not change LicenseGroup, :count
409       end
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)
414       end
415       it '配列を返す' do
416         r = LicenseGroup.import(@fes)
417         r.is_a?(Array).should be_true
418       end
419       it '配列の中身は2件' do
420         r = LicenseGroup.import(@fes)
421         r.should have(2).items
422       end
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'
429       end
430     end
431   end
432   
433 end