OSDN Git Service

fix: any
[pettanr/pettanr.git] / spec / controllers / providers_controller_spec.rb
1 # -*- encoding: utf-8 -*-
2 require 'spec_helper'
3 #貸手
4
5 describe ProvidersController 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       Provider.stub(:list).and_return([@provider, @provider, @provider])
20       Provider.stub(:available_list).and_return([@provider, @provider])
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 Provider.default_page_size
39       end
40       it '最大を超えると@page_sizeにデフォルト最大値が入る' do
41         get :index, :page_size => 1500
42         assigns(:page_size).should eq Provider.max_page_size
43       end
44       it '不正な値が入ると@page_sizeにデフォルト最大値が入る' do
45         get :index, :page_size => 0
46         assigns(:page_size).should eq Provider.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         Provider.should_receive(:list).exactly(1)
60         get :index
61       end
62       it '@providersにリストを取得している' do
63         get :index
64         assigns(:providers).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           Provider.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?("name").should be_true
90           json.first.has_key?("caption").should be_true
91           json.first.has_key?("url").should be_true
92           json.first.has_key?("description").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         Provider.should_receive(:available_list).exactly(1)
103         get :index, :hide => 1
104       end
105       it '@providersにリストを取得している' do
106         get :index, :hide => 1
107         assigns(:providers).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       Provider.stub(:show).and_return(@provider)
142       sign_in @admin
143     end
144     context 'つつがなく終わるとき' do
145       it 'ステータスコード200 OKを返す' do
146         get :show, :id => @provider.id
147         response.should be_success
148       end
149       it '貸手モデルに単体取得を問い合わせている' do
150         Provider.should_receive(:show).exactly(1)
151         get :show
152       end
153       it '@providerにアレを取得している' do
154         get :show, :id => @provider.id
155         assigns(:provider).should eq(@provider)
156       end
157       context 'html形式' do
158         it 'showテンプレートを描画する' do
159           get :show, :id => @provider.id
160           response.should render_template("show")
161         end
162       end
163       context 'json形式' do
164         it 'jsonデータを返す' do
165           get :show, :id => @provider.id, :format => :json
166           lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
167         end
168         it '貸手モデルにjson単体出力オプションを問い合わせている' do
169           Provider.should_receive(:show_json_opt).exactly(1)
170           get :show, :id => @provider.id, :format => :json
171         end
172         it 'データがアレになっている' do
173           get :show, :id => @provider.id, :format => :json
174           json = JSON.parse response.body
175           json.has_key?("name").should be_true
176           json.has_key?("caption").should be_true
177           json.has_key?("url").should be_true
178           json.has_key?("description").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 => @provider.id
189           response.status.should eq 302
190         end
191         it 'サインインページへ遷移する' do
192           get :show, :id => @provider.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 => @provider.id, :format => :json
199           response.status.should eq 401
200         end
201         it '応答メッセージにUnauthorizedを返す' do
202           get :show, :id => @provider.id, :format => :json
203           response.message.should match(/Unauthorized/)
204         end
205       end
206     end
207   end
208
209 end