OSDN Git Service

t#30322:create provider license import func
[pettanr/pettanr.git] / spec / controllers / provider_statuses_controller_spec.rb
1 # -*- encoding: utf-8 -*-
2 #借受状況
3 require 'spec_helper'
4
5 describe ProviderStatusesController 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   
15   describe '一覧表示に於いて' do
16     before do
17       @ps = FactoryGirl.create :provider_status
18       @provider = FactoryGirl.create :provider, :provider_status_id => @ps.id
19       ProviderStatus.stub(:list).and_return([@ps, @ps, @ps])
20       ProviderStatus.stub(:available_list).and_return([@ps, @ps])
21       sign_in @admin
22     end
23     context '事前チェックする' do
24       it '与えられたpageがセットされている' do
25         get :index, :page => 5
26         assigns(:page).should eq 5
27       end
28       it '省略されると@pageに1値が入る' do
29         get :index
30         assigns(:page).should eq 1
31       end
32       it '与えられたpage_sizeがセットされている' do
33         get :index, :page_size => 15
34         assigns(:page_size).should eq 15
35       end
36       it '省略されると@page_sizeにデフォルト値が入る' do
37         get :index
38         assigns(:page_size).should eq ProviderStatus.default_page_size
39       end
40       it '最大を超えると@page_sizeにデフォルト最大値が入る' do
41         get :index, :page_size => 1500
42         assigns(:page_size).should eq ProviderStatus.max_page_size
43       end
44       it '不正な値が入ると@page_sizeにデフォルト最大値が入る' do
45         get :index, :page_size => 0
46         assigns(:page_size).should eq ProviderStatus.default_page_size
47       end
48     end
49     context 'つつがなく終わるとき' do
50       it 'ステータスコード200 OKを返す' do
51         get :index
52         response.should be_success 
53       end
54       it '@hideが空になっている' do
55         get :index
56         assigns(:hide).should be_blank
57       end
58       it '借受状況モデルに全一覧取得を問い合わせている' do
59         ProviderStatus.should_receive(:list).exactly(1)
60         get :index
61       end
62       it '@provider_statusesにリストを取得している' do
63         get :index
64         assigns(:provider_statuses).should have_at_least(3).items
65       end
66       context 'html形式' do
67         it 'indexテンプレートを描画する' do
68           get :index
69           response.should render_template("index")
70         end
71       end
72       context 'json形式' do
73         it 'jsonデータを返す' do
74           get :index, :format => :json
75           lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
76         end
77         it '借受状況モデルにjson一覧出力オプションを問い合わせている' do
78           ProviderStatus.should_receive(:list_json_opt).exactly(1)
79           get :index, :format => :json
80         end
81         it 'データがリスト構造になっている' do
82           get :index, :format => :json
83           json = JSON.parse response.body
84           json.should have_at_least(3).items
85         end
86         it 'リストの先頭くらいは借受状況っぽいものであって欲しい' do
87           get :index, :format => :json
88           json = JSON.parse response.body
89           json.first.has_key?("token").should be_true
90           json.first.has_key?("receive_hour1").should be_true
91           json.first.has_key?("receive_hour2").should be_true
92           json.first.has_key?("received_at").should be_true
93         end
94       end
95     end
96     context '除外フラグが除外のとき' do
97       it '@hideが設定されている' do
98         get :index, :hide => 1
99         assigns(:hide).should_not be_blank
100       end
101       it '借受状況モデルに待機中一覧取得を問い合わせている' do
102         ProviderStatus.should_receive(:available_list).exactly(1)
103         get :index, :hide => 1
104       end
105       it '@provider_statusesにリストを取得している' do
106         get :index, :hide => 1
107         assigns(:provider_statuses).should have_at_least(2).items
108       end
109     end
110     context '管理者権限がないとき' do
111       before do
112         sign_out @admin
113       end
114       context 'html形式' do
115         it 'ステータスコード302 Foundを返す' do
116           get :index
117           response.status.should eq 302
118         end
119         it 'サインインページへ遷移する' do
120           get :index
121           response.should redirect_to '/admins/sign_in'
122         end
123       end
124       context 'json形式' do
125         it 'ステータスコード401 Unauthorizedを返す' do
126           get :index, :format => :json
127           response.status.should eq 401
128         end
129         it '応答メッセージにUnauthorizedを返す' do
130           get :index, :format => :json
131           response.message.should match(/Unauthorized/)
132         end
133       end
134     end
135   end
136   
137   describe '単体表示に於いて' do
138     before do
139       @ps = FactoryGirl.create :provider_status
140       @provider = FactoryGirl.create :provider, :provider_status_id => @ps.id
141       ProviderStatus.stub(:show).and_return(@ps)
142       sign_in @admin
143     end
144     context 'つつがなく終わるとき' do
145       it 'ステータスコード200 OKを返す' do
146         get :show, :id => @ps.id
147         response.should be_success
148       end
149       it '借受状況モデルに単体取得を問い合わせている' do
150         ProviderStatus.should_receive(:show).exactly(1)
151         get :show
152       end
153       it '@provider_statusにアレを取得している' do
154         get :show, :id => @ps.id
155         assigns(:provider_status).should eq(@ps)
156       end
157       context 'html形式' do
158         it 'showテンプレートを描画する' do
159           get :show, :id => @ps.id
160           response.should render_template("show")
161         end
162       end
163       context 'json形式' do
164         it 'jsonデータを返す' do
165           get :show, :id => @ps.id, :format => :json
166           lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
167         end
168         it '借受状況モデルにjson単体出力オプションを問い合わせている' do
169           ProviderStatus.should_receive(:show_json_opt).exactly(1)
170           get :show, :id => @ps.id, :format => :json
171         end
172         it 'データがアレになっている' do
173           get :show, :id => @ps.id, :format => :json
174           json = JSON.parse response.body
175           json.has_key?("token").should be_true
176           json.has_key?("receive_hour1").should be_true
177           json.has_key?("receive_hour2").should be_true
178           json.has_key?("received_at").should be_true
179         end
180       end
181     end
182     context '管理者権限がないとき' do
183       before do
184         sign_out @admin
185       end
186       context 'html形式' do
187         it 'ステータスコード302 Foundを返す' do
188           get :show, :id => @ps.id
189           response.status.should eq 302
190         end
191         it 'サインインページへ遷移する' do
192           get :show, :id => @ps.id
193           response.body.should redirect_to '/admins/sign_in'
194         end
195       end
196       context 'json形式' do
197         it 'ステータスコード401 Unauthorizedを返す' do
198           get :show, :id => @ps.id, :format => :json
199           response.status.should eq 401
200         end
201         it '応答メッセージにUnauthorizedを返す' do
202           get :show, :id => @ps.id, :format => :json
203           response.message.should match(/Unauthorized/)
204         end
205       end
206     end
207   end
208   
209   describe '編集フォーム表示に於いて' do
210     before do
211       @ps = FactoryGirl.create :provider_status
212       @provider = FactoryGirl.create :provider, :provider_status_id => @ps.id
213       sign_in @admin
214       ProviderStatus.stub(:edit).with(@ps.id.to_s, @admin).and_return(@ps)
215     end
216     context 'つつがなく終わるとき' do
217       it 'ステータスコード200 OKを返す' do
218         get :edit, :id => @ps.id
219         response.should be_success 
220       end
221       it '借受状況モデルに編集取得を問い合わせている' do
222         ProviderStatus.should_receive(:edit).exactly(1)
223         get :edit, :id => @ps.id
224       end
225       it '@provider_statusにデータを用意している' do
226         get :edit, :id => @ps.id
227         assigns(:provider_status).should eq @ps
228       end
229       context 'html形式' do
230         it 'editテンプレートを描画する' do
231           get :edit, :id => @ps.id
232           response.should render_template("edit")
233         end
234       end
235     end
236     context '管理者権限がないとき' do
237       before do
238         sign_out @admin
239       end
240       context 'html形式' do
241         it 'ステータスコード302 Foundを返す' do
242           get :edit, :id => @ps.id
243           response.status.should eq 302
244         end
245         it 'サインインページへ遷移する' do
246           get :edit, :id => @ps.id
247           response.body.should redirect_to '/admins/sign_in'
248         end
249       end
250     end
251   end
252
253   describe '更新に於いて' do
254     before do
255       @ps = FactoryGirl.create :provider_status
256       @attr = FactoryGirl.attributes_for(:provider_status, :receive_hour1 => 22)
257       sign_in @admin
258     end
259     context '事前チェックしておく' do
260       it '借受状況モデルに編集取得を問い合わせている' do
261         ProviderStatus.stub(:edit).with(any_args()).and_return @ps
262         ProviderStatus.should_receive(:edit).exactly(1)
263         put :update, :id => @ps.id, :provider_status => @attr
264       end
265       it '借受状況モデルにカラム値復元を依頼している' do
266         ProviderStatus.any_instance.should_receive(:attributes=).exactly(1)
267         put :update, :id => @ps.id, :provider_status => @attr
268       end
269       it '借受状況モデルに上書き補充を依頼している' do
270         ProviderStatus.any_instance.should_receive(:overwrite).exactly(1)
271         put :update, :id => @ps.id, :provider_status => @attr
272       end
273       it 'モデルに更新を依頼する' do
274         ProviderStatus.any_instance.stub(:save).with(any_args).and_return true
275         ProviderStatus.any_instance.should_receive(:save).exactly(1)
276         put :update, :id => @ps.id, :provider_status => @attr
277       end
278       it '@provider_statusにアレを取得している' do
279         put :update, :id => @ps.id, :provider_status => @attr
280         assigns(:provider_status).id.should eq(@ps.id)
281       end
282     end
283     context 'つつがなく終わるとき' do
284       it '更新される' do
285         put :update, :id => @ps.id, :provider_status => @attr
286         ProviderStatus.find(@ps.id).receive_hour1.should eq 22
287       end
288       context 'html形式' do
289         it 'ステータスコード302 Foundを返す' do
290           ProviderStatus.any_instance.stub(:save).with(any_args()).and_return(true)
291           put :update, :id => @ps.id, :provider_status => @attr
292           response.status.should eq 302
293         end
294         it '更新された借受状況の表示ページへ遷移する' do
295           put :update, :id => @ps.id, :provider_status => @attr
296           response.should redirect_to(@ps)
297         end
298       end
299       context 'json形式' do
300         it 'ステータスコード200 OKを返す' do
301           ProviderStatus.any_instance.stub(:save).with(any_args()).and_return(true)
302           put :update, :id => @ps.id, :provider_status => @attr, :format => :json
303           response.should be_success 
304         end
305         it 'ページ本体は特に返さない' do
306           ProviderStatus.any_instance.stub(:save).with(any_args()).and_return(true)
307           put :update, :id => @ps.id, :provider_status => @attr, :format => :json
308           response.body.should match /./
309         end
310       end
311     end
312     context '管理者権限がないとき' do
313       before do
314         sign_out @admin
315       end
316       it 'ステータスコード302 Foundを返す' do
317         put :update, :id => @ps.id, :provider_status => @attr
318         response.status.should eq 302
319       end
320       context 'html形式' do
321         it 'サインインページへ遷移する' do
322           put :update, :id => @ps.id, :provider_status => @attr
323           response.body.should redirect_to '/admins/sign_in'
324         end
325       end
326       context 'json形式' do
327         it '応答メッセージにUnauthorizedを返す' do
328           put :update, :id => @ps.id, :provider_status => @attr, :format => :json
329           response.message.should match(/Unauthorized/)
330         end
331       end
332     end
333     context '検証、保存に失敗したとき' do
334       before do
335         ProviderStatus.any_instance.stub(:save).and_return(false)
336       end
337       context 'html形式' do
338         it 'ステータスコード200 Okを返す' do
339           put :update, :id => @ps.id, :provider_status => @attr
340           response.status.should eq 200
341         end
342         it '編集ページを描画する' do
343           put :update, :id => @ps.id, :provider_status => @attr
344           response.should render_template("edit")
345         end
346       end
347       context 'json形式' do
348         it 'ステータスコード422 unprocessable_entity を返す' do
349           ProviderStatus.any_instance.stub(:save).and_return(false)
350           put :update, :id => @ps.id, :provider_status => @attr, :format => :json
351           response.status.should eq 422
352         end
353         it '応答メッセージUnprocessable Entityを返す' do
354           put :update, :id => @ps.id, :provider_status => @attr, :format => :json
355           response.message.should match(/Unprocessable/)
356         end
357       end
358     end
359   end
360   
361   describe 'ライセンスインポートに於いて' do
362     before do
363       @ps = FactoryGirl.create :provider_status, :token => 'aaaaaaaaaaaaaaaaa'
364       @provider = FactoryGirl.create :provider, :provider_status_id => @ps.id
365       ProviderStatus.stub(:show).and_return(@ps)
366       sign_in @admin
367     end
368     context '事前チェックしておく' do
369       before do
370         ProviderStatus.any_instance.stub(:status).with(any_args).and_return(1)
371         ProviderStatusesController.any_instance.stub(:ymd_to_time).with(any_args).and_return(nil)
372         ProviderStatusesController.any_instance.stub(:export_url).with(any_args).and_return(@ps)
373         ProviderStatusesController.any_instance.stub(:export_from_provider).with(any_args).and_return([@license.attributes])
374         ProviderLicense.stub(:import).with(any_args).and_return([])
375       end
376       it '借受状況モデルに単体取得を問い合わせている' do
377         ProviderStatus.should_receive(:show).exactly(1)
378         get :licenses_import, :id => @ps.id
379       end
380       it '日付文字列変換を依頼している' do
381         ProviderStatusesController.any_instance.should_receive(:ymd_to_time).with('20111010').exactly(1)
382         get :licenses_import, :id => @ps.id, :date => '20111010'
383       end
384       it 'エクスポートurl取得を依頼している' do
385         ProviderStatusesController.any_instance.stub(:ymd_to_time).with('20111010').and_return(Time.parse('2011/10/10'))
386         ProviderStatusesController.any_instance.should_receive(:export_url).with(@provider.demander_url, @ps.token, Time.parse('2011/10/10')).exactly(1)
387         get :licenses_import, :id => @ps.id, :date => '20111010'
388       end
389       it '貸手からのエクスポートを依頼している' do
390         ProviderStatusesController.any_instance.should_receive(:export_from_provider).exactly(1)
391         get :licenses_import, :id => @ps.id
392       end
393       it 'ライセンス対照表モデルにインポートを依頼している' do
394         ProviderLicense.should_receive(:import).exactly(1)
395         get :licenses_import, :id => @ps.id
396       end
397     end
398     context 'つつがなく終わるとき' do
399       before do
400         ProviderStatus.any_instance.stub(:status).with(any_args).and_return(1)
401         ProviderStatusesController.any_instance.stub(:ymd_to_time).with('20111010').and_return(Time.parse('2011/10/10'))
402         ProviderStatusesController.any_instance.stub(:ymd_to_time).with(any_args).and_return(nil)
403         ProviderStatusesController.any_instance.stub(:export_url).with(any_args).and_return(@ps)
404         ProviderStatusesController.any_instance.stub(:export_from_provider).with(any_args).and_return([@license.attributes])
405         ProviderLicense.stub(:import).with(any_args).and_return([])
406       end
407       it 'ステータスコード200 OKを返す' do
408         get :licenses_import, :id => @ps.id
409         response.should be_success
410       end
411       it '@provider_statusに借受状況を取得している' do
412         get :licenses_import, :id => @ps.id
413         assigns(:provider_status).should eq(@ps)
414       end
415       it '@failuresにインポート失敗リストを取得している' do
416         get :licenses_import, :id => @ps.id
417         assigns(:failures).should eq([])
418       end
419       context 'html形式' do
420         it 'licenses_importテンプレートを描画する' do
421           get :licenses_import, :id => @ps.id
422           response.should render_template("licenses_import")
423         end
424       end
425       context 'json形式' do
426         it 'jsonデータを返す' do
427           get :licenses_import, :id => @ps.id, :format => :json
428           lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
429         end
430       end
431     end
432     context '管理者権限がないとき' do
433       before do
434         sign_out @admin
435       end
436       context 'html形式' do
437         it 'ステータスコード302 Foundを返す' do
438           get :licenses_import, :id => @ps.id
439           response.status.should eq 302
440         end
441         it 'サインインページへ遷移する' do
442           get :licenses_import, :id => @ps.id
443           response.body.should redirect_to '/admins/sign_in'
444         end
445       end
446       context 'json形式' do
447         it 'ステータスコード401 Unauthorizedを返す' do
448           get :licenses_import, :id => @ps.id, :format => :json
449           response.status.should eq 401
450         end
451         it '応答メッセージにUnauthorizedを返す' do
452           get :licenses_import, :id => @ps.id, :format => :json
453           response.message.should match(/Unauthorized/)
454         end
455       end
456     end
457     context '借受権限がないとき' do
458       before do
459         ProviderStatus.any_instance.stub(:status).and_return(0)
460       end
461       context 'html形式' do
462         it '例外403 forbiddenを返す' do
463           lambda{
464             get :licenses_import, :id => @ps.id
465           }.should raise_error(ActiveRecord::Forbidden)
466         end
467       end
468       context 'json形式' do
469         it '例外403 forbiddenを返す' do
470           lambda{
471             get :licenses_import, :id => @ps.id, :format => :json
472           }.should raise_error(ActiveRecord::Forbidden)
473         end
474       end
475     end
476   end
477   
478 end