OSDN Git Service

t#31834:new base
[pettanr/pettanr.git] / spec / models / license_spec.rb
1 # -*- encoding: utf-8 -*-
2 #ライセンス
3 require 'spec_helper'
4
5 describe License do
6   before do
7   end
8   describe '検証に於いて' do
9     before do
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
13     end
14     
15     context 'オーソドックスなデータのとき' do
16       it '下限データが通る' do
17         @l.name = 'a'
18         @l.caption = 'a'
19         @l.url = 'http://test.jp/'
20         @l.should be_valid
21       end
22       it '上限データが通る' do
23         @l.name = 'a'*50
24         @l.caption = 'a'*30
25         @l.url = 'http://test.jp/aaaaa' + 'a' * 180
26         @l.should be_valid
27       end
28     end
29     
30     context 'license_group_idを検証するとき' do
31       it 'nullなら失敗する' do
32         @l.license_group_id = ''
33         @l.should_not be_valid
34       end
35       it '数値でなければ失敗する' do
36         @l.license_group_id = 'a'
37         @l.should_not be_valid
38       end
39       it '存在するライセンスグループでなければ失敗する' do
40         @l.license_group_id = 0
41         @l.should_not be_valid
42       end
43     end
44     context 'nameを検証するとき' do
45       it 'nullなら失敗する' do
46         @l.name = ''
47         @l.should_not be_valid
48       end
49       it '51文字以上なら失敗する' do
50         @l.name = 'a'*51
51         @l.should_not be_valid
52       end
53       it '重複していたら失敗する' do
54         lc = FactoryGirl.create :license
55         @l.should_not be_valid
56       end
57     end
58     context 'captionを検証するとき' do
59       it 'nullなら失敗する' do
60         @l.caption = ''
61         @l.should_not be_valid
62       end
63       it '31文字以上なら失敗する' do
64         @l.caption = 'a'*31
65         @l.should_not be_valid
66       end
67     end
68     context 'urlを検証するとき' do
69       it 'nullなら失敗する' do
70         @l.url = ''
71         @l.should_not be_valid
72       end
73       it '201文字以上なら失敗する' do
74         @l.url = 'http://test.jp/aaaaa' + 'a' * 181
75         @l.should_not be_valid
76       end
77       it 'url形式でないなら失敗する' do
78         @l.url = 'a'*200
79         @l.should_not be_valid
80       end
81     end
82     context 'system_picture_idを検証するとき' do
83       it 'nullなら失敗する' do
84         @l.system_picture_id = ''
85         @l.should_not be_valid
86       end
87       it '数値でなければ失敗する' do
88         @l.system_picture_id = 'a'
89         @l.should_not be_valid
90       end
91       it '存在するシステム画像でなければ失敗する' do
92         @l.system_picture_id = 0
93         @l.should_not be_valid
94       end
95     end
96   end
97   
98   describe '文字コード検証に於いて' do
99     before 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
103     end
104     
105     context 'nameを検証するとき' do
106       it 'Shift JISなら失敗する' do
107         @l.name = "\x83G\x83r\x83]\x83D"
108         lambda{
109           @l.valid_encode
110         }.should raise_error(Pettanr::BadRequest)
111       end
112     end
113     
114     context 'captionを検証するとき' do
115       it 'Shift JISなら失敗する' do
116         @l.caption = "\x83G\x83r\x83]\x83D"
117         lambda{
118           @l.valid_encode
119         }.should raise_error(Pettanr::BadRequest)
120       end
121     end
122     
123     context 'urlを検証するとき' do
124       it 'Shift JISなら失敗する' do
125         @l.url = "\x83G\x83r\x83]\x83D"
126         lambda{
127           @l.valid_encode
128         }.should raise_error(Pettanr::BadRequest)
129       end
130     end
131     
132     context 'settingsを検証するとき' do
133       it 'Shift JISなら失敗する' do
134         @l.settings = "\x83G\x83r\x83]\x83D"
135         lambda{
136           @l.valid_encode
137         }.should raise_error(Pettanr::BadRequest)
138       end
139     end
140     
141     context 'credit_picturesを検証するとき' do
142       it 'Shift JISなら失敗する' do
143         @l.credit_pictures = "\x83G\x83r\x83]\x83D"
144         lambda{
145           @l.valid_encode
146         }.should raise_error(Pettanr::BadRequest)
147       end
148     end
149     
150   end
151   
152   describe 'デフォルト値補充に於いて' do
153     it 'defined' 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
157       @l.supply_default
158     end
159   end
160   
161   describe '上書き補充に於いて' do
162     it 'defined' 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
166       @l.overwrite
167     end
168   end
169   
170   describe '閲覧許可に於いて' do
171     #ライセンスは作家作成する前から存在するので、閲覧制限の意味がない
172   end
173   
174   describe '一覧取得に於いて' do
175     before 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'
180     end
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)
185         r = License.list
186       end
187     end
188     it 'リストを返す' do
189       l = License.list 
190       l.should eq [@l]
191     end
192     it '名前順で並んでいる' do
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
195       l = License.list
196       l.should eq [@l3, @l, @l2]
197     end
198   end
199   describe '一覧取得オプションに於いて' do
200     it 'includeキーを含んでいる' do
201       r = License.list_opt
202       r.has_key?(:include).should be_true
203     end
204     it '1つの項目を含んでいる' do
205       r = License.list_opt[:include]
206       r.should have(1).items
207     end
208     it 'ライセンスグループを含んでいる' do
209       r = License.list_opt[:include]
210       r.has_key?(:license_group).should be_true
211     end
212   end
213   describe 'json一覧出力オプションに於いて' do
214     before 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
218     end
219     it 'ライセンスグループを含んでいる' do
220       r = License.list.to_json License.list_json_opt
221       j = JSON.parse r
222       i = j.first
223       i.has_key?('license_group').should be_true
224     end
225   end
226   
227   describe '単体取得に於いて' do
228     before 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
232     end
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
238       end
239     end
240     it '指定のライセンスを返す' do
241       l = License.show @l.id
242       l.should eq @l
243     end
244     context '存在しないライセンスを開こうとしたとき' do
245       it '404RecordNotFound例外を返す' do
246         lambda{
247           License.show 110
248         }.should raise_error(ActiveRecord::RecordNotFound)
249       end
250     end
251   end
252   describe '単体取得オプションに於いて' do
253     it 'includeキーを含んでいる' do
254       r = License.show_opt
255       r.has_key?(:include).should be_true
256     end
257     it '1つの項目を含んでいる' do
258       r = License.show_opt[:include]
259       r.should have(1).items
260     end
261     it 'ライセンスグループを含んでいる' do
262       r = License.show_opt[:include]
263       r.has_key?(:license_group).should be_true
264     end
265   end
266   describe 'json単体出力オプションに於いて' do
267     before 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
271     end
272     it 'ライセンスグループを含んでいる' do
273       r = License.show(@l.id).to_json License.show_json_opt
274       j = JSON.parse r
275       i = j
276       i.has_key?('license_group').should be_true
277     end
278   end
279   
280   describe '管理名検索に於いて' do
281     before 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
287     end
288     context 'つつがなく終わるとき' do
289       it 'リストを返す' do
290         l = License.list_by_name @l2.name
291         l.should eq [@l2]
292       end
293     end
294   end
295   
296   describe '更新に於いて' do
297     before do
298       @lg = FactoryGirl.create :license_group
299       @f = Rails.root + 'spec/json/license_group.json'
300       @t = File.open(@f, 'r').read
301       @j = JSON.parse @t
302       @n = @j.keys.first
303       @a = @j.values.first
304       @attr = @a["licenses_attributes"]
305       @ln = @attr.keys.first
306       @la = @attr.values.first
307       @la["license_group_id"] = @lg.id
308     end
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)
316       end
317       it 'データ更新準備を依頼する' do
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)
323       end
324       it '保存を依頼する' do
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)
330       end
331       it 'オブジェクトを返す' do
332         r = License.store(@ln, @la)
333         r.is_a?(License).should be_true
334         r.name.should eq @ln
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
339       end
340       it 'ライセンスが作成されている' do
341         lambda {
342           r = License.store(@ln, @la)
343         }.should change License, :count
344       end
345       it 'システム画像が作成されている' do
346         lambda {
347           r = License.store(@ln, @la)
348         }.should change SystemPicture, :count
349       end
350     end
351     context 'システム画像置換が失敗するとき' do
352       before 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)
355       end
356       it '全体エラーメッセージがセットされている' do
357         r = License.store(@ln, @la)
358         r.errors[:base].should_not be_blank
359       end
360       it 'ライセンスが作成されていない' do
361         lambda {
362           r = License.store(@ln, @la)
363         }.should_not change License, :count
364       end
365     end
366   end
367   
368   describe '複数の更新に於いて' do
369     before do
370       @lg = FactoryGirl.create :license_group
371       @fs = Rails.root + 'spec/json/license_groups.json'
372       @ts = File.open(@fs, 'r').read
373       @js = JSON.parse @ts
374       @n = @js.keys.last
375       @a = @js.values.last
376       @attr = @a["licenses_attributes"]
377     end
378     context '2件データでつつがなく終わるとき' do
379       it '更新を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)
384       end
385       it '失敗件数0を返す' do
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)
389         r.should eq 0
390       end
391     end
392     context '2件データで失敗するとき' do
393       it '更新を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)
398       end
399       it '失敗件数2を返す' do
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)
403         r.should eq 2
404       end
405     end
406     context 'attrsがnilなどのHashでないとき' do
407       it '処理にかけず0を返す' do
408         r = License.stores(nil, @lg.id)
409         r.should eq 0
410       end
411     end
412   end
413   
414   describe 'エクスポートに於いて' do
415     before 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
420     end
421     it '開始日時が省略された場合はすべてのライセンスを返す' do
422       r = License.export 
423       r.should eq [@l, @l2]
424     end
425     it '開始日時以降に更新されたライセンスを返す' do
426       r = License.export @l.updated_at - 100
427       r.should eq [@l]
428     end
429   end
430   
431 end