OSDN Git Service

3f87421516f0ed20e0eac4711128af94860bdbfe
[pettanr/pettanr.git] / spec / models / provider_spec.rb
1 # -*- encoding: utf-8 -*-
2 #貸手
3 require 'spec_helper'
4
5 describe Provider do
6   before do
7     @admin = FactoryGirl.create :admin
8     @sp = FactoryGirl.create :system_picture
9     @lg = FactoryGirl.create :license_group
10     @license = FactoryGirl.create :license, :license_group_id => @lg.id, :system_picture_id => @sp.id
11     @user = FactoryGirl.create :user_yas
12     @author = @user.author    #ユーザ作成時に連動して作成される
13     
14     #テストデータを用意してね
15     @f = Rails.root + 'spec/json/provider_source.json'
16     @t = File.open(@f, 'r').read
17     @j = JSON.parse @t
18   end
19   describe '検証に於いて' do
20     before do
21       @ps = FactoryGirl.create :provider_status
22       @pd = FactoryGirl.build :provider, :provider_status_id => @ps.id
23     end
24     
25     context 'オーソドックスなデータのとき' do
26       it '下限データが通る' do
27         @pd.name = 'a'
28         @pd.caption = 'a'
29         @pd.url = 'http://test.jp/'
30         @pd.description = 'a'
31         @pd.demander_url = 'http://test.jp/'
32         @pd.should be_valid
33       end
34       it '上限データが通る' do
35         @pd.name = 'a'*50
36         @pd.caption = 'a'*30
37         @pd.url = 'http://test.jp/aaaaa' + 'a' * 180
38         @pd.description = 'a' * 99999
39         @pd.demander_url = 'http://test.jp/aaaaa' + 'a' * 180
40         @pd.should be_valid
41       end
42     end
43     
44     context 'provider_status_idを検証するとき' do
45       it 'nullなら失敗する' do
46         @pd.provider_status_id = nil
47         @pd.should_not be_valid
48       end
49       it '数値でなければ失敗する' do
50         @pd.provider_status_id = 'a'
51         @pd.should_not be_valid
52       end
53       it '存在する借受状況でなければ失敗する' do
54         @pd.provider_status_id = 0
55         @pd.should_not be_valid
56       end
57     end
58     context 'nameを検証するとき' do
59       it 'nullなら失敗する' do
60         @pd.name = ''
61         @pd.should_not be_valid
62       end
63       it '51文字以上なら失敗する' do
64         @pd.name = 'a'*51
65         @pd.should_not be_valid
66       end
67       it '重複していたら失敗する' do
68         l = FactoryGirl.create :provider
69         @pd.should_not be_valid
70       end
71     end
72     context 'captionを検証するとき' do
73       it 'nullなら失敗する' do
74         @pd.caption = ''
75         @pd.should_not be_valid
76       end
77       it '31文字以上なら失敗する' do
78         @pd.caption = 'a'*31
79         @pd.should_not be_valid
80       end
81     end
82     context 'urlを検証するとき' do
83       it 'nullなら失敗する' do
84         @pd.url = ''
85         @pd.should_not be_valid
86       end
87       it '201文字以上なら失敗する' do
88         @pd.url = 'http://test.jp/aaaaa' + 'a' * 181
89         @pd.should_not be_valid
90       end
91       it 'url形式でないなら失敗する' do
92         @pd.url = 'aaaaaaa'
93         @pd.should_not be_valid
94       end
95     end
96     context 'descriptionを検証するとき' do
97       it 'nullなら失敗する' do
98         @pd.description= ''
99         @pd.should_not be_valid
100       end
101     end
102     context 'demander_urlを検証するとき' do
103       it 'nullなら失敗する' do
104         @pd.demander_url = ''
105         @pd.should_not be_valid
106       end
107       it '201文字以上なら失敗する' do
108         @pd.demander_url = 'http://test.jp/aaaaa' + 'a' * 181
109         @pd.should_not be_valid
110       end
111       it 'url形式でないなら失敗する' do
112         @pd.demander_url = 'aaaaaaa'
113         @pd.should_not be_valid
114       end
115     end
116   end
117   
118   describe 'デフォルト値補充に於いて' do
119     it 'defined' do
120       @ps = FactoryGirl.create :provider_status
121       @pd = FactoryGirl.build :provider, :provider_status_id => @ps.id
122       @pd.supply_default
123     end
124   end
125   
126   describe '上書き補充に於いて' do
127     it 'defined' do
128       @ps = FactoryGirl.create :provider_status
129       @pd = FactoryGirl.build :provider, :provider_status_id => @ps.id
130       @pd.overwrite
131     end
132   end
133   
134   describe '所持判定に於いて' do
135     before do
136       @ps = FactoryGirl.create :provider_status
137       @pd = FactoryGirl.build :provider, :provider_status_id => @ps.id
138     end
139     it '管理者ならyes' do
140       @pd.own?(@admin).should == true
141     end
142     it '作家ならno' do
143       @pd.own?(@author).should == false
144     end
145     it 'パラメータが管理者でないならno' do
146       @pd.own?(nil).should == false
147     end
148   end
149   
150   describe '閲覧許可に於いて' do
151     before do
152       @ps = FactoryGirl.create :provider_status
153       @pd = FactoryGirl.build :provider, :provider_status_id => @ps.id
154     end
155     it '管理者なら許可する' do
156       r = @pd.visible?(@admin)
157       r.should == true
158     end
159     it '作家なら許可しない' do
160       r = @pd.visible?(@author)
161       r.should == false
162     end
163     it 'それ以外のとき不許可を返す。' do
164       r = @pd.visible?(nil)
165       r.should be_false
166     end
167   end
168   
169   describe '状態に於いて' do
170     before do
171       @ps = FactoryGirl.create :provider_status
172       @pd = FactoryGirl.create :provider, :provider_status_id => @ps.id
173     end
174     it '借り受けしていないのとき0を返す' do
175       ProviderStatus.any_instance.stub(:token).with(any_args).and_return(nil)
176       r = @pd.status
177       r.should eq 0
178     end
179     it '借り受けしているのとき1を返す' do
180       ProviderStatus.any_instance.stub(:token).with(any_args).and_return('a')
181       r = @pd.status
182       r.should eq 1
183     end
184   end
185   
186   describe '一覧取得に於いて' do
187     before do
188       @ps = FactoryGirl.create :provider_status
189       @pd = FactoryGirl.create :provider, :provider_status_id => @ps.id, :name => "6"
190     end
191     context 'page補正について' do
192       it '文字列から数値に変換される' do
193         Provider.page('8').should eq 8
194       end
195       it 'nilの場合は1になる' do
196         Provider.page().should eq 1
197       end
198       it '0以下の場合は1になる' do
199         Provider.page('0').should eq 1
200       end
201     end
202     context 'page_size補正について' do
203       it '文字列から数値に変換される' do
204         Provider.page_size('7').should eq 7
205       end
206       it 'nilの場合はProvider.default_page_sizeになる' do
207         Provider.page_size().should eq Provider.default_page_size
208       end
209       it '0以下の場合はProvider.default_page_sizeになる' do
210         Provider.page_size('0').should eq Provider.default_page_size
211       end
212       it 'Provider.max_page_sizeを超えた場合はProvider.max_page_sizeになる' do
213         Provider.page_size('1000').should eq Provider.max_page_size
214       end
215     end
216     context 'つつがなく終わるとき' do
217       it '一覧取得オプションを利用している' do
218         Provider.stub(:list_opt).with(any_args).and_return({})
219         Provider.should_receive(:list_opt).with(any_args).exactly(1)
220         r = Provider.list
221       end
222     end
223     it 'リストを返す' do
224       r = Provider.list
225       r.should eq [@pd]
226     end
227     it '管理名で並んでいる' do
228       v = FactoryGirl.create :provider, :name => "0"
229       r = Provider.list
230       r.should eq [v, @pd]
231     end
232     context 'DBに5件あって1ページの件数を2件に変えたとして' do
233       before do
234         @ps2 = FactoryGirl.create :provider_status
235         @pd2 = FactoryGirl.create :provider, :provider_status_id => @ps2.id, :name => "5"
236         @ps3 = FactoryGirl.create :provider_status
237         @pd3 = FactoryGirl.create :provider, :provider_status_id => @ps3.id, :name => "4"
238         @ps4 = FactoryGirl.create :provider_status
239         @pd4 = FactoryGirl.create :provider, :provider_status_id => @ps4.id, :name => "3"
240         @ps5 = FactoryGirl.create :provider_status
241         @pd5 = FactoryGirl.create :provider, :provider_status_id => @ps5.id, :name => "2"
242         Provider.stub(:default_page_size).and_return(2)
243       end
244       it '通常は2件を返す' do
245         r = Provider.list
246         r.should have(2).items 
247       end
248       it 'page=1なら末尾2件を返す' do
249         #管理名で並んでいる
250         r = Provider.list(1)
251         r.should eq [@pd5, @pd4]
252       end
253       it 'page=2なら中間2件を返す' do
254         r = Provider.list(2)
255         r.should eq [@pd3, @pd2]
256       end
257       it 'page=3なら先頭1件を返す' do
258         r = Provider.list(3)
259         r.should eq [@pd]
260       end
261     end
262     context 'DBに5件あって1ページの件数を2件に変えたとして' do
263       before do
264         @ps2 = FactoryGirl.create :provider_status
265         @pd2 = FactoryGirl.create :provider, :provider_status_id => @ps2.id, :name => "5"
266         @ps3 = FactoryGirl.create :provider_status
267         @pd3 = FactoryGirl.create :provider, :provider_status_id => @ps3.id, :name => "4"
268         @ps4 = FactoryGirl.create :provider_status
269         @pd4 = FactoryGirl.create :provider, :provider_status_id => @ps4.id, :name => "3"
270         @ps5 = FactoryGirl.create :provider_status
271         @pd5 = FactoryGirl.create :provider, :provider_status_id => @ps5.id, :name => "2"
272         Provider.stub(:default_page_size).and_return(2)
273       end
274       it '件数0は全件(5件)を返す' do
275         r = Provider.list 5, 0
276         r.should have(5).items 
277       end
278     end
279   end
280   describe '待機中一覧取得に於いて' do
281     before do
282       @ps = FactoryGirl.create :provider_status
283       @pd = FactoryGirl.create :provider, :provider_status_id => @ps.id, :name => "6"
284     end
285     context 'つつがなく終わるとき' do
286       it '一覧取得オプションを利用している' do
287         Provider.stub(:list_opt).with(any_args).and_return({:include => {:provider_status => {}} })
288         Provider.should_receive(:list_opt).with(any_args).exactly(1)
289         r = Provider.available_list
290       end
291     end
292     it 'リストを返す' do
293       r = Provider.available_list
294       r.should eq [@pd]
295     end
296     it '管理名で並んでいる' do
297       @ps2 = FactoryGirl.create :provider_status
298       v = FactoryGirl.create :provider, :name => "0", :provider_status_id => @ps2.id
299       r = Provider.available_list
300       r.should eq [v, @pd]
301     end
302     it '借受状況のトークンが設定されていない貸手に限る' do
303       @ps2 = FactoryGirl.create :provider_status, :token => 'aaaaa'
304       v = FactoryGirl.create :provider, :name => "0", :provider_status_id => @ps2.id
305       r = Provider.available_list
306       r.should eq [@pd]
307     end
308     context 'DBに5件あって1ページの件数を2件に変えたとして' do
309       before do
310         @ps2 = FactoryGirl.create :provider_status
311         @pd2 = FactoryGirl.create :provider, :provider_status_id => @ps2.id, :name => "5"
312         @ps3 = FactoryGirl.create :provider_status
313         @pd3 = FactoryGirl.create :provider, :provider_status_id => @ps3.id, :name => "4"
314         @ps4 = FactoryGirl.create :provider_status
315         @pd4 = FactoryGirl.create :provider, :provider_status_id => @ps4.id, :name => "3"
316         @ps5 = FactoryGirl.create :provider_status
317         @pd5 = FactoryGirl.create :provider, :provider_status_id => @ps5.id, :name => "2"
318         Provider.stub(:default_page_size).and_return(2)
319       end
320       it '通常は2件を返す' do
321         r = Provider.available_list
322         r.should have(2).items 
323       end
324       it 'page=1なら末尾2件を返す' do
325         #管理名で並んでいる
326         r = Provider.available_list(1)
327         r.should eq [@pd5, @pd4]
328       end
329       it 'page=2なら中間2件を返す' do
330         r = Provider.available_list(2)
331         r.should eq [@pd3, @pd2]
332       end
333       it 'page=3なら先頭1件を返す' do
334         r = Provider.available_list(3)
335         r.should eq [@pd]
336       end
337     end
338     context 'DBに5件あって1ページの件数を2件に変えたとして' do
339       before do
340         @ps2 = FactoryGirl.create :provider_status
341         @pd2 = FactoryGirl.create :provider, :provider_status_id => @ps2.id, :name => "5"
342         @ps3 = FactoryGirl.create :provider_status
343         @pd3 = FactoryGirl.create :provider, :provider_status_id => @ps3.id, :name => "4"
344         @ps4 = FactoryGirl.create :provider_status
345         @pd4 = FactoryGirl.create :provider, :provider_status_id => @ps4.id, :name => "3"
346         @ps5 = FactoryGirl.create :provider_status
347         @pd5 = FactoryGirl.create :provider, :provider_status_id => @ps5.id, :name => "2"
348         Provider.stub(:default_page_size).and_return(2)
349       end
350       it '件数0は全件(5件)を返す' do
351         r = Provider.available_list 5, 0
352         r.should have(5).items 
353       end
354     end
355   end
356   describe '一覧取得オプションに於いて' do
357     it 'includeキーを含んでいる' do
358       r = Provider.list_opt
359       r.has_key?(:include).should be_true
360     end
361     it '1つの項目を含んでいる' do
362       r = Provider.list_opt[:include]
363       r.should have(1).items
364     end
365     it '借受状況を含んでいる' do
366       r = Provider.list_opt[:include]
367       r.has_key?(:provider_status).should be_true
368     end
369   end
370   describe 'json一覧出力オプションに於いて' do
371     before do
372       @ps = FactoryGirl.create :provider_status
373       @pd = FactoryGirl.create :provider, :provider_status_id => @ps.id
374     end
375     it '借受状況を含んでいる' do
376       r = Provider.list.to_json Provider.list_json_opt
377       j = JSON.parse r
378       i = j.first
379       i.has_key?('provider_status').should be_true
380     end
381   end
382   
383   describe '単体取得に於いて' do
384     before do
385       @ps = FactoryGirl.create :provider_status
386       @pd = FactoryGirl.create :provider, :provider_status_id => @ps.id
387     end
388     context 'つつがなく終わるとき' do
389       it '単体取得オプションを利用している' do
390         Provider.stub(:show_opt).with(any_args).and_return({})
391         Provider.should_receive(:show_opt).with(any_args).exactly(1)
392         r = Provider.show @pd.id, @admin
393       end
394       it '閲覧許可を問い合わせている' do
395         Provider.any_instance.stub(:visible?).with(any_args).and_return(true)
396         Provider.any_instance.should_receive(:visible?).with(any_args).exactly(1)
397         r = Provider.show @pd.id, @admin
398       end
399     end
400     it '指定の貸手を返す' do
401       r = Provider.show @pd.id, @admin
402       r.should eq @pd
403     end
404     context '閲覧許可が出なかったとき' do
405       it '403Forbidden例外を返す' do
406         Provider.any_instance.stub(:visible?).and_return(false)
407         lambda{
408           Provider.show @pd.id, @admin
409         }.should raise_error(ActiveRecord::Forbidden)
410       end
411     end
412     context '存在しない貸手を開こうとしたとき' do
413       it '404RecordNotFound例外を返す' do
414         lambda{
415           Provider.show 110, @admin
416         }.should raise_error(ActiveRecord::RecordNotFound)
417       end
418     end
419   end
420   
421   describe '編集取得に於いて' do
422     before do
423       @ps = FactoryGirl.create :provider_status
424       @pd = FactoryGirl.create :provider, :provider_status_id => @ps.id
425     end
426     context 'つつがなく終わるとき' do
427       it '単体取得オプションを利用している' do
428         Provider.stub(:show_opt).with(any_args).and_return({})
429         Provider.should_receive(:show_opt).with(any_args).exactly(1)
430         r = Provider.edit @pd.id, @admin
431       end
432       it '所持判定を問い合わせている' do
433         Provider.any_instance.stub(:own?).with(any_args).and_return(true)
434         Provider.any_instance.should_receive(:own?).with(any_args).exactly(1)
435         r = Provider.edit @pd.id, @admin
436       end
437     end
438     it '指定の貸手を返す' do
439       Provider.any_instance.stub(:own?).and_return(true)
440       r = Provider.edit @pd.id, @admin
441       r.should eq @pd
442     end
443     context '権限がなかったとき' do
444       it '403Forbidden例外を返す' do
445         Provider.any_instance.stub(:own?).and_return(false)
446         lambda{
447           Provider.edit @pd.id, @admin
448         }.should raise_error(ActiveRecord::Forbidden)
449       end
450     end
451     context '存在しない貸手を開こうとしたとき' do
452       it '404RecordNotFound例外を返す' do
453         lambda{
454           Provider.edit 110, @admin
455         }.should raise_error(ActiveRecord::RecordNotFound)
456       end
457     end
458   end
459   describe '単体取得オプションに於いて' do
460     it 'includeキーを含んでいる' do
461       r = Provider.show_opt
462       r.has_key?(:include).should be_true
463     end
464     it '1つの項目を含んでいる' do
465       r = Provider.show_opt[:include]
466       r.should have(1).items
467     end
468     it '借受状況を含んでいる' do
469       r = Provider.show_opt[:include]
470       r.has_key?(:provider_status).should be_true
471     end
472   end
473   describe 'json単体出力オプションに於いて' do
474     before do
475       @ps = FactoryGirl.create :provider_status
476       @pd = FactoryGirl.create :provider, :provider_status_id => @ps.id
477     end
478     it '借受状況を含んでいる' do
479       r = Provider.show(@pd.id, @admin).to_json Provider.show_json_opt
480       j = JSON.parse r
481       i = j
482       i.has_key?('provider_status').should be_true
483     end
484   end
485   
486   describe '更新作成に於いて' do
487     before do
488       @ps = FactoryGirl.create :provider_status
489       @pd = FactoryGirl.build :provider, :provider_status_id => @ps.id
490       @name = @pd.name
491       @attr = @pd.attributes
492     end
493     context 'つつがなく終わるとき' do
494       before do
495       end
496       it '貸手が追加される' do
497         lambda {
498           r = Provider.store(@name, @attr)
499         }.should change Provider, :count
500       end
501       it '借受状況が作成される' do
502         lambda {
503           r = Provider.store(@name, @attr)
504         }.should change ProviderStatus, :count
505       end
506       it '貸手を返す' do
507         r = Provider.store(@name, @attr)
508         r.is_a?(Provider).should be_true
509         r.valid?.should be_true
510       end
511     end
512     context '借受状況があるとき' do
513       before do
514         @pd = FactoryGirl.create :provider, :provider_status_id => @ps.id
515         @name = @pd.name
516         @attr = @pd.attributes
517       end
518       it '貸手は変化しない' do
519         lambda {
520           r = Provider.store(@name, @attr)
521         }.should_not change Provider, :count
522       end
523       it '借受状況は変化しない' do
524         lambda {
525           r = Provider.store(@name, @attr)
526         }.should_not change ProviderStatus, :count
527       end
528     end
529     context '借受状況の作成に失敗したとき' do
530       before do
531         ProviderStatus.any_instance.stub(:save).with(any_args).and_return(false)
532       end
533       it '貸手は変化しない' do
534         lambda {
535           r = Provider.store(@name, @attr)
536         }.should_not change Provider, :count
537       end
538       it '借受状況は変化しない' do
539         lambda {
540           r = Provider.store(@name, @attr)
541         }.should_not change ProviderStatus, :count
542       end
543       it '貸手の検証は失敗している' do
544         r = Provider.store(@name, @attr)
545         r.valid?.should be_false
546       end
547     end
548   end
549   
550   describe 'インポートに於いて' do
551     before do
552       @urls = ['http://pettan.net/provider_source.json']
553     end
554     context '事前チェック' do
555       before do
556         WebMock.stub_request(:get, @urls.first).to_return(:body => @t)
557       end
558       it '貸手文献インポートを依頼する' do
559         Provider.should_receive(:import_urls).with(@urls).exactly(1)
560         Provider.stub(:import_urls).with(@urls).and_return({@urls.first => {:validations => []}})
561         Provider.import(@urls)
562       end
563       it '貸手更新を一回依頼する' do
564         Provider.stub(:store).with(any_args).and_return(Provider.new)
565         Provider.should_receive(:store).with(any_args).exactly(1)
566         Provider.import(@urls)
567       end
568     end
569     context 'つつがなく終わるとき' do
570       before do
571         WebMock.stub_request(:get, @urls.first).to_return(:body => @t)
572       end
573       it '貸手が追加される' do
574         lambda {
575           Provider.import(@urls)
576         }.should change Provider, :count
577       end
578       it '借受状況がなかったら、借受状況を作成される' do
579         lambda {
580           Provider.import(@urls)
581         }.should change ProviderStatus, :count
582       end
583       it '借受状況があるなら、借受状況は作成しない' do
584         Provider.import(@urls)
585         ProviderStatus.any_instance.should_not_receive(:save).exactly(1)
586         Provider.import(@urls)
587       end
588       it 'Hashを返す' do
589         r = Provider.import(@urls)
590         r.is_a?(Hash).should be_true
591       end
592     end
593     context '貸手作成に失敗したとき' do
594       before do
595         WebMock.stub_request(:get, @urls.first).to_return(:status => 404)
596       end
597       it '貸手の数に変化がない' do
598         lambda {
599           Provider.import(@urls)
600         }.should_not change Provider, :count
601       end
602       it 'Hashを返す' do
603         r = Provider.import(@urls)
604         r.is_a?(Hash).should be_true
605       end
606     end
607   end
608   
609 end