1 # -*- encoding: utf-8 -*-
5 describe SystemPicturesController do
7 @admin = FactoryGirl.create :admin
8 @demand_user = FactoryGirl.create :demand_user
9 @user = FactoryGirl.create( :user_yas)
10 @author = FactoryGirl.create :author, :user_id => @user.id
11 @artist = FactoryGirl.create :artist_yas, :author_id => @author.id
12 @sp = FactoryGirl.create :system_picture
13 @lg = FactoryGirl.create :license_group
14 @license = FactoryGirl.create :license, :license_group_id => @lg.id, :system_picture_id => @sp.id
17 if MagicNumber['run_mode'] == 1
18 describe '一覧表示に於いて' do
21 SystemPicture.stub(:list).and_return([@sp, @sp, @sp])
23 context 'パラメータpageについて' do
25 get :index, :page => 5
26 assigns(:page).should eq 5
28 it '省略されると@pageに1値が入る' do
30 assigns(:page).should eq 1
32 it '与えられたpage_sizeがセットされている' do
33 get :index, :page_size => 15
34 assigns(:page_size).should eq 15
36 it '省略されると@page_sizeにデフォルト値が入る' do
38 assigns(:page_size).should eq SystemPicture.default_page_size
40 it '最大を超えると@page_sizeにデフォルト最大値が入る' do
41 get :index, :page_size => 1500
42 assigns(:page_size).should eq SystemPicture.max_page_size
44 it '不正な値が入ると@page_sizeにデフォルト最大値が入る' do
45 get :index, :page_size => 0
46 assigns(:page_size).should eq SystemPicture.default_page_size
49 context 'つつがなく終わるとき' do
50 it 'システム画像モデルに一覧を問い合わせている' do
51 SystemPicture.should_receive(:list).exactly(1)
54 it '@system_picturesにリストを取得している' do
56 assigns(:system_pictures).should have_at_least(3).items
59 it 'ステータスコード200 OKを返す' do
61 response.should be_success
63 it 'indexテンプレートを描画する' do
65 response.should render_template("index")
69 it 'ステータスコード200 OKを返す' do
70 get :index, :format => :json
71 response.should be_success
74 get :index, :format => :json
75 lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
77 it 'システム画像モデルにjson一覧出力オプションを問い合わせている' do
78 SystemPicture.should_receive(:list_json_opt).exactly(1)
79 get :index, :format => :json
81 it 'データがリスト構造になっている' do
82 get :index, :format => :json
83 json = JSON.parse response.body
84 json.should have_at_least(3).items
86 it 'リストの先頭くらいはシステム画像っぽいものであって欲しい' do
87 get :index, :format => :json
88 json = JSON.parse response.body
89 json.first.has_key?("ext").should be_true
90 json.first.has_key?("md5").should be_true
94 context 'ユーザ権限がないとき' do
99 it 'ステータスコード302 Foundを返す' do
101 response.status.should eq 302
103 it 'サインインページへ遷移する' do
105 response.should redirect_to '/users/sign_in'
109 it 'ステータスコード401 Unauthorizedを返す' do
110 get :index, :format => :json
111 response.status.should eq 401
113 it '応答メッセージにUnauthorizedを返す' do
114 get :index, :format => :json
115 response.message.should match(/Unauthorized/)
119 context 'ユーザ権限はないが管理者権限があるとき' do
124 it 'ステータスコード200 OKを返す' do
126 response.should be_success
129 context 'ユーザ権限はないが借手権限があるとき' do
134 it 'ステータスコード200 OKを返す' do
136 response.should be_success
141 describe '単体表示に於いて' do
144 SystemPicture.stub(:show).and_return(@sp)
146 context 'つつがなく終わるとき' do
147 it 'ステータスコード200 OKを返す' do
148 get :show, :id => @sp.id
149 response.should be_success
151 it 'システム画像モデルに単体取得を問い合わせている' do
152 SystemPicture.should_receive(:show).exactly(1)
155 it '@system_pictureにアレを取得している' do
156 get :show, :id => @sp.id
157 assigns(:system_picture).should eq @sp
160 it 'showテンプレートを描画する' do
161 get :show, :id => @sp.id
162 response.should render_template("show")
167 get :show, :id => @sp.id, :format => :json
168 lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
170 it 'システム画像モデルにjson単体出力オプションを問い合わせている' do
171 SystemPicture.should_receive(:show_json_opt).exactly(1)
172 get :show, :id => @sp.id, :format => :json
175 get :show, :id => @sp.id, :format => :json
176 json = JSON.parse response.body
177 json["ext"].should match(/png/)
178 json["md5"].should be_true
179 json["width"].should be_true
182 #画像送信では、send_dataにスタブをおいてテストしたいが、ここに噛ませると
183 #renderが働かず、エラーとなってしまう。そこで、システム画像のファイル取得部分に
184 #スタブをおいてsend_dataがデータを返す体裁でテストする。
187 SystemPicture.any_instance.stub(:mime_type).and_return('image/png')
188 SystemPicture.any_instance.stub(:restore).and_return('aaa')
190 it '画像モデルに画像データを問い合わせる' do
191 SystemPicture.any_instance.should_receive(:restore).exactly(1)
192 get :show, :id => @sp.id, :format => :png
194 it '画像モデルにMimeTypeを問い合わせる' do
195 SystemPicture.any_instance.should_receive(:mime_type).exactly(1)
196 get :show, :id => @sp.id, :format => :png
199 get :show, :id => @sp.id, :format => :png
200 response.body.should eq 'aaa'
205 SystemPicture.any_instance.stub(:mime_type).and_return('image/gif')
206 SystemPicture.any_instance.stub(:restore).and_return('bbb')
208 it '画像モデルに画像データを問い合わせる' do
209 SystemPicture.any_instance.should_receive(:restore).exactly(1)
210 get :show, :id => @sp.id, :format => :gif
212 it '画像モデルにMimeTypeを問い合わせる' do
213 SystemPicture.any_instance.should_receive(:mime_type).exactly(1)
214 get :show, :id => @sp.id, :format => :gif
217 get :show, :id => @sp.id, :format => :gif
218 response.body.should eq 'bbb'
223 SystemPicture.any_instance.stub(:mime_type).and_return('image/jpeg')
224 SystemPicture.any_instance.stub(:restore).and_return('ccc')
226 it '画像モデルに画像データを問い合わせる' do
227 SystemPicture.any_instance.should_receive(:restore).exactly(1)
228 get :show, :id => @sp.id, :format => :jpeg
230 it '画像モデルにMimeTypeを問い合わせる' do
231 SystemPicture.any_instance.should_receive(:mime_type).exactly(1)
232 get :show, :id => @sp.id, :format => :jpeg
235 get :show, :id => @sp.id, :format => :jpeg
236 response.body.should eq 'ccc'
240 context 'ユーザ権限がないとき' do
245 it 'ステータスコード302 Foundを返す' do
246 get :show, :id => @sp.id
247 response.status.should eq 302
249 it 'サインインページへ遷移する' do
250 get :show, :id => @sp.id
251 response.body.should redirect_to '/users/sign_in'
255 it 'ステータスコード401 Unauthorizedを返す' do
256 get :show, :id => @sp.id, :format => :json
257 response.status.should eq 401
259 it '応答メッセージにUnauthorizedを返す' do
260 get :show, :id => @sp.id, :format => :json
261 response.message.should match(/Unauthorized/)
265 context 'ユーザ権限はないが管理者権限があるとき' do
270 it 'ステータスコード200 OKを返す' do
271 get :show, :id => @sp.id
272 response.should be_success
275 context 'ユーザ権限はないが借手権限があるとき' do
280 it 'ステータスコード200 OKを返す' do
281 get :show, :id => @sp.id
282 response.should be_success
286 context '対象システム画像がないとき' do
288 SystemPicture.unstub(:show)
291 it '例外404 not_foundを返す' do
294 }.should raise_error(ActiveRecord::RecordNotFound)
298 it '例外404 not_foundを返す' do
300 get :show, :id => 0, :format => :json
301 }.should raise_error(ActiveRecord::RecordNotFound)
305 context '他人のシステム画像を見ようとしたとき' do
307 SystemPicture.stub(:show).and_return(@sp)
308 SystemPicture.any_instance.stub(:own?).with(any_args()).and_return(false)
311 it '例外403 forbiddenを返す' do
313 get :show, :id => @sp.id
314 }.should raise_error(ActiveRecord::Forbidden)
318 it '例外403 forbiddenを返す' do
320 get :show, :id => @sp.id, :format => :json
321 }.should raise_error(ActiveRecord::Forbidden)
329 describe '一覧表示に於いて' do
333 SystemPicture.stub(:list).and_return([@sp, @sp, @sp])
335 context 'つつがなく終わるとき' do
337 it 'ステータスコード200 OKを返す' do
339 response.should be_success
341 it 'indexテンプレートを描画する' do
343 response.should render_template("index")
347 it 'ステータスコード200 OKを返す' do
348 get :index, :format => :json
349 response.should be_success
352 get :index, :format => :json
353 lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
357 context 'ユーザ権限がないとき' do
362 it 'ステータスコード200 OKを返す' do
364 response.should be_success
366 it 'indexテンプレートを描画する' do
368 response.should render_template("index")
372 it 'ステータスコード200 OKを返す' do
373 get :index, :format => :json
374 response.should be_success
377 get :index, :format => :json
378 lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
384 describe '単体表示に於いて' do
387 SystemPicture.stub(:show).and_return(@sp)
389 context 'つつがなく終わるとき' do
390 it 'ステータスコード200 OKを返す' do
391 get :show, :id => @sp.id
392 response.should be_success
395 it 'showテンプレートを描画する' do
396 get :show, :id => @sp.id
397 response.should render_template("show")
402 get :show, :id => @sp.id, :format => :json
403 lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
407 context 'ユーザ権限がないとき' do
411 it 'ステータスコード200 OKを返す' do
412 get :show, :id => @sp.id
413 response.should be_success
416 it 'showテンプレートを描画する' do
417 get :show, :id => @sp.id
418 response.should render_template("show")
423 get :show, :id => @sp.id, :format => :json
424 lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)