OSDN Git Service

t#31223:add them contents list for author and artist
[pettanr/pettanr.git] / spec / models / provider_status_spec.rb
1 # -*- encoding: utf-8 -*-
2 #借受状況
3 require 'spec_helper'
4
5 describe ProviderStatus 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   end
14   describe '検証に於いて' do
15     before do
16       @ps = FactoryGirl.build :provider_status
17     end
18     
19     context 'オーソドックスなデータのとき' do
20       it '下限データが通る' do
21         @ps.receive_hour1 = -99999
22         @ps.receive_hour2 = -99999
23         @ps.should be_valid
24       end
25       it '上限データが通る' do
26         @ps.receive_hour1 = 99999
27         @ps.receive_hour2 = 99999
28         @ps.should be_valid
29       end
30     end
31     
32     context 'receive_hour1を検証するとき' do
33       it '数値でなければ失敗する' do
34         @ps.receive_hour1 = 'a'
35         @ps.should_not be_valid
36       end
37     end
38     context 'receive_hour2を検証するとき' do
39       it '数値でなければ失敗する' do
40         @ps.receive_hour2 = 'a'
41         @ps.should_not be_valid
42       end
43     end
44   end
45   
46   describe 'デフォルト値補充に於いて' do
47     it 'defined' do
48       @ps = FactoryGirl.build :provider_status
49       @ps.supply_default
50     end
51   end
52   
53   describe '上書き補充に於いて' do
54     it 'defined' do
55       @ps = FactoryGirl.build :provider_status
56       @ps.overwrite
57     end
58   end
59   
60   describe '所持判定に於いて' do
61     before do
62       @ps = FactoryGirl.create :provider_status
63     end
64     it '管理者ならyes' do
65       @ps.own?(@admin).should == true
66     end
67     it '作家ならno' do
68       @ps.own?(@author).should == false
69     end
70     it 'パラメータが管理者でないならno' do
71       @ps.own?(nil).should == false
72     end
73   end
74   
75   describe '閲覧許可に於いて' do
76     before do
77       @ps = FactoryGirl.create :provider_status
78     end
79     it '管理者なら許可する' do
80       r = @ps.visible?(@admin)
81       r.should == true
82     end
83     it '作家なら許可しない' do
84       r = @ps.visible?(@author)
85       r.should == false
86     end
87     it 'それ以外のとき不許可を返す。' do
88       r = @ps.visible?(nil)
89       r.should be_false
90     end
91   end
92   
93   describe '状態に於いて' do
94     before do
95     end
96     it '借り受けしていないのとき0を返す' do
97       @ps = FactoryGirl.create :provider_status, :token => nil
98       r = @ps.status
99       r.should eq 0
100     end
101     it '借り受けしているのとき1を返す' do
102       @ps = FactoryGirl.create :provider_status, :token => 'a'
103       r = @ps.status
104       r.should eq 1
105     end
106   end
107   
108   describe '一覧取得に於いて' do
109     before do
110       @ps = FactoryGirl.create :provider_status
111       @pd = FactoryGirl.create :provider, :provider_status_id => @ps.id, :name => "6"
112     end
113     context 'page補正について' do
114       it '文字列から数値に変換される' do
115         ProviderStatus.page('8').should eq 8
116       end
117       it 'nilの場合は1になる' do
118         ProviderStatus.page().should eq 1
119       end
120       it '0以下の場合は1になる' do
121         ProviderStatus.page('0').should eq 1
122       end
123     end
124     context 'page_size補正について' do
125       it '文字列から数値に変換される' do
126         ProviderStatus.page_size('7').should eq 7
127       end
128       it 'nilの場合はProviderStatus.default_page_sizeになる' do
129         ProviderStatus.page_size().should eq ProviderStatus.default_page_size
130       end
131       it '0以下の場合はProviderStatus.default_page_sizeになる' do
132         ProviderStatus.page_size('0').should eq ProviderStatus.default_page_size
133       end
134       it 'ProviderStatus.max_page_sizeを超えた場合はProviderStatus.max_page_sizeになる' do
135         ProviderStatus.page_size('1000').should eq ProviderStatus.max_page_size
136       end
137     end
138     context 'つつがなく終わるとき' do
139       it '一覧取得オプションを利用している' do
140         ProviderStatus.stub(:list_opt).with(any_args).and_return({:include => {:provider => {}} })
141         ProviderStatus.should_receive(:list_opt).with(any_args).exactly(1)
142         r = ProviderStatus.list
143       end
144     end
145     it 'リストを返す' do
146       r = ProviderStatus.list
147       r.should eq [@ps]
148     end
149     it '管理名で並んでいる' do
150       v = FactoryGirl.create :provider_status
151       vd = FactoryGirl.create :provider, :provider_status_id => v.id, :name => "0"
152       r = ProviderStatus.list
153       r.should eq [v, @ps]
154     end
155     context 'DBに5件あって1ページの件数を2件に変えたとして' do
156       before do
157         @ps2 = FactoryGirl.create :provider_status
158         @pd2 = FactoryGirl.create :provider, :provider_status_id => @ps2.id, :name => "5"
159         @ps3 = FactoryGirl.create :provider_status
160         @pd3 = FactoryGirl.create :provider, :provider_status_id => @ps3.id, :name => "4"
161         @ps4 = FactoryGirl.create :provider_status
162         @pd4 = FactoryGirl.create :provider, :provider_status_id => @ps4.id, :name => "3"
163         @ps5 = FactoryGirl.create :provider_status
164         @pd5 = FactoryGirl.create :provider, :provider_status_id => @ps5.id, :name => "2"
165         ProviderStatus.stub(:default_page_size).and_return(2)
166       end
167       it '通常は2件を返す' do
168         r = ProviderStatus.list
169         r.should have(2).items 
170       end
171       it 'page=1なら末尾2件を返す' do
172         #管理名で並んでいる
173         r = ProviderStatus.list(1)
174         r.should eq [@ps5, @ps4]
175       end
176       it 'page=2なら中間2件を返す' do
177         r = ProviderStatus.list(2)
178         r.should eq [@ps3, @ps2]
179       end
180       it 'page=3なら先頭1件を返す' do
181         r = ProviderStatus.list(3)
182         r.should eq [@ps]
183       end
184     end
185     context 'DBに5件あって1ページの件数を2件に変えたとして' do
186       before do
187         @ps2 = FactoryGirl.create :provider_status
188         @pd2 = FactoryGirl.create :provider, :provider_status_id => @ps2.id, :name => "5"
189         @ps3 = FactoryGirl.create :provider_status
190         @pd3 = FactoryGirl.create :provider, :provider_status_id => @ps3.id, :name => "4"
191         @ps4 = FactoryGirl.create :provider_status
192         @pd4 = FactoryGirl.create :provider, :provider_status_id => @ps4.id, :name => "3"
193         @ps5 = FactoryGirl.create :provider_status
194         @pd5 = FactoryGirl.create :provider, :provider_status_id => @ps5.id, :name => "2"
195         ProviderStatus.stub(:default_page_size).and_return(2)
196       end
197       it '件数0は全件(5件)を返す' do
198         r = ProviderStatus.list 5, 0
199         r.should have(5).items 
200       end
201     end
202   end
203   
204   describe '待機中一覧取得に於いて' do
205     before do
206       @ps = FactoryGirl.create :provider_status
207       @pd = FactoryGirl.create :provider, :provider_status_id => @ps.id, :name => "6"
208     end
209     context 'つつがなく終わるとき' do
210       it '一覧取得オプションを利用している' do
211         ProviderStatus.stub(:list_opt).with(any_args).and_return({:include => {:provider => {}} })
212         ProviderStatus.should_receive(:list_opt).with(any_args).exactly(1)
213         r = ProviderStatus.available_list
214       end
215     end
216     it 'リストを返す' do
217       r = ProviderStatus.available_list
218       r.should eq [@ps]
219     end
220     it '管理名で並んでいる' do
221       @ps2 = FactoryGirl.create :provider_status
222       v = FactoryGirl.create :provider, :name => "0", :provider_status_id => @ps2.id
223       r = ProviderStatus.available_list
224       r.should eq [@ps2, @ps]
225     end
226     it '借受状況のトークンが設定されていない借受状況に限る' do
227       @ps2 = FactoryGirl.create :provider_status, :token => 'aaaaa'
228       v = FactoryGirl.create :provider, :name => "0", :provider_status_id => @ps2.id
229       r = ProviderStatus.available_list
230       r.should eq [@ps]
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         ProviderStatus.stub(:default_page_size).and_return(2)
243       end
244       it '通常は2件を返す' do
245         r = ProviderStatus.available_list
246         r.should have(2).items 
247       end
248       it 'page=1なら末尾2件を返す' do
249         #管理名で並んでいる
250         r = ProviderStatus.available_list(1)
251         r.should eq [@ps5, @ps4]
252       end
253       it 'page=2なら中間2件を返す' do
254         r = ProviderStatus.available_list(2)
255         r.should eq [@ps3, @ps2]
256       end
257       it 'page=3なら先頭1件を返す' do
258         r = ProviderStatus.available_list(3)
259         r.should eq [@ps]
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         ProviderStatus.stub(:default_page_size).and_return(2)
273       end
274       it '件数0は全件(5件)を返す' do
275         r = ProviderStatus.available_list 5, 0
276         r.should have(5).items 
277       end
278     end
279   end
280   
281   describe '承認済リスト一覧取得に於いて' do
282     before do
283       @ps = FactoryGirl.create :provider_status, :token => 'aaaaa'
284       @pd = FactoryGirl.create :provider, :provider_status_id => @ps.id, :name => "6"
285     end
286     context 'つつがなく終わるとき' do
287       it '一覧取得オプションを利用している' do
288         ProviderStatus.stub(:list_opt).with(any_args).and_return({:include => {:provider => {}} })
289         ProviderStatus.should_receive(:list_opt).with(any_args).exactly(1)
290         r = ProviderStatus.approve_list
291       end
292     end
293     it 'リストを返す' do
294       r = ProviderStatus.approve_list
295       r.should eq [@ps]
296     end
297     it '管理名で並んでいる' do
298       @ps2 = FactoryGirl.create :provider_status, :token => 'aaaaa'
299       v = FactoryGirl.create :provider, :name => "0", :provider_status_id => @ps2.id
300       r = ProviderStatus.approve_list
301       r.should eq [@ps2, @ps]
302     end
303     it '借受状況のトークンが設定されている借受状況に限る' do
304       @ps2 = FactoryGirl.create :provider_status, :token => nil
305       v = FactoryGirl.create :provider, :name => "0", :provider_status_id => @ps2.id
306       r = ProviderStatus.approve_list
307       r.should eq [@ps]
308     end
309     context 'DBに5件あって1ページの件数を2件に変えたとして' do
310       before do
311         @ps2 = FactoryGirl.create :provider_status, :token => 'aaaaa'
312         @pd2 = FactoryGirl.create :provider, :provider_status_id => @ps2.id, :name => "5"
313         @ps3 = FactoryGirl.create :provider_status, :token => 'aaaaa'
314         @pd3 = FactoryGirl.create :provider, :provider_status_id => @ps3.id, :name => "4"
315         @ps4 = FactoryGirl.create :provider_status, :token => 'aaaaa'
316         @pd4 = FactoryGirl.create :provider, :provider_status_id => @ps4.id, :name => "3"
317         @ps5 = FactoryGirl.create :provider_status, :token => 'aaaaa'
318         @pd5 = FactoryGirl.create :provider, :provider_status_id => @ps5.id, :name => "2"
319         ProviderStatus.stub(:default_page_size).and_return(2)
320       end
321       it '通常は2件を返す' do
322         r = ProviderStatus.approve_list
323         r.should have(2).items 
324       end
325       it 'page=1なら末尾2件を返す' do
326         #管理名で並んでいる
327         r = ProviderStatus.approve_list(1)
328         r.should eq [@ps5, @ps4]
329       end
330       it 'page=2なら中間2件を返す' do
331         r = ProviderStatus.approve_list(2)
332         r.should eq [@ps3, @ps2]
333       end
334       it 'page=3なら先頭1件を返す' do
335         r = ProviderStatus.approve_list(3)
336         r.should eq [@ps]
337       end
338     end
339     context 'DBに5件あって1ページの件数を2件に変えたとして' do
340       before do
341         @ps2 = FactoryGirl.create :provider_status, :token => 'aaaaa'
342         @pd2 = FactoryGirl.create :provider, :provider_status_id => @ps2.id, :name => "5"
343         @ps3 = FactoryGirl.create :provider_status, :token => 'aaaaa'
344         @pd3 = FactoryGirl.create :provider, :provider_status_id => @ps3.id, :name => "4"
345         @ps4 = FactoryGirl.create :provider_status, :token => 'aaaaa'
346         @pd4 = FactoryGirl.create :provider, :provider_status_id => @ps4.id, :name => "3"
347         @ps5 = FactoryGirl.create :provider_status, :token => 'aaaaa'
348         @pd5 = FactoryGirl.create :provider, :provider_status_id => @ps5.id, :name => "2"
349         ProviderStatus.stub(:default_page_size).and_return(2)
350       end
351       it '件数0は全件(5件)を返す' do
352         r = ProviderStatus.approve_list 5, 0
353         r.should have(5).items 
354       end
355     end
356   end
357   
358   describe '一覧取得オプションに於いて' do
359     it 'includeキーを含んでいる' do
360       r = ProviderStatus.list_opt
361       r.has_key?(:include).should be_true
362     end
363     it '1つの項目を含んでいる' do
364       r = ProviderStatus.list_opt[:include]
365       r.should have(1).items
366     end
367     it '貸手を含んでいる' do
368       r = ProviderStatus.list_opt[:include]
369       r.has_key?(:provider).should be_true
370     end
371   end
372   describe 'json一覧出力オプションに於いて' do
373     before do
374       @ps = FactoryGirl.create :provider_status
375       @pd = FactoryGirl.create :provider, :provider_status_id => @ps.id
376     end
377     it '貸手を含んでいる' do
378       r = ProviderStatus.list.to_json ProviderStatus.list_json_opt
379       j = JSON.parse r
380       i = j.first
381       i.has_key?('provider').should be_true
382     end
383   end
384   
385   describe '単体取得に於いて' do
386     before do
387       @ps = FactoryGirl.create :provider_status
388       @pd = FactoryGirl.create :provider, :provider_status_id => @ps.id
389     end
390     context 'つつがなく終わるとき' do
391       it '単体取得オプションを利用している' do
392         ProviderStatus.stub(:show_opt).with(any_args).and_return({})
393         ProviderStatus.should_receive(:show_opt).with(any_args).exactly(1)
394         r = ProviderStatus.show @ps.id, @admin
395       end
396       it '閲覧許可を問い合わせている' do
397         ProviderStatus.any_instance.stub(:visible?).with(any_args).and_return(true)
398         ProviderStatus.any_instance.should_receive(:visible?).with(any_args).exactly(1)
399         r = ProviderStatus.show @ps.id, @admin
400       end
401     end
402     it '指定の借受状況を返す' do
403       r = ProviderStatus.show @ps.id, @admin
404       r.should eq @ps
405     end
406     context '閲覧許可が出なかったとき' do
407       it '403Forbidden例外を返す' do
408         ProviderStatus.any_instance.stub(:visible?).and_return(false)
409         lambda{
410           ProviderStatus.show @ps.id, @admin
411         }.should raise_error(ActiveRecord::Forbidden)
412       end
413     end
414     context '存在しない借受状況を開こうとしたとき' do
415       it '404RecordNotFound例外を返す' do
416         lambda{
417           ProviderStatus.show 110, @admin
418         }.should raise_error(ActiveRecord::RecordNotFound)
419       end
420     end
421   end
422   
423   describe '編集取得に於いて' do
424     before do
425       @ps = FactoryGirl.create :provider_status
426       @pd = FactoryGirl.create :provider, :provider_status_id => @ps.id
427     end
428     context 'つつがなく終わるとき' do
429       it '単体取得オプションを利用している' do
430         ProviderStatus.stub(:show_opt).with(any_args).and_return({})
431         ProviderStatus.should_receive(:show_opt).with(any_args).exactly(1)
432         r = ProviderStatus.edit @ps.id, @admin
433       end
434       it '所持判定を問い合わせている' do
435         ProviderStatus.any_instance.stub(:own?).with(any_args).and_return(true)
436         ProviderStatus.any_instance.should_receive(:own?).with(any_args).exactly(1)
437         r = ProviderStatus.edit @ps.id, @admin
438       end
439     end
440     it '指定の借受状況を返す' do
441       ProviderStatus.any_instance.stub(:own?).and_return(true)
442       r = ProviderStatus.edit @ps.id, @admin
443       r.should eq @ps
444     end
445     context '権限がなかったとき' do
446       it '403Forbidden例外を返す' do
447         ProviderStatus.any_instance.stub(:own?).and_return(false)
448         lambda{
449           ProviderStatus.edit @ps.id, @admin
450         }.should raise_error(ActiveRecord::Forbidden)
451       end
452     end
453     context '存在しない借受状況を開こうとしたとき' do
454       it '404RecordNotFound例外を返す' do
455         lambda{
456           ProviderStatus.edit 110, @admin
457         }.should raise_error(ActiveRecord::RecordNotFound)
458       end
459     end
460   end
461   describe '単体取得オプションに於いて' do
462     it 'includeキーを含んでいる' do
463       r = ProviderStatus.show_opt
464       r.has_key?(:include).should be_true
465     end
466     it '1つの項目を含んでいる' do
467       r = ProviderStatus.show_opt[:include]
468       r.should have(1).items
469     end
470     it '貸手を含んでいる' do
471       r = ProviderStatus.show_opt[:include]
472       r.has_key?(:provider).should be_true
473     end
474   end
475   describe 'json単体出力オプションに於いて' do
476     before do
477       @ps = FactoryGirl.create :provider_status
478       @pd = FactoryGirl.create :provider, :provider_status_id => @ps.id
479     end
480     it '貸手を含んでいる' do
481       r = ProviderStatus.show(@ps.id, @admin).to_json ProviderStatus.show_json_opt
482       j = JSON.parse r
483       i = j
484       i.has_key?('provider').should be_true
485     end
486   end
487   
488   describe 'エクスポートurl取得に於いて' do
489     before do
490       @token = 'aaaaaaaaaaaaaaaaa'
491       @ps = FactoryGirl.create :provider_status, :token => @token
492       @provider = FactoryGirl.create :provider, :provider_status_id => @ps.id
493     end
494     context '事前チェックしておく' do
495       before do
496         Provider.any_instance.stub(:export_url).with(any_args).and_return('http://demander.url/action.json')
497       end
498       it '借手にエクスポートurl取得を依頼している' do
499         Provider.any_instance.should_receive(:export_url).exactly(1)
500         @ps.export_url 'action', Time.now
501       end
502     end
503     context 'つつがなく終わるとき' do
504       before do
505         Provider.any_instance.stub(:export_url).with(any_args).and_return('http://demander.url/action.json')
506       end
507       it 'エクスポートurl文字列を返す' do
508         r = @ps.export_url 'action', nil
509         r.should eq 'http://demander.url/action.json?auth_token=' + @token
510       end
511     end
512     context 'エクスポート日時が設定されているとき' do
513       before do
514         Provider.any_instance.stub(:export_url).with(any_args).and_return('http://demander.url/action.json')
515       end
516       it 'エクスポート日時を追加する' do
517         r = @ps.export_url 'action', Time.parse('2000/1/1')
518         r.should match /20000101/
519       end
520     end
521   end
522   
523   describe '貸手からのエクスポート通信に於いて' do
524     before do
525       @ps = FactoryGirl.create :provider_status, :token => 'aaaaaaaaaaaaaaaaa'
526       @provider = FactoryGirl.create :provider, :provider_status_id => @ps.id
527     end
528     context '事前チェックしておく' do
529       before do
530         RestClient.stub(:get).with(any_args).and_return('{}')
531         JSON.stub(:parse).with(any_args).and_return({})
532       end
533       it 'Restクライアントに取得通信を依頼している' do
534         RestClient.should_receive(:get).exactly(1)
535         @ps.export_from_provider 'url'
536       end
537       it 'JSONライブラリにパースを依頼している' do
538         JSON.should_receive(:parse).exactly(1)
539         @ps.export_from_provider 'url'
540       end
541     end
542     context 'つつがなく終わるとき' do
543       before do
544         RestClient.stub(:get).with(any_args).and_return('{}')
545         JSON.stub(:parse).with(any_args).and_return({})
546       end
547       it 'JSONデータを返す' do
548         r = @ps.export_from_provider 'url'
549         r.is_a?(Hash).should be_true
550       end
551     end
552     context 'エクスポートでエラーが発生したとき' do
553       before do
554         RestClient.stub(:get).with(any_args).and_raise(StandardError)
555       end
556       it 'nilを返す' do
557         r = @ps.export_from_provider 'url'
558         r.should be_nil
559       end
560     end
561     context 'パースでエラーが発生したとき' do
562       before do
563         ProviderStatus.any_instance.stub(:export_by).with(any_args).and_return(nil)
564       end
565       it 'nilを返す' do
566         r = @ps.export_from_provider 'url'
567         r.should be_nil
568       end
569     end
570   end
571   
572   describe '貸手からのエクスポートに於いて' do
573     before do
574       @ps = FactoryGirl.create :provider_status, :token => 'aaaaaaaaaaaaaaaaa'
575       @provider = FactoryGirl.create :provider, :provider_status_id => @ps.id
576     end
577     context '事前チェックしておく' do
578       before do
579         ProviderStatus.any_instance.stub(:ymd_to_time).with(any_args).and_return(Time.parse('2000/1/1'))
580         ProviderStatus.any_instance.stub(:export_url).with(any_args).and_return('http://demander.url/action.json?auth_token=aaaaaaaaaaaaaaaa')
581         ProviderStatus.any_instance.stub(:export_from_provider).with(any_args).and_return([@license.attributes])
582       end
583       it '日付文字列変換を依頼している' do
584         ProviderStatus.any_instance.should_receive(:ymd_to_time).exactly(1)
585         @ps.export_by 'action', '20111010'
586       end
587       it 'エクスポートurl取得を依頼している' do
588         ProviderStatus.any_instance.should_receive(:export_url).exactly(1)
589         @ps.export_by 'action', '20111010'
590       end
591       it '貸手からのエクスポート通信を依頼している' do
592         ProviderStatus.any_instance.should_receive(:export_from_provider).exactly(1)
593         @ps.export_by 'action', '20111010'
594       end
595     end
596     context 'つつがなく終わるとき' do
597       before do
598         ProviderStatus.any_instance.stub(:ymd_to_time).with(any_args).and_return(Time.parse('2000/1/1'))
599         ProviderStatus.any_instance.stub(:export_url).with(any_args).and_return('http://demander.url/action.json?auth_token=aaaaaaaaaaaaaaaa')
600         ProviderStatus.any_instance.stub(:export_from_provider).with(any_args).and_return([@license.attributes])
601       end
602       it 'リストを返す' do
603         r = @ps.export_by 'action', '20111010'
604         r.is_a?(Array).should be_true
605       end
606     end
607     context 'エクスポートでエラーが発生したとき' do
608       before do
609         ProviderStatus.any_instance.stub(:ymd_to_time).with(any_args).and_return(Time.parse('2000/1/1'))
610         ProviderStatus.any_instance.stub(:export_url).with(any_args).and_return('http://demander.url/action.json?auth_token=aaaaaaaaaaaaaaaa')
611         ProviderStatus.any_instance.stub(:export_from_provider).with(any_args).and_return(nil)
612       end
613       it 'nilを返す' do
614         r = @ps.export_by 'action', '20111010'
615         r.should be_nil
616       end
617     end
618     context 'パースでエラーが発生したとき' do
619       before do
620         ProviderStatus.any_instance.stub(:ymd_to_time).with(any_args).and_return(Time.parse('2000/1/1'))
621         ProviderStatus.any_instance.stub(:export_url).with(any_args).and_return('http://demander.url/action.json?auth_token=aaaaaaaaaaaaaaaa')
622         ProviderStatus.any_instance.stub(:export_from_provider).with(any_args).and_return(nil)
623       end
624       it 'nilを返す' do
625         r = @ps.export_by 'action', '20111010'
626         r.should be_nil
627       end
628     end
629   end
630   
631   describe 'ライセンスインポートに於いて' do
632     before do
633       @ps = FactoryGirl.create :provider_status, :token => 'aaaaaaaaaaaaaaaaa'
634       @provider = FactoryGirl.create :provider, :provider_status_id => @ps.id
635     end
636     context '事前チェックしておく' do
637       before do
638         ProviderStatus.any_instance.stub(:export_by).with(any_args).and_return([@license.attributes])
639         ProviderLicense.stub(:import).with(any_args).and_return([])
640       end
641       it '貸手からのエクスポートを依頼している' do
642         ProviderStatus.any_instance.should_receive(:export_by).exactly(1)
643         r = @ps.licenses_import '20111010'
644       end
645       it 'ライセンス対照表モデルにインポートを依頼している' do
646         ProviderLicense.should_receive(:import).exactly(1)
647         r = @ps.licenses_import '20111010'
648       end
649     end
650     context 'つつがなく終わるとき' do
651       before do
652         ProviderStatus.any_instance.stub(:export_by).with(any_args).and_return([@license.attributes])
653         ProviderLicense.stub(:import).with(any_args).and_return([])
654       end
655       it 'ライセンスインポート結果を返す' do
656         r = @ps.licenses_import '20111010'
657         r.should eq []
658       end
659     end
660     context 'エクスポートに失敗したとき' do
661       before do
662         ProviderStatus.any_instance.stub(:export_by).with(any_args).and_return(nil)
663       end
664       it 'nilを返す' do
665         r = @ps.licenses_import '20111010'
666         r.should be_nil
667       end
668     end
669   end
670   
671   describe '絵師インポートに於いて' do
672     before do
673       @ps = FactoryGirl.create :provider_status, :token => 'aaaaaaaaaaaaaaaaa'
674       @provider = FactoryGirl.create :provider, :provider_status_id => @ps.id
675     end
676     context '事前チェックしておく' do
677       before do
678         ProviderStatus.any_instance.stub(:export_by).with(any_args).and_return([])
679         ProviderArtist.stub(:import).with(any_args).and_return([])
680       end
681       it '貸手からのエクスポートを依頼している' do
682         ProviderStatus.any_instance.should_receive(:export_by).exactly(1)
683         @ps.artists_import '20111010'
684       end
685       it '絵師対照表モデルにインポートを依頼している' do
686         ProviderArtist.should_receive(:import).exactly(1)
687         @ps.artists_import '20111010'
688       end
689     end
690     context 'つつがなく終わるとき' do
691       before do
692         ProviderStatus.any_instance.stub(:export_by).with(any_args).and_return([])
693         ProviderArtist.stub(:import).with(any_args).and_return([])
694       end
695       it '絵師インポート結果を返す' do
696         r = @ps.artists_import '20111010'
697         r.should eq []
698       end
699     end
700     context 'エクスポートに失敗したとき' do
701       before do
702         ProviderStatus.any_instance.stub(:export_by).with(any_args).and_return(nil)
703       end
704       it 'nilを返す' do
705         r = @ps.artists_import '20111010'
706         r.should be_nil
707       end
708     end
709   end
710   
711   describe '素材インポートに於いて' do
712     before do
713       @ps = FactoryGirl.create :provider_status, :token => 'aaaaaaaaaaaaaaaaa'
714       @provider = FactoryGirl.create :provider, :provider_status_id => @ps.id
715     end
716     context '事前チェックしておく' do
717       before do
718         ProviderStatus.any_instance.stub(:export_by).with(any_args).and_return([])
719         ProviderOriginalPicture.stub(:import).with(any_args).and_return({:original_pictures => [], :pictures => [], :resource_pictures => []})
720       end
721       it '貸手からのエクスポートを依頼している' do
722         ProviderStatus.any_instance.should_receive(:export_by).exactly(2)
723         @ps.original_pictures_import '20111010'
724       end
725       it '原画対照表モデルにインポートを依頼している' do
726         ProviderOriginalPicture.should_receive(:import).exactly(1)
727         @ps.original_pictures_import '20111010'
728       end
729     end
730     context 'つつがなく終わるとき' do
731       before do
732         ProviderStatus.any_instance.stub(:export_by).with(any_args).and_return([])
733         ProviderOriginalPicture.stub(:import).with(any_args).and_return({:original_pictures => [], :pictures => [], :resource_pictures => []})
734       end
735       it '原画インポート結果を返す' do
736         r = @ps.original_pictures_import '20111010'
737         r.is_a?(Hash).should be_true
738       end
739       it 'インポート失敗リストを取得している' do
740         r = @ps.original_pictures_import '20111010'
741         r[:original_pictures].should be_empty
742         r[:pictures].should be_empty
743         r[:resource_pictures].should be_empty
744       end
745     end
746     context 'エクスポートに失敗したとき' do
747       before do
748         ProviderStatus.any_instance.stub(:export_by).with(any_args).and_return(nil)
749       end
750       it 'nilを返す' do
751         r = @ps.original_pictures_import '20111010'
752         r.should be_nil
753       end
754     end
755   end
756   
757 end