1 # -*- encoding: utf-8 -*-
5 describe SystemPicturesController do
7 SpeechBalloonTemplate.delete_all
8 @admin = FactoryGirl.create :admin
9 @demand_user = FactoryGirl.create :demand_user
10 @user = FactoryGirl.create( :user_yas)
11 @author = FactoryGirl.create :author, :user_id => @user.id
12 @artist = FactoryGirl.create :artist_yas, :author_id => @author.id
13 @sp = FactoryGirl.create :system_picture
14 @lg = FactoryGirl.create :license_group
15 @license = FactoryGirl.create :license, :license_group_id => @lg.id, :system_picture_id => @sp.id
18 if MagicNumber['run_mode'] == 1
19 describe '一覧表示に於いて' do
22 SystemPicture.stub(:list).and_return([@sp, @sp, @sp])
24 context 'パラメータpageについて' do
26 get :index, :page => 5
27 assigns(:page).should eq 5
29 it '省略されると@pageに1値が入る' do
31 assigns(:page).should eq 1
33 it '与えられたpage_sizeがセットされている' do
34 get :index, :page_size => 15
35 assigns(:page_size).should eq 15
37 it '省略されると@page_sizeにデフォルト値が入る' do
39 assigns(:page_size).should eq SystemPicture.default_page_size
41 it '最大を超えると@page_sizeにデフォルト最大値が入る' do
42 get :index, :page_size => 1500
43 assigns(:page_size).should eq SystemPicture.max_page_size
45 it '不正な値が入ると@page_sizeにデフォルト最大値が入る' do
46 get :index, :page_size => 0
47 assigns(:page_size).should eq SystemPicture.default_page_size
50 context 'つつがなく終わるとき' do
51 it 'システム画像モデルに一覧を問い合わせている' do
52 SystemPicture.should_receive(:list).exactly(1)
55 it '@system_picturesにリストを取得している' do
57 assigns(:system_pictures).should have_at_least(3).items
60 it 'ステータスコード200 OKを返す' do
62 response.should be_success
64 it 'indexテンプレートを描画する' do
66 response.should render_template("index")
70 it 'ステータスコード200 OKを返す' do
71 get :index, :format => :json
72 response.should be_success
75 get :index, :format => :json
76 lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
78 it 'システム画像モデルにjson一覧出力オプションを問い合わせている' do
79 SystemPicture.should_receive(:list_json_opt).exactly(1)
80 get :index, :format => :json
82 it 'データがリスト構造になっている' do
83 get :index, :format => :json
84 json = JSON.parse response.body
85 json.should have_at_least(3).items
87 it 'リストの先頭くらいはシステム画像っぽいものであって欲しい' do
88 get :index, :format => :json
89 json = JSON.parse response.body
90 json.first.has_key?("ext").should be_true
91 json.first.has_key?("md5").should be_true
95 context 'ユーザ権限がないとき' do
100 it 'ステータスコード302 Foundを返す' do
102 response.status.should eq 302
104 it 'サインインページへ遷移する' do
106 response.should redirect_to '/users/sign_in'
110 it 'ステータスコード401 Unauthorizedを返す' do
111 get :index, :format => :json
112 response.status.should eq 401
114 it '応答メッセージにUnauthorizedを返す' do
115 get :index, :format => :json
116 response.message.should match(/Unauthorized/)
120 context 'ユーザ権限はないが管理者権限があるとき' do
125 it 'ステータスコード200 OKを返す' do
127 response.should be_success
130 context 'ユーザ権限はないが借手権限があるとき' do
135 it 'ステータスコード200 OKを返す' do
137 response.should be_success
142 describe '単体表示に於いて' do
145 SystemPicture.stub(:show).and_return(@sp)
147 context 'つつがなく終わるとき' do
148 it 'ステータスコード200 OKを返す' do
149 get :show, :id => @sp.id
150 response.should be_success
152 it 'システム画像モデルに単体取得を問い合わせている' do
153 SystemPicture.should_receive(:show).exactly(1)
156 it '@system_pictureにアレを取得している' do
157 get :show, :id => @sp.id
158 assigns(:system_picture).should eq @sp
161 it 'showテンプレートを描画する' do
162 get :show, :id => @sp.id
163 response.should render_template("show")
168 get :show, :id => @sp.id, :format => :json
169 lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
171 it 'システム画像モデルにjson単体出力オプションを問い合わせている' do
172 SystemPicture.should_receive(:show_json_opt).exactly(1)
173 get :show, :id => @sp.id, :format => :json
176 get :show, :id => @sp.id, :format => :json
177 json = JSON.parse response.body
178 json["ext"].should match(/png/)
179 json["md5"].should be_true
180 json["width"].should be_true
183 #画像送信では、send_dataにスタブをおいてテストしたいが、ここに噛ませると
184 #renderが働かず、エラーとなってしまう。そこで、システム画像のファイル取得部分に
185 #スタブをおいてsend_dataがデータを返す体裁でテストする。
188 SystemPicture.any_instance.stub(:mime_type).and_return('image/png')
189 SystemPicture.any_instance.stub(:restore).and_return('aaa')
191 it '画像モデルに画像データを問い合わせる' do
192 SystemPicture.any_instance.should_receive(:restore).exactly(1)
193 get :show, :id => @sp.id, :format => :png
195 it '画像モデルにMimeTypeを問い合わせる' do
196 SystemPicture.any_instance.should_receive(:mime_type).exactly(1)
197 get :show, :id => @sp.id, :format => :png
200 get :show, :id => @sp.id, :format => :png
201 response.body.should eq 'aaa'
206 SystemPicture.any_instance.stub(:mime_type).and_return('image/gif')
207 SystemPicture.any_instance.stub(:restore).and_return('bbb')
209 it '画像モデルに画像データを問い合わせる' do
210 SystemPicture.any_instance.should_receive(:restore).exactly(1)
211 get :show, :id => @sp.id, :format => :gif
213 it '画像モデルにMimeTypeを問い合わせる' do
214 SystemPicture.any_instance.should_receive(:mime_type).exactly(1)
215 get :show, :id => @sp.id, :format => :gif
218 get :show, :id => @sp.id, :format => :gif
219 response.body.should eq 'bbb'
224 SystemPicture.any_instance.stub(:mime_type).and_return('image/jpeg')
225 SystemPicture.any_instance.stub(:restore).and_return('ccc')
227 it '画像モデルに画像データを問い合わせる' do
228 SystemPicture.any_instance.should_receive(:restore).exactly(1)
229 get :show, :id => @sp.id, :format => :jpeg
231 it '画像モデルにMimeTypeを問い合わせる' do
232 SystemPicture.any_instance.should_receive(:mime_type).exactly(1)
233 get :show, :id => @sp.id, :format => :jpeg
236 get :show, :id => @sp.id, :format => :jpeg
237 response.body.should eq 'ccc'
241 context 'ユーザ権限がないとき' do
246 it 'ステータスコード302 Foundを返す' do
247 get :show, :id => @sp.id
248 response.status.should eq 302
250 it 'サインインページへ遷移する' do
251 get :show, :id => @sp.id
252 response.body.should redirect_to '/users/sign_in'
256 it 'ステータスコード401 Unauthorizedを返す' do
257 get :show, :id => @sp.id, :format => :json
258 response.status.should eq 401
260 it '応答メッセージにUnauthorizedを返す' do
261 get :show, :id => @sp.id, :format => :json
262 response.message.should match(/Unauthorized/)
266 context 'ユーザ権限はないが管理者権限があるとき' do
271 it 'ステータスコード200 OKを返す' do
272 get :show, :id => @sp.id
273 response.should be_success
276 context 'ユーザ権限はないが借手権限があるとき' do
281 it 'ステータスコード200 OKを返す' do
282 get :show, :id => @sp.id
283 response.should be_success
287 context '対象システム画像がないとき' do
289 SystemPicture.unstub(:show)
292 it '例外404 not_foundを返す' do
295 }.should raise_error(ActiveRecord::RecordNotFound)
299 it '例外404 not_foundを返す' do
301 get :show, :id => 0, :format => :json
302 }.should raise_error(ActiveRecord::RecordNotFound)
306 context '他人のシステム画像を見ようとしたとき' do
308 SystemPicture.stub(:show).and_return(@sp)
309 SystemPicture.any_instance.stub(:own?).with(any_args()).and_return(false)
312 it '例外403 forbiddenを返す' do
314 get :show, :id => @sp.id
315 }.should raise_error(ActiveRecord::Forbidden)
319 it '例外403 forbiddenを返す' do
321 get :show, :id => @sp.id, :format => :json
322 }.should raise_error(ActiveRecord::Forbidden)
330 describe '一覧表示に於いて' do
334 SystemPicture.stub(:list).and_return([@sp, @sp, @sp])
336 context 'つつがなく終わるとき' do
338 it 'ステータスコード200 OKを返す' do
340 response.should be_success
342 it 'indexテンプレートを描画する' do
344 response.should render_template("index")
348 it 'ステータスコード200 OKを返す' do
349 get :index, :format => :json
350 response.should be_success
353 get :index, :format => :json
354 lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
358 context 'ユーザ権限がないとき' do
363 it 'ステータスコード200 OKを返す' do
365 response.should be_success
367 it 'indexテンプレートを描画する' do
369 response.should render_template("index")
373 it 'ステータスコード200 OKを返す' do
374 get :index, :format => :json
375 response.should be_success
378 get :index, :format => :json
379 lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
385 describe '単体表示に於いて' do
388 SystemPicture.stub(:show).and_return(@sp)
390 context 'つつがなく終わるとき' do
391 it 'ステータスコード200 OKを返す' do
392 get :show, :id => @sp.id
393 response.should be_success
396 it 'showテンプレートを描画する' do
397 get :show, :id => @sp.id
398 response.should render_template("show")
403 get :show, :id => @sp.id, :format => :json
404 lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
408 context 'ユーザ権限がないとき' do
412 it 'ステータスコード200 OKを返す' do
413 get :show, :id => @sp.id
414 response.should be_success
417 it 'showテンプレートを描画する' do
418 get :show, :id => @sp.id
419 response.should render_template("show")
424 get :show, :id => @sp.id, :format => :json
425 lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)