OSDN Git Service

t#30322:create provider license import func
[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     it 'defined' 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
103       @l.supply_default
104     end
105   end
106   
107   describe '上書き補充に於いて' do
108     it 'defined' 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
112       @l.overwrite
113     end
114   end
115   
116   describe '閲覧許可に於いて' do
117     #ライセンスは作家作成する前から存在するので、閲覧制限の意味がない
118   end
119   
120   describe '一覧取得に於いて' do
121     before 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'
126     end
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)
131         r = License.list
132       end
133     end
134     it 'リストを返す' do
135       l = License.list 
136       l.should eq [@l]
137     end
138     it '名前順で並んでいる' do
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
141       l = License.list
142       l.should eq [@l3, @l, @l2]
143     end
144   end
145   describe '一覧取得オプションに於いて' do
146     it 'includeキーを含んでいる' do
147       r = License.list_opt
148       r.has_key?(:include).should be_true
149     end
150     it '1つの項目を含んでいる' do
151       r = License.list_opt[:include]
152       r.should have(1).items
153     end
154     it 'ライセンスグループを含んでいる' do
155       r = License.list_opt[:include]
156       r.has_key?(:license_group).should be_true
157     end
158   end
159   describe 'json一覧出力オプションに於いて' do
160     before 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
164     end
165     it 'ライセンスグループを含んでいる' do
166       r = License.list.to_json License.list_json_opt
167       j = JSON.parse r
168       i = j.first
169       i.has_key?('license_group').should be_true
170     end
171   end
172   
173   describe '単体取得に於いて' do
174     before 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
178     end
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
184       end
185     end
186     it '指定のライセンスを返す' do
187       l = License.show @l.id
188       l.should eq @l
189     end
190     context '存在しないライセンスを開こうとしたとき' do
191       it '404RecordNotFound例外を返す' do
192         lambda{
193           License.show 110
194         }.should raise_error(ActiveRecord::RecordNotFound)
195       end
196     end
197   end
198   describe '単体取得オプションに於いて' do
199     it 'includeキーを含んでいる' do
200       r = License.show_opt
201       r.has_key?(:include).should be_true
202     end
203     it '1つの項目を含んでいる' do
204       r = License.show_opt[:include]
205       r.should have(1).items
206     end
207     it 'ライセンスグループを含んでいる' do
208       r = License.show_opt[:include]
209       r.has_key?(:license_group).should be_true
210     end
211   end
212   describe 'json単体出力オプションに於いて' do
213     before 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
217     end
218     it 'ライセンスグループを含んでいる' do
219       r = License.show(@l.id).to_json License.show_json_opt
220       j = JSON.parse r
221       i = j
222       i.has_key?('license_group').should be_true
223     end
224   end
225   
226   describe '更新に於いて' do
227     before do
228       @lg = FactoryGirl.create :license_group
229       @f = Rails.root + 'spec/json/license_group.json'
230       @t = File.open(@f, 'r').read
231       @j = JSON.parse @t
232       @n = @j.keys.first
233       @a = @j.values.first
234       @attr = @a["licenses_attributes"]
235       @ln = @attr.keys.first
236       @la = @attr.values.first
237       @la["license_group_id"] = @lg.id
238     end
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)
246       end
247       it 'データ更新準備を依頼する' do
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)
253       end
254       it '保存を依頼する' do
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)
260       end
261       it 'オブジェクトを返す' do
262         r = License.store(@ln, @la)
263         r.is_a?(License).should be_true
264         r.name.should eq @ln
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
269       end
270       it 'ライセンスが作成されている' do
271         lambda {
272           r = License.store(@ln, @la)
273         }.should change License, :count
274       end
275       it 'システム画像が作成されている' do
276         lambda {
277           r = License.store(@ln, @la)
278         }.should change SystemPicture, :count
279       end
280     end
281     context 'システム画像置換が失敗するとき' do
282       before 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)
285       end
286       it '全体エラーメッセージがセットされている' do
287         r = License.store(@ln, @la)
288         r.errors[:base].should_not be_blank
289       end
290       it 'ライセンスが作成されていない' do
291         lambda {
292           r = License.store(@ln, @la)
293         }.should_not change License, :count
294       end
295     end
296   end
297   
298   describe '複数の更新に於いて' do
299     before do
300       @lg = FactoryGirl.create :license_group
301       @fs = Rails.root + 'spec/json/license_groups.json'
302       @ts = File.open(@fs, 'r').read
303       @js = JSON.parse @ts
304       @n = @js.keys.last
305       @a = @js.values.last
306       @attr = @a["licenses_attributes"]
307     end
308     context '2件データでつつがなく終わるとき' do
309       it '更新を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)
314       end
315       it '失敗件数0を返す' do
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)
319         r.should eq 0
320       end
321     end
322     context '2件データで失敗するとき' do
323       it '更新を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)
328       end
329       it '失敗件数2を返す' do
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)
333         r.should eq 2
334       end
335     end
336     context 'attrsがnilなどのHashでないとき' do
337       it '処理にかけず0を返す' do
338         r = License.stores(nil, @lg.id)
339         r.should eq 0
340       end
341     end
342   end
343   
344   describe 'エクスポートに於いて' do
345     before do
346       @sp = FactoryGirl.create :system_picture
347       @lg = FactoryGirl.create :license_group
348       @l = FactoryGirl.create :license, :license_group_id => @lg.id, :system_picture_id => @sp.id
349       @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
350     end
351     it '開始日時が省略された場合はすべてのライセンスを返す' do
352       r = License.export 
353       r.should eq [@l, @l2]
354     end
355     it '開始日時以降に更新されたライセンスを返す' do
356       r = License.export @l.updated_at - 100
357       r.should eq [@l]
358     end
359   end
360   
361 end